郑永安
2023-06-19 7a6abd05683528032687c75e80e0bd2030a3e46c
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
package com.gkhy.safePlatform.account.service.impl;
 
import com.gkhy.safePlatform.account.entity.schedule.*;
import com.gkhy.safePlatform.account.enums.schedule.*;
import com.gkhy.safePlatform.account.model.bo.ScheduleTimeTableBO;
import com.gkhy.safePlatform.account.model.dto.resp.GroupStrategyTimeTableRespDTO;
import com.gkhy.safePlatform.account.model.dto.resp.GroupTimeTableDayRespDTO;
import com.gkhy.safePlatform.account.model.dto.resp.GroupTimeTableRespDTO;
import com.gkhy.safePlatform.account.model.dto.resp.GroupTimeTableTimeDetailRespDTO;
import com.gkhy.safePlatform.account.model.query.GroupStrategyScheduleQuery;
import com.gkhy.safePlatform.account.model.query.db.GroupScheduleDBQuery;
import com.gkhy.safePlatform.account.service.GroupStrategyTimeTableService;
import com.gkhy.safePlatform.account.service.baseService.*;
import com.gkhy.safePlatform.account.utils.schedule.GroupStrategyUtil;
import com.gkhy.safePlatform.commons.co.ContextCacheUser;
import com.gkhy.safePlatform.commons.enums.E;
import com.gkhy.safePlatform.commons.enums.ResultCodes;
import com.gkhy.safePlatform.commons.exception.AusinessException;
import com.gkhy.safePlatform.commons.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collectors;
 
@Service("groupStrategyTimeTableService")
public class GroupStrategyTimeTableServiceImpl implements GroupStrategyTimeTableService {
 
    @Autowired
    private GroupInfoService groupInfoService;
    @Autowired
    private GroupStrategyInfoService groupStrategyInfoService;
    @Autowired
    private GroupStrategyTimeTableInfoService groupStrategyTimeTableInfoService;
    @Autowired
    private GroupStrategyUserTimeTableInfoService groupStrategyUserTimeTableInfoService;
    @Autowired
    private WorkTimePeriodInfoService workTimePeriodInfoService;
    @Autowired
    private TimeStrategyInfoSerive timeStrategyInfoSerive;
    @Autowired
    private BreakTimeGroupInfoService breakTimeGroupInfoService;
    @Autowired
    private GroupMemberInfoService groupMemberInfoService;
 
 
 
    @Override
    @Transactional
    public void makeGroupStrategyTimeTable(ContextCacheUser currentUser,Long groupStrategyId) {
 
        // 1.获取班组策略
        GroupStrategyInfoDO groupStrategyInfoDO = groupStrategyInfoService.getGroupStrategyInfoById(groupStrategyId);
        if (groupStrategyInfoDO == null) {
            throw new AusinessException(E.DATA_DATABASE_NO_EXISTENT, "组策略不存在");
        }
        if (!groupStrategyInfoDO.getStatus().equals(GroupStrategyStatusEnum.ENABLED.getCode())) {
            throw new AusinessException(E.DATA_DATABASE_EXIST_BUT_NOT_VALID, "组策略不存在");
        }
        // 按照周循环
        // 2. 获取班组策略的时间策略(包含 休/班)  休(时间点按日休息) 班(包含时间段上班)
        GroupStrategyCycleTypeEnum cycle = GroupStrategyCycleTypeEnum.parse(groupStrategyInfoDO.getCycle());
        if (cycle == null) {
            throw new AusinessException(E.DATA_STATUS_NOT_EXIST, "循环类型不存在");
        }
        // 3.获取班组情况 根据时间策略获取所有作用班组班组
 
        List<GroupInfoDO> groupInfoDOs = groupInfoService.listGroupInfoDOsByGroupStrategyId(groupStrategyInfoDO.getId());
        Long timeStrategyId = groupStrategyInfoDO.getTimeStrategyId();
        TimeStrategyInfo timeStrategyInfo = timeStrategyInfoSerive.getById(timeStrategyId);
        if (timeStrategyInfo == null) {
            throw new AusinessException(E.DATA_DATABASE_NO_EXISTENT, "组策略的时间策略不存在");
        }
        List<WorkTimePeriodInfoDO> workTimePeriods = workTimePeriodInfoService.getWorkTimePeriodsByWorkTimeGroupId(timeStrategyInfo.getWorkTimeGroupId());
        if (workTimePeriods.size() < 1) {
            throw new AusinessException(E.DATA_DATABASE_NO_EXISTENT, "组策略工作时间段不存在");
        }
        Long breakTimeGroupId = timeStrategyInfo.getBreakTimeGroupId();
        List<BreakTimeRuleInfoDO> breakTimeRules = breakTimeGroupInfoService.getBreakTimeRuleDOsByBeakTimeGroupId(breakTimeGroupId);
        if (breakTimeRules.size() < 1) {
            throw new AusinessException(E.DATA_DATABASE_NO_EXISTENT, "组策略休班不存在");
        }
        // 获取所有班组人员
        Map<Long,List<Long>> groupUserPool = new HashMap<>();
        List<Long> userIds;
        for (GroupInfoDO groupInfo : groupInfoDOs) {
            userIds = groupMemberInfoService.getGroupMembersIdsByGroupId(groupInfo.getId());
            groupUserPool.put(groupInfo.getId(), userIds);
        }
 
 
        GroupStrategyUtil groupStrategyUtil = new GroupStrategyUtil(
                groupStrategyInfoDO,
                LocalDate.now(),
                cycle,
                groupInfoDOs,
                workTimePeriods,
                breakTimeRules,
                groupUserPool,
                GroupStrategyMemberCycleMethodEnum.RESERVE);
        groupStrategyUtil.run();
        // 3.删除 新的开始时间后的旧数据
        groupStrategyTimeTableInfoService.deleteByGroupStrategyId(groupStrategyInfoDO.getId(), groupStrategyUtil.getFirstDayOfStrategyType());
        groupStrategyUserTimeTableInfoService.deleteByGroupStrategyId(groupStrategyInfoDO.getId(), groupStrategyUtil.getFirstDayOfStrategyType());
        // 4.保存时间表
        ScheduleTimeTableBO scheduleTimeTable = groupStrategyUtil.getScheduleTimeTable();
        if (scheduleTimeTable.getTimeTableInfo().size() > 0) {
            groupStrategyTimeTableInfoService.saveTimeTableInfos(scheduleTimeTable.getTimeTableInfo());
        }
        // 5.保存用户时间表
        if (scheduleTimeTable.getUserTimeTableInfos().size() > 0) {
            groupStrategyUserTimeTableInfoService.saveUserTimeTableInfos(scheduleTimeTable.getUserTimeTableInfos());
        }
 
    }
 
 
 
