Lius
2024-11-04 d603739c0320f355cfc3152865918729fea790de
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
package org.jeecg.modules.mdc.job;
 
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.mdc.dto.EquipmentMachiningHistoryDto;
import org.jeecg.modules.mdc.dto.MachineXYZHistoryDto;
import org.jeecg.modules.mdc.entity.Equipment;
import org.jeecg.modules.mdc.entity.EquipmentSpindleStatistical;
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.*;
 
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
/**
 * 设备运行最大主轴负载统计
 *
 * @author Lius
 * @date 2024/7/18 15:59
 */
@PersistJobDataAfterExecution
@DisallowConcurrentExecution
@Slf4j
public class StatisticalSpindleJob implements Job {
 
    @Resource
    private IQuartzJobService quartzJobService;
 
    @Resource
    private ISysQuartzLogService sysQuartzLogService;
 
    @Resource
    private ISysAnnouncementService sysAnnouncementService;
 
    @Resource
    private IEquipmentService equipmentService;
 
    @Resource
    private IEquipmentSpindleStatisticalService equipmentSpindleStatisticalService;
 
    @Resource
    private IEquipmentMachiningHistoryService equipmentMachiningHistoryService;
 
    @Resource
    private IMachineXYZHistoryService machineXYZHistoryService;
 
    @Resource
    private IEquipmentWorkLineService equipmentWorkLineService;
 
    @Resource
    private IEquipmentXYZService equipmentXYZService;
 
    @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());
        }
        log.info("设备运行最大主轴负载统计 StatisticalSpindleJob start!  时间:" + DateUtils.getNow());
        long startTime = System.currentTimeMillis();
        try {
            List<Equipment> equipmentList = equipmentService.list();
            if (equipmentList != null && !equipmentList.isEmpty()) {
                Date initDate = null;
                EquipmentSpindleStatistical spindleStatistical;
                List<EquipmentSpindleStatistical> resultList = new ArrayList<>();
                for (Equipment equipment : equipmentList) {
                    if (!equipment.getDrivetype().equals("CurrentState")) {
                        String saveTableName = equipment.getSavetablename();
                        Date lastDate = equipmentSpindleStatisticalService.getMaxDate(equipment.getEquipmentid());
                        if (lastDate == null) {
                            Date minCollectTime = equipmentWorkLineService.getMinDate(saveTableName);
                            if (minCollectTime == null) {
                                continue;
                            }
                            initDate = DateUtils.removeTime(minCollectTime);
                        } else {
                            initDate = DateUtils.plusTime(lastDate, 1);
                        }
 
                        spindleStatistical = new EquipmentSpindleStatistical();
                        EquipmentMachiningHistoryDto machiningHistoryDto = equipmentWorkLineService.getMaxSpindleLoad(saveTableName, initDate, DateUtils.plusTime(initDate, 1));
                        if(machiningHistoryDto == null || machiningHistoryDto.getEquipmentID() == null || machiningHistoryDto.getCollectTime() == null) {
                            continue;
                        }
                        spindleStatistical.setCreatedate(initDate);
                        spindleStatistical.setSpindlespeed(machiningHistoryDto.getSpindleSpeed());
                        spindleStatistical.setEquipmentid(equipment.getEquipmentid());
                        spindleStatistical.setEquipmentname(equipment.getEquipmentname());
                        spindleStatistical.setSpindleload(machiningHistoryDto.getSpindleLoad());
                        spindleStatistical.setSpindletime(machiningHistoryDto.getCollectTime());
                        MachineXYZHistoryDto machineXYZHistoryDto = equipmentXYZService.getNearAxisType(equipment.getEquipmentid(), initDate, DateUtils.plusTime(initDate, 1), machiningHistoryDto.getCollectTime());
                        if(machineXYZHistoryDto != null) {
                            spindleStatistical.setAxistime(machineXYZHistoryDto.getCollectTime());
                            spindleStatistical.setAxisx(machineXYZHistoryDto.getXMachine());
                            spindleStatistical.setAxisy(machineXYZHistoryDto.getYMachine());
                            spindleStatistical.setAxisz(machineXYZHistoryDto.getZMachine());
                            spindleStatistical.setAxisa(machineXYZHistoryDto.getAMachine());
                            spindleStatistical.setAxisb(machineXYZHistoryDto.getBMachine());
                        }
                        resultList.add(spindleStatistical);
                    }
                }
                if (!resultList.isEmpty()) {
                    equipmentSpindleStatisticalService.saveBatch(resultList);
                }
            }
            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);
    }
}