From 249ffc84601a73a04c9fb6b663c28a9601fd8fba Mon Sep 17 00:00:00 2001 From: lihongjie0209 Date: Mon, 8 Jan 2024 09:02:59 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=A7=E5=93=81=E7=AE=A1?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../product/controller/ProductController.java | 54 ++++++++++++++ .../coal/product/dto/CreateProductDto.java | 25 +++++++ .../coal/product/dto/ProductDto.java | 23 ++++++ .../coal/product/dto/UpdateProductDto.java | 23 ++++++ .../coal/product/entity/ProductEntity.java | 29 ++++++++ .../coal/product/mapper/ProductMapper.java | 18 +++++ .../product/repository/ProductRepository.java | 9 +++ .../coal/product/service/ProductService.java | 70 +++++++++++++++++++ 8 files changed, 251 insertions(+) create mode 100644 src/main/java/cn/lihongjie/coal/product/controller/ProductController.java create mode 100644 src/main/java/cn/lihongjie/coal/product/dto/CreateProductDto.java create mode 100644 src/main/java/cn/lihongjie/coal/product/dto/ProductDto.java create mode 100644 src/main/java/cn/lihongjie/coal/product/dto/UpdateProductDto.java create mode 100644 src/main/java/cn/lihongjie/coal/product/entity/ProductEntity.java create mode 100644 src/main/java/cn/lihongjie/coal/product/mapper/ProductMapper.java create mode 100644 src/main/java/cn/lihongjie/coal/product/repository/ProductRepository.java create mode 100644 src/main/java/cn/lihongjie/coal/product/service/ProductService.java diff --git a/src/main/java/cn/lihongjie/coal/product/controller/ProductController.java b/src/main/java/cn/lihongjie/coal/product/controller/ProductController.java new file mode 100644 index 00000000..8a787138 --- /dev/null +++ b/src/main/java/cn/lihongjie/coal/product/controller/ProductController.java @@ -0,0 +1,54 @@ +package cn.lihongjie.coal.product.controller; + +import cn.lihongjie.coal.annotation.OrgScope; +import cn.lihongjie.coal.annotation.SysLog; +import cn.lihongjie.coal.base.dto.CommonQuery; +import cn.lihongjie.coal.base.dto.IdRequest; +import cn.lihongjie.coal.product.dto.CreateProductDto; +import cn.lihongjie.coal.product.dto.ProductDto; +import cn.lihongjie.coal.product.dto.UpdateProductDto; +import cn.lihongjie.coal.product.service.ProductService; + +import lombok.extern.slf4j.Slf4j; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Page; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("/product") +@SysLog(module = "产品管理") +@Slf4j +@OrgScope +public class ProductController { + @Autowired private ProductService service; + + @PostMapping("/create") + public ProductDto create(@RequestBody CreateProductDto request) { + return this.service.create(request); + } + + @PostMapping("/update") + public ProductDto update(@RequestBody UpdateProductDto request) { + return this.service.update(request); + } + + @PostMapping("/delete") + public Object delete(@RequestBody IdRequest request) { + this.service.delete(request); + return true; + } + + @PostMapping("/getById") + public ProductDto getById(@RequestBody IdRequest request) { + return this.service.getById(request.getId()); + } + + @PostMapping("/list") + public Page list(@RequestBody CommonQuery request) { + return this.service.list(request); + } +} diff --git a/src/main/java/cn/lihongjie/coal/product/dto/CreateProductDto.java b/src/main/java/cn/lihongjie/coal/product/dto/CreateProductDto.java new file mode 100644 index 00000000..20fdcf70 --- /dev/null +++ b/src/main/java/cn/lihongjie/coal/product/dto/CreateProductDto.java @@ -0,0 +1,25 @@ +package cn.lihongjie.coal.product.dto; + +import cn.lihongjie.coal.base.dto.OrgCommonDto; + +import lombok.Data; + +import org.hibernate.annotations.Comment; +import org.hibernate.annotations.Formula; + +@Data +public class CreateProductDto extends OrgCommonDto { + + + @Comment("煤源类型") + private String coalType; + + @Formula( + "(select i.name\n" + + "from t_dictionary d,\n" + + " t_dictionary_item i\n" + + "where d.id = i.dictionary_id\n" + + " and d.code = 'coal.type'\n" + + " and i.code = coal_type)") + private String coalTypeName; +} diff --git a/src/main/java/cn/lihongjie/coal/product/dto/ProductDto.java b/src/main/java/cn/lihongjie/coal/product/dto/ProductDto.java new file mode 100644 index 00000000..d13f5043 --- /dev/null +++ b/src/main/java/cn/lihongjie/coal/product/dto/ProductDto.java @@ -0,0 +1,23 @@ +package cn.lihongjie.coal.product.dto; + +import cn.lihongjie.coal.base.dto.OrgCommonDto; + +import lombok.Data; + +import org.hibernate.annotations.Comment; +import org.hibernate.annotations.Formula; + +@Data +public class ProductDto extends OrgCommonDto { + @Comment("煤源类型") + private String coalType; + + @Formula( + "(select i.name\n" + + "from t_dictionary d,\n" + + " t_dictionary_item i\n" + + "where d.id = i.dictionary_id\n" + + " and d.code = 'coal.type'\n" + + " and i.code = coal_type)") + private String coalTypeName; +} diff --git a/src/main/java/cn/lihongjie/coal/product/dto/UpdateProductDto.java b/src/main/java/cn/lihongjie/coal/product/dto/UpdateProductDto.java new file mode 100644 index 00000000..617fc23f --- /dev/null +++ b/src/main/java/cn/lihongjie/coal/product/dto/UpdateProductDto.java @@ -0,0 +1,23 @@ +package cn.lihongjie.coal.product.dto; + +import cn.lihongjie.coal.base.dto.OrgCommonDto; + +import lombok.Data; + +import org.hibernate.annotations.Comment; +import org.hibernate.annotations.Formula; + +@Data +public class UpdateProductDto extends OrgCommonDto { + @Comment("煤源类型") + private String coalType; + + @Formula( + "(select i.name\n" + + "from t_dictionary d,\n" + + " t_dictionary_item i\n" + + "where d.id = i.dictionary_id\n" + + " and d.code = 'coal.type'\n" + + " and i.code = coal_type)") + private String coalTypeName; +} diff --git a/src/main/java/cn/lihongjie/coal/product/entity/ProductEntity.java b/src/main/java/cn/lihongjie/coal/product/entity/ProductEntity.java new file mode 100644 index 00000000..202b66a4 --- /dev/null +++ b/src/main/java/cn/lihongjie/coal/product/entity/ProductEntity.java @@ -0,0 +1,29 @@ +package cn.lihongjie.coal.product.entity; + +import cn.lihongjie.coal.base.entity.OrgCommonEntity; + +import jakarta.persistence.Entity; + +import lombok.Data; + +import org.hibernate.annotations.Comment; +import org.hibernate.annotations.Formula; + +@Data +@Entity +public class ProductEntity extends OrgCommonEntity { + + + @Comment("煤源类型") + private String coalType; + + @Formula( + "(select i.name\n" + + "from t_dictionary d,\n" + + " t_dictionary_item i\n" + + "where d.id = i.dictionary_id\n" + + " and d.code = 'coal.type'\n" + + " and i.code = coal_type)") + private String coalTypeName; + +} diff --git a/src/main/java/cn/lihongjie/coal/product/mapper/ProductMapper.java b/src/main/java/cn/lihongjie/coal/product/mapper/ProductMapper.java new file mode 100644 index 00000000..33c5e1f5 --- /dev/null +++ b/src/main/java/cn/lihongjie/coal/product/mapper/ProductMapper.java @@ -0,0 +1,18 @@ +package cn.lihongjie.coal.product.mapper; + +import cn.lihongjie.coal.base.mapper.BaseMapper; +import cn.lihongjie.coal.base.mapper.CommonMapper; +import cn.lihongjie.coal.product.dto.CreateProductDto; +import cn.lihongjie.coal.product.dto.ProductDto; +import cn.lihongjie.coal.product.dto.UpdateProductDto; +import cn.lihongjie.coal.product.entity.ProductEntity; + +import org.mapstruct.Mapper; +import org.mapstruct.control.DeepClone; + +@Mapper( + componentModel = org.mapstruct.MappingConstants.ComponentModel.SPRING, + uses = {CommonMapper.class}, + mappingControl = DeepClone.class) +public interface ProductMapper + extends BaseMapper {} diff --git a/src/main/java/cn/lihongjie/coal/product/repository/ProductRepository.java b/src/main/java/cn/lihongjie/coal/product/repository/ProductRepository.java new file mode 100644 index 00000000..0cb840e2 --- /dev/null +++ b/src/main/java/cn/lihongjie/coal/product/repository/ProductRepository.java @@ -0,0 +1,9 @@ +package cn.lihongjie.coal.product.repository; + +import cn.lihongjie.coal.base.dao.BaseRepository; +import cn.lihongjie.coal.product.entity.ProductEntity; + +import org.springframework.stereotype.Repository; + +@Repository +public interface ProductRepository extends BaseRepository {} diff --git a/src/main/java/cn/lihongjie/coal/product/service/ProductService.java b/src/main/java/cn/lihongjie/coal/product/service/ProductService.java new file mode 100644 index 00000000..efa2c223 --- /dev/null +++ b/src/main/java/cn/lihongjie/coal/product/service/ProductService.java @@ -0,0 +1,70 @@ +package cn.lihongjie.coal.product.service; + +import cn.lihongjie.coal.base.dto.CommonQuery; +import cn.lihongjie.coal.base.dto.IdRequest; +import cn.lihongjie.coal.base.service.BaseService; +import cn.lihongjie.coal.product.dto.CreateProductDto; +import cn.lihongjie.coal.product.dto.ProductDto; +import cn.lihongjie.coal.product.dto.UpdateProductDto; +import cn.lihongjie.coal.product.entity.ProductEntity; +import cn.lihongjie.coal.product.mapper.ProductMapper; +import cn.lihongjie.coal.product.repository.ProductRepository; + +import lombok.extern.slf4j.Slf4j; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.convert.ConversionService; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Sort; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@Service +@Slf4j +@Transactional +public class ProductService extends BaseService { + @Autowired private ProductRepository repository; + + @Autowired private ProductMapper mapper; + + @Autowired private ConversionService conversionService; + + public ProductDto create(CreateProductDto request) { + ProductEntity entity = mapper.toEntity(request); + + this.repository.save(entity); + return getById(entity.getId()); + } + + public ProductDto update(UpdateProductDto request) { + ProductEntity entity = this.repository.get(request.getId()); + this.mapper.updateEntity(entity, request); + + this.repository.save(entity); + + return getById(entity.getId()); + } + + public void delete(IdRequest request) { + this.repository.deleteAllById(request.getIds()); + } + + public ProductDto getById(String id) { + ProductEntity entity = repository.get(id); + + return mapper.toDto(entity); + } + + public Page list(CommonQuery query) { + Page page = + repository.findAll( + query.specification(conversionService), + PageRequest.of( + query.getPageNo(), + query.getPageSize(), + Sort.by(query.getOrders()))); + + return page.map(this.mapper::toDto); + } +}