    @Override
    public GroupStrategyTimeTableRespDTO getGroupStrategySchedule(ContextCacheUser currentUser, GroupStrategyScheduleQuery groupStrategyScheduleQuery) {
        ;
        if (groupStrategyScheduleQuery.getStartTime() == null || groupStrategyScheduleQuery.getEndTime() == null) {
            throw new AusinessException(E.DATA_PARAM_NULL,"时间不能为空");
        }
        if (groupStrategyScheduleQuery.getStartTime().after(groupStrategyScheduleQuery.getEndTime())) {
            throw new AusinessException(E.DATA_PARAM_CHECK_INVALID,"时间区间非法");
        }
        if (groupStrategyScheduleQuery.getGroupStrategyId() == null) {
            throw new BusinessException(ResultCodes.CLIENT_PARAM_NULL);
        }
        if (groupStrategyScheduleQuery.getType() != null) {
            TimeTypeEnum timeTypeEnum = TimeTypeEnum.parse(groupStrategyScheduleQuery.getType());
            if (timeTypeEnum == null) {
                throw new AusinessException(E.DATA_STATUS_NOT_EXIST, "状态越界");
            }
        }
        // 策略判断
        GroupStrategyInfoDO groupStrategyInfo = groupStrategyInfoService
                .getGroupStrategyInfoById(groupStrategyScheduleQuery.getGroupStrategyId());
        if (groupStrategyInfo == null) {
            throw new AusinessException(E.DATA_DATABASE_NO_EXISTENT, "组策略不存在");
        }
        if (!groupStrategyInfo.getStatus().equals(GroupStrategyStatusEnum.ENABLED.getCode())) {
            throw new AusinessException(E.DATA_DATABASE_EXIST_BUT_NOT_VALID, "组策略不存在");
        }
        List<GroupInfoDO> groupInfoDOS = groupInfoService.listGroupInfoDOsByGroupStrategyId(groupStrategyInfo.getId());
        // 返回结果
        GroupStrategyTimeTableRespDTO result = new GroupStrategyTimeTableRespDTO();
        result.setTimeStrategyId(groupStrategyInfo.getId());
        result.setGroupStrategyName(groupStrategyInfo.getName());
        result.setGroupStrategyInfo(groupStrategyInfo.getInfo());
        result.setGroupSchedules(new ArrayList<>(0));
        // 查询对象
        GroupScheduleDBQuery dbQuery;
        GroupTimeTableRespDTO groupTimeTable;
        if (groupInfoDOS.size() > 0) {
            dbQuery = new GroupScheduleDBQuery();
            dbQuery.setStartTime(groupStrategyScheduleQuery.getStartTime());
            dbQuery.setEndTime(groupStrategyScheduleQuery.getEndTime());
            dbQuery.setType(groupStrategyScheduleQuery.getType());
            Map<LocalDate, GroupTimeTableDayRespDTO> dayPool;
            GroupTimeTableDayRespDTO day;
            TimeTypeEnum typeEnum;
            for (GroupInfoDO groupInfo : groupInfoDOS) {
                groupTimeTable = new GroupTimeTableRespDTO();
                groupTimeTable.setGroupId(groupInfo.getId());
                groupTimeTable.setGroupInfo(groupInfo.getInfo());
                groupTimeTable.setGroupName(groupInfo.getName());
                dbQuery.setGroupId(groupInfo.getId());
                List<GroupStrategyTimeTableInfoDO> groupTableTableInfos = groupStrategyTimeTableInfoService.getGroupTableTableInfo(dbQuery);
                if (groupTableTableInfos.size() > 0) {
                    dayPool = new HashMap<>();
                    GroupStrategyTimeTableInfoDO timeTableInfoDO;
                    GroupTimeTableTimeDetailRespDTO timeDetail;
                    for (int i = 0; i < groupTableTableInfos.size(); i++) {
                        timeTableInfoDO = groupTableTableInfos.get(i);
                        timeDetail = new GroupTimeTableTimeDetailRespDTO();
                        timeDetail.setStartTime(timeTableInfoDO.getStartTime());
                        timeDetail.setEndTime(timeTableInfoDO.getEndTime());
                        timeDetail.setType(timeTableInfoDO.getType());
                        typeEnum = TimeTypeEnum.parse(timeTableInfoDO.getType());
                        if (typeEnum != null) {
                            timeDetail.setTypeDesc(typeEnum.getValue());
                        }
 
                        if (!dayPool.containsKey(timeTableInfoDO.getSpecificDate())) {
                            day = new GroupTimeTableDayRespDTO();
                            day.setDay(timeTableInfoDO.getSpecificDate());
                            day.setTimeDetails(Collections.singletonList(timeDetail));
                            dayPool.put(day.getDay(), day);
                        }else{
                            day = dayPool.get(timeTableInfoDO.getSpecificDate());
                            day.getTimeDetails().add(timeDetail);
                        }
                    }
                    // Map无需变有序
                    List<GroupTimeTableDayRespDTO> sortedDays = dayPool.values().stream()
                            .sorted(Comparator.comparing(GroupTimeTableDayRespDTO::getDay)).collect(Collectors.toList());
                    groupTimeTable.setDays(sortedDays);
                    result.getGroupSchedules().add(groupTimeTable);
                }
 
            }
 
 
        }
        return result;
    }
 
