diff --git a/src/main/java/cn/lihongjie/coal/coalWashingDailyAnalysis/controller/CoalWashingDailyAnalysisController.java b/src/main/java/cn/lihongjie/coal/coalWashingDailyAnalysis/controller/CoalWashingDailyAnalysisController.java index 454b64bb..e90510dd 100644 --- a/src/main/java/cn/lihongjie/coal/coalWashingDailyAnalysis/controller/CoalWashingDailyAnalysisController.java +++ b/src/main/java/cn/lihongjie/coal/coalWashingDailyAnalysis/controller/CoalWashingDailyAnalysisController.java @@ -5,6 +5,7 @@ import cn.lihongjie.coal.annotation.SysLog; import cn.lihongjie.coal.base.controller.BaseController; import cn.lihongjie.coal.base.dto.CommonQuery; import cn.lihongjie.coal.base.dto.IdRequest; +import cn.lihongjie.coal.coalWashingDailyAnalysis.dto.CoalWashingDailyAnalysisCoalReportRequest; import cn.lihongjie.coal.coalWashingDailyAnalysis.dto.CoalWashingDailyAnalysisDto; import cn.lihongjie.coal.coalWashingDailyAnalysis.dto.CreateCoalWashingDailyAnalysisDto; import cn.lihongjie.coal.coalWashingDailyAnalysis.dto.UpdateCoalWashingDailyAnalysisDto; @@ -80,4 +81,11 @@ public class CoalWashingDailyAnalysisController extends BaseController { public CoalWashingDailyAnalysisDto getById(@RequestBody IdRequest dto) { return this.service.getById(dto.getId()); } + + + + @PostMapping("/report") + public Object report(@RequestBody CoalWashingDailyAnalysisCoalReportRequest request) { + return this.service.report(request); + } } diff --git a/src/main/java/cn/lihongjie/coal/coalWashingDailyAnalysis/dto/CoalWashingDailyAnalysisCoalReportRequest.java b/src/main/java/cn/lihongjie/coal/coalWashingDailyAnalysis/dto/CoalWashingDailyAnalysisCoalReportRequest.java new file mode 100644 index 00000000..c553e809 --- /dev/null +++ b/src/main/java/cn/lihongjie/coal/coalWashingDailyAnalysis/dto/CoalWashingDailyAnalysisCoalReportRequest.java @@ -0,0 +1,50 @@ +package cn.lihongjie.coal.coalWashingDailyAnalysis.dto; + +import cn.lihongjie.coal.base.dto.CommonQuery; + +import lombok.Data; + +import org.hibernate.annotations.Comment; + +import java.time.LocalDate; +import java.util.*; + +@Data +public class CoalWashingDailyAnalysisCoalReportRequest extends CommonQuery { + + @Comment( + """ +时间维度 +year +month + +""") + private String timeDimension; + + @Comment( + """ +统计字段 + + +""") + private List reportFields; + + private List fieldInfos; + + private LocalDate startTime; + private LocalDate endTime; + + /** 过滤条件 */ + + private List productIds; + private List coalInfoIds; + + + @Data + public static class FieldInfo { + private String fieldName; + + @Comment("sum avg count max min") + private String function; + } +} diff --git a/src/main/java/cn/lihongjie/coal/coalWashingDailyAnalysis/service/CoalWashingDailyAnalysisService.java b/src/main/java/cn/lihongjie/coal/coalWashingDailyAnalysis/service/CoalWashingDailyAnalysisService.java index 02f677f6..c32cc186 100644 --- a/src/main/java/cn/lihongjie/coal/coalWashingDailyAnalysis/service/CoalWashingDailyAnalysisService.java +++ b/src/main/java/cn/lihongjie/coal/coalWashingDailyAnalysis/service/CoalWashingDailyAnalysisService.java @@ -3,6 +3,7 @@ package cn.lihongjie.coal.coalWashingDailyAnalysis.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.coalWashingDailyAnalysis.dto.CoalWashingDailyAnalysisCoalReportRequest; import cn.lihongjie.coal.coalWashingDailyAnalysis.dto.CoalWashingDailyAnalysisDto; import cn.lihongjie.coal.coalWashingDailyAnalysis.dto.CreateCoalWashingDailyAnalysisDto; import cn.lihongjie.coal.coalWashingDailyAnalysis.dto.UpdateCoalWashingDailyAnalysisDto; @@ -11,21 +12,34 @@ import cn.lihongjie.coal.coalWashingDailyAnalysis.mapper.CoalWashingDailyAnalysi import cn.lihongjie.coal.coalWashingDailyAnalysis.mapper.RoundMapper; import cn.lihongjie.coal.coalWashingDailyAnalysis.repository.CoalWashingDailyAnalysisRepository; import cn.lihongjie.coal.common.Ctx; +import cn.lihongjie.coal.common.FreeMakerUtils; +import cn.lihongjie.coal.common.JpaUtils; import cn.lihongjie.coal.exception.BizException; +import com.google.common.base.CaseFormat; + import jakarta.annotation.PostConstruct; +import jakarta.persistence.EntityManager; +import jakarta.persistence.PersistenceContext; +import jakarta.persistence.Tuple; import lombok.extern.slf4j.Slf4j; import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.lang3.ObjectUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.convert.ConversionService; import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageImpl; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Sort; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + @Service @Slf4j @Transactional @@ -131,4 +145,112 @@ public class CoalWashingDailyAnalysisService public void unarchive(IdRequest dto) { this.repository.unArchive(dto); } + + @PersistenceContext EntityManager em; + + public Object report(CoalWashingDailyAnalysisCoalReportRequest request) { + + + + String sql = + FreeMakerUtils.render( + """ + + + select to_char(date_trunc('${timeDimension}', a.end_time), 'YYYY-MM-DD') as time + + + <#list reportFields as field> + + <#if field?is_first> , + + + <#if field == 'coalInfoId'>, d.id as coal_info_id, d.code as coal_info_code, d.name as coal_info_name + <#if field == 'productId'>, p.id as product_id, p.code as product_code, p.name as product_name + + + <#if field?has_next>, + + , sum(d.amount) as amount + + from t_coal_washing_daily_analysis_ym_details d + inner join t_coal_washing_daily_analysis a on a.id = d.coal_washing_daily_analysis_id + inner join t_product p on p.id = a.product_id + + + where 1=1 + + <#if startTime??> and a.end_time >= :startTime + <#if endTime??> and a.end_time <= :endTime + <#if productIds??> and a.product_id in (:productIds) + <#if coalInfoIds??> and d.id in (:coalInfoIds) + + + + + group by 1, date_trunc('${timeDimension}', a.end_time) + <#list reportFields as field> + + <#if field?is_first> , + + <#if field == 'coalInfoId'>, d.id + <#if field == 'productId'>, p.id + + <#if field?has_next>, + + + + + + + + + + """, + request); + + var countSql = "select count(1) from (" + sql + ") as t"; + + var selectSql = + "select * from (" + + sql + + ") as t limit " + + request.getPageSize() + + " offset " + + request.getPageNo() * request.getPageSize(); + + Integer count = JpaUtils.execNativeQuery(em, countSql, request, Integer.class).get(0); + + List data = JpaUtils.execNativeQuery(em, selectSql, request, Tuple.class); + + var resultList = JpaUtils.convertTuplesToMap(data); + + var ans = + resultList.stream() + .map( + x -> + ((Map) x) + .entrySet().stream() + .collect( + Collectors.toMap( + (Map.Entry e) -> + CaseFormat + .LOWER_UNDERSCORE + .to( + CaseFormat + .LOWER_CAMEL, + e.getKey() + .toString()), + e -> + ObjectUtils + .defaultIfNull( + e + .getValue(), + "")))) + .toList(); + + return new PageImpl<>(ans, PageRequest.of(0, request.getPageSize()), count); + } + + } diff --git a/src/main/java/cn/lihongjie/coal/coalWashingDailyAnalysisCoalReport/controller/CoalWashingDailyAnalysisCoalReportController.java b/src/main/java/cn/lihongjie/coal/coalWashingDailyAnalysisCoalReport/controller/CoalWashingDailyAnalysisCoalReportController.java new file mode 100644 index 00000000..c03aca4d --- /dev/null +++ b/src/main/java/cn/lihongjie/coal/coalWashingDailyAnalysisCoalReport/controller/CoalWashingDailyAnalysisCoalReportController.java @@ -0,0 +1,56 @@ +package cn.lihongjie.coal.coalWashingDailyAnalysisCoalReport.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.coalWashingDailyAnalysisCoalReport.dto.CoalWashingDailyAnalysisCoalReportDto; +import cn.lihongjie.coal.coalWashingDailyAnalysisCoalReport.dto.CreateCoalWashingDailyAnalysisCoalReportDto; +import cn.lihongjie.coal.coalWashingDailyAnalysisCoalReport.dto.UpdateCoalWashingDailyAnalysisCoalReportDto; +import cn.lihongjie.coal.coalWashingDailyAnalysisCoalReport.service.CoalWashingDailyAnalysisCoalReportService; + +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("/coalWashingDailyAnalysisCoalReport") +@SysLog(module = "") +@Slf4j +@OrgScope +public class CoalWashingDailyAnalysisCoalReportController { + @Autowired private CoalWashingDailyAnalysisCoalReportService service; + + @PostMapping("/create") + public CoalWashingDailyAnalysisCoalReportDto create( + @RequestBody CreateCoalWashingDailyAnalysisCoalReportDto request) { + return this.service.create(request); + } + + @PostMapping("/update") + public CoalWashingDailyAnalysisCoalReportDto update( + @RequestBody UpdateCoalWashingDailyAnalysisCoalReportDto request) { + return this.service.update(request); + } + + @PostMapping("/delete") + public Object delete(@RequestBody IdRequest request) { + this.service.delete(request); + return true; + } + + @PostMapping("/getById") + public CoalWashingDailyAnalysisCoalReportDto 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/coalWashingDailyAnalysisCoalReport/dto/CoalWashingDailyAnalysisCoalReportDto.java b/src/main/java/cn/lihongjie/coal/coalWashingDailyAnalysisCoalReport/dto/CoalWashingDailyAnalysisCoalReportDto.java new file mode 100644 index 00000000..db8be781 --- /dev/null +++ b/src/main/java/cn/lihongjie/coal/coalWashingDailyAnalysisCoalReport/dto/CoalWashingDailyAnalysisCoalReportDto.java @@ -0,0 +1,11 @@ +package cn.lihongjie.coal.coalWashingDailyAnalysisCoalReport.dto; + +import cn.lihongjie.coal.base.dto.OrgCommonDto; + +import lombok.Data; + +@Data +public class CoalWashingDailyAnalysisCoalReportDto extends OrgCommonDto { + private String jsonConfig; + +} diff --git a/src/main/java/cn/lihongjie/coal/coalWashingDailyAnalysisCoalReport/dto/CreateCoalWashingDailyAnalysisCoalReportDto.java b/src/main/java/cn/lihongjie/coal/coalWashingDailyAnalysisCoalReport/dto/CreateCoalWashingDailyAnalysisCoalReportDto.java new file mode 100644 index 00000000..4740ad2c --- /dev/null +++ b/src/main/java/cn/lihongjie/coal/coalWashingDailyAnalysisCoalReport/dto/CreateCoalWashingDailyAnalysisCoalReportDto.java @@ -0,0 +1,11 @@ +package cn.lihongjie.coal.coalWashingDailyAnalysisCoalReport.dto; + +import cn.lihongjie.coal.base.dto.OrgCommonDto; + +import lombok.Data; + +@Data +public class CreateCoalWashingDailyAnalysisCoalReportDto extends OrgCommonDto { + + private String jsonConfig; +} diff --git a/src/main/java/cn/lihongjie/coal/coalWashingDailyAnalysisCoalReport/dto/UpdateCoalWashingDailyAnalysisCoalReportDto.java b/src/main/java/cn/lihongjie/coal/coalWashingDailyAnalysisCoalReport/dto/UpdateCoalWashingDailyAnalysisCoalReportDto.java new file mode 100644 index 00000000..4f5df095 --- /dev/null +++ b/src/main/java/cn/lihongjie/coal/coalWashingDailyAnalysisCoalReport/dto/UpdateCoalWashingDailyAnalysisCoalReportDto.java @@ -0,0 +1,12 @@ +package cn.lihongjie.coal.coalWashingDailyAnalysisCoalReport.dto; + +import cn.lihongjie.coal.base.dto.OrgCommonDto; + +import lombok.Data; + +@Data +public class UpdateCoalWashingDailyAnalysisCoalReportDto extends OrgCommonDto { + + + private String jsonConfig; +} diff --git a/src/main/java/cn/lihongjie/coal/coalWashingDailyAnalysisCoalReport/entity/CoalWashingDailyAnalysisCoalReportEntity.java b/src/main/java/cn/lihongjie/coal/coalWashingDailyAnalysisCoalReport/entity/CoalWashingDailyAnalysisCoalReportEntity.java new file mode 100644 index 00000000..a044ed2e --- /dev/null +++ b/src/main/java/cn/lihongjie/coal/coalWashingDailyAnalysisCoalReport/entity/CoalWashingDailyAnalysisCoalReportEntity.java @@ -0,0 +1,16 @@ +package cn.lihongjie.coal.coalWashingDailyAnalysisCoalReport.entity; + +import cn.lihongjie.coal.base.entity.OrgCommonEntity; + +import jakarta.persistence.Entity; + +import lombok.Data; + +@Data +@Entity +public class CoalWashingDailyAnalysisCoalReportEntity extends OrgCommonEntity { + + + private String jsonConfig; + +} diff --git a/src/main/java/cn/lihongjie/coal/coalWashingDailyAnalysisCoalReport/mapper/CoalWashingDailyAnalysisCoalReportMapper.java b/src/main/java/cn/lihongjie/coal/coalWashingDailyAnalysisCoalReport/mapper/CoalWashingDailyAnalysisCoalReportMapper.java new file mode 100644 index 00000000..34809a2b --- /dev/null +++ b/src/main/java/cn/lihongjie/coal/coalWashingDailyAnalysisCoalReport/mapper/CoalWashingDailyAnalysisCoalReportMapper.java @@ -0,0 +1,23 @@ +package cn.lihongjie.coal.coalWashingDailyAnalysisCoalReport.mapper; + +import cn.lihongjie.coal.base.mapper.BaseMapper; +import cn.lihongjie.coal.base.mapper.CommonEntityMapper; +import cn.lihongjie.coal.base.mapper.CommonMapper; +import cn.lihongjie.coal.coalWashingDailyAnalysisCoalReport.dto.CoalWashingDailyAnalysisCoalReportDto; +import cn.lihongjie.coal.coalWashingDailyAnalysisCoalReport.dto.CreateCoalWashingDailyAnalysisCoalReportDto; +import cn.lihongjie.coal.coalWashingDailyAnalysisCoalReport.dto.UpdateCoalWashingDailyAnalysisCoalReportDto; +import cn.lihongjie.coal.coalWashingDailyAnalysisCoalReport.entity.CoalWashingDailyAnalysisCoalReportEntity; + +import org.mapstruct.Mapper; +import org.mapstruct.control.DeepClone; + +@Mapper( + componentModel = org.mapstruct.MappingConstants.ComponentModel.SPRING, + uses = {CommonMapper.class, CommonEntityMapper.class}, + mappingControl = DeepClone.class) +public interface CoalWashingDailyAnalysisCoalReportMapper + extends BaseMapper< + CoalWashingDailyAnalysisCoalReportEntity, + CoalWashingDailyAnalysisCoalReportDto, + CreateCoalWashingDailyAnalysisCoalReportDto, + UpdateCoalWashingDailyAnalysisCoalReportDto> {} diff --git a/src/main/java/cn/lihongjie/coal/coalWashingDailyAnalysisCoalReport/repository/CoalWashingDailyAnalysisCoalReportRepository.java b/src/main/java/cn/lihongjie/coal/coalWashingDailyAnalysisCoalReport/repository/CoalWashingDailyAnalysisCoalReportRepository.java new file mode 100644 index 00000000..9237c17b --- /dev/null +++ b/src/main/java/cn/lihongjie/coal/coalWashingDailyAnalysisCoalReport/repository/CoalWashingDailyAnalysisCoalReportRepository.java @@ -0,0 +1,16 @@ +package cn.lihongjie.coal.coalWashingDailyAnalysisCoalReport.repository; + +import cn.lihongjie.coal.base.dao.BaseRepository; +import cn.lihongjie.coal.coalWashingDailyAnalysisCoalReport.entity.CoalWashingDailyAnalysisCoalReportEntity; + +import org.springframework.data.jpa.repository.Query; +import org.springframework.stereotype.Repository; + +import java.util.List; + +@Repository +public interface CoalWashingDailyAnalysisCoalReportRepository + extends BaseRepository { + @Query("select false") + boolean isLinked(List ids); +} diff --git a/src/main/java/cn/lihongjie/coal/coalWashingDailyAnalysisCoalReport/service/CoalWashingDailyAnalysisCoalReportService.java b/src/main/java/cn/lihongjie/coal/coalWashingDailyAnalysisCoalReport/service/CoalWashingDailyAnalysisCoalReportService.java new file mode 100644 index 00000000..6dd8560a --- /dev/null +++ b/src/main/java/cn/lihongjie/coal/coalWashingDailyAnalysisCoalReport/service/CoalWashingDailyAnalysisCoalReportService.java @@ -0,0 +1,85 @@ +package cn.lihongjie.coal.coalWashingDailyAnalysisCoalReport.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.coalWashingDailyAnalysisCoalReport.dto.CoalWashingDailyAnalysisCoalReportDto; +import cn.lihongjie.coal.coalWashingDailyAnalysisCoalReport.dto.CreateCoalWashingDailyAnalysisCoalReportDto; +import cn.lihongjie.coal.coalWashingDailyAnalysisCoalReport.dto.UpdateCoalWashingDailyAnalysisCoalReportDto; +import cn.lihongjie.coal.coalWashingDailyAnalysisCoalReport.entity.CoalWashingDailyAnalysisCoalReportEntity; +import cn.lihongjie.coal.coalWashingDailyAnalysisCoalReport.mapper.CoalWashingDailyAnalysisCoalReportMapper; +import cn.lihongjie.coal.coalWashingDailyAnalysisCoalReport.repository.CoalWashingDailyAnalysisCoalReportRepository; +import cn.lihongjie.coal.dbFunctions.DbFunctionService; +import cn.lihongjie.coal.exception.BizException; + +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 CoalWashingDailyAnalysisCoalReportService + extends BaseService< + CoalWashingDailyAnalysisCoalReportEntity, + CoalWashingDailyAnalysisCoalReportRepository> { + @Autowired private CoalWashingDailyAnalysisCoalReportRepository repository; + + @Autowired private CoalWashingDailyAnalysisCoalReportMapper mapper; + + @Autowired private ConversionService conversionService; + + @Autowired private DbFunctionService dbFunctionService; + + public CoalWashingDailyAnalysisCoalReportDto create( + CreateCoalWashingDailyAnalysisCoalReportDto request) { + CoalWashingDailyAnalysisCoalReportEntity entity = mapper.toEntity(request); + + this.repository.save(entity); + return getById(entity.getId()); + } + + public CoalWashingDailyAnalysisCoalReportDto update( + UpdateCoalWashingDailyAnalysisCoalReportDto request) { + CoalWashingDailyAnalysisCoalReportEntity entity = this.repository.get(request.getId()); + this.mapper.updateEntity(entity, request); + + this.repository.save(entity); + + return getById(entity.getId()); + } + + public void delete(IdRequest request) { + boolean linked = this.repository.isLinked(request.getIds()); + + if (linked) { + throw new BizException("数据已被关联,无法删除"); + } + + this.repository.deleteAllById(request.getIds()); + } + + public CoalWashingDailyAnalysisCoalReportDto getById(String id) { + CoalWashingDailyAnalysisCoalReportEntity 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); + } +} diff --git a/src/main/resources/scripts/dict/enum/coalWashingDailyAnalysisCoalReportDict.groovy b/src/main/resources/scripts/dict/enum/coalWashingDailyAnalysisCoalReportDict.groovy new file mode 100644 index 00000000..4dfe139f --- /dev/null +++ b/src/main/resources/scripts/dict/enum/coalWashingDailyAnalysisCoalReportDict.groovy @@ -0,0 +1,17 @@ + +package scripts.dict + +import cn.lihongjie.coal.base.dto.CommonQuery +import cn.lihongjie.coal.coalWashingDailyAnalysisCoalReport.controller.CoalWashingDailyAnalysisCoalReportController +import org.springframework.context.ApplicationContext + +ApplicationContext ioc = ioc + +def controller = ioc.getBean(CoalWashingDailyAnalysisCoalReportController.class) + + + + +return controller.list(new CommonQuery()) + +