lius
2023-06-08 534aec7a687ceca8120ba798ad20d80d7058ffe6
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.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
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.MdcDeviceCalendar;
import org.jeecg.modules.mdc.entity.MdcShiftSub;
import org.jeecg.modules.mdc.mapper.MdcDeviceCalendarMapper;
import org.jeecg.modules.mdc.service.IMdcDeviceCalendarService;
import org.jeecg.modules.mdc.service.IMdcEquipmentService;
import org.jeecg.modules.mdc.service.IMdcShiftSubService;
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 javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
 
/**
 * @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;
 
    /**
     * 分页列表查询
     */
    @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);
            }
        }
        mdcDeviceCalendarQueryVo.setEquipmentIdList(equipmentIds);
 
        return this.baseMapper.pageList(pageData, mdcDeviceCalendarQueryVo);
    }
 
    @Override
    public boolean saveCalendar(EquipmentCalendarVo calendarVo) {
        String[] equipmentIdList = calendarVo.getEquipmentId().split(",");
        //生效时间
        LocalDate takeEffectDate = calendarVo.getTakeEffectDate();
        //失效时间
        LocalDate invalidDate = calendarVo.getInvalidDate();
        //处理星期
        Map<String, String> map = new HashMap<>();
        if (org.apache.commons.lang.StringUtils.isNotBlank(calendarVo.getMonShiftId())) {
            map.put("1", calendarVo.getMonShiftId());
        }
        if (org.apache.commons.lang.StringUtils.isNotBlank(calendarVo.getTueShiftId())) {
            map.put("2", calendarVo.getTueShiftId());
        }
        if (org.apache.commons.lang.StringUtils.isNotBlank(calendarVo.getWedShiftId())) {
            map.put("3", calendarVo.getWedShiftId());
        }
        if (org.apache.commons.lang.StringUtils.isNotBlank(calendarVo.getThuShiftId())) {
            map.put("4", calendarVo.getThuShiftId());
        }
        if (org.apache.commons.lang.StringUtils.isNotBlank(calendarVo.getFriShiftId())) {
            map.put("5", calendarVo.getFriShiftId());
        }
        if (org.apache.commons.lang.StringUtils.isNotBlank(calendarVo.getSatShiftId())) {
            map.put("6", calendarVo.getSatShiftId());
        }
        if (org.apache.commons.lang.StringUtils.isNotBlank(calendarVo.getSunShiftId())) {
            map.put("7", calendarVo.getSunShiftId());
        }
        if (map.isEmpty()) {
            return false;
        }
        List<MdcDeviceCalendar> calendarList = new ArrayList<>();
        map.forEach((key, value) -> {
            List<MdcShiftSub> mdcShiftSubList = mdcShiftSubService.list(new LambdaQueryWrapper
                    <MdcShiftSub>().eq(MdcShiftSub::getShiftId, value).eq(MdcShiftSub::getShiftSubStatus, "1"));
            List<LocalDate> meetDayOfWeekLocalDates = getMeetDayOfWeekLocalDates(takeEffectDate, invalidDate, key);
            if (!meetDayOfWeekLocalDates.isEmpty()) {
                List<String> dates = meetDayOfWeekLocalDates.stream().map(localDate -> localDate.format(DateTimeFormatter.BASIC_ISO_DATE)).collect(Collectors.toList());
                LambdaUpdateChainWrapper<MdcDeviceCalendar> lambdaUpdate = this.lambdaUpdate();
                lambdaUpdate.in(MdcDeviceCalendar::getEffectiveDate, dates).in(MdcDeviceCalendar::getEqumentId, Arrays.asList(equipmentIdList));
                lambdaUpdate.remove();
                List<MdcDeviceCalendar> calendars = handleMdcDeviceCalendar(dates, equipmentIdList, mdcShiftSubList, key);
                calendarList.addAll(calendars);
            }
        });
        this.saveBatch(calendarList);
        return true;
    }
 
    /**
     * 获取一段时间范围内符合星期几的日期集合
     *
     * @param startDate 开始时间
     * @param endDate   结束时间
     * @param workDate  周几 1,2,3,4
     */
    public static List<LocalDate> getMeetDayOfWeekLocalDates(LocalDate startDate, LocalDate endDate, String workDate) {
 
        List<DayOfWeek> dayOfWeeks = Stream.of(workDate.split(","))
                .map(Integer::valueOf)
                .map(DayOfWeek::of)
                .collect(Collectors.toList());
 
        long distance = ChronoUnit.DAYS.between(startDate, endDate);
 
        return Stream.iterate(startDate, d -> d.plusDays(1))
                .limit(distance + 1)
                .filter(localDate -> dayOfWeeks.contains(localDate.getDayOfWeek()))
                .collect(Collectors.toList());
    }
 
    public List<MdcDeviceCalendar> handleMdcDeviceCalendar(List<String> dates, String[] equipment, List<MdcShiftSub> mdcShiftSubList, String weekNumber) {
        List<MdcDeviceCalendar> ds = new ArrayList<>();
        for (String date : dates) {
            for (String equipmentId : equipment) {
                for (MdcShiftSub temp : mdcShiftSubList) {
                    MdcDeviceCalendar mdcDeviceCalendar = new MdcDeviceCalendar();
                    if (org.apache.commons.lang.StringUtils.isNotEmpty(equipmentId)) {
                        mdcDeviceCalendar.setEqumentId(equipmentId);
                    }
                    mdcDeviceCalendar.setShiftId(temp.getShiftId());
                    mdcDeviceCalendar.setShiftSubId(temp.getId());
                    mdcDeviceCalendar.setEffectiveDate(date);
                    mdcDeviceCalendar.setWeekNumber(weekNumber);
                    ds.add(mdcDeviceCalendar);
                }
            }
        }
        return ds;
    }
}