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 implements IMdcDeviceCalendarService { @Resource private IMdcEquipmentService mdcEquipmentService; @Resource private IMdcShiftSubService mdcShiftSubService; /** * 分页列表查询 */ @Override public IPage pageList(String userId, MdcDeviceCalendarQueryVo mdcDeviceCalendarQueryVo, Integer pageNo, Integer pageSize, HttpServletRequest req) { IPage pageData = new Page<>(pageNo, pageSize); List 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 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 calendarList = new ArrayList<>(); map.forEach((key, value) -> { List mdcShiftSubList = mdcShiftSubService.list(new LambdaQueryWrapper ().eq(MdcShiftSub::getShiftId, value).eq(MdcShiftSub::getShiftSubStatus, "1")); List meetDayOfWeekLocalDates = getMeetDayOfWeekLocalDates(takeEffectDate, invalidDate, key); if (!meetDayOfWeekLocalDates.isEmpty()) { List dates = meetDayOfWeekLocalDates.stream().map(localDate -> localDate.format(DateTimeFormatter.BASIC_ISO_DATE)).collect(Collectors.toList()); LambdaUpdateChainWrapper lambdaUpdate = this.lambdaUpdate(); lambdaUpdate.in(MdcDeviceCalendar::getEffectiveDate, dates).in(MdcDeviceCalendar::getEqumentId, Arrays.asList(equipmentIdList)); lambdaUpdate.remove(); List 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 getMeetDayOfWeekLocalDates(LocalDate startDate, LocalDate endDate, String workDate) { List 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 handleMdcDeviceCalendar(List dates, String[] equipment, List mdcShiftSubList, String weekNumber) { List 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; } }