Lius
2025-05-12 54bfeae64b0d4a85b8f3e146b0f261d35c45ddc0
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
package org.jeecg.modules.mdc.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.modules.mdc.entity.*;
import org.jeecg.modules.mdc.mapper.MdcDeviceCalendarMapper;
import org.jeecg.modules.mdc.service.*;
import org.jeecg.modules.mdc.util.DateUtils;
import org.jeecg.modules.mdc.vo.EquipmentCalendarVo;
import org.jeecg.modules.mdc.vo.MdcDeviceCalendarQueryVo;
import org.jeecg.modules.mdc.vo.MdcDeviceCalendarVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
 
/**
 * @Description: 设备工作日历表
 * @Author: jeecg-boot
 * @Date: 2023-04-10
 * @Version: V1.0
 */
@Service
public class MdcDeviceCalendarServiceImpl extends ServiceImpl<MdcDeviceCalendarMapper, MdcDeviceCalendar> implements IMdcDeviceCalendarService {
 
    @Resource
    private IMdcEquipmentService mdcEquipmentService;
    @Resource
    private IMdcShiftSubService mdcShiftSubService;
 
    @Resource
    private IMdcVacationManagementService mdcVacationManagementService;
 
    @Resource
    private IMdcEquipmentOvertimeService mdcEquipmentOvertimeService;
 
