diff --git a/src/main/java/cn/lihongjie/coal/coalWashingDailyAnalysis/dto/CoalWashingDailyAnalysisDto.java b/src/main/java/cn/lihongjie/coal/coalWashingDailyAnalysis/dto/CoalWashingDailyAnalysisDto.java index 9c410412..1d3bd329 100644 --- a/src/main/java/cn/lihongjie/coal/coalWashingDailyAnalysis/dto/CoalWashingDailyAnalysisDto.java +++ b/src/main/java/cn/lihongjie/coal/coalWashingDailyAnalysis/dto/CoalWashingDailyAnalysisDto.java @@ -40,6 +40,8 @@ public class CoalWashingDailyAnalysisDto extends OrgCommonDto { @Comment("停机时间") private LocalDateTime endTime; + @Comment("运行时长(小时)") + private Double runHours; @Comment("皮带秤设备组") private String pdcDeviceGroup; diff --git a/src/main/java/cn/lihongjie/coal/coalWashingDailyAnalysis/entity/CoalWashingDailyAnalysisEntity.java b/src/main/java/cn/lihongjie/coal/coalWashingDailyAnalysis/entity/CoalWashingDailyAnalysisEntity.java index e0222e51..36c95fa2 100644 --- a/src/main/java/cn/lihongjie/coal/coalWashingDailyAnalysis/entity/CoalWashingDailyAnalysisEntity.java +++ b/src/main/java/cn/lihongjie/coal/coalWashingDailyAnalysis/entity/CoalWashingDailyAnalysisEntity.java @@ -16,6 +16,9 @@ import org.hibernate.annotations.Comment; import org.hibernate.annotations.Fetch; import org.hibernate.annotations.FetchMode; +import java.math.BigDecimal; +import java.math.RoundingMode; +import java.time.Duration; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; @@ -32,17 +35,18 @@ public class CoalWashingDailyAnalysisEntity extends OrgCommonEntity { @Comment("归档状态") private String archiveStatus = "0"; - @Comment("日期") private LocalDate date; - @Comment("开机时间") private LocalDateTime startTime; @Comment("停机时间") private LocalDateTime endTime; + @Comment("运行时长(小时)") + private Double runHours; + @Comment("皮带秤设备组") private String pdcDeviceGroup; @@ -56,11 +60,9 @@ public class CoalWashingDailyAnalysisEntity extends OrgCommonEntity { @Fetch(FetchMode.SUBSELECT) private List ymDetails; - @ElementCollection() @CollectionTable(foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT)) @Fetch(FetchMode.SUBSELECT) - private List kfItems; // @ElementCollection @@ -71,7 +73,6 @@ public class CoalWashingDailyAnalysisEntity extends OrgCommonEntity { @ElementCollection() @CollectionTable(foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT)) @Fetch(FetchMode.SUBSELECT) - private List paramsInfo; @Comment("目标大堆 灰") @@ -121,8 +122,6 @@ public class CoalWashingDailyAnalysisEntity extends OrgCommonEntity { private String remark11; private String remark12; - - public void calculate(String type) { if (CollectionUtils.isEmpty(inputItems)) { @@ -245,4 +244,31 @@ public class CoalWashingDailyAnalysisEntity extends OrgCommonEntity { getWeight, (x, v) -> x.setDdAvgP2(v == null ? null : v.doubleValue())); } + + @Override + public void prePersist() { + super.prePersist(); + this.updateBeforeSave(); + } + + @Override + public void preUpdate() { + super.preUpdate(); + this.updateBeforeSave(); + } + + private void updateBeforeSave() { + + if (startTime != null && endTime != null) { + + this.runHours = + BigDecimal.valueOf( + Math.max( + 0.0, + Duration.between(startTime, endTime).toMinutes() + / 60.0)) + .setScale(2, RoundingMode.HALF_UP) + .doubleValue(); + } + } } diff --git a/src/main/java/cn/lihongjie/coal/dataCollector/service/DataCollectorService.java b/src/main/java/cn/lihongjie/coal/dataCollector/service/DataCollectorService.java index a5417e0c..3069343a 100644 --- a/src/main/java/cn/lihongjie/coal/dataCollector/service/DataCollectorService.java +++ b/src/main/java/cn/lihongjie/coal/dataCollector/service/DataCollectorService.java @@ -11,6 +11,10 @@ import cn.lihongjie.coal.dataCollector.mapper.DataCollectorMapper; import cn.lihongjie.coal.dataCollector.repository.DataCollectorRepository; import cn.lihongjie.coal.dbFunctions.DbFunctionService; import cn.lihongjie.coal.exception.BizException; +import cn.lihongjie.coal.rabbitmq.RabbitMQService; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; import jakarta.annotation.PostConstruct; @@ -50,6 +54,7 @@ public class DataCollectorService @Autowired AmqpAdmin amqpAdmin; @Autowired RabbitTemplate rabbitTemplate; + @Autowired ObjectMapper objectMapper; @PostConstruct public void init() { @@ -70,16 +75,14 @@ public class DataCollectorService return getById(entity.getId()); } + @Autowired private RabbitMQService rabbitMQService; public Object getLocalStatus(String appKey) { - var o = rabbitTemplate.convertSendAndReceive( - "dataCollector." + appKey, - Map.of("action", "getLocalStatus")); - new CorrelationData(UUID.randomUUID().toString()); - - - + var o = + rabbitTemplate.convertSendAndReceive( + "dataCollector." + appKey, Map.of("action", "getLocalStatus")); + new CorrelationData(UUID.randomUUID().toString()); return o; } @@ -87,29 +90,41 @@ public class DataCollectorService public void updateLocalStatus(String appKey, List> status) { rabbitTemplate.convertSendAndReceive( - "dataCollector." + appKey, - Map.of("action", "updateLocalStatus", "data", status)); - new CorrelationData(UUID.randomUUID().toString()); - - + "dataCollector." + appKey, Map.of("action", "updateLocalStatus", "data", status)); + new CorrelationData(UUID.randomUUID().toString()); } - public void deleteLocalStatus(String appKey, List keys) { - rabbitTemplate.convertSendAndReceive( - "dataCollector." + appKey, - Map.of("action", "deleteLocalStatus", "data", keys)); + rabbitTemplate.convertSendAndReceive( + "dataCollector." + appKey, Map.of("action", "deleteLocalStatus", "data", keys)); new CorrelationData(UUID.randomUUID().toString()); - - } + public Object sendMsg(String id, String action, Object data, Map headers) { + + DataCollectorEntity collector = get(id); + + if (data == null){ + data = new HashMap<>(); + } + if (collector == null) { + throw new BizException("数据采集器不存在"); + } + return rabbitTemplate.convertSendAndReceive( + "dataCollector." + collector.getAppKey(), + Map.of("action", action, "data", objectMapper.convertValue(data, JsonNode.class)), + m -> { + m.getMessageProperties().getHeaders().putAll(headers); + return m; + }, + new CorrelationData(UUID.randomUUID().toString())); + } @Nullable private void createQueue(DataCollectorEntity entity) { 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 a39da76e..a4f9a234 100644 --- a/src/main/java/cn/lihongjie/coal/weightDeviceData/controller/WeightDeviceDataController.java +++ b/src/main/java/cn/lihongjie/coal/weightDeviceData/controller/WeightDeviceDataController.java @@ -4,10 +4,7 @@ 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.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.dto.*; import cn.lihongjie.coal.weightDeviceData.service.WeightDeviceDataService; import lombok.extern.slf4j.Slf4j; @@ -53,12 +50,18 @@ public class WeightDeviceDataController { return this.service.list(request); } - @PostMapping("/report") public Page report(@RequestBody WeightDeviceDataReportRequest request) { return this.service.report(request); } + @PostMapping("/reSync") + public Object reSync(@RequestBody ReSyncRequest request) { + + this.service.reSync(request); + return true; + } + @PostMapping("/archive") public Object archive(@RequestBody IdRequest request) { this.service.archive(request); diff --git a/src/main/java/cn/lihongjie/coal/weightDeviceData/dto/ReSyncRequest.java b/src/main/java/cn/lihongjie/coal/weightDeviceData/dto/ReSyncRequest.java new file mode 100644 index 00000000..89b9322e --- /dev/null +++ b/src/main/java/cn/lihongjie/coal/weightDeviceData/dto/ReSyncRequest.java @@ -0,0 +1,26 @@ +package cn.lihongjie.coal.weightDeviceData.dto; + +import lombok.Data; + +import org.hibernate.annotations.Comment; + +import java.time.LocalDateTime; +import java.util.List; + +@Data +public class ReSyncRequest { + + @Comment("需要重新同步的id") + private List ids; + + @Comment("开始时间") + private LocalDateTime start; + + @Comment("结束时间") + private LocalDateTime end; + + @Comment("流水号") + private List flowNumbers; + + +} 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 837ce7a0..3803304d 100644 --- a/src/main/java/cn/lihongjie/coal/weightDeviceData/service/WeightDeviceDataService.java +++ b/src/main/java/cn/lihongjie/coal/weightDeviceData/service/WeightDeviceDataService.java @@ -3,23 +3,21 @@ package cn.lihongjie.coal.weightDeviceData.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.common.FreeMakerUtils; import cn.lihongjie.coal.common.JpaUtils; +import cn.lihongjie.coal.dataCollector.service.DataCollectorService; +import cn.lihongjie.coal.dataCollectorLog.service.DataCollectorLogService; import cn.lihongjie.coal.dbFunctions.DbFunctionService; import cn.lihongjie.coal.exception.BizException; -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.rabbitmq.RabbitMQService; +import cn.lihongjie.coal.weightDeviceData.dto.*; import cn.lihongjie.coal.weightDeviceData.entity.WeightDeviceDataEntity; import cn.lihongjie.coal.weightDeviceData.mapper.WeightDeviceDataMapper; import cn.lihongjie.coal.weightDeviceData.repository.WeightDeviceDataRepository; import com.google.common.base.CaseFormat; -import jakarta.persistence.EntityManager; -import jakarta.persistence.PersistenceContext; -import jakarta.persistence.Query; -import jakarta.persistence.Tuple; +import jakarta.persistence.*; import lombok.extern.slf4j.Slf4j; @@ -35,6 +33,7 @@ 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; @@ -52,6 +51,11 @@ public class WeightDeviceDataService @Autowired private DbFunctionService dbFunctionService; @PersistenceContext private EntityManager em; + @Autowired DataCollectorService dataCollectorService; + + @Autowired RabbitMQService rabbitMQService; + private DataCollectorLogService dataCollectorLogService; + public WeightDeviceDataDto create(CreateWeightDeviceDataDto request) { WeightDeviceDataEntity entity = mapper.toEntity(request); @@ -59,9 +63,66 @@ public class WeightDeviceDataService return getById(entity.getId()); } + public void reSync(ReSyncRequest requst) { + + TypedQuery query = + em.createQuery( + FreeMakerUtils.render( + """ + + + from WeightDeviceDataEntity d where 1=1 + + <#if ids??> + and d.id in (:ids) + + + <#if start??> + + and d.minTime >= :start + + + + <#if end??> + + and d.minTime <= :end + + + + <#if flowNumbers??> + + and d.flowNumber in (:flowNumbers) + + + + + + """, + requst), + WeightDeviceDataEntity.class); + + JpaUtils.setQueryParameter(query, requst); + + List datas = query.getResultList(); + + Map> groupByDc = + datas.stream() + .collect( + Collectors.groupingBy( + e -> e.getDevice().getDataCollector().getId())); + + for (Map.Entry> entry : groupByDc.entrySet()) { + + Object object = + dataCollectorService.sendMsg(entry.getKey(), "weigth20.sync", requst, Map.of()); + + log.info("reSync result: {}", object); + } + } + public WeightDeviceDataDto update(UpdateWeightDeviceDataDto request) { WeightDeviceDataEntity entity = this.repository.get(request.getId()); - if (this.repository.containArchived(request.getId())){ + if (this.repository.containArchived(request.getId())) { throw new BizException("部分数据已归档,无法编辑或删除"); } this.mapper.updateEntity(entity, request); @@ -72,7 +133,7 @@ public class WeightDeviceDataService } public void delete(IdRequest request) { - if (this.repository.containArchived(request)){ + if (this.repository.containArchived(request)) { throw new BizException("部分数据已归档,无法编辑或删除"); } this.repository.deleteAllById(request.getIds()); @@ -125,24 +186,26 @@ public class WeightDeviceDataService .map( x -> { String s = - StringUtils.equalsAnyIgnoreCase(x.getFunction(), "sum", "min", "max", "avg") ? - x.getFunction() - + "( cast( d." - + CaseFormat.UPPER_CAMEL.to( - CaseFormat.LOWER_UNDERSCORE, - x.getFieldName()) - - + " as numeric)) " - - : - - x.getFunction() - + "( d." - + CaseFormat.UPPER_CAMEL.to( - CaseFormat.LOWER_UNDERSCORE, - x.getFieldName()) - - + ") "; + StringUtils.equalsAnyIgnoreCase( + x.getFunction(), + "sum", + "min", + "max", + "avg") + ? x.getFunction() + + "( cast( d." + + CaseFormat.UPPER_CAMEL.to( + CaseFormat + .LOWER_UNDERSCORE, + x.getFieldName()) + + " as numeric)) " + : 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)"; @@ -191,8 +254,6 @@ public class WeightDeviceDataService where += " and ( d.invalid is null or not d.invalid ) "; where += " and ( d.finished is null or d.finished ) "; - - var sql = "select DATE_TRUNC('" + request.getTimeDimension() @@ -264,8 +325,6 @@ public class WeightDeviceDataService countQuery.setParameter("specification", "%" + request.getSpecification() + "%"); } - - var resultList = JpaUtils.convertTuplesToMap(selectQuery.getResultList()); var ans =