From fdcd91b49ce868b39b168c7ddaf890000d9b7033 Mon Sep 17 00:00:00 2001 From: lihongjie0209 Date: Mon, 15 Apr 2024 08:52:20 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E6=8A=A5=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../coal/device/dto/CreateDeviceDto.java | 9 ++ .../coal/device/dto/UpdateDeviceDto.java | 9 ++ .../dto/UpdateWeightColumnConfigDto.java | 2 + .../dto/WeightColumnConfigDto.java | 2 +- .../entity/WeightColumnConfigEntity.java | 2 + .../WeightDeviceDataController.java | 7 + .../dto/WeightDeviceDataReportRequest.java | 28 ++-- .../service/WeightDeviceDataService.java | 122 ++++++++++-------- 8 files changed, 114 insertions(+), 67 deletions(-) diff --git a/src/main/java/cn/lihongjie/coal/device/dto/CreateDeviceDto.java b/src/main/java/cn/lihongjie/coal/device/dto/CreateDeviceDto.java index 7613ee56..6e169a0e 100644 --- a/src/main/java/cn/lihongjie/coal/device/dto/CreateDeviceDto.java +++ b/src/main/java/cn/lihongjie/coal/device/dto/CreateDeviceDto.java @@ -1,6 +1,11 @@ package cn.lihongjie.coal.device.dto; import cn.lihongjie.coal.base.dto.OrgCommonDto; +import cn.lihongjie.coal.device.entity.DeviceEntity; +import cn.lihongjie.coal.errorMsg.ErrorMsgCode; +import cn.lihongjie.coal.validator.OrgUniq; +import cn.lihongjie.coal.validator.RequireCode; +import cn.lihongjie.coal.validator.RequireName; import jakarta.persistence.ManyToOne; @@ -9,6 +14,10 @@ import lombok.Data; import org.hibernate.annotations.Comment; @Data +@RequireName +@RequireCode +@OrgUniq(fields = {"code"}, message = ErrorMsgCode.UNIQ_CODE, entityClass = DeviceEntity.class) +@OrgUniq(fields = {"name"}, message = ErrorMsgCode.UNIQ_NAME, entityClass = DeviceEntity.class) public class CreateDeviceDto extends OrgCommonDto { diff --git a/src/main/java/cn/lihongjie/coal/device/dto/UpdateDeviceDto.java b/src/main/java/cn/lihongjie/coal/device/dto/UpdateDeviceDto.java index 491d3c39..067235fb 100644 --- a/src/main/java/cn/lihongjie/coal/device/dto/UpdateDeviceDto.java +++ b/src/main/java/cn/lihongjie/coal/device/dto/UpdateDeviceDto.java @@ -1,6 +1,11 @@ package cn.lihongjie.coal.device.dto; import cn.lihongjie.coal.base.dto.OrgCommonDto; +import cn.lihongjie.coal.device.entity.DeviceEntity; +import cn.lihongjie.coal.errorMsg.ErrorMsgCode; +import cn.lihongjie.coal.validator.OrgUniq; +import cn.lihongjie.coal.validator.RequireCode; +import cn.lihongjie.coal.validator.RequireName; import jakarta.persistence.ManyToOne; @@ -9,6 +14,10 @@ import lombok.Data; import org.hibernate.annotations.Comment; @Data +@RequireName +@RequireCode +@OrgUniq(fields = {"code"}, message = ErrorMsgCode.UNIQ_CODE, entityClass = DeviceEntity.class) +@OrgUniq(fields = {"name"}, message = ErrorMsgCode.UNIQ_NAME, entityClass = DeviceEntity.class) public class UpdateDeviceDto extends OrgCommonDto { @Comment("设备型号") private String model; diff --git a/src/main/java/cn/lihongjie/coal/weightColumnConfig/dto/UpdateWeightColumnConfigDto.java b/src/main/java/cn/lihongjie/coal/weightColumnConfig/dto/UpdateWeightColumnConfigDto.java index 73143289..5f1f2070 100644 --- a/src/main/java/cn/lihongjie/coal/weightColumnConfig/dto/UpdateWeightColumnConfigDto.java +++ b/src/main/java/cn/lihongjie/coal/weightColumnConfig/dto/UpdateWeightColumnConfigDto.java @@ -7,4 +7,6 @@ import lombok.Data; @Data public class UpdateWeightColumnConfigDto extends OrgCommonDto { private Integer width; + + private String displayName; } diff --git a/src/main/java/cn/lihongjie/coal/weightColumnConfig/dto/WeightColumnConfigDto.java b/src/main/java/cn/lihongjie/coal/weightColumnConfig/dto/WeightColumnConfigDto.java index fcd32eba..6c21521b 100644 --- a/src/main/java/cn/lihongjie/coal/weightColumnConfig/dto/WeightColumnConfigDto.java +++ b/src/main/java/cn/lihongjie/coal/weightColumnConfig/dto/WeightColumnConfigDto.java @@ -6,6 +6,6 @@ import lombok.Data; @Data public class WeightColumnConfigDto extends OrgCommonDto { - + private String displayName; private Integer width; } diff --git a/src/main/java/cn/lihongjie/coal/weightColumnConfig/entity/WeightColumnConfigEntity.java b/src/main/java/cn/lihongjie/coal/weightColumnConfig/entity/WeightColumnConfigEntity.java index 7b7112ea..a081206d 100644 --- a/src/main/java/cn/lihongjie/coal/weightColumnConfig/entity/WeightColumnConfigEntity.java +++ b/src/main/java/cn/lihongjie/coal/weightColumnConfig/entity/WeightColumnConfigEntity.java @@ -10,6 +10,8 @@ import lombok.Data; @Entity public class WeightColumnConfigEntity extends OrgCommonEntity { + private String displayName; + private Integer width; diff --git a/src/main/java/cn/lihongjie/coal/weightDeviceData/controller/WeightDeviceDataController.java b/src/main/java/cn/lihongjie/coal/weightDeviceData/controller/WeightDeviceDataController.java index 801a503d..a39da76e 100644 --- a/src/main/java/cn/lihongjie/coal/weightDeviceData/controller/WeightDeviceDataController.java +++ b/src/main/java/cn/lihongjie/coal/weightDeviceData/controller/WeightDeviceDataController.java @@ -7,6 +7,7 @@ import cn.lihongjie.coal.base.dto.IdRequest; import cn.lihongjie.coal.weightDeviceData.dto.CreateWeightDeviceDataDto; import cn.lihongjie.coal.weightDeviceData.dto.UpdateWeightDeviceDataDto; import cn.lihongjie.coal.weightDeviceData.dto.WeightDeviceDataDto; +import cn.lihongjie.coal.weightDeviceData.dto.WeightDeviceDataReportRequest; import cn.lihongjie.coal.weightDeviceData.service.WeightDeviceDataService; import lombok.extern.slf4j.Slf4j; @@ -52,6 +53,12 @@ public class WeightDeviceDataController { return this.service.list(request); } + + @PostMapping("/report") + public Page report(@RequestBody WeightDeviceDataReportRequest request) { + return this.service.report(request); + } + @PostMapping("/archive") public Object archive(@RequestBody IdRequest request) { this.service.archive(request); diff --git a/src/main/java/cn/lihongjie/coal/weightDeviceData/dto/WeightDeviceDataReportRequest.java b/src/main/java/cn/lihongjie/coal/weightDeviceData/dto/WeightDeviceDataReportRequest.java index 36b7d173..73636453 100644 --- a/src/main/java/cn/lihongjie/coal/weightDeviceData/dto/WeightDeviceDataReportRequest.java +++ b/src/main/java/cn/lihongjie/coal/weightDeviceData/dto/WeightDeviceDataReportRequest.java @@ -12,7 +12,8 @@ import java.util.*; @Data public class WeightDeviceDataReportRequest extends CommonQuery { - @Comment(""" + @Comment( + """ 时间维度 year month @@ -21,37 +22,34 @@ day hour """) private String timeDimension; - @Comment(""" + + @Comment( + """ 统计字段 """) private List reportFields; + private List fieldInfos; private LocalDateTime startTime; private LocalDateTime endTime; - /** - * 过滤条件 - */ - private String plateNo; - private String sendOrganization; + /** 过滤条件 */ + private String plateNo; + + private String sendOrganization; private String receiveOrganization; - private String goods; + private String goods; private String specification; - private String mzUser; + private String mzUser; private String pzUser; @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/weightDeviceData/service/WeightDeviceDataService.java b/src/main/java/cn/lihongjie/coal/weightDeviceData/service/WeightDeviceDataService.java index 384919f5..63baf47f 100644 --- a/src/main/java/cn/lihongjie/coal/weightDeviceData/service/WeightDeviceDataService.java +++ b/src/main/java/cn/lihongjie/coal/weightDeviceData/service/WeightDeviceDataService.java @@ -25,6 +25,7 @@ import jakarta.persistence.Tuple; import lombok.extern.slf4j.Slf4j; import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.convert.ConversionService; @@ -50,8 +51,7 @@ public class WeightDeviceDataService @Autowired private ConversionService conversionService; @Autowired private DbFunctionService dbFunctionService; - @PersistenceContext - private EntityManager em; + @PersistenceContext private EntityManager em; public WeightDeviceDataDto create(CreateWeightDeviceDataDto request) { WeightDeviceDataEntity entity = mapper.toEntity(request); @@ -138,7 +138,7 @@ public class WeightDeviceDataService public Page report(WeightDeviceDataReportRequest request) { - if (StringUtils.isEmpty(request.getTimeDimension())){ + if (StringUtils.isEmpty(request.getTimeDimension())) { request.setTimeDimension("day"); } @@ -146,24 +146,37 @@ public class WeightDeviceDataService CollectionUtils.isEmpty(request.getReportFields()) ? " 1 " : request.getReportFields().stream() - .map(x -> CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, x)) - .map(s -> "d." + s) - .collect(Collectors.joining(",")); + .map(x -> CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, x)) + .map(s -> "d." + s) + .collect(Collectors.joining(",")); String fieldSql = CollectionUtils.isEmpty(request.getFieldInfos()) - ? " " + ? " 1 " : request.getFieldInfos().stream() .map( - x -> - x.getFunction() - + "( d." - + CaseFormat.UPPER_CAMEL.to( - CaseFormat.LOWER_UNDERSCORE, - x.getFieldName()) - + ") " - + " as " - + x.getFieldName()) + x -> { + String s = + x.getFunction() + + "( d." + + CaseFormat.UPPER_CAMEL.to( + CaseFormat.LOWER_UNDERSCORE, + x.getFieldName()) + + ") "; + + if (ObjectUtils.notEqual(x.getFunction(), "count")) { + + s = "round(cast(" + s + " as numeric), 2)"; + } + + s += + (" as " + + x.getFieldName() + + StringUtils.capitalize( + x.getFunction())); + + return s; + }) .collect(Collectors.joining(",")); String where = "where 1 = 1 "; @@ -198,26 +211,32 @@ public class WeightDeviceDataService var sql = "select DATE_TRUNC('" - + request.getTimeDimension() - + "', d.pass_time) as time,\n" - + groupSql - + ",\n" - + fieldSql - + "\n from t_weight_device_data d\n" - + " " - + where - + " " - + "\n" - + "group by DATE_TRUNC('" - + request.getTimeDimension() - + "', d.pass_time), " - + groupSql - + "\n" - + "order by time desc , cnt desc"; + + request.getTimeDimension() + + "', d.mz_time) as time,\n" + + groupSql + + ",\n" + + fieldSql + + "\n from t_weight_device_data d\n" + + " " + + where + + " " + + "\n" + + "group by DATE_TRUNC('" + + request.getTimeDimension() + + "', d.mz_time), " + + groupSql + + "\n" + + "order by time desc "; var countSql = "select count(1) from (" + sql + ") as t"; - var selectSql = "select * from (" + sql + ") as t limit " + request.getPageSize() + " offset " + request.getPageNo() * request.getPageSize(); + var selectSql = + "select * from (" + + sql + + ") as t limit " + + request.getPageSize() + + " offset " + + request.getPageNo() * request.getPageSize(); Query selectQuery = em.createNativeQuery(selectSql, Tuple.class); Query countQuery = em.createNativeQuery(countSql, Integer.class); @@ -232,7 +251,6 @@ public class WeightDeviceDataService countQuery.setParameter("endTime", request.getEndTime()); } - if (StringUtils.isNotEmpty(request.getPlateNo())) { selectQuery.setParameter("plateNo", "%" + request.getPlateNo() + "%"); countQuery.setParameter("plateNo", "%" + request.getPlateNo() + "%"); @@ -244,8 +262,10 @@ public class WeightDeviceDataService } if (StringUtils.isNotEmpty(request.getReceiveOrganization())) { - selectQuery.setParameter("receiveOrganization", "%" + request.getReceiveOrganization() + "%"); - countQuery.setParameter("receiveOrganization", "%" + request.getReceiveOrganization() + "%"); + selectQuery.setParameter( + "receiveOrganization", "%" + request.getReceiveOrganization() + "%"); + countQuery.setParameter( + "receiveOrganization", "%" + request.getReceiveOrganization() + "%"); } if (StringUtils.isNotEmpty(request.getGoods())) { @@ -259,7 +279,6 @@ public class WeightDeviceDataService countQuery.setParameter("specification", "%" + request.getSpecification() + "%"); } - var resultList = JpaUtils.convertTuplesToMap(selectQuery.getResultList()); var ans = @@ -268,21 +287,22 @@ public class WeightDeviceDataService x -> ((Map) x) .entrySet().stream() - .collect( - Collectors.toMap( - (Map.Entry e) -> - CaseFormat - .LOWER_UNDERSCORE - .to( - CaseFormat - .LOWER_CAMEL, - e.getKey() - .toString()), - e -> e.getValue()))) + .collect( + Collectors.toMap( + (Map.Entry e) -> + CaseFormat + .LOWER_UNDERSCORE + .to( + CaseFormat + .LOWER_CAMEL, + e.getKey() + .toString()), + e -> e.getValue()))) .toList(); - - - return new PageImpl<>(ans, PageRequest.of(0, request.getPageSize()), Integer.parseInt(countQuery.getSingleResult().toString())); + return new PageImpl<>( + ans, + PageRequest.of(0, request.getPageSize()), + Integer.parseInt(countQuery.getSingleResult().toString())); } }