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); + } +}