    /**
     * 分页列表查询
     */
    @Override
    public IPage<MdcDeviceCalendarVo> pageList(String userId, MdcDeviceCalendarQueryVo mdcDeviceCalendarQueryVo, Integer pageNo, Integer pageSize, HttpServletRequest req) {
        IPage<MdcDeviceCalendarVo> pageData = new Page<>(pageNo, pageSize);
        List<String> equipmentIds = new ArrayList<>();
        if (StringUtils.isNotEmpty(mdcDeviceCalendarQueryVo.getParentId()) && StringUtils.isEmpty(mdcDeviceCalendarQueryVo.getEquipmentId())) {
            if ("2".equals(mdcDeviceCalendarQueryVo.getTypeTree())) {
                // 部门层级
                equipmentIds = mdcEquipmentService.getEquipmentIdsByDepart(userId, mdcDeviceCalendarQueryVo.getParentId());
            } else {
                // 产线层级
                equipmentIds = mdcEquipmentService.getEquipmentIdsProduction(userId, mdcDeviceCalendarQueryVo.getParentId());
            }
        } else if (StringUtils.isNotEmpty(mdcDeviceCalendarQueryVo.getEquipmentId())) {
            // 单台设备信息
            mdcDeviceCalendarQueryVo.setEquipmentIdList(Collections.singletonList(mdcDeviceCalendarQueryVo.getEquipmentId()));
        } else {
            // 查询用户拥有的所有设备信息
            if ("2".equals(mdcDeviceCalendarQueryVo.getTypeTree())) {
                //部门层级
                equipmentIds = mdcEquipmentService.getEquipmentIdsByDepart(userId, null);
            } else {
                //产线层级
                equipmentIds = mdcEquipmentService.getEquipmentIdsProduction(userId, null);
            }
        }
        if (mdcDeviceCalendarQueryVo.getEquipmentIdList() == null || mdcDeviceCalendarQueryVo.getEquipmentIdList().isEmpty()) {
            mdcDeviceCalendarQueryVo.setEquipmentIdList(equipmentIds);
        }
 
        if (mdcDeviceCalendarQueryVo.getEquipmentIdList() == null || mdcDeviceCalendarQueryVo.getEquipmentIdList().isEmpty()) {
            return null;
        }
 
        return this.baseMapper.pageList(pageData, mdcDeviceCalendarQueryVo);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveCalendar(EquipmentCalendarVo calendarVo) {
        String[] equipmentIdList = calendarVo.getEquipmentId().split(",");
        List<String> dateList = calendarVo.getDateList();
        List<MdcShiftSub> mdcShiftSubList = mdcShiftSubService.list(new LambdaQueryWrapper<MdcShiftSub>().eq(MdcShiftSub::getShiftId, calendarVo.getShiftId()).eq(MdcShiftSub::getShiftSubStatus, "1"));
        List<MdcDeviceCalendar> calendarList = new ArrayList<>();
        for (String equipmentId : equipmentIdList) {
            for (String date : dateList) {
                for (MdcShiftSub mdcShiftSub : mdcShiftSubList) {
                    this.baseMapper.delete(new LambdaQueryWrapper<MdcDeviceCalendar>().eq(MdcDeviceCalendar::getEquipmentId, equipmentId).eq(MdcDeviceCalendar::getEffectiveDate, date));
                    MdcDeviceCalendar mdcDeviceCalendar = new MdcDeviceCalendar();
                    mdcDeviceCalendar.setEffectiveDate(date);
                    mdcDeviceCalendar.setEquipmentId(equipmentId);
                    mdcDeviceCalendar.setShiftId(mdcShiftSub.getShiftId());
                    mdcDeviceCalendar.setShiftSubId(mdcShiftSub.getId());
                    calendarList.add(mdcDeviceCalendar);
                }
            }
        }
        this.saveBatch(calendarList);
        return true;
    }
 
    /**
     * 查询班制数据
     *
     * @param equipmentId
     * @param stringDates
     * @return
     */
    @Override
    public List<MdcDeviceCalendarVo> listByEquipmentAndDate(String equipmentId, List<String> stringDates) {
        try {
            List<MdcDeviceCalendarVo> mdcDeviceCalendarVos = this.baseMapper.listByEquipmentAndDates(equipmentId, stringDates);
            if (mdcDeviceCalendarVos != null && !mdcDeviceCalendarVos.isEmpty()) {
                return mdcDeviceCalendarVos;
            } else {
                return null;
            }
        } catch (Exception e) {
            return null;
        }
    }
 
    /**
     * 计算实际班产天数
     *
     * @param equipmentId
     * @param validDate
     * @return
     */
    @Override
    public BigDecimal computeActualWorkDayCount(String equipmentId, String validDate) {
        validDate = validDate.replaceAll("-", "");
        List<String> validDateList = this.baseMapper.computeActualWorkDayCount(equipmentId, validDate);
        return new BigDecimal(validDateList.size());
    }
 
    /**
     * 查询班次分类
     *
     * @param equipmentId
     * @param validDate
     * @return
     */
    @Override
    public List<String> findShiftSort(String equipmentId, String validDate) {
        validDate = validDate.replaceAll("-", "");
        return this.baseMapper.findShiftSort(equipmentId, validDate);
    }
 
    /**
     * 计算班次时间
     *
     * @param equipmentId
     * @param validDate
     * @return
     */
    @Override
    public BigDecimal computeShiftTimeCount(String equipmentId, String validDate) {
        validDate = validDate.replaceAll("-", "");
        Integer shiftCount = this.baseMapper.computeShiftTimeCount(equipmentId, validDate);
        return new BigDecimal(shiftCount).multiply(new BigDecimal("8")).multiply(new BigDecimal("60"));
    }
 
    /**
     * 自动生成设备工作日历
     */
    @Override
    public void generateDeviceCalendar() {
        // 暂定每天晚上11点执行
        //查询默认班制
        List<MdcDeviceCalendarVo> acquiesceShift = this.baseMapper.findAcquiesceShift();
        // 获取明日日期
        String localDate = LocalDate.now().plusDays(1).toString();
        // 获取设备列表
        List<MdcEquipment> equipmentList = mdcEquipmentService.list();
        List<MdcDeviceCalendar> result = new ArrayList<>();
        for (MdcEquipment mdcEquipment : equipmentList) {
            List<MdcDeviceCalendar> list = super.list(new LambdaQueryWrapper<MdcDeviceCalendar>().eq(MdcDeviceCalendar::getEquipmentId, mdcEquipment.getEquipmentId()).eq(MdcDeviceCalendar::getEffectiveDate, localDate.replaceAll("-", "")));
            if (list == null || list.isEmpty()) {
                List<MdcVacationManagement> vacationManagementList = mdcVacationManagementService.list(new LambdaQueryWrapper<MdcVacationManagement>().eq(MdcVacationManagement::getEquipmentId, mdcEquipment.getEquipmentId()).eq(MdcVacationManagement::getVacationDate, localDate));
                if (vacationManagementList == null || vacationManagementList.isEmpty()) {
                    String effectiveDate = localDate.replaceAll("-", "");
                    for (MdcDeviceCalendarVo mdcDeviceCalendarVo : acquiesceShift) {
                        MdcDeviceCalendar mdcDeviceCalendar = new MdcDeviceCalendar();
                        mdcDeviceCalendar.setEquipmentId(mdcEquipment.getEquipmentId());
                        mdcDeviceCalendar.setEffectiveDate(effectiveDate);
                        mdcDeviceCalendar.setShiftId(mdcDeviceCalendarVo.getShiftId());
                        mdcDeviceCalendar.setShiftSubId(mdcDeviceCalendarVo.getShiftSubId());
                        result.add(mdcDeviceCalendar);
                    }
                }
            }
        }
        if (!result.isEmpty()) {
            super.saveBatch(result);
        }
    }
 
    @Override
    public MdcDeviceCalendar getFirstData(String equipmentId) {
        return this.baseMapper.getFirstData(equipmentId);
    }
 
    @Override
    public List<MdcDeviceCalendarVo> listByEquipmentIdAndDate(String equipmentId, String date) {
        List<MdcDeviceCalendarVo> mdcDeviceCalendarVos = this.baseMapper.listByEquipmentAndDate(equipmentId, date);
        if (mdcDeviceCalendarVos != null && !mdcDeviceCalendarVos.isEmpty()) {
            for (MdcDeviceCalendarVo mdcDeviceCalendarVo : mdcDeviceCalendarVos) {
                List<MdcEquipmentOvertime> list = mdcEquipmentOvertimeService.list(new LambdaQueryWrapper<MdcEquipmentOvertime>().eq(MdcEquipmentOvertime::getEquipmentId, mdcDeviceCalendarVo.getEquipmentId()).eq(MdcEquipmentOvertime::getTheDate, mdcDeviceCalendarVo.getEndDate()));
                if (list != null && !list.isEmpty()) {
                    mdcDeviceCalendarVo.setOvertimeStartTime(list.get(0).getStartTime());
                    mdcDeviceCalendarVo.setOvertimeEndTime(list.get(0).getEndTime());
                }
            }
        }
        return mdcDeviceCalendarVos;
    }
 
    @Override
    public String findShiftByEquId(String equipmentId) {
        String result = "";
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
        String date = now.format(formatter);
        String format = DateUtils.format(DateUtils.toDate(date, DateUtils.STRDATE), DateUtils.STR_DATE);
        List<MdcShiftSub> shiftSubList = this.baseMapper.findShiftByEquId(equipmentId, date);
        if (shiftSubList != null && !shiftSubList.isEmpty()) {
            for (MdcShiftSub mdcShiftSub : shiftSubList) {
                LocalDateTime startDate = DateUtils.toDate(format + " " + mdcShiftSub.getStartDate(), DateUtils.STR_DATE_TIME_SMALL).toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
                LocalDateTime endDate = DateUtils.toDate(format + " " + mdcShiftSub.getEndDate(), DateUtils.STR_DATE_TIME_SMALL).toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
                if ("true".equals(mdcShiftSub.getIsDaySpan())) {
                    endDate = endDate.plusDays(1);
                }
                if (now.isAfter(startDate) && now.isBefore(endDate)) {
                    result = mdcShiftSub.getShiftSubName();
                    break;
                }
            }
        }
        if (StringUtils.isBlank(result)) {
            LocalDateTime yesterday = now.plusDays(-1);
            String yes = yesterday.format(formatter);
            String previous = DateUtils.format(DateUtils.toDate(yes, DateUtils.STRDATE), DateUtils.STR_DATE);
            List<MdcShiftSub> shiftSubs = this.baseMapper.findShiftByEquId(equipmentId, previous);
            if (shiftSubs != null && !shiftSubs.isEmpty()) {
                for (MdcShiftSub mdcShiftSub : shiftSubs) {
                    LocalDateTime startDate = DateUtils.toDate(format + " " + mdcShiftSub.getStartDate(), DateUtils.STR_DATE_TIME_SMALL).toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
                    LocalDateTime endDate = DateUtils.toDate(format + " " + mdcShiftSub.getEndDate(), DateUtils.STR_DATE_TIME_SMALL).toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
                    if ("true".equals(mdcShiftSub.getIsDaySpan())) {
                        endDate = endDate.plusDays(1);
                    }
                    if (now.isAfter(startDate) && now.isBefore(endDate)) {
                        result = mdcShiftSub.getShiftSubName();
                        break;
                    }
                }
            }
        }
        return result;
    }
 
}