zenglf
2023-09-28 f84d9e69907cb678150eaa6393fd74cf042fcca4
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
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.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 org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;
 
/**
 * @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);
            }
        }
        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) {
        List<MdcDeviceCalendarVo> result = new ArrayList<>();
        try {
            //查询默认班制
            List<MdcDeviceCalendarVo> acquiesceShift = this.baseMapper.findAcquiesceShift();
 
            for (String stringDate : stringDates) {
                List<MdcDeviceCalendarVo> mdcDeviceCalendarVos = this.baseMapper.listByEquipmentAndDate(equipmentId, stringDate);
                if (mdcDeviceCalendarVos != null && !mdcDeviceCalendarVos.isEmpty()) {
                    result.addAll(mdcDeviceCalendarVos);
                } else {
                    acquiesceShift.forEach(mdcDeviceCalendarVo -> {
                        mdcDeviceCalendarVo.setEquipmentId(equipmentId);
                        mdcDeviceCalendarVo.setEffectiveDate(stringDate);
                    });
                    // 设置默认班制
                    result.addAll(acquiesceShift);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
 
        return result;
    }
 
}