    @Override
    public void userScheduleTransfer(Long groupId, List<Long> uids, LocalDate localDate) {
        ;
        if (groupId == null) {
            throw new BusinessException(ResultCodes.SERVER_PARAM_NULL);
        }
        if (uids == null || uids.size() == 0) {
            throw new BusinessException(ResultCodes.SERVER_PARAM_NULL);
        }
        // 班组排班信息
        List<GroupStrategyTimeTableInfoDO> groupTimeTableInfos = groupStrategyTimeTableInfoService
                .getGroupTimeTableInfosByGroupId(groupId, localDate);
        // 班组排班信息转移到员工
        List<GroupStrategyUserTimeTableInfo> groupStrategyUserTimeTableInfos =  new ArrayList<>();
        GroupStrategyUserTimeTableInfo groupStrategyUserTimeTableInfo;
        GroupStrategyTimeTableInfoDO groupTimeTableInfo;
        if (groupTimeTableInfos.size() > 0) {
            for (int i = 0; i < groupTimeTableInfos.size(); i++) {
                groupTimeTableInfo = groupTimeTableInfos.get(i);
                groupStrategyUserTimeTableInfo = new GroupStrategyUserTimeTableInfo();
                groupStrategyUserTimeTableInfo.setGroupStrategyId(groupTimeTableInfo.getGroupStrategyId());
                groupStrategyUserTimeTableInfo.setStartTime(groupTimeTableInfo.getStartTime());
                groupStrategyUserTimeTableInfo.setEndTime(groupTimeTableInfo.getEndTime());
                groupStrategyUserTimeTableInfo.setSpecificDate(groupTimeTableInfo.getSpecificDate());
                groupStrategyUserTimeTableInfo.setType(groupTimeTableInfo.getType());
                groupStrategyUserTimeTableInfo.setGroupId(groupTimeTableInfo.getGroupId());
                for (int j = 0; j < uids.size(); j++) {
                    groupStrategyUserTimeTableInfo.setUid(uids.get(j));
                    groupStrategyUserTimeTableInfos.add(groupStrategyUserTimeTableInfo);
                }
            }
            if (groupStrategyUserTimeTableInfos.size() > 0) {
                groupStrategyUserTimeTableInfoService.saveUserTimeTableInfos(groupStrategyUserTimeTableInfos);
            }
        }
 
 
    }
 
    @Override
    public void cancelUserSchedule(List<Long> toDelMemberIds, LocalDate localDate) {
 
    }
}