Lius
2024-12-25 68b2447151e509fef215c9f8515fe38bc36b2b8c
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
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.entity.MdcEquipment;
import org.jeecg.modules.mdc.entity.MdcEquipmentOvertime;
import org.jeecg.modules.mdc.entity.MdcEquipmentRunningSection;
import org.jeecg.modules.mdc.entity.MdcSystemParameters;
import org.jeecg.modules.mdc.service.*;
import org.jeecg.modules.mdc.util.DateUtils;
import org.jeecg.modules.mdc.util.ThrowableUtil;
import org.jeecg.modules.mdc.vo.MdcDeviceCalendarVo;
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.*;
 
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
/**
 * @author Lius
 * @Description: 设备加班时长算法统计
 * @date 2024/1/24 17:12
 */
@Slf4j
public class RunningOvertimeDurationJob implements Job {
 
    @Resource
    private IQuartzJobService quartzJobService;
 
    @Resource
    private ISysQuartzLogService sysQuartzLogService;
 
    @Resource
    private IMdcEquipmentService mdcEquipmentService;
 
    @Resource
    private IMdcEquipmentOvertimeService mdcEquipmentOvertimeService;
 
    /**
     * 若参数变量名修改 QuartzJobController中也需对应修改  时间: yyyyMMdd 例: 20230414
     */
    private String parameter;
 
    public void setParameter(String parameter) {
        this.parameter = parameter;
    }
 
    @Resource
    private ISysAnnouncementService sysAnnouncementService;
 
    @Resource
    private IMdcDeviceCalendarService mdcDeviceCalendarService;
 
    @Resource
    private IMdcSystemParametersService mdcSystemParametersService;
 
    @Resource
    private IMdcEquipmentRunningSectionService mdcEquipmentRunningSectionService;
 
