lyh
2025-06-12 9a8739dde0429a15e4a09c48fd45ff6d850da0e1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package org.jeecg.modules.mdc.job;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.modules.mdc.dto.MdcEquipmentStatisticalDto;
import org.jeecg.modules.mdc.entity.MdcEquipment;
import org.jeecg.modules.mdc.entity.MdcEquipmentStatisticalInfoMonth;
import org.jeecg.modules.mdc.entity.MdcEquipmentStatisticalShiftInfoMonth;
import org.jeecg.modules.mdc.service.*;
import org.jeecg.modules.mdc.util.DateUtils;
import org.jeecg.modules.mdc.util.ThrowableUtil;
import org.jeecg.modules.quartz.entity.QuartzJob;
import org.jeecg.modules.quartz.entity.SysQuartzLog;
import org.jeecg.modules.quartz.service.IQuartzJobService;
import org.jeecg.modules.quartz.service.ISysQuartzLogService;
import org.jeecg.modules.system.service.ISysAnnouncementService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
 
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
/**
 * @Author: Lius
 * @CreateTime: 2025-03-24
 * @Description: 统计月汇总数据
 */
@Slf4j
public class CollectEquipmentDataMonthJob implements Job {
 
    /**
     * 若参数变量名修改 QuartzJobController中也需对应修改  时间: yyyyMMdd 例: 20230414
     */
    private String parameter;
 
    public void setParameter(String parameter) {
        this.parameter = parameter;
    }
 
    @Resource
    private IQuartzJobService quartzJobService;
 
    @Resource
    private ISysAnnouncementService sysAnnouncementService;
 
    @Resource
    private ISysQuartzLogService sysQuartzLogService;
 
    @Resource
    private IMdcEquipmentStatisticalInfoMonthService mdcEquipmentStatisticalInfoMonthService;
 
    @Resource
    private IMdcEquipmentStatisticalInfoService mdcEquipmentStatisticalInfoService;
 
    @Resource
    private IMdcEquipmentStatisticalShiftInfoMonthService mdcEquipmentStatisticalShiftInfoMonthService;
 
    @Resource
    private IMdcEquipmentStatisticalShiftInfoService mdcEquipmentStatisticalShiftInfoService;
 
