添加产品管理

This commit is contained in:
2024-01-08 09:02:59 +08:00
parent 456a6aadaf
commit 249ffc8460
8 changed files with 251 additions and 0 deletions

View File

@@ -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<ProductDto> list(@RequestBody CommonQuery request) {
return this.service.list(request);
}
}

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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<ProductEntity, ProductDto, CreateProductDto, UpdateProductDto> {}

View File

@@ -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<ProductEntity> {}

View File

@@ -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<ProductEntity, ProductRepository> {
@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<ProductDto> list(CommonQuery query) {
Page<ProductEntity> page =
repository.findAll(
query.specification(conversionService),
PageRequest.of(
query.getPageNo(),
query.getPageSize(),
Sort.by(query.getOrders())));
return page.map(this.mapper::toDto);
}
}