From 3d4e5941c728ff1c13d05195299c9c2163060ea4 Mon Sep 17 00:00:00 2001 From: Lius <Lius2225@163.com> Date: 星期六, 28 六月 2025 10:17:20 +0800 Subject: [PATCH] update --- lxzn-module-msi/src/main/java/org/jeecg/modules/msi/webapi/controller/MsiWebapiJsonController.java | 4 lxzn-module-system/lxzn-system-start/src/main/resources/application-dev.yml | 2 lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/service/IMdcEquipmentRunningSectionService.java | 3 lxzn-module-system/lxzn-system-biz/src/main/java/org/jeecg/modules/system/controller/TriggerCreateController.java | 204 ++++++++++ lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/mapper/MdcEquipmentFaultInfoMapper.java | 22 + lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/service/impl/MdcEquipmentFaultInfoServiceImpl.java | 380 +++++++++++++++++++ lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/vo/EquFaultRecord.java | 32 + lxzn-module-system/lxzn-system-biz/src/main/java/org/jeecg/modules/system/vo/TriggerVo.java | 17 lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/service/impl/MdcEquipmentRunningSectionServiceImpl.java | 5 lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/service/impl/MdcEquipmentStatisticalInfoServiceImpl.java | 28 - lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/vo/TimeInterval.java | 21 + lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/mapper/xml/MdcEquipmentFaultInfoMapper.xml | 30 + lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/job/CollectEquipmentDataMonthJob.java | 70 ++- lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/mapper/MdcEquipmentRunningSectionMapper.java | 2 lxzn-module-msi/src/main/java/org/jeecg/modules/msi/webapi/service/impl/MsiWebapiJsonServiceImpl.java | 148 ++++-- lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/mapper/xml/MdcEquipmentRunningSectionMapper.xml | 4 lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/service/IMdcEquipmentFaultInfoService.java | 19 lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/entity/MdcEquipmentFaultInfo.java | 87 ++++ lxzn-module-system/lxzn-system-start/src/main/resources/application-prod.yml | 36 + lxzn-module-mdc-common/src/main/java/org/jeecg/modules/mdc/util/DateUtils.java | 32 + lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/mapper/xml/MdcEfficiencyReportMapper.xml | 6 21 files changed, 1,034 insertions(+), 118 deletions(-) diff --git a/lxzn-module-mdc-common/src/main/java/org/jeecg/modules/mdc/util/DateUtils.java b/lxzn-module-mdc-common/src/main/java/org/jeecg/modules/mdc/util/DateUtils.java index 1a41d3e..34592cb 100644 --- a/lxzn-module-mdc-common/src/main/java/org/jeecg/modules/mdc/util/DateUtils.java +++ b/lxzn-module-mdc-common/src/main/java/org/jeecg/modules/mdc/util/DateUtils.java @@ -6,9 +6,7 @@ import java.text.DecimalFormat; import java.text.ParseException; import java.text.SimpleDateFormat; -import java.time.DayOfWeek; -import java.time.LocalDate; -import java.time.ZoneId; +import java.time.*; import java.time.temporal.ChronoField; import java.time.temporal.ChronoUnit; import java.util.*; @@ -1086,4 +1084,32 @@ return dateString; } + + /** + * Date涓嶭ocalDateTime浜掔浉杞崲鐨勫伐鍏锋柟娉� + * + * @param date + * @return + */ + public static LocalDateTime convertToLocalDateTime(Date date) { + if (date == null) { + return null; + } + return Instant.ofEpochMilli(date.getTime()) + .atZone(ZoneId.systemDefault()) + .toLocalDateTime(); + } + + /** + * Date涓嶭ocalDateTime浜掔浉杞崲鐨勫伐鍏锋柟娉� + * + * @param localDateTime + * @return + */ + public static Date convertToDate(LocalDateTime localDateTime) { + if (localDateTime == null) { + return null; + } + return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant()); + } } diff --git a/lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/entity/MdcEquipmentFaultInfo.java b/lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/entity/MdcEquipmentFaultInfo.java new file mode 100644 index 0000000..575a8c7 --- /dev/null +++ b/lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/entity/MdcEquipmentFaultInfo.java @@ -0,0 +1,87 @@ +package org.jeecg.modules.mdc.entity; + +import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableId; +import com.baomidou.mybatisplus.annotation.TableName; +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.experimental.Accessors; +import org.jeecgframework.poi.excel.annotation.Excel; + +import java.io.Serializable; +import java.math.BigDecimal; +import java.util.Date; + +/** + * @Description: 鏁呴殰鐜囪〃 + * @Author: Lius + * @Date: 2025-06-16 + * @Version: V1.0 + */ +@Data +@TableName("mdc_equipment_fault_info") +@EqualsAndHashCode(callSuper = false) +@Accessors(chain = true) +@ApiModel(value = "mdc_equipment_fault_info瀵硅薄", description = "鏁呴殰鐜囪〃") +public class MdcEquipmentFaultInfo implements Serializable { + + private static final long serialVersionUID = 7825739575204258911L; + + /** + * 涓婚敭 + */ + @TableId(type = IdType.ASSIGN_ID) + @ApiModelProperty(value = "涓婚敭") + private String id; + /** + * 璁惧id + */ + @Excel(name = "璁惧id", width = 15) + @ApiModelProperty(value = "璁惧id") + private String equipmentId; + /** + * 鏁呴殰鏃堕暱 + */ + @Excel(name = "鏁呴殰鏃堕暱", width = 15) + @ApiModelProperty(value = "鏁呴殰鏃堕暱") + private Integer faultLong = 0; + /** + * 鏁呴殰鐜� + */ + @Excel(name = "鏁呴殰鐜�", width = 15) + @ApiModelProperty(value = "鏁呴殰鐜�") + private BigDecimal faultRate = BigDecimal.ZERO; + /** + * 鍘婚櫎鏁呴殰鏃堕棿鐨勮繍琛屾椂闂� + */ + @Excel(name = "鍘婚櫎鏁呴殰鏃堕棿鐨勮繍琛屾椂闂�", width = 15) + @ApiModelProperty(value = "鍘婚櫎鏁呴殰鏃堕棿鐨勮繍琛屾椂闂�") + private Integer removeFaultRunLong = 0; + /** + * 鍘婚櫎鏁呴殰鏃堕棿鍒╃敤鐜� + */ + @Excel(name = "鍘婚櫎鏁呴殰鏃堕棿鍒╃敤鐜�", width = 15) + @ApiModelProperty(value = "鍘婚櫎鏁呴殰鏃堕棿鍒╃敤鐜�") + private BigDecimal removeFaultRate = BigDecimal.ZERO; + /** + * 鏈夋晥鏃ユ湡 + */ + @Excel(name = "鏈夋晥鏃ユ湡", width = 15) + @ApiModelProperty(value = "鏈夋晥鏃ユ湡") + private String theDate; + /** + * 鍒涘缓鏃堕棿 + */ + @ApiModelProperty(value = "鍒涘缓鏃堕棿") + private Date createTime; + + public MdcEquipmentFaultInfo() { + } + + public MdcEquipmentFaultInfo(String equipmentId, String theDate) { + this.equipmentId = equipmentId; + this.theDate = theDate; + } +} diff --git a/lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/job/CollectEquipmentDataMonthJob.java b/lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/job/CollectEquipmentDataMonthJob.java index 07bbabf..1b9b0c7 100644 --- a/lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/job/CollectEquipmentDataMonthJob.java +++ b/lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/job/CollectEquipmentDataMonthJob.java @@ -101,18 +101,18 @@ mdcEquipmentStatisticalInfoMonth.setEquipmentId(mdcEquipment.getEquipmentId()); mdcEquipmentStatisticalInfoMonth.setTheMonth(date); if (mdcEquipmentStatisticalDto != null) { - mdcEquipmentStatisticalInfoMonth.setCloseLong(mdcEquipmentStatisticalDto.getCloseLong().intValue()); - mdcEquipmentStatisticalInfoMonth.setErrorLong(mdcEquipmentStatisticalDto.getErrorLong().intValue()); - mdcEquipmentStatisticalInfoMonth.setOpenLong(mdcEquipmentStatisticalDto.getOpenLong().intValue()); - mdcEquipmentStatisticalInfoMonth.setProcessLong(mdcEquipmentStatisticalDto.getProcessLong().intValue()); - mdcEquipmentStatisticalInfoMonth.setWaitLong(mdcEquipmentStatisticalDto.getWaitLong().intValue()); - mdcEquipmentStatisticalInfoMonth.setOpenRate(mdcEquipmentStatisticalDto.getOpenLong().divide((new BigDecimal("864").multiply(mdcEquipmentStatisticalDto.getDayNum())), 4, RoundingMode.HALF_UP)); - mdcEquipmentStatisticalInfoMonth.setStartRate(mdcEquipmentStatisticalDto.getOpenLong().compareTo(BigDecimal.ZERO) > 0 ? mdcEquipmentStatisticalDto.getProcessLong().divide(mdcEquipmentStatisticalDto.getOpenLong(), 4, RoundingMode.HALF_UP).multiply(new BigDecimal("100")).setScale(2, RoundingMode.HALF_UP) : BigDecimal.ZERO); - mdcEquipmentStatisticalInfoMonth.setUtilizationRate(mdcEquipmentStatisticalDto.getProcessLong().divide((new BigDecimal("864").multiply(mdcEquipmentStatisticalDto.getDayNum())), 4, RoundingMode.HALF_UP)); - mdcEquipmentStatisticalInfoMonth.setFaultLong(mdcEquipmentStatisticalDto.getFaultLong().intValue()); - mdcEquipmentStatisticalInfoMonth.setFaultRate(mdcEquipmentStatisticalDto.getFaultRate()); - mdcEquipmentStatisticalInfoMonth.setRemoveFaultRate(mdcEquipmentStatisticalDto.getRemoveFaultRate()); - mdcEquipmentStatisticalInfoMonth.setRemoveFaultRunLong(mdcEquipmentStatisticalDto.getRemoveFaultRunLong().intValue()); + mdcEquipmentStatisticalInfoMonth.setCloseLong(mdcEquipmentStatisticalDto.getCloseLong() == null ? 0 : mdcEquipmentStatisticalDto.getCloseLong().intValue()); + mdcEquipmentStatisticalInfoMonth.setErrorLong(mdcEquipmentStatisticalDto.getErrorLong() == null ? 0 : mdcEquipmentStatisticalDto.getErrorLong().intValue()); + mdcEquipmentStatisticalInfoMonth.setOpenLong(mdcEquipmentStatisticalDto.getOpenLong() == null ? 0 : mdcEquipmentStatisticalDto.getOpenLong().intValue()); + mdcEquipmentStatisticalInfoMonth.setProcessLong(mdcEquipmentStatisticalDto.getProcessLong() == null ? 0 : mdcEquipmentStatisticalDto.getProcessLong().intValue()); + mdcEquipmentStatisticalInfoMonth.setWaitLong(mdcEquipmentStatisticalDto.getWaitLong() == null ? 0 : mdcEquipmentStatisticalDto.getWaitLong().intValue()); + mdcEquipmentStatisticalInfoMonth.setOpenRate(mdcEquipmentStatisticalDto.getOpenLong() == null || mdcEquipmentStatisticalDto.getDayNum() == null ? BigDecimal.ZERO : mdcEquipmentStatisticalDto.getOpenLong().divide((new BigDecimal("864").multiply(mdcEquipmentStatisticalDto.getDayNum())), 4, RoundingMode.HALF_UP)); + mdcEquipmentStatisticalInfoMonth.setStartRate(mdcEquipmentStatisticalDto.getOpenLong() == null || mdcEquipmentStatisticalDto.getProcessLong() == null ? BigDecimal.ZERO : (mdcEquipmentStatisticalDto.getOpenLong().compareTo(BigDecimal.ZERO) > 0 ? mdcEquipmentStatisticalDto.getProcessLong().divide(mdcEquipmentStatisticalDto.getOpenLong(), 4, RoundingMode.HALF_UP).multiply(new BigDecimal("100")).setScale(2, RoundingMode.HALF_UP) : BigDecimal.ZERO)); + mdcEquipmentStatisticalInfoMonth.setUtilizationRate(mdcEquipmentStatisticalDto.getProcessLong() == null || mdcEquipmentStatisticalDto.getDayNum() == null ? BigDecimal.ZERO : mdcEquipmentStatisticalDto.getProcessLong().divide((new BigDecimal("864").multiply(mdcEquipmentStatisticalDto.getDayNum())), 4, RoundingMode.HALF_UP)); + mdcEquipmentStatisticalInfoMonth.setFaultLong(mdcEquipmentStatisticalDto.getFaultLong() == null ? 0 : mdcEquipmentStatisticalDto.getFaultLong().intValue()); + mdcEquipmentStatisticalInfoMonth.setFaultRate(mdcEquipmentStatisticalDto.getFaultRate() == null ? BigDecimal.ZERO : mdcEquipmentStatisticalDto.getFaultRate()); + mdcEquipmentStatisticalInfoMonth.setRemoveFaultRate(mdcEquipmentStatisticalDto.getRemoveFaultRate() == null ? BigDecimal.ZERO : mdcEquipmentStatisticalDto.getRemoveFaultRate()); + mdcEquipmentStatisticalInfoMonth.setRemoveFaultRunLong(mdcEquipmentStatisticalDto.getRemoveFaultRunLong() == null ? 0 : mdcEquipmentStatisticalDto.getRemoveFaultRunLong().intValue()); } mdcEquipmentStatisticalInfoMonthList.add(mdcEquipmentStatisticalInfoMonth); // step.3 姹囨�荤彮娆″埄鐢ㄧ巼鏁版嵁 @@ -122,15 +122,25 @@ mdcEquipmentShiftInfoMonth.setTheMonth(date); mdcEquipmentShiftInfoMonth.setShiftType(CommonConstant.SHIFT_TYPE_1); if (mdcEquipmentShiftStatisticalDto != null) { - mdcEquipmentShiftInfoMonth.setCloseLong(mdcEquipmentShiftStatisticalDto.getCloseLong().intValue()); - mdcEquipmentShiftInfoMonth.setTotalLong(mdcEquipmentShiftStatisticalDto.getTotalLong().intValue()); - mdcEquipmentShiftInfoMonth.setOpenLong(mdcEquipmentShiftStatisticalDto.getOpenLong().intValue()); - mdcEquipmentShiftInfoMonth.setErrorLong(mdcEquipmentShiftStatisticalDto.getErrorLong().intValue()); - mdcEquipmentShiftInfoMonth.setWaitLong(mdcEquipmentShiftStatisticalDto.getWaitLong().intValue()); - mdcEquipmentShiftInfoMonth.setProcessLong(mdcEquipmentShiftStatisticalDto.getProcessLong().intValue()); - mdcEquipmentShiftInfoMonth.setUtilizationRate(mdcEquipmentShiftStatisticalDto.getTotalLong().compareTo(BigDecimal.ZERO) > 0 ? mdcEquipmentShiftStatisticalDto.getProcessLong().divide(mdcEquipmentShiftStatisticalDto.getTotalLong(), 4, RoundingMode.HALF_UP).multiply(new BigDecimal("100")).setScale(2, RoundingMode.HALF_UP) : BigDecimal.ZERO); - mdcEquipmentShiftInfoMonth.setStartRate(mdcEquipmentShiftStatisticalDto.getOpenLong().compareTo(BigDecimal.ZERO) > 0 ? mdcEquipmentShiftStatisticalDto.getProcessLong().divide(mdcEquipmentShiftStatisticalDto.getOpenLong(), 4, RoundingMode.HALF_UP).multiply(new BigDecimal("100")).setScale(2, RoundingMode.HALF_UP) : BigDecimal.ZERO); - mdcEquipmentShiftInfoMonth.setOpenRate(mdcEquipmentShiftStatisticalDto.getTotalLong().compareTo(BigDecimal.ZERO) > 0 ? mdcEquipmentShiftStatisticalDto.getOpenLong().divide(mdcEquipmentShiftStatisticalDto.getTotalLong(), 4, RoundingMode.HALF_UP).multiply(new BigDecimal("100")).setScale(2, RoundingMode.HALF_UP) : BigDecimal.ZERO); + mdcEquipmentShiftInfoMonth.setCloseLong(mdcEquipmentShiftStatisticalDto.getCloseLong() == null ? 0 : mdcEquipmentShiftStatisticalDto.getCloseLong().intValue()); + mdcEquipmentShiftInfoMonth.setTotalLong(mdcEquipmentShiftStatisticalDto.getTotalLong() == null ? 0 : mdcEquipmentShiftStatisticalDto.getTotalLong().intValue()); + mdcEquipmentShiftInfoMonth.setOpenLong(mdcEquipmentShiftStatisticalDto.getOpenLong() == null ? 0 : mdcEquipmentShiftStatisticalDto.getOpenLong().intValue()); + mdcEquipmentShiftInfoMonth.setErrorLong(mdcEquipmentShiftStatisticalDto.getErrorLong() == null ? 0 : mdcEquipmentShiftStatisticalDto.getErrorLong().intValue()); + mdcEquipmentShiftInfoMonth.setWaitLong(mdcEquipmentShiftStatisticalDto.getWaitLong() == null ? 0 : mdcEquipmentShiftStatisticalDto.getWaitLong().intValue()); + mdcEquipmentShiftInfoMonth.setProcessLong(mdcEquipmentShiftStatisticalDto.getProcessLong() == null ? 0 : mdcEquipmentShiftStatisticalDto.getProcessLong().intValue()); + mdcEquipmentShiftInfoMonth.setUtilizationRate(mdcEquipmentShiftStatisticalDto.getTotalLong() == null || mdcEquipmentShiftStatisticalDto.getProcessLong() == null ? BigDecimal.ZERO : (mdcEquipmentShiftStatisticalDto.getTotalLong().compareTo(BigDecimal.ZERO) > 0 ? mdcEquipmentShiftStatisticalDto.getProcessLong().divide(mdcEquipmentShiftStatisticalDto.getTotalLong(), 4, RoundingMode.HALF_UP).multiply(new BigDecimal("100")).setScale(2, RoundingMode.HALF_UP) : BigDecimal.ZERO)); + mdcEquipmentShiftInfoMonth.setStartRate(mdcEquipmentShiftStatisticalDto.getOpenLong() == null || mdcEquipmentShiftStatisticalDto.getProcessLong() == null ? BigDecimal.ZERO : (mdcEquipmentShiftStatisticalDto.getOpenLong().compareTo(BigDecimal.ZERO) > 0 ? mdcEquipmentShiftStatisticalDto.getProcessLong().divide(mdcEquipmentShiftStatisticalDto.getOpenLong(), 4, RoundingMode.HALF_UP).multiply(new BigDecimal("100")).setScale(2, RoundingMode.HALF_UP) : BigDecimal.ZERO)); + mdcEquipmentShiftInfoMonth.setOpenRate(mdcEquipmentShiftStatisticalDto.getOpenLong() == null || mdcEquipmentShiftStatisticalDto.getTotalLong() == null ? BigDecimal.ZERO : (mdcEquipmentShiftStatisticalDto.getTotalLong().compareTo(BigDecimal.ZERO) > 0 ? mdcEquipmentShiftStatisticalDto.getOpenLong().divide(mdcEquipmentShiftStatisticalDto.getTotalLong(), 4, RoundingMode.HALF_UP).multiply(new BigDecimal("100")).setScale(2, RoundingMode.HALF_UP) : BigDecimal.ZERO)); + } else { + mdcEquipmentShiftInfoMonth.setCloseLong(0); + mdcEquipmentShiftInfoMonth.setTotalLong(0); + mdcEquipmentShiftInfoMonth.setOpenLong(0); + mdcEquipmentShiftInfoMonth.setErrorLong(0); + mdcEquipmentShiftInfoMonth.setWaitLong(0); + mdcEquipmentShiftInfoMonth.setProcessLong(0); + mdcEquipmentShiftInfoMonth.setUtilizationRate(BigDecimal.ZERO); + mdcEquipmentShiftInfoMonth.setStartRate(BigDecimal.ZERO); + mdcEquipmentShiftInfoMonth.setOpenRate(BigDecimal.ZERO); } mdcEquipmentStatisticalShiftInfoMonthList.add(mdcEquipmentShiftInfoMonth); // step.4 姹囨�诲悇鐝鍒╃敤鐜囨暟鎹� @@ -141,15 +151,15 @@ equipmentStatisticalShiftInfoMonth.setEquipmentId(mdcEquipment.getEquipmentId()); equipmentStatisticalShiftInfoMonth.setTheMonth(date); equipmentStatisticalShiftInfoMonth.setShiftType(CommonConstant.SHIFT_TYPE_2); - equipmentStatisticalShiftInfoMonth.setCloseLong(equipmentStatisticalDto.getCloseLong().intValue()); - equipmentStatisticalShiftInfoMonth.setTotalLong(equipmentStatisticalDto.getTotalLong().intValue()); - equipmentStatisticalShiftInfoMonth.setOpenLong(equipmentStatisticalDto.getOpenLong().intValue()); - equipmentStatisticalShiftInfoMonth.setErrorLong(equipmentStatisticalDto.getErrorLong().intValue()); - equipmentStatisticalShiftInfoMonth.setWaitLong(equipmentStatisticalDto.getWaitLong().intValue()); - equipmentStatisticalShiftInfoMonth.setProcessLong(equipmentStatisticalDto.getProcessLong().intValue()); - equipmentStatisticalShiftInfoMonth.setUtilizationRate(equipmentStatisticalDto.getTotalLong().compareTo(BigDecimal.ZERO) > 0 ? equipmentStatisticalDto.getProcessLong().divide(equipmentStatisticalDto.getTotalLong(), 4, RoundingMode.HALF_UP).multiply(new BigDecimal("100")).setScale(2, RoundingMode.HALF_UP) : BigDecimal.ZERO); - equipmentStatisticalShiftInfoMonth.setStartRate(equipmentStatisticalDto.getOpenLong().compareTo(BigDecimal.ZERO) > 0 ? equipmentStatisticalDto.getProcessLong().divide(equipmentStatisticalDto.getOpenLong(), 4, RoundingMode.HALF_UP).multiply(new BigDecimal("100")).setScale(2, RoundingMode.HALF_UP) : BigDecimal.ZERO); - equipmentStatisticalShiftInfoMonth.setOpenRate(equipmentStatisticalDto.getTotalLong().compareTo(BigDecimal.ZERO) > 0 ? equipmentStatisticalDto.getOpenLong().divide(equipmentStatisticalDto.getTotalLong(), 4, RoundingMode.HALF_UP).multiply(new BigDecimal("100")).setScale(2, RoundingMode.HALF_UP) : BigDecimal.ZERO); + equipmentStatisticalShiftInfoMonth.setCloseLong(equipmentStatisticalDto.getCloseLong() == null ? 0 : equipmentStatisticalDto.getCloseLong().intValue()); + equipmentStatisticalShiftInfoMonth.setTotalLong(equipmentStatisticalDto.getTotalLong() == null ? 0 : equipmentStatisticalDto.getTotalLong().intValue()); + equipmentStatisticalShiftInfoMonth.setOpenLong(equipmentStatisticalDto.getOpenLong() == null ? 0 : equipmentStatisticalDto.getOpenLong().intValue()); + equipmentStatisticalShiftInfoMonth.setErrorLong(equipmentStatisticalDto.getErrorLong() == null ? 0 : equipmentStatisticalDto.getErrorLong().intValue()); + equipmentStatisticalShiftInfoMonth.setWaitLong(equipmentStatisticalDto.getWaitLong() == null ? 0 : equipmentStatisticalDto.getWaitLong().intValue()); + equipmentStatisticalShiftInfoMonth.setProcessLong(equipmentStatisticalDto.getProcessLong() == null ? 0 : equipmentStatisticalDto.getProcessLong().intValue()); + equipmentStatisticalShiftInfoMonth.setUtilizationRate(equipmentStatisticalDto.getTotalLong() == null || equipmentStatisticalDto.getProcessLong() == null ? BigDecimal.ZERO : (equipmentStatisticalDto.getTotalLong().compareTo(BigDecimal.ZERO) > 0 ? equipmentStatisticalDto.getProcessLong().divide(equipmentStatisticalDto.getTotalLong(), 4, RoundingMode.HALF_UP).multiply(new BigDecimal("100")).setScale(2, RoundingMode.HALF_UP) : BigDecimal.ZERO)); + equipmentStatisticalShiftInfoMonth.setStartRate(equipmentStatisticalDto.getOpenLong() == null || equipmentStatisticalDto.getProcessLong() == null ? BigDecimal.ZERO : (equipmentStatisticalDto.getOpenLong().compareTo(BigDecimal.ZERO) > 0 ? equipmentStatisticalDto.getProcessLong().divide(equipmentStatisticalDto.getOpenLong(), 4, RoundingMode.HALF_UP).multiply(new BigDecimal("100")).setScale(2, RoundingMode.HALF_UP) : BigDecimal.ZERO)); + equipmentStatisticalShiftInfoMonth.setOpenRate(equipmentStatisticalDto.getTotalLong() == null || equipmentStatisticalDto.getOpenLong() == null ? BigDecimal.ZERO : (equipmentStatisticalDto.getTotalLong().compareTo(BigDecimal.ZERO) > 0 ? equipmentStatisticalDto.getOpenLong().divide(equipmentStatisticalDto.getTotalLong(), 4, RoundingMode.HALF_UP).multiply(new BigDecimal("100")).setScale(2, RoundingMode.HALF_UP) : BigDecimal.ZERO)); mdcEquipmentStatisticalShiftInfoMonthList.add(equipmentStatisticalShiftInfoMonth); } } diff --git a/lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/mapper/MdcEquipmentFaultInfoMapper.java b/lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/mapper/MdcEquipmentFaultInfoMapper.java new file mode 100644 index 0000000..3aa3fd3 --- /dev/null +++ b/lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/mapper/MdcEquipmentFaultInfoMapper.java @@ -0,0 +1,22 @@ +package org.jeecg.modules.mdc.mapper; + +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import org.apache.ibatis.annotations.Param; +import org.jeecg.modules.mdc.entity.MdcEquipmentFaultInfo; +import org.jeecg.modules.mdc.vo.EquFaultRecord; + +import java.util.List; + +/** + * @Description: 鏁呴殰鐜囪〃 + * @Author: jeecg-boot + * @Date: 2025-06-16 + * @Version: V1.0 + */ +public interface MdcEquipmentFaultInfoMapper extends BaseMapper<MdcEquipmentFaultInfo> { + + List<EquFaultRecord> findFaultRecord(@Param("equipmentIdList") List<String> equipmentIdList, @Param("startTime") String startTime, @Param("endTime") String endTime); + + String getMaxStaticsData(); + +} diff --git a/lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/mapper/MdcEquipmentRunningSectionMapper.java b/lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/mapper/MdcEquipmentRunningSectionMapper.java index 3395eea..b9997c0 100644 --- a/lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/mapper/MdcEquipmentRunningSectionMapper.java +++ b/lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/mapper/MdcEquipmentRunningSectionMapper.java @@ -78,4 +78,6 @@ List<Integer> getDataList(@Param("equipmentId") String equipmentId, @Param("date") Date date); List<MdcEquipmentWaitSectionDto> findWaitList(@Param("date") String date); + + MdcEquipmentRunningSection getFirstRecord(); } diff --git a/lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/mapper/xml/MdcEfficiencyReportMapper.xml b/lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/mapper/xml/MdcEfficiencyReportMapper.xml index d7c9488..74ccdcb 100644 --- a/lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/mapper/xml/MdcEfficiencyReportMapper.xml +++ b/lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/mapper/xml/MdcEfficiencyReportMapper.xml @@ -69,10 +69,14 @@ t1.open_long / 86400 openRate, t1.open_long openLong, t1.wait_long waitLong, - t1.close_long closeLong + t1.close_long closeLong, + COALESCE(t3.fault_long, 0) faultLong, + COALESCE(t3.fault_rate, 0) faultRate, + COALESCE(t3.remove_fault_run_long, 0) removeFaultRunLong FROM mdc_equipment t2 LEFT JOIN mdc_equipment_statistical_info t1 ON t1.equipment_id = t2.equipment_id + LEFT JOIN mdc_equipment_fault_info t3 ON t2.equipment_id = t3.equipment_id AND t1.the_date = t3.the_date WHERE t1.the_date <= #{ vo.endTime } AND t1.the_date >= #{ vo.startTime } diff --git a/lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/mapper/xml/MdcEquipmentFaultInfoMapper.xml b/lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/mapper/xml/MdcEquipmentFaultInfoMapper.xml new file mode 100644 index 0000000..3136e97 --- /dev/null +++ b/lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/mapper/xml/MdcEquipmentFaultInfoMapper.xml @@ -0,0 +1,30 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> +<mapper namespace="org.jeecg.modules.mdc.mapper.MdcEquipmentFaultInfoMapper"> + + <select id="findFaultRecord" resultType="org.jeecg.modules.mdc.vo.EquFaultRecord"> + SELECT + t3.equipment_code equipmentId, + t1.fault_start_time startTime, + t2.actual_end_time endTime + FROM + eam_report_repair t1 + LEFT JOIN eam_repair_order t2 ON t2.report_id = t1.id + LEFT JOIN eam_equipment t3 ON t1.equipment_id = t3.id + WHERE + ( + ( t1.fault_start_time BETWEEN #{startTime} AND #{endTime} ) + OR ( t2.actual_end_time BETWEEN #{startTime} AND #{endTime} ) + OR ( t1.fault_start_time < #{startTime} AND t2.actual_end_time IS NULL ) + ) + AND t3.equipment_code IN + <foreach collection="equipmentIdList" item="id" index="index" open="(" close=")" separator=","> + #{ id } + </foreach> + AND t1.report_status != 'ABOLISH' + </select> + + <select id="getMaxStaticsData" resultType="java.lang.String"> + SELECT TOP 1 the_date FROM mdc_equipment_fault_info ORDER BY the_date DESC + </select> +</mapper> \ No newline at end of file diff --git a/lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/mapper/xml/MdcEquipmentRunningSectionMapper.xml b/lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/mapper/xml/MdcEquipmentRunningSectionMapper.xml index dd35b9f..acf92f2 100644 --- a/lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/mapper/xml/MdcEquipmentRunningSectionMapper.xml +++ b/lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/mapper/xml/MdcEquipmentRunningSectionMapper.xml @@ -188,5 +188,7 @@ equipment_id, start_time DESC </select> - + <select id="getFirstRecord" resultType="org.jeecg.modules.mdc.entity.MdcEquipmentRunningSection"> + SELECT TOP 1 * FROM mdc_equipment_running_section ORDER BY start_time + </select> </mapper> \ No newline at end of file diff --git a/lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/service/IMdcEquipmentFaultInfoService.java b/lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/service/IMdcEquipmentFaultInfoService.java new file mode 100644 index 0000000..d8f63a3 --- /dev/null +++ b/lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/service/IMdcEquipmentFaultInfoService.java @@ -0,0 +1,19 @@ +package org.jeecg.modules.mdc.service; + +import com.baomidou.mybatisplus.extension.service.IService; +import org.jeecg.modules.mdc.entity.MdcEquipmentFaultInfo; + +/** + * @Description: 鏁呴殰鐜囪〃 + * @Author: Lius + * @Date: 2025-06-16 + * @Version: V1.0 + */ +public interface IMdcEquipmentFaultInfoService extends IService<MdcEquipmentFaultInfo> { + + /** + * 缁熻鏁呴殰鏃堕暱锛岃绠楁晠闅滅巼 + * @param parameter + */ + void runningAllEquFaultStatistical(String parameter); +} diff --git a/lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/service/IMdcEquipmentRunningSectionService.java b/lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/service/IMdcEquipmentRunningSectionService.java index 9cbf340..697f76e 100644 --- a/lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/service/IMdcEquipmentRunningSectionService.java +++ b/lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/service/IMdcEquipmentRunningSectionService.java @@ -90,4 +90,7 @@ List<Integer> getDataList(String equipmentId, Date date); List<MdcEquipmentWaitSectionDto> findWaitList(String date); + + MdcEquipmentRunningSection getFirstRecord(); + } diff --git a/lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/service/impl/MdcEquipmentFaultInfoServiceImpl.java b/lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/service/impl/MdcEquipmentFaultInfoServiceImpl.java new file mode 100644 index 0000000..fc76c70 --- /dev/null +++ b/lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/service/impl/MdcEquipmentFaultInfoServiceImpl.java @@ -0,0 +1,380 @@ +package org.jeecg.modules.mdc.service.impl; + +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import org.apache.commons.lang.StringUtils; +import org.jeecg.modules.mdc.entity.Equipment; +import org.jeecg.modules.mdc.entity.MdcEquipmentFaultInfo; +import org.jeecg.modules.mdc.entity.MdcEquipmentRunningSection; +import org.jeecg.modules.mdc.entity.MdcSystemParameters; +import org.jeecg.modules.mdc.mapper.MdcEquipmentFaultInfoMapper; +import org.jeecg.modules.mdc.service.IEquipmentService; +import org.jeecg.modules.mdc.service.IMdcEquipmentFaultInfoService; +import org.jeecg.modules.mdc.service.IMdcEquipmentRunningSectionService; +import org.jeecg.modules.mdc.service.IMdcSystemParametersService; +import org.jeecg.modules.mdc.util.DateUtils; +import org.jeecg.modules.mdc.vo.EquFaultRecord; +import org.jeecg.modules.mdc.vo.TimeInterval; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import javax.annotation.Resource; +import java.math.BigDecimal; +import java.math.RoundingMode; +import java.time.LocalDateTime; +import java.time.temporal.ChronoUnit; +import java.util.*; +import java.util.stream.Collectors; + +/** + * @Description: 鏁呴殰鐜囪〃 + * @Author: Lius + * @Date: 2025-06-16 + * @Version: V1.0 + */ +@Service +public class MdcEquipmentFaultInfoServiceImpl extends ServiceImpl<MdcEquipmentFaultInfoMapper, MdcEquipmentFaultInfo> implements IMdcEquipmentFaultInfoService { + + @Resource + private IEquipmentService equipmentService; + + @Resource + private IMdcSystemParametersService mdcSystemParametersService; + + @Resource + private IMdcEquipmentRunningSectionService mdcEquipmentRunningSectionService; + + @Override + @Transactional(rollbackFor = {Exception.class}) + public void runningAllEquFaultStatistical(String dateTime) { + Date initDate = null; + + if (StringUtils.isNotBlank(dateTime)) { + initDate = DateUtils.toDate(dateTime, DateUtils.STRDATE); + } else { + // 鍙栨渶鍚庣殑缁熻鏃堕棿 + String date = this.baseMapper.getMaxStaticsData(); + if (date != null) { + initDate = DateUtils.toDate(date, DateUtils.STRDATE); + } else { + // 鍒濇鍙栧�� 鍙栨渶鏃╂椂闂磋褰� + MdcEquipmentRunningSection equipmentRunningSection = mdcEquipmentRunningSectionService.getFirstRecord(); + if (equipmentRunningSection != null) { + initDate = equipmentRunningSection.getStartTime(); + } + } + } + Date endDate = DateUtils.addDays(DateUtils.getNow(), -1); + List<String> dateList = DateUtils.getDatesStringList2(initDate, endDate); + + if (dateList.isEmpty()) { + return; + } + try { + this.remove(new LambdaQueryWrapper<MdcEquipmentFaultInfo>().in(MdcEquipmentFaultInfo::getTheDate, dateList)); + } catch (Exception e) { + log.error("鍙傛暟鏍煎紡涓嶅", e); + } + + List<Equipment> equipmentList = equipmentService.list(); + if (equipmentList == null || equipmentList.isEmpty()) { + return; + } + + String planTime = "00:00:00"; + MdcSystemParameters mdcSystemParameters = mdcSystemParametersService.getOne(new LambdaQueryWrapper<MdcSystemParameters>().eq(MdcSystemParameters::getCode, "equip_log_statis_time")); + if (mdcSystemParameters != null) { + planTime = mdcSystemParameters.getValue(); + } + + List<String> equipmentIdList = equipmentList.stream().map(Equipment::getEquipmentid).collect(Collectors.toList()); + + for (String validDate : dateList) { + Map<String, MdcEquipmentFaultInfo> map = new HashMap<>(); + String finalValidDate = validDate; + equipmentList.forEach(equipment -> { + MdcEquipmentFaultInfo mdcEquipmentFaultInfo = new MdcEquipmentFaultInfo(equipment.getEquipmentid(), finalValidDate); + map.put(equipment.getEquipmentid(), mdcEquipmentFaultInfo); + }); + + String startTime = DateUtils.format(DateUtils.setTimeForDay(DateUtils.toDate(validDate, DateUtils.STRDATE), planTime), DateUtils.STR_DATE_TIME_SMALL); + Date start = DateUtils.toDate(startTime, DateUtils.STR_DATE_TIME_SMALL); + String endTime = DateUtils.format(DateUtils.addDays(DateUtils.toDate(startTime, DateUtils.STR_DATE_TIME_SMALL), 1), DateUtils.STR_DATE_TIME_SMALL); + Date end = DateUtils.toDate(endTime, DateUtils.STR_DATE_TIME_SMALL); + //鏌ヨ鏁呴殰璁板綍 + List<EquFaultRecord> equFaultRecordList = this.baseMapper.findFaultRecord(equipmentIdList, startTime, endTime); + if (equFaultRecordList != null && !equFaultRecordList.isEmpty()) { + // 淇暣鏃堕棿 + Map<String, List<EquFaultRecord>> equFaultRecordMap = equFaultRecordList.stream().collect(Collectors.groupingBy(EquFaultRecord::getEquipmentId)); + map.forEach((key, value1) -> { + MdcEquipmentFaultInfo equFaultRecords = value1; + if (equFaultRecordMap.containsKey(key)) { + List<EquFaultRecord> value = equFaultRecordMap.get(key); + long faultLong = calculateTotalFaultDuration(value, start, end); + equFaultRecords.setFaultLong((int) faultLong); + if (faultLong != 0) { + equFaultRecords.setFaultRate(new BigDecimal(faultLong).divide(new BigDecimal("864"), 2, RoundingMode.HALF_UP)); + } + // 璁$畻鍘婚櫎鏁呴殰鏃堕暱鐨勫姞宸ユ椂闂� + // step.1 鏌ヨ鍔犲伐鏃堕棿 + List<MdcEquipmentRunningSection> mdcEquipmentRunningSections = mdcEquipmentRunningSectionService.listEquipmentRunningSectionRun(key, start.getTime(), end.getTime()); + if (mdcEquipmentRunningSections != null && !mdcEquipmentRunningSections.isEmpty()) { + // 鏃堕棿淇 + if (mdcEquipmentRunningSections.get(0).getStartTime().before(start)) { + mdcEquipmentRunningSections.get(0).setStartTime(start); + } + if (mdcEquipmentRunningSections.size() > 1) { + if (mdcEquipmentRunningSections.get(mdcEquipmentRunningSections.size() - 1).getEndTime().after(end)) { + mdcEquipmentRunningSections.get(mdcEquipmentRunningSections.size() - 1).setEndTime(end); + } + } else { + if (mdcEquipmentRunningSections.get(0).getEndTime().after(end)) { + mdcEquipmentRunningSections.get(0).setEndTime(end); + } + } + List<MdcEquipmentRunningSection> collect = mdcEquipmentRunningSections.stream().filter(mdcEquipmentRunningSection -> mdcEquipmentRunningSection.getStatus() == 3).collect(Collectors.toList()); + if (!collect.isEmpty()) { + // step.2 璁$畻鍘婚櫎鏁呴殰鏃堕暱鐨勫姞宸ユ椂闂� + long processingTime = calculateProcessingTimeWithoutFaults(collect, value, start, end); + equFaultRecords.setRemoveFaultRunLong((int) processingTime); + if (faultLong != 0 && faultLong != 86400) { + // 璁$畻鍘婚櫎鏁呴殰鏃堕暱鐨勫埄鐢ㄧ巼 + BigDecimal removeFaultRate = new BigDecimal(processingTime).divide(new BigDecimal("864").subtract(new BigDecimal(faultLong)), 2, RoundingMode.HALF_UP); + equFaultRecords.setRemoveFaultRate(removeFaultRate); + } + } + } + } else { + List<MdcEquipmentRunningSection> mdcEquipmentRunningSections = mdcEquipmentRunningSectionService.listEquipmentRunningSectionRun(key, start.getTime(), end.getTime()); + if (mdcEquipmentRunningSections != null && !mdcEquipmentRunningSections.isEmpty()) { + // 鏃堕棿淇 + if (mdcEquipmentRunningSections.get(0).getStartTime().before(start)) { + mdcEquipmentRunningSections.get(0).setStartTime(start); + } + if (mdcEquipmentRunningSections.size() > 1) { + if (mdcEquipmentRunningSections.get(mdcEquipmentRunningSections.size() - 1).getEndTime().after(end)) { + mdcEquipmentRunningSections.get(mdcEquipmentRunningSections.size() - 1).setEndTime(end); + } + } else { + if (mdcEquipmentRunningSections.get(0).getEndTime().after(end)) { + mdcEquipmentRunningSections.get(0).setEndTime(end); + } + } + List<MdcEquipmentRunningSection> collect = mdcEquipmentRunningSections.stream().filter(mdcEquipmentRunningSection -> mdcEquipmentRunningSection.getStatus() == 3).collect(Collectors.toList()); + + if (!collect.isEmpty()) { + long totalProcessingTime = 0; + for (MdcEquipmentRunningSection mdcEquipmentRunningSection : collect) { + totalProcessingTime += ChronoUnit.SECONDS.between(DateUtils.convertToLocalDateTime(mdcEquipmentRunningSection.getStartTime()), DateUtils.convertToLocalDateTime(mdcEquipmentRunningSection.getEndTime())); + } + equFaultRecords.setRemoveFaultRunLong((int) totalProcessingTime); + // 璁$畻鍘婚櫎鏁呴殰鏃堕暱鐨勫埄鐢ㄧ巼 + BigDecimal removeFaultRate = new BigDecimal(totalProcessingTime).divide(new BigDecimal("864"), 2, RoundingMode.HALF_UP); + equFaultRecords.setRemoveFaultRate(removeFaultRate); + } + } + } + map.put(key, equFaultRecords); + }); + } else { + map.forEach((key, value1) -> { + MdcEquipmentFaultInfo equFaultRecords = value1; + List<MdcEquipmentRunningSection> mdcEquipmentRunningSections = mdcEquipmentRunningSectionService.listEquipmentRunningSectionRun(key, start.getTime(), end.getTime()); + if (mdcEquipmentRunningSections != null && !mdcEquipmentRunningSections.isEmpty()) { + // 鏃堕棿淇 + if (mdcEquipmentRunningSections.get(0).getStartTime().before(start)) { + mdcEquipmentRunningSections.get(0).setStartTime(start); + } + if (mdcEquipmentRunningSections.size() > 1) { + if (mdcEquipmentRunningSections.get(mdcEquipmentRunningSections.size() - 1).getEndTime().after(end)) { + mdcEquipmentRunningSections.get(mdcEquipmentRunningSections.size() - 1).setEndTime(end); + } + } else { + if (mdcEquipmentRunningSections.get(0).getEndTime().after(end)) { + mdcEquipmentRunningSections.get(0).setEndTime(end); + } + } + List<MdcEquipmentRunningSection> collect = mdcEquipmentRunningSections.stream().filter(mdcEquipmentRunningSection -> mdcEquipmentRunningSection.getStatus() == 3).collect(Collectors.toList()); + + if (!collect.isEmpty()) { + long totalProcessingTime = 0; + for (MdcEquipmentRunningSection mdcEquipmentRunningSection : collect) { + totalProcessingTime += ChronoUnit.SECONDS.between(DateUtils.convertToLocalDateTime(mdcEquipmentRunningSection.getStartTime()), DateUtils.convertToLocalDateTime(mdcEquipmentRunningSection.getEndTime())); + } + equFaultRecords.setRemoveFaultRunLong((int) totalProcessingTime); + // 璁$畻鍘婚櫎鏁呴殰鏃堕暱鐨勫埄鐢ㄧ巼 + BigDecimal removeFaultRate = new BigDecimal(totalProcessingTime).divide(new BigDecimal("864"), 2, RoundingMode.HALF_UP); + equFaultRecords.setRemoveFaultRate(removeFaultRate); + } + } + map.put(key, equFaultRecords); + }); + } + if (!map.isEmpty()) { + this.saveBatch(new ArrayList<>(map.values())); + } + } + } + + public static long calculateTotalFaultDuration(List<EquFaultRecord> records, Date startTime, Date endTime) { + LocalDateTime start = DateUtils.convertToLocalDateTime(startTime); + LocalDateTime end = DateUtils.convertToLocalDateTime(endTime); + + // 淇璁板綍鏃堕棿 + List<EquFaultRecord> correctedRecords = correctRecordTimes(records, start, end); + + // 鎸夊紑濮嬫椂闂存帓搴� + correctedRecords.sort(Comparator.comparing(EquFaultRecord::getStartTime)); + + // 鍚堝苟閲嶅彔鏃堕棿娈� + List<TimeInterval> mergedIntervals = mergeIntervals(correctedRecords); + + // 璁$畻鎬绘椂闀匡紙绉掞級 + return mergedIntervals.stream() + .mapToLong(interval -> ChronoUnit.SECONDS.between(interval.getStart(), interval.getEnd())) + .sum(); + } + + private static List<EquFaultRecord> correctRecordTimes(List<EquFaultRecord> records, LocalDateTime startTime, LocalDateTime endTime) { + return records.stream() + .map(record -> { + LocalDateTime recordStart = DateUtils.convertToLocalDateTime(record.getStartTime()); + LocalDateTime recordEnd = record.getEndTime() != null ? + DateUtils.convertToLocalDateTime(record.getEndTime()) : null; + + // 淇寮�濮嬫椂闂� + LocalDateTime correctedStart = recordStart.isBefore(startTime) ? + startTime : recordStart; + + // 淇缁撴潫鏃堕棿 + LocalDateTime correctedEnd = recordEnd == null || recordEnd.isAfter(endTime) ? + endTime : recordEnd; + + // 鍒涘缓淇鍚庣殑璁板綍 + return new EquFaultRecord( + record.getEquipmentId(), + DateUtils.convertToDate(correctedStart), + DateUtils.convertToDate(correctedEnd) + ); + }) + .collect(Collectors.toList()); + } + + private static List<TimeInterval> mergeIntervals(List<EquFaultRecord> records) { + List<TimeInterval> intervals = records.stream() + .map(record -> new TimeInterval( + DateUtils.convertToLocalDateTime(record.getStartTime()), + DateUtils.convertToLocalDateTime(record.getEndTime()))) + .collect(Collectors.toList()); + + if (intervals.isEmpty()) { + return Collections.emptyList(); + } + + List<TimeInterval> merged = new ArrayList<>(); + TimeInterval current = intervals.get(0); + + for (int i = 1; i < intervals.size(); i++) { + TimeInterval next = intervals.get(i); + if (next.getStart().isBefore(current.getEnd()) || next.getStart().equals(current.getEnd())) { + // 鏈夐噸鍙狅紝鍚堝苟鍖洪棿 + current.setEnd(current.getEnd().isAfter(next.getEnd()) ? current.getEnd() : next.getEnd()); + } else { + // 鏃犻噸鍙狅紝娣诲姞褰撳墠鍖洪棿骞舵洿鏂板綋鍓嶅尯闂� + merged.add(current); + current = next; + } + } + merged.add(current); // 娣诲姞鏈�鍚庝竴涓尯闂� + + return merged; + } + + // 璁$畻鍘婚櫎鏁呴殰鏃堕暱鍚庣殑鍔犲伐鏃堕棿 + private long calculateProcessingTimeWithoutFaults( + List<MdcEquipmentRunningSection> runningSections, + List<EquFaultRecord> faultRecords, + Date startTime, + Date endTime) { + + // 杞崲涓篖ocalDateTime杩涜澶勭悊 + LocalDateTime start = DateUtils.convertToLocalDateTime(startTime); + LocalDateTime end = DateUtils.convertToLocalDateTime(endTime); + + // 淇璁板綍鏃堕棿 + List<EquFaultRecord> correctedRecords = correctRecordTimes(faultRecords, start, end); + List<TimeInterval> mergedFaultIntervals = mergeIntervals(correctedRecords); + + long totalProcessingTime = 0; + + // 閬嶅巻姣忎釜鍔犲伐鍖洪棿锛屾帓闄ゆ晠闅滄椂闂� + for (MdcEquipmentRunningSection section : runningSections) { + LocalDateTime sectionStart = DateUtils.convertToLocalDateTime(section.getStartTime()); + LocalDateTime sectionEnd = DateUtils.convertToLocalDateTime(section.getEndTime()); + + // 鎺掗櫎鏁呴殰鏃堕棿鍚庣殑鏈夋晥鍔犲伐鏃堕棿 + List<TimeInterval> validIntervals = excludeFaultsFromSection( + new TimeInterval(sectionStart, sectionEnd), + mergedFaultIntervals); + + // 绱姞鏈夋晥鍔犲伐鏃堕棿锛堢锛� + for (TimeInterval interval : validIntervals) { + totalProcessingTime += ChronoUnit.SECONDS.between(interval.getStart(), interval.getEnd()); + } + } + + return totalProcessingTime; + } + + // 浠庤繍琛屽尯闂翠腑鎺掗櫎鏁呴殰鏃堕棿 + private List<TimeInterval> excludeFaultsFromSection( + TimeInterval section, + List<TimeInterval> faultIntervals) { + + List<TimeInterval> validIntervals = new ArrayList<>(); + validIntervals.add(section); + + // 閬嶅巻姣忎釜鏁呴殰鍖洪棿锛屼粠鏈夋晥鍖洪棿涓墸闄� + for (TimeInterval fault : faultIntervals) { + List<TimeInterval> newValidIntervals = new ArrayList<>(); + + for (TimeInterval valid : validIntervals) { + // 璁$畻鏈夋晥鍖洪棿涓庢晠闅滃尯闂寸殑浜ら泦 + if (isOverlapping(valid, fault)) { + // 鍒嗗壊鏈夋晥鍖洪棿 + splitInterval(valid, fault, newValidIntervals); + } else { + // 鏃犱氦闆嗭紝淇濈暀鍘熸湁鏁堝尯闂� + newValidIntervals.add(valid); + } + } + + validIntervals = newValidIntervals; + } + + return validIntervals; + } + + // 鍒ゆ柇涓や釜鏃堕棿鍖洪棿鏄惁閲嶅彔 + private boolean isOverlapping(TimeInterval a, TimeInterval b) { + return a.getStart().isBefore(b.getEnd()) && b.getStart().isBefore(a.getEnd()); + } + + // 鍒嗗壊鍖洪棿锛堟墸闄ら噸鍙犻儴鍒嗭級 + private void splitInterval( + TimeInterval valid, + TimeInterval fault, + List<TimeInterval> result) { + + // 閲嶅彔鍓嶇殑閮ㄥ垎 + if (valid.getStart().isBefore(fault.getStart())) { + result.add(new TimeInterval(valid.getStart(), fault.getStart())); + } + + // 閲嶅彔鍚庣殑閮ㄥ垎 + if (valid.getEnd().isAfter(fault.getEnd())) { + result.add(new TimeInterval(fault.getEnd(), valid.getEnd())); + } + } + +} diff --git a/lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/service/impl/MdcEquipmentRunningSectionServiceImpl.java b/lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/service/impl/MdcEquipmentRunningSectionServiceImpl.java index 47fe41c..0a0d678 100644 --- a/lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/service/impl/MdcEquipmentRunningSectionServiceImpl.java +++ b/lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/service/impl/MdcEquipmentRunningSectionServiceImpl.java @@ -746,6 +746,11 @@ return this.baseMapper.findWaitList(date); } + @Override + public MdcEquipmentRunningSection getFirstRecord() { + return this.baseMapper.getFirstRecord(); + } + private Map<String, List<MdcEquipmentRunningSectionDto>> logCharts(MdcEquipmentRunningSectionVo equipmentRunningSectionVo, String date) { Map<String, List<MdcEquipmentRunningSectionDto>> map = new HashMap<>(); List<MdcEquipmentRunningSectionDto> normal = new ArrayList<>(); diff --git a/lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/service/impl/MdcEquipmentStatisticalInfoServiceImpl.java b/lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/service/impl/MdcEquipmentStatisticalInfoServiceImpl.java index c149df2..4e6a883 100644 --- a/lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/service/impl/MdcEquipmentStatisticalInfoServiceImpl.java +++ b/lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/service/impl/MdcEquipmentStatisticalInfoServiceImpl.java @@ -37,10 +37,7 @@ private IMdcSystemParametersService mdcSystemParametersService; @Resource - private ISysDictService sysDictService; - - @Resource - private IEquipmentStatisticalInfoService equipmentStatisticalInfoService; + private IMdcEquipmentFaultInfoService mdcEquipmentFaultInfoService; /** * 璁$畻璁惧鍗曟棩杩愯鏁版嵁 @@ -69,28 +66,8 @@ } if (!result.isEmpty()) { this.saveBatch(result); - List<DictModel> dictList = sysDictService.queryDictItemsByCode("data_synchronization_flag"); - if (dictList != null && !dictList.isEmpty() && "0".equals(dictList.get(0).getValue())) { - List<EquipmentStatisticalInfo> list = this.dataHandle(result); - equipmentStatisticalInfoService.saveBatch(list); - } } - } - - private List<EquipmentStatisticalInfo> dataHandle(List<MdcEquipmentStatisticalInfo> list) { - List<EquipmentStatisticalInfo> result = new ArrayList<>(); - list.forEach(item -> { - EquipmentStatisticalInfo equipmentStatisticalInfo = new EquipmentStatisticalInfo(); - equipmentStatisticalInfo.setEquipment(item.getEquipmentId()); - equipmentStatisticalInfo.setTheDate(item.getTheDate()); - equipmentStatisticalInfo.setClosedLong(item.getCloseLong()); - equipmentStatisticalInfo.setOpeningLong(item.getOpenLong()); - equipmentStatisticalInfo.setErroringLong(item.getErrorLong()); - equipmentStatisticalInfo.setProcessingLong(item.getProcessLong()); - equipmentStatisticalInfo.setWaitingLong(item.getWaitLong()); - result.add(equipmentStatisticalInfo); - }); - return result; + mdcEquipmentFaultInfoService.runningAllEquFaultStatistical(dateTime); } private List<MdcEquipmentStatisticalInfo> equipmentStatisticalProcess(Equipment equipment, String dateTime) { @@ -141,7 +118,6 @@ List<MdcEquipmentRunningSection> equipmentRunningSectionList = mdcEquipmentRunningSectionService.listForEquipmentStatisticalInfo(equipment.getEquipmentid(), startDate, endDate); return this.statisticsData(equipmentRunningSectionList, planTime, startDate, endDate, equipment); } else { - //寰呴獙璇� Date end = DateUtils.plusTime(initDate, 1); endDate = DateUtils.setTimeForDay(end, planTime); List<MdcEquipmentRunningSection> equipmentRunningSectionList = mdcEquipmentRunningSectionService.listForEquipmentStatisticalInfo(equipment.getEquipmentid(), startDate, endDate); diff --git a/lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/vo/EquFaultRecord.java b/lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/vo/EquFaultRecord.java new file mode 100644 index 0000000..beae4c6 --- /dev/null +++ b/lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/vo/EquFaultRecord.java @@ -0,0 +1,32 @@ +package org.jeecg.modules.mdc.vo; + +import lombok.Data; + +import java.util.Date; + +/** + * @Author: Lius + * @CreateTime: 2025-06-16 + * @Description: + */ +@Data +public class EquFaultRecord { + /** + * 璁惧缂栧彿 + */ + private String equipmentId; + /** + * 寮�濮嬫椂闂� + */ + private Date startTime; + /** + * 缁撴潫鏃堕棿 + */ + private Date endTime; + + public EquFaultRecord(String equipmentId, Date startTime, Date endTime) { + this.equipmentId = equipmentId; + this.startTime = startTime; + this.endTime = endTime; + } +} diff --git a/lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/vo/TimeInterval.java b/lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/vo/TimeInterval.java new file mode 100644 index 0000000..8193ab1 --- /dev/null +++ b/lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/vo/TimeInterval.java @@ -0,0 +1,21 @@ +package org.jeecg.modules.mdc.vo; + +import lombok.Data; + +import java.time.LocalDateTime; + +/** + * @Author: Lius + * @CreateTime: 2025-06-17 + * @Description: + */ +@Data +public class TimeInterval { + LocalDateTime start; + LocalDateTime end; + + public TimeInterval(LocalDateTime start, LocalDateTime end) { + this.start = start; + this.end = end; + } +} diff --git a/lxzn-module-msi/src/main/java/org/jeecg/modules/msi/webapi/controller/MsiWebapiJsonController.java b/lxzn-module-msi/src/main/java/org/jeecg/modules/msi/webapi/controller/MsiWebapiJsonController.java index 54c5d15..b3d2a0d 100644 --- a/lxzn-module-msi/src/main/java/org/jeecg/modules/msi/webapi/controller/MsiWebapiJsonController.java +++ b/lxzn-module-msi/src/main/java/org/jeecg/modules/msi/webapi/controller/MsiWebapiJsonController.java @@ -42,14 +42,14 @@ entity.setModuleType("MDC"); entity.setWebapiInfo(data.toString()); //鍚庣画杩涜鍏朵粬涓氬姟鍏宠仈 - JsonMapper mapper = new JsonMapper(); +// JsonMapper mapper = new JsonMapper(); + service.save(entity); try { service.saveTableAutomationOne(data); } catch (Exception e) { log.error("JSON parsing error: {}", e.getMessage()); return Result.error("鏁版嵁寮傚父", e.getMessage()); } - service.save(entity); return Result.OK("鎺ユ敹鎴愬姛"); } diff --git a/lxzn-module-msi/src/main/java/org/jeecg/modules/msi/webapi/service/impl/MsiWebapiJsonServiceImpl.java b/lxzn-module-msi/src/main/java/org/jeecg/modules/msi/webapi/service/impl/MsiWebapiJsonServiceImpl.java index 5364928..d0f9f2c 100644 --- a/lxzn-module-msi/src/main/java/org/jeecg/modules/msi/webapi/service/impl/MsiWebapiJsonServiceImpl.java +++ b/lxzn-module-msi/src/main/java/org/jeecg/modules/msi/webapi/service/impl/MsiWebapiJsonServiceImpl.java @@ -3,8 +3,10 @@ import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import liquibase.util.JdbcUtils; +import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.jeecg.common.util.DateUtils; +import org.jeecg.common.util.RedisUtil; import org.jeecg.modules.msi.webapi.entity.MsiWebapiJsonEntity; import org.jeecg.modules.msi.webapi.mapper.MsiWebapiJsonMapper; import org.jeecg.modules.msi.webapi.service.IMsiWebapiJsonService; @@ -15,7 +17,9 @@ import org.springframework.jdbc.datasource.DataSourceUtils; import org.springframework.stereotype.Service; import org.springframework.transaction.interceptor.TransactionAspectSupport; +import org.jetbrains.annotations.NotNull; +import javax.annotation.Resource; import java.sql.Connection; import java.sql.SQLException; import java.sql.Statement; @@ -24,29 +28,33 @@ import java.util.*; @Service +@Slf4j public class MsiWebapiJsonServiceImpl extends ServiceImpl<MsiWebapiJsonMapper, MsiWebapiJsonEntity> implements IMsiWebapiJsonService { @Autowired private JdbcTemplate jdbcTemplate; + @Resource + private RedisUtil redisUtil; + + private final static String ZDHKEY = "ZDHKEY_"; + /** * 瑙f瀽鍗曟潯鏁版嵁淇濆瓨鍒板崟琛� + * * @param data * @return */ @Override public boolean saveTableAutomationOne(List<MachineEquipentInfo> data) { - if (data ==null || data.isEmpty()) { + if (data == null || data.isEmpty()) { return false; } List<String> listStatus = new ArrayList<>(); List<String> listParameter = new ArrayList<>(); - // 鐢ㄤ簬璁板綍鍓嶄竴鏉¤褰曠殑鐘舵�佸�� - String prevRunningStatus = null; - for (MachineEquipentInfo machineInfo : data) { - System.out.println(machineInfo); +// log.info("鎺ユ敹鑷姩鍖栨暟鎹細" + machineInfo); // 鑾峰彇褰撳墠璁板綍鐨剅unningStatus鍊� String currentRunningStatus = null; @@ -57,23 +65,38 @@ } } - // 濡傛灉褰撳墠鐘舵�佷笌鍓嶄竴鏉$浉鍚岋紝鍒欒烦杩� - if (currentRunningStatus != null && currentRunningStatus.equals(prevRunningStatus)) { - continue; - } + // 鑾峰彇鍓嶄竴鏉$姸鎬� + if (redisUtil.hasKey(ZDHKEY + machineInfo.getMachineNo())) { + String prevRunningStatus = (String) redisUtil.get(ZDHKEY + machineInfo.getMachineNo()); - // 鏇存柊鍓嶄竴鏉¤褰曠殑鐘舵�佸�� - prevRunningStatus = currentRunningStatus; + // 濡傛灉褰撳墠鐘舵�佷笌鍓嶄竴鏉$浉鍚岋紝鍒欏彧鍐欏叆鍗曡〃鏁版嵁 + if (currentRunningStatus != null && currentRunningStatus.equals(prevRunningStatus)) { + String parameter = listSqlParameter(machineInfo); + if (parameter != null && !parameter.equals("")) { + listParameter.add(parameter); + } + continue; + } + + // 鏇存柊鍓嶄竴鏉¤褰曠殑鐘舵�佸�� + redisUtil.set(ZDHKEY + machineInfo.getMachineNo(), currentRunningStatus); + } else { + if (currentRunningStatus != null) { + redisUtil.set(ZDHKEY + machineInfo.getMachineNo(), currentRunningStatus); + } + } // 鐢熸垚SQL String stutus = listSqls(machineInfo); if (stutus != null && !stutus.equals("")) { listStatus.add(stutus); } + String parameter = listSqlParameter(machineInfo); if (parameter != null && !parameter.equals("")) { listParameter.add(parameter); } + } // 鎵ц鐘舵�佹暟鎹甋QL try { @@ -91,6 +114,7 @@ /** * 鎵归噺鎵цSQL璇彞 + * * @param sqlList SQL璇彞鍒楄〃 * @throws SQLException */ @@ -126,22 +150,23 @@ /** - * 鎻掑叆鍒癊quipmentLog + * 鎻掑叆鍒拌澶囧崟琛� + * * @param machineInfo * @return */ public String listSqlParameter(MachineEquipentInfo machineInfo) { String listSqls = null; List<DetailedListVo> itemList = machineInfo.getItemList(); - Map<String ,DetailedListVo> itemMap = new HashMap<String ,DetailedListVo>(); - for (DetailedListVo item: itemList) { - itemMap.put(item.getItemName(),item); + Map<String, DetailedListVo> itemMap = new HashMap<String, DetailedListVo>(); + for (DetailedListVo item : itemList) { + itemMap.put(item.getItemName(), item); } String sql = "insert into "; sql = sql + "ZDH_" + machineInfo.getMachineNo() + " "; sql = sql + "(EquipmentID,EquipmentName,CollectTime,runningStatus,spindleSpeed,feedRatio,runDuration,spindleDuration,progName,progStatus,toolNo) values ( "; //EquipmentID - if (machineInfo.getMachineNo() == null || machineInfo.getMachineNo().equals("")) { + if (machineInfo.getMachineNo() == null || machineInfo.getMachineNo().equals("")) { return null; } else { sql = sql + "'" + machineInfo.getMachineNo() + "', "; @@ -151,18 +176,7 @@ //CollectTime // 鍋囪 endTime 鏄� "MM/dd/yyyy HH:mm:ss" 鏍煎紡鐨勫瓧绗︿覆 if (machineInfo.getEndTime() != null && !machineInfo.getEndTime().isEmpty()) { - try { - // 1. 灏嗗瓧绗︿覆瑙f瀽涓� Date 瀵硅薄 - SimpleDateFormat inputFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss"); - Date endDate = inputFormat.parse(machineInfo.getEndTime()); - - // 2. 灏� Date 鏍煎紡鍖栦负鐩爣 SQL 鏍煎紡锛堝 "yyyy-MM-dd HH:mm:ss"锛� - SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - sql = sql + "'" + outputFormat.format(endDate) + "', "; - } catch (ParseException e) { - // 澶勭悊瑙f瀽澶辫触鐨勬儏鍐碉紙濡傛棩蹇楄褰曟垨鎶涘嚭寮傚父锛� - throw new RuntimeException("Invalid end time format: " + machineInfo.getEndTime(), e); - } + sql = getTime(machineInfo, sql); } else { // 濡傛灉 endTime 涓� null 鎴栫┖锛屼娇鐢ㄥ綋鍓嶆椂闂� sql = sql + "'" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + "', "; @@ -182,7 +196,7 @@ //feedRatio if (itemMap.containsKey("feedRatio")) { sql = sql + "'" + itemMap.get("feedRatio").getItemValue() + "', "; - }else { + } else { sql = sql + "NULL, "; } //runDuration @@ -217,7 +231,7 @@ } sql = sql + "); \n"; - if (StringUtils.isBlank(listSqls) ) { + if (StringUtils.isBlank(listSqls)) { listSqls = sql; } else { listSqls += sql; @@ -227,7 +241,8 @@ /** - * 鎻掑叆鍒癊quipmentLog + * 鎻掑叆鍒癊quipmentLog + * * @param machineInfo * @return */ @@ -242,37 +257,72 @@ } sql = sql + null + ", "; if (machineInfo.getEndTime() != null) { - - sql = sql + "'" + DateUtils.formattedDate(machineInfo.getEndTime(),DateUtils.STR_DD_MM_YYYY,DateUtils.STR_DATE_TIME_SMALL) + "', "; + sql = getTime(machineInfo, sql); } else { return null; } - Map<String ,DetailedListVo> itemMap = new HashMap<String ,DetailedListVo>(); - for (DetailedListVo item: itemList) { - itemMap.put(item.getItemName(),item); + Map<String, DetailedListVo> itemMap = new HashMap<String, DetailedListVo>(); + for (DetailedListVo item : itemList) { + itemMap.put(item.getItemName(), item); } if (itemMap.containsKey("runningStatus")) { int oporation = 0; switch (itemMap.get("runningStatus").getItemValue()) { - case "0" : oporation =0;break; - case "1" : oporation =2;break; - case "2" : oporation =3;break; - case "3" : oporation =2;break; - case "4" : oporation =2;break; - case "10" : oporation =2;break; - case "13" : oporation =2;break; - case "14" : oporation =2;break; - case "10001" : oporation =2;break; - default: break; + case "0": + oporation = 0; + break; + case "1": + oporation = 2; + break; + case "2": + oporation = 3; + break; + case "3": + oporation = 2; + break; + case "4": + oporation = 2; + break; + case "10": + oporation = 2; + break; + case "13": + oporation = 2; + break; + case "14": + oporation = 2; + break; + case "10001": + oporation = 2; + break; + default: + break; } - sql = sql + " " + oporation + ", "; + sql = sql + " " + oporation + ", "; } else { return null; } - sql = sql + null + " ); \n"; - if (StringUtils.isBlank(listSqls) ) { + sql = sql + null + " ); \n"; + if (StringUtils.isBlank(listSqls)) { listSqls = sql; } return listSqls; } + + @NotNull + private String getTime(MachineEquipentInfo machineInfo, String sql) { + try { + // 1. 灏嗗瓧绗︿覆瑙f瀽涓� Date 瀵硅薄 + SimpleDateFormat inputFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss"); + Date endDate = inputFormat.parse(machineInfo.getEndTime()); + + // 2. 灏� Date 鏍煎紡鍖栦负鐩爣 SQL 鏍煎紡锛堝 "yyyy-MM-dd HH:mm:ss"锛� + SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + sql = sql + "'" + outputFormat.format(endDate) + "', "; + } catch (ParseException e) { + // 澶勭悊瑙f瀽澶辫触鐨勬儏鍐碉紙濡傛棩蹇楄褰曟垨鎶涘嚭寮傚父锛� + throw new RuntimeException("Invalid end time format: " + machineInfo.getEndTime(), e); + } + return sql; + } } diff --git a/lxzn-module-system/lxzn-system-biz/src/main/java/org/jeecg/modules/system/controller/TriggerCreateController.java b/lxzn-module-system/lxzn-system-biz/src/main/java/org/jeecg/modules/system/controller/TriggerCreateController.java new file mode 100644 index 0000000..7635c72 --- /dev/null +++ b/lxzn-module-system/lxzn-system-biz/src/main/java/org/jeecg/modules/system/controller/TriggerCreateController.java @@ -0,0 +1,204 @@ +package org.jeecg.modules.system.controller; + +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import lombok.extern.slf4j.Slf4j; +import org.jeecg.common.api.vo.Result; +import org.jeecg.common.aspect.annotation.AutoLog; +import org.jeecg.modules.system.vo.TriggerVo; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.io.FileWriter; +import java.io.IOException; +import java.sql.*; +import java.util.ArrayList; +import java.util.List; + +/** + * @Author: Lius + * @CreateTime: 2025-06-26 + * @Description: + */ +@Api(tags = "瑙﹀彂鍣�") +@RestController +@RequestMapping("/sys/trigger") +@Slf4j +public class TriggerCreateController { + + @AutoLog(value = "瑙﹀彂鍣�-鐢熸垚瑙﹀彂鍣�") + @ApiOperation(value = "瑙﹀彂鍣�-鐢熸垚瑙﹀彂鍣�", notes = "瑙﹀彂鍣�-鐢熸垚瑙﹀彂鍣�") + @GetMapping(value = "/list") + public Result create(@RequestBody TriggerVo triggerVo) { + String schemaName = "dbo"; // SQL Server 榛樿鏋舵瀯 + String logTableName = "log_table"; // 鏃ュ織琛ㄥ悕 + + try (Connection connection = DriverManager.getConnection(triggerVo.getJdbcUrl(), triggerVo.getUsername(), triggerVo.getPassword())) { + // 鑾峰彇鎸囧畾鏋舵瀯涓嬬殑鎵�鏈夎〃 + List<String> tables = getTables(connection, schemaName); + + // 鐢熸垚瑙﹀彂鍣� SQL 骞朵繚瀛樺埌鏂囦欢 + generateTriggersForTables(connection, schemaName, logTableName, tables); + + } catch (SQLException | IOException e) { + e.printStackTrace(); + } + + return Result.ok(); + } + + /** + * 鑾峰彇鎸囧畾鏋舵瀯涓嬬殑鎵�鏈夎〃 + */ + private static List<String> getTables(Connection connection, String schemaName) throws SQLException { + List<String> tables = new ArrayList<>(); + DatabaseMetaData metaData = connection.getMetaData(); + + try (ResultSet resultSet = metaData.getTables(null, schemaName, null, new String[]{"TABLE"})) { + while (resultSet.next()) { + String tableName = resultSet.getString("TABLE_NAME"); + tables.add(tableName); + } + } + + return tables; + } + + /** + * 鑾峰彇琛ㄧ殑瀛楁鍒楄〃 + */ + private static List<String> getTableColumns(Connection connection, String schemaName, String tableName) throws SQLException { + List<String> columns = new ArrayList<>(); + DatabaseMetaData metaData = connection.getMetaData(); + + try (ResultSet resultSet = metaData.getColumns(null, schemaName, tableName, null)) { + while (resultSet.next()) { + String columnName = resultSet.getString("COLUMN_NAME"); + columns.add(columnName); + } + } + + return columns; + } + + /** + * 鑾峰彇琛ㄧ殑涓婚敭鍒� + */ + private static String getPrimaryKeyColumn(Connection connection, String schemaName, String tableName) throws SQLException { + DatabaseMetaData metaData = connection.getMetaData(); + try (ResultSet resultSet = metaData.getPrimaryKeys(null, schemaName, tableName)) { + if (resultSet.next()) { + return resultSet.getString("COLUMN_NAME"); + } + } + return "id"; // 榛樿浣跨敤id浣滀负涓婚敭锛屽鏋滄病鏈夋壘鍒颁富閿� + } + + /** + * 涓烘墍鏈夎〃鐢熸垚瑙﹀彂鍣� + */ + private static void generateTriggersForTables(Connection connection, String schemaName, String logTableName, List<String> tables) throws SQLException, IOException { + try (FileWriter writer = new FileWriter("triggers.sql")) { + for (String tableName : tables) { + // 鑾峰彇琛ㄧ粨鏋勪俊鎭� + List<String> columns = getTableColumns(connection, schemaName, tableName); + String primaryKey = getPrimaryKeyColumn(connection, schemaName, tableName); + + // 鐢熸垚瑙﹀彂鍣� SQL + String triggerSQL = generateTriggerSQL(schemaName, tableName, logTableName, columns, primaryKey); + + // 灏嗙敓鎴愮殑瑙﹀彂鍣� SQL 鍐欏叆鏂囦欢 + writer.write("-- 瑙﹀彂鍣ㄨ〃: " + tableName + "\n"); + writer.write(triggerSQL); + writer.write("\n\n"); + + // 濡傛灉闇�瑕侊紝鍙互鐩存帴鍦ㄦ暟鎹簱涓垱寤鸿Е鍙戝櫒 + // createTrigger(connection, triggerSQL); + } + System.out.println("鎴愬姛鐢熸垚瑙﹀彂鍣ㄥ苟淇濆瓨鍒癟riggers.sql涓�!"); + } + } + + /** + * 鐢熸垚 SQL Server 瑙﹀彂鍣� SQL + */ + private static String generateTriggerSQL(String schemaName, String tableName, String logTableName, List<String> columns, String primaryKey) { + StringBuilder triggerSQL = new StringBuilder(); + + // 瑙﹀彂鍣ㄥご閮� + triggerSQL.append("CREATE TRIGGER [") + .append(tableName) + .append("_trigger]\n") + .append("ON [") + .append(schemaName) + .append("].[").append(tableName).append("]\n") + .append("AFTER INSERT, UPDATE, DELETE\n") + .append("AS\n") + .append("BEGIN\n") + .append(" SET NOCOUNT ON;\n\n"); + + // 鎻掑叆鎿嶄綔 + triggerSQL.append(" -- 鎻掑叆鎿嶄綔\n") + .append(" IF EXISTS (SELECT * FROM inserted) AND NOT EXISTS (SELECT * FROM deleted)\n") + .append(" BEGIN\n") + .append(" INSERT INTO [").append(logTableName).append("] (action, create_time, sql_log)\n") + .append(" SELECT 'INSERT', GETDATE(), 'INSERT INTO [").append(tableName).append("] ("); + + // 鎷兼帴瀛楁鍚� + for (int i = 0; i < columns.size(); i++) { + triggerSQL.append("[").append(columns.get(i)).append("]"); + if (i < columns.size() - 1) { + triggerSQL.append(", "); + } + } + + triggerSQL.append(") VALUES (''' + "); + + // 鎷兼帴瀛楁鍊� + for (int i = 0; i < columns.size(); i++) { + triggerSQL.append("ISNULL(CAST(i.").append(columns.get(i)).append(" AS NVARCHAR(MAX)), '') + '''"); + if (i < columns.size() - 1) { + triggerSQL.append(", ''' + "); + } + } + + triggerSQL.append(");'\n") + .append(" FROM inserted i;\n") + .append(" END\n\n"); + + // 鏇存柊鎿嶄綔 + triggerSQL.append(" -- 鏇存柊鎿嶄綔\n") + .append(" IF EXISTS (SELECT * FROM inserted) AND EXISTS (SELECT * FROM deleted)\n") + .append(" BEGIN\n") + .append(" INSERT INTO [").append(logTableName).append("] (action, create_time, sql_log)\n") + .append(" SELECT 'UPDATE', GETDATE(), 'UPDATE [").append(tableName).append("] SET "); + + // 鎷兼帴鏇存柊瀛楁 + for (int i = 0; i < columns.size(); i++) { + triggerSQL.append("[").append(columns.get(i)).append("] = ''' + ISNULL(CAST(i.").append(columns.get(i)).append(" AS NVARCHAR(MAX)), '') + '''"); + if (i < columns.size() - 1) { + triggerSQL.append(", "); + } + } + + triggerSQL.append(" WHERE [").append(primaryKey).append("] = ''' + ISNULL(CAST(d.").append(primaryKey).append(" AS NVARCHAR(MAX)), '') + ''';'\n") + .append(" FROM inserted i\n") + .append(" INNER JOIN deleted d ON i.").append(primaryKey).append(" = d.").append(primaryKey).append(";\n") + .append(" END\n\n"); + + // 鍒犻櫎鎿嶄綔 + triggerSQL.append(" -- 鍒犻櫎鎿嶄綔\n") + .append(" IF NOT EXISTS (SELECT * FROM inserted) AND EXISTS (SELECT * FROM deleted)\n") + .append(" BEGIN\n") + .append(" INSERT INTO [").append(logTableName).append("] (action, create_time, sql_log)\n") + .append(" SELECT 'DELETE', GETDATE(), 'DELETE FROM [").append(tableName).append("] WHERE [").append(primaryKey).append("] = ''' + ISNULL(CAST(d.").append(primaryKey).append(" AS NVARCHAR(MAX)), '') + ''';'\n") + .append(" FROM deleted d;\n") + .append(" END\n\n"); + + triggerSQL.append("END;\n"); + + return triggerSQL.toString(); + } +} diff --git a/lxzn-module-system/lxzn-system-biz/src/main/java/org/jeecg/modules/system/vo/TriggerVo.java b/lxzn-module-system/lxzn-system-biz/src/main/java/org/jeecg/modules/system/vo/TriggerVo.java new file mode 100644 index 0000000..3bbf0c3 --- /dev/null +++ b/lxzn-module-system/lxzn-system-biz/src/main/java/org/jeecg/modules/system/vo/TriggerVo.java @@ -0,0 +1,17 @@ +package org.jeecg.modules.system.vo; + +import lombok.Data; + +/** + * @Author: Lius + * @CreateTime: 2025-06-26 + * @Description: + */ +@Data +public class TriggerVo { + + private String jdbcUrl; + private String username; + private String password; + +} diff --git a/lxzn-module-system/lxzn-system-start/src/main/resources/application-dev.yml b/lxzn-module-system/lxzn-system-start/src/main/resources/application-dev.yml index 55b1f1f..a1413e7 100644 --- a/lxzn-module-system/lxzn-system-start/src/main/resources/application-dev.yml +++ b/lxzn-module-system/lxzn-system-start/src/main/resources/application-dev.yml @@ -126,7 +126,7 @@ connectionProperties: druid.stat.mergeSql\=true;druid.stat.slowSqlMillis\=5000 datasource: master: - url: jdbc:sqlserver://192.168.1.118:1433;databasename=LXZN_TEST_HANGYU_GK;nullCatalogMeansCurrent=true + url: jdbc:sqlserver://localhost:1433;databasename=LXZN_TEST_HANGYU_GK;nullCatalogMeansCurrent=true username: sa password: 123 driverClassName: com.microsoft.sqlserver.jdbc.SQLServerDriver diff --git a/lxzn-module-system/lxzn-system-start/src/main/resources/application-prod.yml b/lxzn-module-system/lxzn-system-start/src/main/resources/application-prod.yml index dd516f0..f3e45d8 100644 --- a/lxzn-module-system/lxzn-system-start/src/main/resources/application-prod.yml +++ b/lxzn-module-system/lxzn-system-start/src/main/resources/application-prod.yml @@ -7,7 +7,7 @@ include-stacktrace: ALWAYS include-message: ALWAYS servlet: - context-path: + context-path: /jeecg-boot compression: enabled: true min-response-size: 1024 @@ -132,9 +132,9 @@ connectionProperties: druid.stat.mergeSql\=true;druid.stat.slowSqlMillis\=5000 datasource: master: - url: jdbc:sqlserver://localhost:1433;databasename=LXZN_TEST_COMMON + url: jdbc:sqlserver://DNC2\LXZN:1444;databasename=LXZN_TEST_COMMON;nullCatalogMeansCurrent=true username: sa - password: 123 + password: sa123 driverClassName: com.microsoft.sqlserver.jdbc.SQLServerDriver #redis 閰嶇疆 redis: @@ -247,8 +247,8 @@ production: false basic: enable: true - username: jeecg - password: jeecg1314 + username: lxzn + password: lxzn1688 #绗笁鏂圭櫥褰� justauth: enabled: true @@ -297,8 +297,34 @@ # appSecret client-secret: ?? agent-id: ?? +#staticAccessPath: /api/ffile/** # 褰撳墠椤圭洰鐨勯潤鎬佽祫婧愯闂厤缃湪nginx涓� +#鏂囦欢涓婁紶鐩綍锛堟敞鎰廘inux鍜學indows涓婄殑鐩綍缁撴瀯涓嶅悓锛� fileHomePath: ${UPLOAD_FOLDER:d://lxzn_storage} fileNCPath: ${NC_FOLDER:D://NC} # NC鏂囦欢浣嶇疆 ncSend: addOrDelete: false #//鏄惁鍒犻櫎seed 涓殑涔嬪墠鏂囦欢 #staticAccessPath: /api/ffile/** # 褰撳墠椤圭洰鐨勯潤鎬佽祫婧愯闂厤缃湪nginx涓� +#宸ユ帶缃�/娑夊瘑缃戦儴缃茬浉鍏抽厤缃�------------閫氳繃鍏夌洏鎽嗘浮 +deploy: + #宸ユ帶缃�/娑夊瘑缃戦儴缃� 0涓烘秹瀵嗙綉 1涓哄伐鎺х綉 + deployType: 0 + #娑夊瘑缃戜紶杈撳伐鎺х綉nc鏂囦欢澶�(鎸囨淳璁惧nc鏂囦欢) 宸ユ帶缃戜紶杈撴秹瀵嗙綉nc鏂囦欢澶�(瑙f瀽鍥炰紶鍚庣殑nc鏂囦欢) + secretFolder: D:\\test\\a +file: + monitor: + # 涓変釜鐩戞帶鐩綍閰嶇疆 + paths: + - D:\\hy_test\\ncFile #宸ユ帶璐熻矗瑙f瀽閲岄潰鐨勬枃浠�;娑夊瘑缃戣繘琛岃В鏋愬洖浼犵殑鎿嶄綔 + - D:\\hy_test\\b + - D:\\hy_test\\c + interval: 10000 # 鐩戞帶闂撮殧(ms) +ferry: + file: + path: /data/ferry + max-file-size: 10MB + +security: + encryption-key: 1234567890abcdef +#mdc鏂囦欢璺緞閰嶇疆 +fileService: + localFilePath: D:/data/sql/ #鏈湴鏂囦欢璺緞 -- Gitblit v1.9.3