    @Override
    public void execute(JobExecutionContext jobExecutionContext) 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 RunningOvertimeDurationJob start!  时间:" + DateUtils.getNow(), this.parameter));
        long startTime = System.currentTimeMillis();
        String date = "";
        try {
            if (StringUtils.isNotBlank(this.parameter)) {
                date = DateUtils.format(DateUtils.toDate(this.parameter, DateUtils.STRDATE), DateUtils.STR_DATE);
                Date initDate = DateUtils.toDate(this.parameter, DateUtils.STRDATE);
                if (initDate != null) {
                    mdcEquipmentOvertimeService.remove(new LambdaQueryWrapper<MdcEquipmentOvertime>().eq(MdcEquipmentOvertime::getTheDate, this.parameter).eq(MdcEquipmentOvertime::getAutoFlag, CommonConstant.AUTO_FLAG_Y));
                }
            } else {
                date = DateUtils.format(DateUtils.getNow(), DateUtils.STR_DATE);
            }
            List<MdcEquipment> equipmentList = mdcEquipmentService.list();
            MdcSystemParameters mdcSystemParameters = mdcSystemParametersService.getOne(new LambdaQueryWrapper<MdcSystemParameters>().eq(MdcSystemParameters::getCode, "equip_log_statis_time"));
            if (mdcSystemParameters == null) {
                throw new JobExecutionException("mdc_system_parameters 表中数据缺失");
            }
            String value = mdcSystemParameters.getValue();
            List<MdcEquipmentOvertime> result = new ArrayList<>();
            for (MdcEquipment mdcEquipment : equipmentList) {
                // 获取设备工作日历
                List<MdcDeviceCalendarVo> mdcDeviceCalendarVos = mdcDeviceCalendarService.listByEquipmentIdAndDate(mdcEquipment.getEquipmentId(), DateUtils.format(DateUtils.toDate(date, DateUtils.STR_DATE),DateUtils.STRDATE));
                if (mdcDeviceCalendarVos != null && !mdcDeviceCalendarVos.isEmpty()) {
                    //获取最后一个班次结束时间
                    Date startDate = this.getCalendarEndDate(mdcDeviceCalendarVos, date);
                    Date endDate = DateUtils.plusTime(DateUtils.toDate(date + " " + value, DateUtils.STR_DATE_TIME_SMALL), 1);
                    List<MdcEquipmentRunningSection> equipmentRunningSectionList = mdcEquipmentRunningSectionService.selectRunningData(mdcEquipment.getEquipmentId(), startDate, endDate);
                    if (equipmentRunningSectionList != null && !equipmentRunningSectionList.isEmpty()) {
                        // 时间修正
                        if (equipmentRunningSectionList.get(0).getStartTime().before(startDate)) {
                            equipmentRunningSectionList.get(0).setStartTime(startDate);
                        }
                        if (equipmentRunningSectionList.get(equipmentRunningSectionList.size() - 1).getEndTime().after(endDate)) {
                            equipmentRunningSectionList.get(equipmentRunningSectionList.size() - 1).setEndTime(endDate);
                        }
                        BigDecimal duration = BigDecimal.ZERO;
                        for (MdcEquipmentRunningSection mdcEquipmentRunningSection : equipmentRunningSectionList) {
                            duration = duration.add(new BigDecimal(DateUtils.differentSecond(mdcEquipmentRunningSection.getStartTime(), mdcEquipmentRunningSection.getEndTime())));
                        }
                        MdcEquipmentOvertime mdcEquipmentOvertime = new MdcEquipmentOvertime();
                        mdcEquipmentOvertime.setEquipmentId(mdcEquipment.getEquipmentId());
                        mdcEquipmentOvertime.setDuration(duration);
                        mdcEquipmentOvertime.setAutoFlag(CommonConstant.AUTO_FLAG_Y);
                        mdcEquipmentOvertime.setTheDate(DateUtils.format(DateUtils.toDate(date, DateUtils.STR_DATE),DateUtils.STRDATE));
                        result.add(mdcEquipmentOvertime);
                    }
 
                } else {
                    // 没有班次配置,计算全天的设备加工状态
                    Date startDate = DateUtils.toDate(date + " " + value, DateUtils.STR_DATE_TIME_SMALL);
                    Date endDate = DateUtils.plusTime(startDate, 1);    
                    List<MdcEquipmentRunningSection> equipmentRunningSectionList = mdcEquipmentRunningSectionService.selectRunningData(mdcEquipment.getEquipmentId(), startDate, endDate);
                    if (equipmentRunningSectionList != null && !equipmentRunningSectionList.isEmpty()) {
                        // 时间修正
                        if (equipmentRunningSectionList.get(0).getStartTime().before(startDate)) {
                            equipmentRunningSectionList.get(0).setStartTime(startDate);
                        }
                        if (equipmentRunningSectionList.get(equipmentRunningSectionList.size() - 1).getEndTime().after(endDate)) {
                            equipmentRunningSectionList.get(equipmentRunningSectionList.size() - 1).setEndTime(endDate);
                        }
                        BigDecimal duration = BigDecimal.ZERO;
                        for (MdcEquipmentRunningSection mdcEquipmentRunningSection : equipmentRunningSectionList) {
                            duration = duration.add(new BigDecimal(DateUtils.differentSecond(mdcEquipmentRunningSection.getStartTime(), mdcEquipmentRunningSection.getEndTime())));
                        }
                        MdcEquipmentOvertime mdcEquipmentOvertime = new MdcEquipmentOvertime();
                        mdcEquipmentOvertime.setEquipmentId(mdcEquipment.getEquipmentId());
                        mdcEquipmentOvertime.setDuration(duration);
                        mdcEquipmentOvertime.setAutoFlag(CommonConstant.AUTO_FLAG_Y);
                        mdcEquipmentOvertime.setTheDate(DateUtils.format(DateUtils.toDate(date, DateUtils.STR_DATE),DateUtils.STRDATE));
                        result.add(mdcEquipmentOvertime);
                    }
 
                }
            }
            if (!result.isEmpty()) {
                mdcEquipmentOvertimeService.saveBatch(result);
            }
            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);
    }
 
    private Date getCalendarEndDate(List<MdcDeviceCalendarVo> mdcDeviceCalendarVos, String date) {
        date = DateUtils.format(DateUtils.toDate(date, DateUtils.STRDATE), DateUtils.STR_DATE);
        String endDate = "";
        for (MdcDeviceCalendarVo mdcDeviceCalendarVo : mdcDeviceCalendarVos) {
            String shiftEndDate = "";
            if ("true".equals(mdcDeviceCalendarVo.getIsDaySpan())) {
                shiftEndDate = DateUtils.format(DateUtils.plusTime(DateUtils.toDate(date, DateUtils.STR_DATE), 1), DateUtils.STR_DATE) + " " + mdcDeviceCalendarVo.getEndDate();
            } else {
                shiftEndDate = date + " " + mdcDeviceCalendarVo.getEndDate();
            }
            if (StringUtils.isBlank(endDate)) {
                endDate = shiftEndDate;
            } else {
                // endDate
                Date date1 = DateUtils.toDate(endDate, DateUtils.STR_DATE_TIME_SMALL);
                // shiftEndDate
                Date date2 = DateUtils.toDate(shiftEndDate, DateUtils.STR_DATE_TIME_SMALL);
                if (date2.after(date1)) {
                    endDate = shiftEndDate;
                }
            }
        }
        return DateUtils.toDate(endDate, DateUtils.STR_DATE_TIME_SMALL);
    }
}