    @Resource
    private IMdcEquipmentService mdcEquipmentService;
 
    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        SysQuartzLog quartzLog = new SysQuartzLog();
        quartzLog.setCreateTime(new Date());
        List<QuartzJob> byJobClassName = this.quartzJobService.findByJobClassName(this.getClass().getName());
        if (byJobClassName != null && !byJobClassName.isEmpty()) {
            quartzLog.setJobId(byJobClassName.get(0).getId());
        }
        quartzLog.setParams(this.parameter);
        log.info(String.format("统计月汇总数据 param: %s CollectEquipmentDataMonthJob start!  时间:" + DateUtils.getNow(), this.parameter));
        long startTime = System.currentTimeMillis();
        try {
            String date = "";
            if (StringUtils.isNotBlank(this.parameter)) {
                date = DateUtils.format(DateUtils.toDate(this.parameter, DateUtils.STRYEARMONTH), DateUtils.STRYEARMONTH);
            } else {
                date = DateUtils.format(DateUtils.getNow(), DateUtils.STRYEARMONTH);
            }
            mdcEquipmentStatisticalInfoMonthService.remove(new LambdaQueryWrapper<MdcEquipmentStatisticalInfoMonth>().eq(MdcEquipmentStatisticalInfoMonth::getTheMonth, date));
            mdcEquipmentStatisticalShiftInfoMonthService.remove(new LambdaQueryWrapper<MdcEquipmentStatisticalShiftInfoMonth>().eq(MdcEquipmentStatisticalShiftInfoMonth::getTheMonth, date));
 
            // step.1 获取设备列表
            List<MdcEquipment> mdcEquipmentList = mdcEquipmentService.list();
            List<MdcEquipmentStatisticalInfoMonth> mdcEquipmentStatisticalInfoMonthList = new ArrayList<>();
            List<MdcEquipmentStatisticalShiftInfoMonth> mdcEquipmentStatisticalShiftInfoMonthList = new ArrayList<>();
            if (mdcEquipmentList != null && !mdcEquipmentList.isEmpty()) {
                for (MdcEquipment mdcEquipment : mdcEquipmentList) {
                    // step.2 汇总24小时利用率数据
                    MdcEquipmentStatisticalDto mdcEquipmentStatisticalDto = mdcEquipmentStatisticalInfoService.findByEquipmentAndMonth(mdcEquipment.getEquipmentId(), date);
                    MdcEquipmentStatisticalInfoMonth mdcEquipmentStatisticalInfoMonth = new MdcEquipmentStatisticalInfoMonth();
                    mdcEquipmentStatisticalInfoMonth.setEquipmentId(mdcEquipment.getEquipmentId());
                    mdcEquipmentStatisticalInfoMonth.setTheMonth(date);
                    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("86400").multiply(mdcEquipmentStatisticalDto.getDayNum())), 4, RoundingMode.HALF_UP));
                    mdcEquipmentStatisticalInfoMonth.setStartRate(mdcEquipmentStatisticalDto.getOpenLong().compareTo(BigDecimal.ZERO) > 0 ? mdcEquipmentStatisticalDto.getProcessLong().divide(mdcEquipmentStatisticalDto.getOpenLong(), 4, RoundingMode.HALF_UP) : BigDecimal.ZERO);
                    mdcEquipmentStatisticalInfoMonth.setUtilizationRate(mdcEquipmentStatisticalDto.getProcessLong().divide((new BigDecimal("86400").multiply(mdcEquipmentStatisticalDto.getDayNum())), 4, RoundingMode.HALF_UP));
                    mdcEquipmentStatisticalInfoMonthList.add(mdcEquipmentStatisticalInfoMonth);
                    // step.3 汇总班次利用率数据
                    MdcEquipmentStatisticalDto mdcEquipmentShiftStatisticalDto = mdcEquipmentStatisticalShiftInfoService.findByEquipmentAndMonth(mdcEquipment.getEquipmentId(), date);
                    MdcEquipmentStatisticalShiftInfoMonth mdcEquipmentShiftInfoMonth = new MdcEquipmentStatisticalShiftInfoMonth();
                    mdcEquipmentShiftInfoMonth.setEquipmentId(mdcEquipment.getEquipmentId());
                    mdcEquipmentShiftInfoMonth.setTheMonth(date);
                    mdcEquipmentShiftInfoMonth.setShiftType(CommonConstant.SHIFT_TYPE_1);
                    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) : BigDecimal.ZERO);
                    mdcEquipmentShiftInfoMonth.setStartRate(mdcEquipmentShiftStatisticalDto.getOpenLong().compareTo(BigDecimal.ZERO) > 0 ? mdcEquipmentShiftStatisticalDto.getProcessLong().divide(mdcEquipmentShiftStatisticalDto.getOpenLong(), 4, RoundingMode.HALF_UP) : BigDecimal.ZERO);
                    mdcEquipmentShiftInfoMonth.setOpenRate(mdcEquipmentShiftStatisticalDto.getTotalLong().compareTo(BigDecimal.ZERO) > 0 ? mdcEquipmentShiftStatisticalDto.getOpenLong().divide(mdcEquipmentShiftStatisticalDto.getTotalLong(), 4, RoundingMode.HALF_UP) : BigDecimal.ZERO);
                    mdcEquipmentStatisticalShiftInfoMonthList.add(mdcEquipmentShiftInfoMonth);
                    // step.4 汇总各班次利用率数据
                    List<MdcEquipmentStatisticalDto> essdList = mdcEquipmentStatisticalShiftInfoService.findShiftByEquipmentAndMonth(mdcEquipment.getEquipmentId(), date);
                    if (essdList != null && !essdList.isEmpty()) {
                        for (MdcEquipmentStatisticalDto equipmentStatisticalDto : essdList) {
                            MdcEquipmentStatisticalShiftInfoMonth equipmentStatisticalShiftInfoMonth = new MdcEquipmentStatisticalShiftInfoMonth();
                            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) : BigDecimal.ZERO);
                            equipmentStatisticalShiftInfoMonth.setStartRate(equipmentStatisticalDto.getOpenLong().compareTo(BigDecimal.ZERO) > 0 ? equipmentStatisticalDto.getProcessLong().divide(equipmentStatisticalDto.getOpenLong(), 4, RoundingMode.HALF_UP) : BigDecimal.ZERO);
                            equipmentStatisticalShiftInfoMonth.setOpenRate(equipmentStatisticalDto.getTotalLong().compareTo(BigDecimal.ZERO) > 0 ? equipmentStatisticalDto.getOpenLong().divide(equipmentStatisticalDto.getTotalLong(), 4, RoundingMode.HALF_UP) : BigDecimal.ZERO);
                            mdcEquipmentStatisticalShiftInfoMonthList.add(equipmentStatisticalShiftInfoMonth);
                        }
                    }
                }
            }
            if (!mdcEquipmentStatisticalInfoMonthList.isEmpty()) {
                mdcEquipmentStatisticalInfoMonthService.saveBatch(mdcEquipmentStatisticalInfoMonthList);
            }
            if (!mdcEquipmentStatisticalShiftInfoMonthList.isEmpty()) {
                mdcEquipmentStatisticalShiftInfoMonthService.saveBatch(mdcEquipmentStatisticalShiftInfoMonthList);
            }
            quartzLog.setIsSuccess(0);
        } catch (Exception e) {
            quartzLog.setIsSuccess(-1);
            quartzLog.setExceptionDetail(ThrowableUtil.getStackTrace(e));
            // 发送消息通知
            sysAnnouncementService.jobSendMessage("统计月汇总数据", quartzLog.getExceptionDetail());
        }
        long endTime = System.currentTimeMillis();
        quartzLog.setExecutionTime(Integer.parseInt(String.valueOf(endTime - startTime)));
        sysQuartzLogService.save(quartzLog);
    }
 
 
}