diff --git a/src/main/java/com/xkrs/controller/CraftItemController.java b/src/main/java/com/xkrs/controller/CraftItemController.java new file mode 100644 index 0000000..6a3fc3d --- /dev/null +++ b/src/main/java/com/xkrs/controller/CraftItemController.java @@ -0,0 +1,50 @@ +package com.xkrs.controller; + +import com.xkrs.model.qo.CraftItemQoDelete; +import com.xkrs.model.qo.CraftItemQoInsert; +import com.xkrs.model.qo.CraftItemQoUpdate; +import com.xkrs.service.CraftItemService; +import org.springframework.web.bind.annotation.*; + +import javax.annotation.Resource; + +@RestController +public class CraftItemController { + + @Resource + private CraftItemService craftItemService; + + /** + * 添加工艺项目 + */ + @PostMapping("/insertCraftItem") + public String insertCraftItem(@RequestBody CraftItemQoInsert insertQo) { + return craftItemService.insertCraftItem(insertQo); + } + + /** + * 删除工艺项目 + */ + @PostMapping("/deleteCraftItem") + public String deleteCraftItem(@RequestBody CraftItemQoDelete deleteQo) { + return craftItemService.deleteCraftItem(deleteQo); + } + + /** + * 更新工艺项目名称 + */ + @PostMapping("/updateCraftItem") + public String updateCraftItem(@RequestBody CraftItemQoUpdate updateQo) { + return craftItemService.updateCraftItem(updateQo); + } + + /** + * 查询工艺项目 + */ + @GetMapping("/queryCraftItem") + public String queryCraftItem(@RequestParam(required = false, value = "no") String no, @RequestParam(required = false, value = "name") String name) { + return craftItemService.queryCraftItem(no, name); + } + + +} diff --git a/src/main/java/com/xkrs/dao/CraftItemDao.java b/src/main/java/com/xkrs/dao/CraftItemDao.java new file mode 100644 index 0000000..e8bc3d4 --- /dev/null +++ b/src/main/java/com/xkrs/dao/CraftItemDao.java @@ -0,0 +1,36 @@ +package com.xkrs.dao; + +import com.xkrs.model.entity.CraftItemEntity; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.JpaSpecificationExecutor; +import org.springframework.data.jpa.repository.Modifying; +import org.springframework.data.jpa.repository.Query; +import org.springframework.stereotype.Component; + +import javax.transaction.Transactional; +import java.util.List; +import java.util.Map; +import java.util.Optional; + +/** + * 工艺项目Dao + */ +@Component +public interface CraftItemDao extends JpaRepository, JpaSpecificationExecutor { + + Optional findByNo(String no); + + /** + * 更新工艺项目名称 + */ + @Transactional(rollbackOn = Exception.class) + @Modifying(clearAutomatically = true) + @Query(value = "UPDATE craft_item SET update_time = ?2, name = ?3 WHERE id = ?1", nativeQuery = true) + void updateNameById(Integer id, String updateTime, String name); + + /** + * 查询工艺项目 + */ + @Query(value = "SELECT id, no, name FROM craft_item WHERE no LIKE %?1% AND name LIKE %?2% ORDER BY create_time ASC", nativeQuery = true) + List> queryCraftItem(String no, String name); +} diff --git a/src/main/java/com/xkrs/model/entity/CraftItemEntity.java b/src/main/java/com/xkrs/model/entity/CraftItemEntity.java new file mode 100644 index 0000000..929aca7 --- /dev/null +++ b/src/main/java/com/xkrs/model/entity/CraftItemEntity.java @@ -0,0 +1,91 @@ +package com.xkrs.model.entity; + +import javax.persistence.*; + +/** + * 工艺项目 + */ +@Entity +@Table(name = "craft_item") +public class CraftItemEntity { + + /** + * 主键id + */ + @Id + @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "craft_item_seq_gen") + @SequenceGenerator(name = "craft_item_seq_gen", sequenceName = "craft_item_id_seq", allocationSize = 1) + private Integer id; + + /** + * 创建时间 + */ + @Column(length = 85, columnDefinition = "varchar(85)") + private String createTime; + + /** + * 更新时间 + */ + @Column(length = 85, columnDefinition = "varchar(85)") + private String updateTime; + + /** + * 编号 + */ + @Column(length = 85, columnDefinition = "varchar(85)") + private String no; + + /** + * 名称 + */ + @Column(length = 85, columnDefinition = "varchar(85)") + private String name; + + public CraftItemEntity() { + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getCreateTime() { + return createTime; + } + + public void setCreateTime(String createTime) { + this.createTime = createTime; + } + + public String getUpdateTime() { + return updateTime; + } + + public void setUpdateTime(String updateTime) { + this.updateTime = updateTime; + } + + public String getNo() { + return no; + } + + public void setNo(String no) { + this.no = no; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @Override + public String toString() { + return "CraftItemEntity{" + "id=" + id + ", createTime='" + createTime + '\'' + ", updateTime='" + updateTime + '\'' + ", no='" + no + '\'' + ", name='" + name + '\'' + '}'; + } +} diff --git a/src/main/java/com/xkrs/model/qo/CraftItemQoDelete.java b/src/main/java/com/xkrs/model/qo/CraftItemQoDelete.java new file mode 100644 index 0000000..f038e5d --- /dev/null +++ b/src/main/java/com/xkrs/model/qo/CraftItemQoDelete.java @@ -0,0 +1,33 @@ +package com.xkrs.model.qo; + +import com.xkrs.model.va.CraftItemQoDeleteVa; + +import javax.validation.constraints.NotBlank; + +/** + * 删除工艺项目接收类 + */ +public class CraftItemQoDelete { + + /** + * 主键id + */ + @NotBlank(message = "{CraftItemEntity.id.blank}", groups = {CraftItemQoDeleteVa.class}) + private Integer id; + + public CraftItemQoDelete() { + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + @Override + public String toString() { + return "CraftItemQoDelete{" + "id=" + id + '}'; + } +} diff --git a/src/main/java/com/xkrs/model/qo/CraftItemQoInsert.java b/src/main/java/com/xkrs/model/qo/CraftItemQoInsert.java new file mode 100644 index 0000000..9fb204d --- /dev/null +++ b/src/main/java/com/xkrs/model/qo/CraftItemQoInsert.java @@ -0,0 +1,47 @@ +package com.xkrs.model.qo; + +import com.xkrs.model.va.CraftItemQoInsertVa; + +import javax.validation.constraints.NotBlank; + +/** + * 新增工艺项目接收类 + */ +public class CraftItemQoInsert { + + /** + * 编号 + */ + @NotBlank(message = "{CraftItemEntity.no.blank}", groups = {CraftItemQoInsertVa.class}) + private String no; + + /** + * 名称 + */ + @NotBlank(message = "{CraftItemEntity.name.blank}", groups = {CraftItemQoInsertVa.class}) + private String name; + + public CraftItemQoInsert() { + } + + public String getNo() { + return no; + } + + public void setNo(String no) { + this.no = no; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @Override + public String toString() { + return "CraftItemQoInsert{" + "no='" + no + '\'' + ", name='" + name + '\'' + '}'; + } +} diff --git a/src/main/java/com/xkrs/model/qo/CraftItemQoUpdate.java b/src/main/java/com/xkrs/model/qo/CraftItemQoUpdate.java new file mode 100644 index 0000000..f9b1b94 --- /dev/null +++ b/src/main/java/com/xkrs/model/qo/CraftItemQoUpdate.java @@ -0,0 +1,44 @@ +package com.xkrs.model.qo; + +import com.xkrs.model.va.CraftItemQoUpdateVa; + +import javax.validation.constraints.NotBlank; + +public class CraftItemQoUpdate { + + /** + * 主键id + */ + @NotBlank(message = "{CraftItemEntity.id.blank}", groups = {CraftItemQoUpdateVa.class}) + private Integer id; + + /** + * 名称 + */ + @NotBlank(message = "{CraftItemEntity.name.blank}", groups = {CraftItemQoUpdateVa.class}) + private String name; + + public CraftItemQoUpdate() { + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @Override + public String toString() { + return "CraftItemQoUpdate{" + "id=" + id + ", name='" + name + '\'' + '}'; + } +} diff --git a/src/main/java/com/xkrs/model/va/CraftItemQoDeleteVa.java b/src/main/java/com/xkrs/model/va/CraftItemQoDeleteVa.java new file mode 100644 index 0000000..f8a7263 --- /dev/null +++ b/src/main/java/com/xkrs/model/va/CraftItemQoDeleteVa.java @@ -0,0 +1,4 @@ +package com.xkrs.model.va; + +public interface CraftItemQoDeleteVa { +} diff --git a/src/main/java/com/xkrs/model/va/CraftItemQoInsertVa.java b/src/main/java/com/xkrs/model/va/CraftItemQoInsertVa.java new file mode 100644 index 0000000..0db7800 --- /dev/null +++ b/src/main/java/com/xkrs/model/va/CraftItemQoInsertVa.java @@ -0,0 +1,4 @@ +package com.xkrs.model.va; + +public interface CraftItemQoInsertVa { +} diff --git a/src/main/java/com/xkrs/model/va/CraftItemQoUpdateVa.java b/src/main/java/com/xkrs/model/va/CraftItemQoUpdateVa.java new file mode 100644 index 0000000..436c4ad --- /dev/null +++ b/src/main/java/com/xkrs/model/va/CraftItemQoUpdateVa.java @@ -0,0 +1,4 @@ +package com.xkrs.model.va; + +public interface CraftItemQoUpdateVa { +} diff --git a/src/main/java/com/xkrs/service/CraftItemService.java b/src/main/java/com/xkrs/service/CraftItemService.java new file mode 100644 index 0000000..63f7ceb --- /dev/null +++ b/src/main/java/com/xkrs/service/CraftItemService.java @@ -0,0 +1,29 @@ +package com.xkrs.service; + +import com.xkrs.model.qo.CraftItemQoDelete; +import com.xkrs.model.qo.CraftItemQoInsert; +import com.xkrs.model.qo.CraftItemQoUpdate; + +public interface CraftItemService { + + /** + * 添加工艺项目 + */ + String insertCraftItem(CraftItemQoInsert insertQo); + + /** + * 删除工艺项目 + */ + String deleteCraftItem(CraftItemQoDelete deleteQo); + + /** + * 更新工艺项目名称 + */ + String updateCraftItem(CraftItemQoUpdate updateQo); + + /** + * 查询工艺项目 + */ + String queryCraftItem(String no, String name); + +} diff --git a/src/main/java/com/xkrs/service/impl/CraftItemServiceImpl.java b/src/main/java/com/xkrs/service/impl/CraftItemServiceImpl.java new file mode 100644 index 0000000..a6fba4e --- /dev/null +++ b/src/main/java/com/xkrs/service/impl/CraftItemServiceImpl.java @@ -0,0 +1,91 @@ +package com.xkrs.service.impl; + +import com.xkrs.dao.CraftItemDao; +import com.xkrs.encapsulation.PromptMessageEnum; +import com.xkrs.model.entity.CraftItemEntity; +import com.xkrs.model.qo.CraftItemQoDelete; +import com.xkrs.model.qo.CraftItemQoInsert; +import com.xkrs.model.qo.CraftItemQoUpdate; +import com.xkrs.service.CraftItemService; +import com.xkrs.util.LocalDateUtils; +import com.xkrs.util.LocalStringUtils; +import org.springframework.context.i18n.LocaleContextHolder; +import org.springframework.stereotype.Service; + +import javax.annotation.Resource; +import java.util.List; +import java.util.Locale; +import java.util.Map; +import java.util.Optional; + +import static com.xkrs.encapsulation.OutputEncapsulation.outputEncapsulationObject; + +@Service +public class CraftItemServiceImpl implements CraftItemService { + + private Locale locale = LocaleContextHolder.getLocale(); + + @Resource + private CraftItemDao craftItemDao; + + /** + * 添加工艺项目 + */ + @Override + public String insertCraftItem(CraftItemQoInsert insertQo) { + String no = insertQo.getNo(); + String name = insertQo.getName(); + Optional entityByNo = craftItemDao.findByNo(no); + if (entityByNo.isPresent()) { + return outputEncapsulationObject(PromptMessageEnum.DATA_EXIT, "该工艺项目已存在,请勿重复添加!", locale); + } + CraftItemEntity entity = new CraftItemEntity(); + entity.setCreateTime(LocalDateUtils.getCurrentSecond()); + entity.setUpdateTime(""); + entity.setNo(LocalStringUtils.formatEmptyValue(no)); + entity.setName(LocalStringUtils.formatEmptyValue(name)); + craftItemDao.save(entity); + return outputEncapsulationObject(PromptMessageEnum.SUCCESS, "添加成功!", locale); + } + + /** + * 删除工艺项目 + */ + @Override + public String deleteCraftItem(CraftItemQoDelete deleteQo) { + Integer id = deleteQo.getId(); + Optional entityById = craftItemDao.findById(id); + if (entityById.isEmpty()) { + return outputEncapsulationObject(PromptMessageEnum.DATA_NONE, "不存在该工艺项目,请先添加工艺项目!", locale); + } + craftItemDao.deleteById(id); + return outputEncapsulationObject(PromptMessageEnum.SUCCESS, "删除成功!", locale); + } + + /** + * 更新工艺项目名称 + */ + @Override + public String updateCraftItem(CraftItemQoUpdate updateQo) { + Integer id = updateQo.getId(); + String name = updateQo.getName(); + Optional entityById = craftItemDao.findById(id); + if (entityById.isEmpty()) { + return outputEncapsulationObject(PromptMessageEnum.DATA_NONE, "不存在该工艺项目,请先添加工艺项目!", locale); + } + craftItemDao.updateNameById(id, LocalDateUtils.getCurrentSecond(), LocalStringUtils.formatEmptyValue(name)); + return outputEncapsulationObject(PromptMessageEnum.SUCCESS, "更新成功!", locale); + } + + /** + * 查询工艺项目 + */ + @Override + public String queryCraftItem(String no, String name) { + List> resultList = craftItemDao.queryCraftItem(LocalStringUtils.formatEmptyValue(no), LocalStringUtils.formatEmptyValue(name)); + if (resultList == null || resultList.isEmpty()) { + return outputEncapsulationObject(PromptMessageEnum.DATA_NONE, "暂时没有该工艺项目的信息!", locale); + } + return outputEncapsulationObject(PromptMessageEnum.SUCCESS, resultList, locale); + } +}