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 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 workTimePeriods = workTimePeriodInfoService.getWorkTimePeriodsByWorkTimeGroupId(timeStrategyInfo.getWorkTimeGroupId()); if (workTimePeriods.size() < 1) { throw new AusinessException(E.DATA_DATABASE_NO_EXISTENT, "组策略工作时间段不存在"); } Long breakTimeGroupId = timeStrategyInfo.getBreakTimeGroupId(); List breakTimeRules = breakTimeGroupInfoService.getBreakTimeRuleDOsByBeakTimeGroupId(breakTimeGroupId); if (breakTimeRules.size() < 1) { throw new AusinessException(E.DATA_DATABASE_NO_EXISTENT, "组策略休班不存在"); } // 获取所有班组人员 Map> groupUserPool = new HashMap<>(); List 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 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 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 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 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 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 groupTimeTableInfos = groupStrategyTimeTableInfoService .getGroupTimeTableInfosByGroupId(groupId, localDate); // 班组排班信息转移到员工 List 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 toDelMemberIds, LocalDate localDate) { } }