package com.qianwen.smartman.modules.smis.service.impl; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; import java.time.ZoneOffset; import java.time.format.DateTimeFormatter; import java.time.temporal.ChronoUnit; import java.time.temporal.TemporalAdjusters; import java.time.temporal.TemporalUnit; import java.util.ArrayList; import java.util.Calendar; import java.util.Collections; import java.util.Comparator; import java.util.Date; import java.util.HashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Optional; import java.util.Set; import java.util.stream.Collectors; import java.util.stream.Stream; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import org.apache.commons.lang3.StringUtils; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.google.common.collect.Lists; import com.qianwen.core.log.exception.ServiceException; import com.qianwen.core.redis.cache.BladeRedis; import com.qianwen.core.secure.utils.AuthUtil; import com.qianwen.core.tool.utils.CollectionUtil; import com.qianwen.core.tool.utils.DateUtil; import com.qianwen.core.tool.utils.Func; import com.qianwen.smartman.common.cache.ParamCache; import com.qianwen.smartman.common.cache.cps.TimeSliceCache; import com.qianwen.smartman.common.constant.CalendarConstant; import com.qianwen.smartman.common.constant.CommonConstant; import com.qianwen.smartman.common.constant.DateConstant; import com.qianwen.smartman.common.constant.ProductionTimeConstant; import com.qianwen.smartman.common.utils.CommonUtil; import com.qianwen.smartman.common.utils.LocalDateTimeUtils; import com.qianwen.smartman.common.utils.MessageUtils; import com.qianwen.smartman.modules.smis.convert.ProductionCalendarConvert; import com.qianwen.smartman.modules.smis.dto.CacheBuildDTO; import com.qianwen.smartman.modules.smis.dto.CalendarCacheDTO; import com.qianwen.smartman.modules.smis.dto.CalendarDateDTO; import com.qianwen.smartman.modules.smis.dto.CalendarShiftDTO; import com.qianwen.smartman.modules.smis.dto.CalendarShiftDetailDTO; import com.qianwen.smartman.modules.smis.dto.CalendarShiftTimeSlicesDTO; import com.qianwen.smartman.modules.smis.dto.CurShiftDTO; import com.qianwen.smartman.modules.smis.dto.FactoryDayInfoDTO; import com.qianwen.smartman.modules.smis.dto.ShiftCacheDTO; import com.qianwen.smartman.modules.smis.dto.ShiftDetailInfoDTO; import com.qianwen.smartman.modules.smis.dto.ShiftIndexInfoDTO; import com.qianwen.smartman.modules.smis.dto.ShiftInfoDTO; import com.qianwen.smartman.modules.smis.dto.ShiftSlicesCalendarCodeDTO; import com.qianwen.smartman.modules.smis.dto.ShiftSlicesClientCalendarCodesDTO; import com.qianwen.smartman.modules.smis.dto.ShiftSlicesClientDTO; import com.qianwen.smartman.modules.smis.dto.ShiftSlicesDTO; import com.qianwen.smartman.modules.smis.dto.TimestampToProductionTimeCacheDto; import com.qianwen.smartman.modules.smis.entity.ProductionCalendar; import com.qianwen.smartman.modules.smis.entity.ProductionCalendarDay; import com.qianwen.smartman.modules.smis.entity.ProductionCalendarDaytime; import com.qianwen.smartman.modules.smis.entity.Workstation; import com.qianwen.smartman.modules.smis.enums.WorkstationTypeEnum; import com.qianwen.smartman.modules.smis.mapper.CalendarMapper; import com.qianwen.smartman.modules.smis.service.ICalendarDayService; import com.qianwen.smartman.modules.smis.service.ICalendarDaytimeService; import com.qianwen.smartman.modules.smis.service.ICalendarService; import com.qianwen.smartman.modules.smis.service.IShiftModelService; import com.qianwen.smartman.modules.smis.service.IWorkstationService; import com.qianwen.smartman.modules.smis.vo.CalendarAssociateWorkstationVO; import com.qianwen.smartman.modules.smis.vo.CalendarCopyVO; import com.qianwen.smartman.modules.smis.vo.CalendarDayVO; import com.qianwen.smartman.modules.smis.vo.CalendarSaveVO; import com.qianwen.smartman.modules.smis.vo.CalendarSimpleVO; import com.qianwen.smartman.modules.smis.vo.CalendarUpdateVO; import com.qianwen.smartman.modules.smis.vo.CalendarVO; import com.qianwen.smartman.modules.smis.vo.ShiftDetailVO; import com.qianwen.smartman.modules.smis.vo.ShiftIndexNameVO; import com.qianwen.smartman.modules.smis.vo.ShiftRestTimeVO; import com.qianwen.smartman.modules.smis.vo.ShiftTimeDetailVO; import com.qianwen.smartman.modules.smis.vo.ShiftVO; import com.qianwen.smartman.modules.mdc.dto.ShiftIndexNameDTO; import cn.hutool.core.lang.Snowflake; import cn.hutool.core.util.IdUtil; import cn.hutool.core.util.ObjectUtil; @Service public class CalendarServiceImpl extends ServiceImpl implements ICalendarService { private static final Logger log = LoggerFactory.getLogger(CalendarServiceImpl.class); @Autowired @Lazy private IWorkstationService workstationService; @Autowired private IShiftModelService shiftModelService; @Autowired private ICalendarDayService calendarDayService; @Autowired private ICalendarDaytimeService calendarDaytimeService; @Autowired private BladeRedis bladeRedis; @Override @Transactional(rollbackFor = {Exception.class}) public ProductionCalendar saveCalendar(CalendarSaveVO calendarSaveVO) { checkCalendar(calendarSaveVO, AuthUtil.getTenantId()); checkDateDTOList(calendarSaveVO.getDateDTOList()); ProductionCalendar productionCalendar = ProductionCalendarConvert.INSTANCE.conver(calendarSaveVO); save(productionCalendar); saveCalendarDayTime(calendarSaveVO.getDateDTOList(), productionCalendar); return productionCalendar; } @Override public ProductionCalendar saveYearCalendar(CalendarSaveVO calendarSaveVO) { checkCalendar(calendarSaveVO, AuthUtil.getTenantId()); ProductionCalendar productionCalendar = new ProductionCalendar(); BeanUtils.copyProperties(calendarSaveVO, productionCalendar); save(productionCalendar); saveYearCalendarDayTime(calendarSaveVO.getDateDTOList(), productionCalendar); return productionCalendar; } @Transactional @Override public ProductionCalendar updateCalendar(CalendarUpdateVO calendarUpdateVO) { checkDateDTOList(calendarUpdateVO.getDateDTOList()); this.calendarDayService.deleteByCalendarId(calendarUpdateVO.getId()); this.calendarDaytimeService.deleteByCalendarId(calendarUpdateVO.getId()); ProductionCalendar calendar = getById(calendarUpdateVO.getId()); updateCalendarDayTime(calendarUpdateVO.getDateDTOList(), calendar); return calendar; } /** * 检查入参日期列表的重复数据,有重复则抛出错误信息 * @param dateDTOList */ void checkDateDTOList(List dateDTOList) { List dupList = dupulicateDate(dateDTOList); if(!dupList.isEmpty()) { String msg = StringUtils.join(dupList.stream().map(dt -> dt.format(DateTimeFormatter.ISO_DATE)).collect(Collectors.toList()), ","); throw new ServiceException("日期数据重复:"+msg); } } /*** * 获取dateDTOList里面重复的数据(根据日期),日期一天只有一条 * @param dateDTOList * @return */ List dupulicateDate(List dateDTOList) { if(ObjectUtil.isEmpty(dateDTOList)) { return Collections.emptyList(); } List dupList = new ArrayList<>(); Map> map = dateDTOList.stream().collect(Collectors.groupingBy(CalendarDateDTO::getCalendarDate)); map.forEach((date,dtoList) -> { if(dtoList.size()>1) { dupList.add(date); } }); return dupList; } private void saveCalendarDayTime(List dateDTOList, ProductionCalendar productionCalendar) { LocalDate today = LocalDate.now(); Integer thisYear = Integer.valueOf(today.getYear()); Integer year = productionCalendar.getYear(); LocalDate queryStartDay = LocalDate.of(year.intValue(), 1, 1); Long calendarId = productionCalendar.getId(); /* Set dateList = dateDTOList.stream().filter(c -> { return !Func.isNull(c.getModelId()) && Func.isNull(c.getOffDayId()); }).map((v0) -> { return v0.getCalendarDate(); }).collect(Collectors.toSet()); */ Set dateList = dateDTOList.stream().filter(c -> { return !Func.isNull(c.getModelId()) && Func.isNull(c.getOffDayId()); }).map(CalendarDateDTO::getCalendarDate).collect(Collectors.toSet()); /* Set offDay = dateDTOList.stream().filter(c2 -> { return !Func.isNull(c2.getOffDayId()); }).map((v0) -> { return v0.getCalendarDate(); }).collect(Collectors.toSet()); */ Set offDay = dateDTOList.stream().filter(c2 -> { return !Func.isNull(c2.getOffDayId()); }).map(CalendarDateDTO::getCalendarDate).collect(Collectors.toSet()); List productionCalendarDaytimeList = new ArrayList<>(); List productionCalendarDayList = new ArrayList<>(); /* List modelIds = dateDTOList.stream().map((v0) -> { return v0.getModelId(); }).distinct().collect(Collectors.toList()); */ List modelIds = dateDTOList.stream().map(CalendarDateDTO::getModelId).distinct().collect(Collectors.toList()); Map shiftDetailMap = this.shiftModelService.getShiftDetail(modelIds); Snowflake snowflake = IdUtil.createSnowflake(1L, 1L); List curProductionCalendarDayTimeList = new ArrayList<>(); LocalDate lastDayOfYear = LocalDateTimeUtils.getLastDayOfYear(queryStartDay); long difference = year.equals(thisYear) ? LocalDateTimeUtils.getDifference(today, lastDayOfYear).intValue() : LocalDateTimeUtils.getDayOfYear(year).intValue() - 1; long j = 0; while (true) { long i = j; if (i <= difference) { LocalDate needHandleDate = year.equals(thisYear) ? today.plus(difference - i, (TemporalUnit) ChronoUnit.DAYS) : queryStartDay.plus(difference - i, (TemporalUnit) ChronoUnit.DAYS); LocalDate nextNeedHandleDate = needHandleDate.plus(1L, (TemporalUnit) ChronoUnit.DAYS); if (dateList.contains(needHandleDate)) { handleCalendarDay(dateDTOList, calendarId, year, productionCalendarDayList, shiftDetailMap, snowflake, curProductionCalendarDayTimeList, needHandleDate); } else if (offDay.contains(needHandleDate)) { handleOffDayCalendarDay(dateDTOList, calendarId, year, productionCalendarDayList, shiftDetailMap, snowflake, curProductionCalendarDayTimeList, needHandleDate); } else { handleUndefinedCalendarDay(calendarId, year, curProductionCalendarDayTimeList, needHandleDate); } curProductionCalendarDayTimeList.sort(Comparator.comparing((v0) -> { return v0.getStartTime(); })); productionCalendarDaytimeList.sort(Comparator.comparing((v0) -> { return v0.getStartTime(); })); handleConflictCalendarDay(i, productionCalendar, productionCalendarDaytimeList, curProductionCalendarDayTimeList, needHandleDate, nextNeedHandleDate); if (i == difference && i + 1 == LocalDateTimeUtils.getDayOfYear(year).intValue()) { productionCalendarDaytimeList.sort(Comparator.comparing((v0) -> { return v0.getStartTime(); })); handleFirstDayOfYear(productionCalendar, productionCalendarDaytimeList, needHandleDate); } curProductionCalendarDayTimeList.clear(); j = i + 1; } else { this.calendarDayService.saveBatchDay(productionCalendarDayList); this.calendarDaytimeService.saveBatchDaytime(productionCalendarDaytimeList); return; } } } private void handleOffDayCalendarDay(List dateDTOList, Long calendarId, Integer year, List productionCalendarDayList, Map shiftDetailMap, Snowflake snowflake, List curProductionCalendarDayTimeList, LocalDate needHandleDate) { CalendarDateDTO calendarDateDTO = dateDTOList.stream().filter(dateDTO -> { return dateDTO.getCalendarDate().equals(needHandleDate); }).findFirst().orElse(new CalendarDateDTO()); ShiftVO shiftDetail = shiftDetailMap.get(calendarDateDTO.getModelId()); Integer week = LocalDateTimeUtils.getWeek(calendarDateDTO.getCalendarDate()); Integer month = LocalDateTimeUtils.getMonth(calendarDateDTO.getCalendarDate()); Integer startDate = shiftDetail.getStartTime(); Integer endDate = shiftDetail.getEndTime(); ProductionCalendarDay productionCalendarDay = ProductionCalendarDay.builder().calendarId(calendarId).calendarDate(calendarDateDTO.getCalendarDate()).modelId(calendarDateDTO.getModelId()).isHighPriority(calendarDateDTO.getIsHighPriority()).week(week).month(month).year(year).isOffDay(CalendarConstant.IS_OFF_DAY).offId(calendarDateDTO.getOffDayId()).startTime(LocalDateTimeUtils.plus(calendarDateDTO.getCalendarDate(), shiftDetail.getStartTime().intValue(), ChronoUnit.MINUTES)).endTime(LocalDateTimeUtils.plus(calendarDateDTO.getCalendarDate(), shiftDetail.getEndTime().intValue(), ChronoUnit.MINUTES)).build(); buildCalendarOffDay(productionCalendarDay, snowflake, productionCalendarDayList, shiftDetail, calendarId, curProductionCalendarDayTimeList, calendarDateDTO, week, month, year, startDate, endDate); } private void buildCalendarOffDay(ProductionCalendarDay productionCalendarDay, Snowflake snowflake, List productionCalendarDayList, ShiftVO shiftDetail, Long calendarId, List curProductionCalendarDayTimeList, CalendarDateDTO calendarDateDTO, Integer week, Integer month, Integer year, Integer startDate, Integer endDate) { productionCalendarDay.setId(Long.valueOf(snowflake.nextId())); productionCalendarDayList.add(productionCalendarDay); List collect = shiftDetail.getShiftDetailVOList().stream().sorted(Comparator.comparing((v0) -> { return v0.getShiftStartTime(); })).collect(Collectors.toList()); for (int i = 0; i < collect.size(); i++) { ShiftDetailVO shiftDetailVO = collect.get(i); Integer startTime = shiftDetailVO.getShiftStartTime(); Integer endTime = shiftDetailVO.getShiftEndTime(); createUndefinedDaytime(calendarId, curProductionCalendarDayTimeList, calendarDateDTO, week, month, year, startDate, endDate, productionCalendarDay, collect, i, startTime, endTime); if (CollectionUtil.isNotEmpty(shiftDetailVO.getShiftRestTimeVOList())) { List shiftRestTimeVOList = shiftDetailVO.getShiftRestTimeVOList().stream().sorted(Comparator.comparing((v0) -> { return v0.getRestStartTime(); })).collect(Collectors.toList()); createShiftDayTime(calendarId, curProductionCalendarDayTimeList, calendarDateDTO, week, month, year, productionCalendarDay, shiftDetailVO, startTime, endTime, shiftRestTimeVOList); } else { ProductionCalendarDaytime productionCalendarDaytime = ProductionCalendarDaytime.builder().calendarDate(calendarDateDTO.getCalendarDate()).year(year).isUndefined(ProductionTimeConstant.DEFINED).isHighPriority(calendarDateDTO.getIsHighPriority()).week(week).month(month).shiftIndex(shiftDetailVO.getShiftIndex()).calendarDayId(productionCalendarDay.getId()).calendarId(calendarId).startTime(LocalDateTimeUtils.plus(calendarDateDTO.getCalendarDate(), startTime.intValue(), ChronoUnit.MINUTES)).endTime(LocalDateTimeUtils.plus(calendarDateDTO.getCalendarDate(), endTime.intValue(), ChronoUnit.MINUTES)).shiftType(ProductionTimeConstant.WORK_TIME).build(); curProductionCalendarDayTimeList.add(productionCalendarDaytime); } } curProductionCalendarDayTimeList.removeIf(productionCalendarDaytime2 -> { return productionCalendarDaytime2.getStartTime().equals(productionCalendarDaytime2.getEndTime()); }); } private void updateCalendarDayTime(List dateDTOList, ProductionCalendar calenadar) { final LocalDate localDateNow = LocalDate.now(); Long calendarId = calenadar.getId(); Integer year = calenadar.getYear(); Integer currentYear = localDateNow.getYear(); //提取出班制id不是null且不是休息日的 日期 Set dateList = dateDTOList.stream().filter(c -> (!Func.isNull(c.getModelId()) && Func.isNull(c.getOffDayId()))).map(CalendarDateDTO::getCalendarDate).collect(Collectors.toSet()); //提取所有为休息日的日期 /**/ Set offDay = dateDTOList.stream().filter(c -> !Func.isNull(c.getOffDayId())).map(CalendarDateDTO::getCalendarDate).collect(Collectors.toSet()); //初始化最终要保存的day和daytime列表,等待后面代码填充 List productionCalendarDaytimeList = new ArrayList<>(); List productionCalendarDayList = new ArrayList<>(); //提取所有班制id并去重 List modelIds = dateDTOList.stream().map(CalendarDateDTO::getModelId).distinct().collect(Collectors.toList()); Map shiftDetailMap = this.shiftModelService.getShiftDetail(modelIds); Snowflake snowflake = IdUtil.createSnowflake(1L, 1L); //查询今天的daytime数据列表 List todayDateTimeList = this.calendarDaytimeService.list(new QueryWrapper().lambda() .eq(ProductionCalendarDaytime::getCalendarDate, localDateNow) .eq(ProductionCalendarDaytime::getCalendarId, calendarId) .orderByAsc(ProductionCalendarDaytime::getStartTime)); List curProductionCalendarDayTimeList = new ArrayList<>(); LocalDate lastDayOfYear = LocalDateTimeUtils.getLastDayOfYear(localDateNow);//今年最后一天 //日历的年份与当前年份相同,则返回: 今年最后一天-今天 的天数差.否则返回:一整年的天数-1 //difference 也就是实际需要排班的总天数 long difference = year.intValue() == localDateNow.getYear() ? LocalDateTimeUtils.getDifference(localDateNow, lastDayOfYear) : LocalDateTimeUtils.getDayOfYear(year) - 1; //计算并生成day和daytime数据 for (long i = 0; i < difference; i++) {//修改i<=difference 改为i productionCalendarDaytimeList, List todayDateTimeList, List curProductionCalendarDayTimeList, long difference, long i, LocalDate needHandleDate, LocalDate nextNeedHandleDate) { if (LocalDate.now().isBefore(LocalDate.of(year.intValue(), 1, 1))) { handleConflictCalendarDay(i, calenadar, productionCalendarDaytimeList, curProductionCalendarDayTimeList, needHandleDate, nextNeedHandleDate); productionCalendarDaytimeList.sort(Comparator.comparing((v0) -> { return v0.getStartTime(); })); if (i == difference && i + 1 == LocalDateTimeUtils.getDayOfYear(year).intValue()) { handleFirstDayOfYear(calenadar, productionCalendarDaytimeList, needHandleDate); } } else { if (i <= difference - 1) { handleConflictCalendarDay(i, calenadar, productionCalendarDaytimeList, curProductionCalendarDayTimeList, needHandleDate, nextNeedHandleDate); } if (i == difference - 1) { productionCalendarDaytimeList.sort(Comparator.comparing((v0) -> { return v0.getStartTime(); })); handleTomorrow(year, productionCalendarDaytimeList, todayDateTimeList, needHandleDate); } } curProductionCalendarDayTimeList.clear(); } private void buildCurrentDayTime(List dateDTOList, Long calendarId, Integer year, Set dateList, Set offDay, List productionCalendarDaytimeList, List productionCalendarDayList, Map shiftDetailMap, Snowflake snowflake, List curProductionCalendarDayTimeList, LocalDate needHandleDate) { if (dateList.contains(needHandleDate)) { handleCalendarDay(dateDTOList, calendarId, year, productionCalendarDayList, shiftDetailMap, snowflake, curProductionCalendarDayTimeList, needHandleDate); } else if (offDay.contains(needHandleDate)) { handleOffDayCalendarDay(dateDTOList, calendarId, year, productionCalendarDayList, shiftDetailMap, snowflake, curProductionCalendarDayTimeList, needHandleDate); } else { handleUndefinedCalendarDay(calendarId, year, curProductionCalendarDayTimeList, needHandleDate); } curProductionCalendarDayTimeList.sort(Comparator.comparing((v0) -> { return v0.getStartTime(); })); productionCalendarDaytimeList.sort(Comparator.comparing((v0) -> { return v0.getStartTime(); })); } private void handleTomorrow(Integer year, List productionCalendarDaytimeList, List todayDateTimeList, LocalDate needHandleDate) { ProductionCalendarDaytime tomorrow = productionCalendarDaytimeList.get(0); if (Func.isNotEmpty(todayDateTimeList)) { ProductionCalendarDaytime todayDateTime = todayDateTimeList.get(todayDateTimeList.size() - 1); if (tomorrow.getStartTime().isBefore(todayDateTime.getEndTime()) && tomorrow.getIsUndefined().equals(ProductionTimeConstant.DEFINED)) { throw new ServiceException(MessageUtils.message("calendar.time.slice.of.the.day.affected", new Object[0])); } if (tomorrow.getStartTime().isBefore(todayDateTime.getEndTime()) && tomorrow.getIsUndefined().equals(ProductionTimeConstant.UNDEFINED)) { productionCalendarDaytimeList.removeIf(productionCalendarDaytime -> { return productionCalendarDaytime.getStartTime().isBefore(todayDateTime.getEndTime()) && (productionCalendarDaytime.getEndTime().isBefore(todayDateTime.getEndTime()) || productionCalendarDaytime.getEndTime().equals(todayDateTime.getEndTime())); }); productionCalendarDaytimeList.get(0).setStartTime(todayDateTime.getEndTime()); } else if (tomorrow.getStartTime().isAfter(todayDateTime.getEndTime())) { ProductionCalendarDaytime productionCalendarDaytime2 = ProductionCalendarDaytime.builder().calendarDate(needHandleDate).year(year).week(tomorrow.getWeek()).month(tomorrow.getMonth()).shiftIndex(ProductionTimeConstant.OUT_OF_SHIFT_TIME).calendarId(tomorrow.getCalendarId()).startTime(todayDateTime.getEndTime()).endTime(tomorrow.getStartTime()).shiftType(ProductionTimeConstant.UNDEFINED_TIME_OUT).isHighPriority(ProductionTimeConstant.LOW_PRIORITY).isUndefined(tomorrow.getIsUndefined()).build(); productionCalendarDaytimeList.add(productionCalendarDaytime2); } } } /* JADX WARN: Multi-variable type inference failed */ /** * 处理年的第一天? * @param productionCalendar * @param productionCalendarDaytimeList * @param needHandleDate */ private void handleFirstDayOfYear(ProductionCalendar productionCalendar, List productionCalendarDaytimeList, LocalDate needHandleDate) { Integer year = productionCalendar.getYear(); List lastYearDateTimeList = new ArrayList<>(); //查询上一年(year.intValue() - 1) 日历编号相同的日历 ProductionCalendar item = this.baseMapper.selectOne((new QueryWrapper()).lambda() .eq(ProductionCalendar::getStatus, ProductionTimeConstant.ENABLE) .eq(ProductionCalendar::getCode, productionCalendar.getCode()) .eq(ProductionCalendar::getYear, Integer.valueOf(year.intValue() - 1))); /* ProductionCalendar item = (ProductionCalendar) this.baseMapper.selectOne((Wrapper) ((LambdaQueryWrapper) ((LambdaQueryWrapper) new QueryWrapper().lambda().eq((v0) -> { return v0.getStatus(); }, ProductionTimeConstant.ENABLE)).eq((v0) -> { return v0.getCode(); }, productionCalendar.getCode())).eq((v0) -> { return v0.getYear(); }, Integer.valueOf(year.intValue() - 1))); */ if (Func.isNotEmpty(item)) { //若找到了去年的日历,查询去年12.31日的那天数据 lastYearDateTimeList = this.calendarDaytimeService.list(new QueryWrapper().lambda() .eq(ProductionCalendarDaytime::getCalendarDate, LocalDate.of(productionCalendar.getYear().intValue() - 1, 12, 31)) .eq(ProductionCalendarDaytime::getCalendarId, item.getId()) .orderByAsc(ProductionCalendarDaytime::getStartTime)); /* lastYearDateTimeList = this.calendarDaytimeService.list((Wrapper) ((LambdaQueryWrapper) ((LambdaQueryWrapper) new QueryWrapper().lambda().eq((v0) -> { return v0.getCalendarDate(); }, LocalDate.of(productionCalendar.getYear().intValue() - 1, 12, 31))).eq((v0) -> { return v0.getCalendarId(); }, item.getId())).orderByAsc((v0) -> { return v0.getStartTime(); }));*/ } ProductionCalendarDaytime firstDateTime = productionCalendarDaytimeList.get(0); if (!Func.isNotEmpty(lastYearDateTimeList)) {//啥代码啊?isEmpty不行吗? if (Func.isEmpty(lastYearDateTimeList) && firstDateTime.getStartTime().isAfter(LocalDateTime.of(needHandleDate, LocalTime.of(0, 0, 0)))) { ProductionCalendarDaytime productionCalendarDaytime = ProductionCalendarDaytime.builder().calendarDate(needHandleDate).year(year).week(firstDateTime.getWeek()).month(firstDateTime.getMonth()).shiftIndex(ProductionTimeConstant.OUT_OF_SHIFT_TIME).calendarId(firstDateTime.getCalendarId()).startTime(LocalDateTime.of(needHandleDate, LocalTime.of(0, 0, 0))).endTime(firstDateTime.getStartTime()).shiftType(ProductionTimeConstant.UNDEFINED_TIME_OUT).isHighPriority(ProductionTimeConstant.LOW_PRIORITY).isUndefined(firstDateTime.getIsUndefined()).build(); productionCalendarDaytimeList.add(productionCalendarDaytime); return; } return; } //找到去年12.31日数据的情况,先取最后一条 ProductionCalendarDaytime lastYearDateTime = lastYearDateTimeList.get(lastYearDateTimeList.size() - 1); boolean useLastShift = isUseLastShift(firstDateTime, lastYearDateTime);//是否使用上一个日历的班制 boolean useThisShift = isUseThisShift(firstDateTime, lastYearDateTime);//是否使用当前日历的班制,里面有isHighPriority属性的使用(isCurrentHighPriority方法内) boolean conflictShift = isConflictShift(firstDateTime, lastYearDateTime); if (conflictShift) { throw new ServiceException(MessageUtils.message("calendar.time.slice.of.the.day.affected", new Object[0])); } if (useLastShift) { productionCalendarDaytimeList.removeIf(productionCalendarDaytime2 -> { return productionCalendarDaytime2.getStartTime().isBefore(lastYearDateTime.getEndTime()) && (productionCalendarDaytime2.getEndTime().isBefore(lastYearDateTime.getEndTime()) || productionCalendarDaytime2.getEndTime().equals(lastYearDateTime.getEndTime())); }); productionCalendarDaytimeList.get(0).setStartTime(lastYearDateTime.getEndTime()); } else if (useThisShift) { lastYearDateTimeList.removeIf(productionCalendarDaytime3 -> { return (productionCalendarDaytime3.getStartTime().isAfter(firstDateTime.getStartTime()) || productionCalendarDaytime3.getStartTime().equals(firstDateTime.getStartTime())) && productionCalendarDaytime3.getEndTime().isAfter(firstDateTime.getStartTime()); }); lastYearDateTimeList.get(lastYearDateTimeList.size() - 1).setEndTime(firstDateTime.getStartTime()); this.calendarDaytimeService.deleteByCalendarDate(lastYearDateTime.getCalendarDate(), lastYearDateTime.getCalendarId()); productionCalendarDaytimeList.addAll(lastYearDateTimeList); } else if (firstDateTime.getStartTime().isAfter(lastYearDateTime.getEndTime())) { ProductionCalendarDaytime productionCalendarDaytime4 = ProductionCalendarDaytime.builder().calendarDate(needHandleDate).year(year).week(firstDateTime.getWeek()).month(firstDateTime.getMonth()).shiftIndex(ProductionTimeConstant.OUT_OF_SHIFT_TIME).calendarId(firstDateTime.getCalendarId()).startTime(lastYearDateTime.getEndTime()).endTime(firstDateTime.getStartTime()).shiftType(ProductionTimeConstant.UNDEFINED_TIME_OUT).isHighPriority(ProductionTimeConstant.LOW_PRIORITY).isUndefined(firstDateTime.getIsUndefined()).build(); productionCalendarDaytimeList.add(productionCalendarDaytime4); } } /** * 日历天时间上是否冲突 * @param firstDateTime * @param lastYearDateTime * @return */ private boolean isConflictShift(ProductionCalendarDaytime firstDateTime, ProductionCalendarDaytime lastYearDateTime) { return lastYearDateTime.getCalendarDate().equals(LocalDate.now()) && firstDateTime.getStartTime().isBefore(lastYearDateTime.getEndTime()) && firstDateTime.getIsUndefined().equals(ProductionTimeConstant.DEFINED); } /** * 是否使用去年12.31的班制,逻辑是:当前日历的年份在去年12.31日之前,且去年12.31日的日历数据优先(IsHighPriority=1) * @param firstDateTime 当前日历第一天 * @param lastYearDateTime 去年日历(相同编码)12.31 * @return */ private boolean isUseLastShift(ProductionCalendarDaytime firstDateTime, ProductionCalendarDaytime lastYearDateTime) { boolean useLastShift = firstDateTime.getStartTime().isBefore(lastYearDateTime.getEndTime()) && (lastYearDateTime.getIsHighPriority().equals(ProductionTimeConstant.HIGH_PRIORITY) || (lastYearDateTime.getIsUndefined().equals(ProductionTimeConstant.DEFINED) && firstDateTime.getIsUndefined().equals(ProductionTimeConstant.UNDEFINED))); return useLastShift; } /** * 是否使用firstDateTime所在的班制 * @param firstDateTime * @param lastYearDateTime * @return */ private boolean isUseThisShift(ProductionCalendarDaytime firstDateTime, ProductionCalendarDaytime lastYearDateTime) { boolean useThisShift = firstDateTime.getStartTime().isBefore(lastYearDateTime.getEndTime()) && !lastYearDateTime.getCalendarDate().equals(LocalDate.now()) && isCurrentHighPriority(firstDateTime, lastYearDateTime); return useThisShift; } /** * 当前日期(firstDateTime)是否是高优先级 * @param firstDateTime * @param lastYearDateTime * @return */ private boolean isCurrentHighPriority(ProductionCalendarDaytime firstDateTime, ProductionCalendarDaytime lastYearDateTime) { return firstDateTime.getIsHighPriority().equals(ProductionTimeConstant.HIGH_PRIORITY) || (firstDateTime.getIsUndefined().equals(ProductionTimeConstant.DEFINED) && lastYearDateTime.getIsHighPriority().equals(ProductionTimeConstant.LOW_PRIORITY)); } private void handleConflictCalendarDay(long i, ProductionCalendar productionCalendar, List productionCalendarDaytimeList, List curProductionCalendarDayTimeList, LocalDate needHandleDate, LocalDate nextNeedHandleDate) { ProductionCalendarDaytime nextProductionDayTime; Integer year = productionCalendar.getYear(); String code = productionCalendar.getCode(); new ProductionCalendarDaytime(); List nextYearDateTimeList = new ArrayList<>(); if (i == 0) { ProductionCalendar item =this.baseMapper.selectOne(new QueryWrapper().lambda() .eq(ProductionCalendar::getStatus, ProductionTimeConstant.ENABLE) .eq(ProductionCalendar::getCode, code) .eq(ProductionCalendar::getYear, Integer.valueOf(year.intValue() + 1))); /* ProductionCalendar item = (ProductionCalendar) this.baseMapper.selectOne((Wrapper) ((LambdaQueryWrapper) ((LambdaQueryWrapper) new QueryWrapper().lambda().eq((v0) -> { return v0.getStatus(); }, ProductionTimeConstant.ENABLE)).eq((v0) -> { return v0.getCode(); }, code)).eq((v0) -> { return v0.getYear(); }, Integer.valueOf(year.intValue() + 1))); */ if (Func.isNotEmpty(item)) { nextYearDateTimeList = this.calendarDaytimeService.list(new QueryWrapper().lambda() .eq(ProductionCalendarDaytime::getCalendarDate, LocalDate.of(year.intValue() + 1, 1, 1)) .eq(ProductionCalendarDaytime::getCalendarId, item.getId()) .orderByAsc(ProductionCalendarDaytime::getStartTime)); /* nextYearDateTimeList = this.calendarDaytimeService.list((Wrapper) ((LambdaQueryWrapper) ((LambdaQueryWrapper) new QueryWrapper().lambda().eq((v0) -> { return v0.getCalendarDate(); }, LocalDate.of(year.intValue() + 1, 1, 1))).eq((v0) -> { return v0.getCalendarId(); }, item.getId())).orderByAsc((v0) -> { return v0.getStartTime(); }));*/ } if (Func.isNotEmpty(nextYearDateTimeList)) { nextProductionDayTime = nextYearDateTimeList.get(0); } else { nextProductionDayTime = ProductionCalendarDaytime.builder().isHighPriority(ProductionTimeConstant.LOW_PRIORITY).startTime(LocalDateTime.of(year.intValue() + 1, 1, 1, 0, 0, 0)).isUndefined(ProductionTimeConstant.UNDEFINED).calendarDate(LocalDate.of(year.intValue() + 1, 1, 1)).build(); } } else { nextProductionDayTime = productionCalendarDaytimeList.get(0); } ProductionCalendarDaytime curProductionDayTime = curProductionCalendarDayTimeList.stream().max(Comparator.comparing((v0) -> { return v0.getStartTime(); })).orElse(new ProductionCalendarDaytime()); LocalDateTime nextStartTime = nextProductionDayTime.getStartTime(); LocalDateTime beforeZore = LocalDateTime.of(needHandleDate, LocalTime.of(23, 59, 59)); LocalDateTime zore = needHandleDate.atStartOfDay().plus(1L, (TemporalUnit) ChronoUnit.DAYS); LocalDateTime afterZore = LocalDateTime.of(nextNeedHandleDate, LocalTime.of(0, 0, 1)); LocalDateTime curEndTime = curProductionDayTime.getEndTime(); if (curEndTime.isAfter(nextStartTime)) { handleConflietDayTime(i, productionCalendarDaytimeList, curProductionCalendarDayTimeList, nextProductionDayTime, nextYearDateTimeList, curProductionDayTime, nextStartTime, curEndTime); } else if (curEndTime.equals(nextStartTime)) { productionCalendarDaytimeList.addAll(curProductionCalendarDayTimeList); } else if (curEndTime.isBefore(nextStartTime) && curEndTime.isAfter(beforeZore)) { buildEndTimeAfterZero(productionCalendarDaytimeList, curProductionCalendarDayTimeList, nextNeedHandleDate, year, nextProductionDayTime, nextStartTime, curEndTime); } else if (curEndTime.isBefore(nextStartTime) && nextStartTime.isBefore(afterZore)) { buildEndTimeBeforeZero(productionCalendarDaytimeList, curProductionCalendarDayTimeList, needHandleDate, year, curProductionDayTime, nextStartTime, curEndTime); } else if (curEndTime.isBefore(nextStartTime) && nextStartTime.isAfter(afterZore) && curEndTime.isBefore(beforeZore)) { buildEndTimeBothSideZero(productionCalendarDaytimeList, curProductionCalendarDayTimeList, needHandleDate, nextNeedHandleDate, year, nextProductionDayTime, curProductionDayTime, nextStartTime, zore, curEndTime); } } private void handleConflietDayTime(long i, List productionCalendarDaytimeList, List curProductionCalendarDayTimeList, ProductionCalendarDaytime nextProductionDayTime, List nextYearDateTimeList, ProductionCalendarDaytime curProductionDayTime, LocalDateTime nextStartTime, LocalDateTime curEndTime) { boolean useCurShift = curProductionDayTime.getIsHighPriority().equals(ProductionTimeConstant.HIGH_PRIORITY) || (curProductionDayTime.getIsUndefined().equals(ProductionTimeConstant.DEFINED) && nextProductionDayTime.getIsUndefined().equals(ProductionTimeConstant.UNDEFINED)); if (useCurShift) { if (i == 0) { if (Func.isNotEmpty(nextYearDateTimeList)) { nextYearDateTimeList.removeIf(productionCalendarDaytime -> { return productionCalendarDaytime.getStartTime().isBefore(curEndTime) && productionCalendarDaytime.getEndTime().isBefore(curEndTime); }); nextYearDateTimeList.get(0).setStartTime(curEndTime); this.calendarDaytimeService.deleteByCalendarDate(nextProductionDayTime.getCalendarDate(), nextProductionDayTime.getCalendarId()); productionCalendarDaytimeList.addAll(nextYearDateTimeList); } } else { productionCalendarDaytimeList.removeIf(productionCalendarDaytime2 -> { return productionCalendarDaytime2.getStartTime().isBefore(curEndTime) && (productionCalendarDaytime2.getEndTime().isBefore(curEndTime) || productionCalendarDaytime2.getEndTime().equals(curEndTime)); }); productionCalendarDaytimeList.get(0).setStartTime(curEndTime); } productionCalendarDaytimeList.addAll(curProductionCalendarDayTimeList); return; } List filterList = curProductionCalendarDayTimeList.stream().filter(item -> { return item.getStartTime().isBefore(nextStartTime); }).sorted(Comparator.comparing((v0) -> { return v0.getStartTime(); })).collect(Collectors.toList()); if (Func.isNotEmpty(filterList)) { filterList.get(filterList.size() - 1).setEndTime(nextStartTime); productionCalendarDaytimeList.addAll(filterList); } } private void buildEndTimeBothSideZero(List productionCalendarDaytimeList, List curProductionCalendarDayTimeList, LocalDate needHandleDate, LocalDate nextNeedHandleDate, Integer year, ProductionCalendarDaytime nextProductionDayTime, ProductionCalendarDaytime curProductionDayTime, LocalDateTime nextStartTime, LocalDateTime zore, LocalDateTime curEndTime) { ProductionCalendarDaytime before = ProductionCalendarDaytime.builder().calendarDate(needHandleDate).isUndefined(curProductionDayTime.getIsUndefined()).isHighPriority(ProductionTimeConstant.LOW_PRIORITY).year(year).week(curProductionDayTime.getWeek()).month(curProductionDayTime.getMonth()).shiftIndex(ProductionTimeConstant.OUT_OF_SHIFT_TIME).calendarId(curProductionDayTime.getCalendarId()).startTime(curEndTime).endTime(zore).shiftType(ProductionTimeConstant.UNDEFINED_TIME_OUT).build(); ProductionCalendarDaytime next = ProductionCalendarDaytime.builder().calendarDate(nextNeedHandleDate).isUndefined(nextProductionDayTime.getIsUndefined()).isHighPriority(ProductionTimeConstant.LOW_PRIORITY).year(year).week(nextProductionDayTime.getWeek()).month(nextProductionDayTime.getMonth()).shiftIndex(ProductionTimeConstant.OUT_OF_SHIFT_TIME).calendarId(nextProductionDayTime.getCalendarId()).startTime(zore).endTime(nextStartTime).shiftType(ProductionTimeConstant.UNDEFINED_TIME_OUT).build(); curProductionCalendarDayTimeList.add(before); curProductionCalendarDayTimeList.add(next); productionCalendarDaytimeList.addAll(curProductionCalendarDayTimeList); } private void buildEndTimeBeforeZero(List productionCalendarDaytimeList, List curProductionCalendarDayTimeList, LocalDate needHandleDate, Integer year, ProductionCalendarDaytime curProductionDayTime, LocalDateTime nextStartTime, LocalDateTime curEndTime) { ProductionCalendarDaytime productionCalendarDaytime = ProductionCalendarDaytime.builder().calendarDate(needHandleDate).isUndefined(ProductionTimeConstant.UNDEFINED).isHighPriority(ProductionTimeConstant.LOW_PRIORITY).year(year).week(curProductionDayTime.getWeek()).month(curProductionDayTime.getMonth()).shiftIndex(ProductionTimeConstant.OUT_OF_SHIFT_TIME).calendarId(curProductionDayTime.getCalendarId()).startTime(curEndTime).endTime(nextStartTime).shiftType(ProductionTimeConstant.UNDEFINED_TIME_OUT).build(); curProductionCalendarDayTimeList.add(productionCalendarDaytime); productionCalendarDaytimeList.addAll(curProductionCalendarDayTimeList); } private void buildEndTimeAfterZero(List productionCalendarDaytimeList, List curProductionCalendarDayTimeList, LocalDate nextNeedHandleDate, Integer year, ProductionCalendarDaytime nextProductionDayTime, LocalDateTime nextStartTime, LocalDateTime curEndTime) { ProductionCalendarDaytime productionCalendarDaytime = ProductionCalendarDaytime.builder().calendarDate(nextNeedHandleDate).isUndefined(nextProductionDayTime.getIsUndefined()).isHighPriority(ProductionTimeConstant.LOW_PRIORITY).year(year).week(nextProductionDayTime.getWeek()).month(nextProductionDayTime.getMonth()).shiftIndex(ProductionTimeConstant.OUT_OF_SHIFT_TIME).calendarId(nextProductionDayTime.getCalendarId()).startTime(curEndTime).endTime(nextStartTime).shiftType(ProductionTimeConstant.UNDEFINED_TIME_OUT).build(); curProductionCalendarDayTimeList.add(productionCalendarDaytime); productionCalendarDaytimeList.addAll(curProductionCalendarDayTimeList); } private void handleCalendarDay(List dateDTOList, Long calendarId, Integer year, List productionCalendarDayList, Map shiftDetailMap, Snowflake snowflake, List curProductionCalendarDayTimeList, LocalDate needHandleDate) { CalendarDateDTO calendarDateDTO = dateDTOList.stream().filter(dateDTO -> { return dateDTO.getCalendarDate().equals(needHandleDate); }).findFirst().orElse(new CalendarDateDTO()); ShiftVO shiftDetail = shiftDetailMap.get(calendarDateDTO.getModelId()); Integer week = LocalDateTimeUtils.getWeek(calendarDateDTO.getCalendarDate()); Integer month = LocalDateTimeUtils.getMonth(calendarDateDTO.getCalendarDate()); Integer startDate = shiftDetail.getStartTime(); Integer endDate = shiftDetail.getEndTime(); ProductionCalendarDay productionCalendarDay = ProductionCalendarDay.builder().calendarId(calendarId).calendarDate(calendarDateDTO.getCalendarDate()).modelId(calendarDateDTO.getModelId()).isHighPriority(calendarDateDTO.getIsHighPriority()).week(week).month(month).year(year).isOffDay(CalendarConstant.NO_OFF_DAY).startTime(LocalDateTimeUtils.plus(calendarDateDTO.getCalendarDate(), shiftDetail.getStartTime().intValue(), ChronoUnit.MINUTES)).endTime(LocalDateTimeUtils.plus(calendarDateDTO.getCalendarDate(), shiftDetail.getEndTime().intValue(), ChronoUnit.MINUTES)).build(); buildCalendarOffDay(productionCalendarDay, snowflake, productionCalendarDayList, shiftDetail, calendarId, curProductionCalendarDayTimeList, calendarDateDTO, week, month, year, startDate, endDate); } private void handleUndefinedCalendarDay(Long calendarId, Integer year, List curProductionCalendarDayTimeList, LocalDate needHandleDate) { ProductionCalendarDaytime productionCalendarDaytime = ProductionCalendarDaytime.builder().calendarDate(needHandleDate).year(year).week(LocalDateTimeUtils.getWeek(needHandleDate)).month(LocalDateTimeUtils.getMonth(needHandleDate)).shiftIndex(ProductionTimeConstant.OUT_OF_SHIFT_TIME).calendarId(calendarId).startTime(LocalDateTime.of(needHandleDate, LocalTime.of(0, 0, 0))).endTime(LocalDateTimeUtils.plus(needHandleDate, 1L, ChronoUnit.DAYS)).shiftType(ProductionTimeConstant.UNDEFINED_TIME_IN).isHighPriority(ProductionTimeConstant.LOW_PRIORITY).isUndefined(ProductionTimeConstant.UNDEFINED).build(); curProductionCalendarDayTimeList.add(productionCalendarDaytime); } private void createUndefinedDaytime(Long calendarId, List curProductionCalendarDayTimeList, CalendarDateDTO calendarDateDTO, Integer week, Integer month, Integer year, Integer startDate, Integer endDate, ProductionCalendarDay productionCalendarDay, List collect, int i, Integer startTime, Integer endTime) { ProductionCalendarDaytime undefinedDaytime = ProductionCalendarDaytime.builder().calendarDate(calendarDateDTO.getCalendarDate()).year(year).week(week).month(month).shiftIndex(ProductionTimeConstant.OUT_OF_SHIFT_TIME).calendarDayId(productionCalendarDay.getId()).isUndefined(ProductionTimeConstant.DEFINED).isHighPriority(calendarDateDTO.getIsHighPriority()).calendarDayId(productionCalendarDay.getId()).calendarId(calendarId).build(); if (i == 0) { ProductionCalendarDaytime undefinedDaytime1 = new ProductionCalendarDaytime(); BeanUtils.copyProperties(undefinedDaytime, undefinedDaytime1); undefinedDaytime1.setStartTime(LocalDateTimeUtils.plus(calendarDateDTO.getCalendarDate(), startDate.intValue(), ChronoUnit.MINUTES)); undefinedDaytime1.setEndTime(LocalDateTimeUtils.plus(calendarDateDTO.getCalendarDate(), startTime.intValue(), ChronoUnit.MINUTES)); undefinedDaytime1.setShiftType(ProductionTimeConstant.UNDEFINED_TIME_IN); curProductionCalendarDayTimeList.add(undefinedDaytime1); } if (i < collect.size() - 1) { undefinedDaytime.setStartTime(LocalDateTimeUtils.plus(calendarDateDTO.getCalendarDate(), endTime.intValue(), ChronoUnit.MINUTES)); undefinedDaytime.setEndTime(LocalDateTimeUtils.plus(calendarDateDTO.getCalendarDate(), collect.get(i + 1).getShiftStartTime().intValue(), ChronoUnit.MINUTES)); undefinedDaytime.setShiftType(ProductionTimeConstant.UNDEFINED_TIME_IN); curProductionCalendarDayTimeList.add(undefinedDaytime); } else if (i == collect.size() - 1) { undefinedDaytime.setStartTime(LocalDateTimeUtils.plus(calendarDateDTO.getCalendarDate(), endTime.intValue(), ChronoUnit.MINUTES)); undefinedDaytime.setEndTime(LocalDateTimeUtils.plus(calendarDateDTO.getCalendarDate(), endDate.intValue(), ChronoUnit.MINUTES)); undefinedDaytime.setShiftType(ProductionTimeConstant.UNDEFINED_TIME_IN); curProductionCalendarDayTimeList.add(undefinedDaytime); } } private void createShiftDayTime(Long calendarId, List curProductionCalendarDayTimeList, CalendarDateDTO calendarDateDTO, Integer week, Integer month, Integer year, ProductionCalendarDay productionCalendarDay, ShiftDetailVO shiftDetailVO, Integer startTime, Integer endTime, List shiftRestTimeVOList) { Stream.iterate(0, j -> { return Integer.valueOf(j.intValue() + 1); }).limit(shiftRestTimeVOList.size()).forEach(j2 -> { ProductionCalendarDaytime productionCalendarDaytime = ProductionCalendarDaytime.builder().calendarDate(calendarDateDTO.getCalendarDate()).year(year).week(week).month(month).shiftIndex(shiftDetailVO.getShiftIndex()).calendarDayId(productionCalendarDay.getId()).isUndefined(ProductionTimeConstant.DEFINED).isHighPriority(calendarDateDTO.getIsHighPriority()).calendarId(calendarId).build(); if (j2.intValue() == 0) { productionCalendarDaytime.setStartTime(LocalDateTimeUtils.plus(calendarDateDTO.getCalendarDate(), startTime.intValue(), ChronoUnit.MINUTES)); productionCalendarDaytime.setEndTime(LocalDateTimeUtils.plus(calendarDateDTO.getCalendarDate(), ((ShiftRestTimeVO) shiftRestTimeVOList.get(0)).getRestStartTime().intValue(), ChronoUnit.MINUTES)); productionCalendarDaytime.setShiftType(ProductionTimeConstant.WORK_TIME); ProductionCalendarDaytime productionCalendarDaytime1 = new ProductionCalendarDaytime(); BeanUtils.copyProperties(productionCalendarDaytime, productionCalendarDaytime1); productionCalendarDaytime1.setStartTime(LocalDateTimeUtils.plus(calendarDateDTO.getCalendarDate(), ((ShiftRestTimeVO) shiftRestTimeVOList.get(j2.intValue())).getRestStartTime().intValue(), ChronoUnit.MINUTES)); productionCalendarDaytime1.setEndTime(LocalDateTimeUtils.plus(calendarDateDTO.getCalendarDate(), ((ShiftRestTimeVO) shiftRestTimeVOList.get(j2.intValue())).getRestEndTime().intValue(), ChronoUnit.MINUTES)); productionCalendarDaytime1.setShiftType(ProductionTimeConstant.REST_TIME); curProductionCalendarDayTimeList.add(productionCalendarDaytime); curProductionCalendarDayTimeList.add(productionCalendarDaytime1); } else if (j2.intValue() > 0 && j2.intValue() < shiftRestTimeVOList.size()) { productionCalendarDaytime.setStartTime(LocalDateTimeUtils.plus(calendarDateDTO.getCalendarDate(), ((ShiftRestTimeVO) shiftRestTimeVOList.get(j2.intValue())).getRestStartTime().intValue(), ChronoUnit.MINUTES)); productionCalendarDaytime.setEndTime(LocalDateTimeUtils.plus(calendarDateDTO.getCalendarDate(), ((ShiftRestTimeVO) shiftRestTimeVOList.get(j2.intValue())).getRestEndTime().intValue(), ChronoUnit.MINUTES)); productionCalendarDaytime.setShiftType(ProductionTimeConstant.REST_TIME); ProductionCalendarDaytime productionCalendarDaytime12 = new ProductionCalendarDaytime(); BeanUtils.copyProperties(productionCalendarDaytime, productionCalendarDaytime12); productionCalendarDaytime12.setStartTime(LocalDateTimeUtils.plus(calendarDateDTO.getCalendarDate(), ((ShiftRestTimeVO) shiftRestTimeVOList.get(j2.intValue() - 1)).getRestEndTime().intValue(), ChronoUnit.MINUTES)); productionCalendarDaytime12.setEndTime(LocalDateTimeUtils.plus(calendarDateDTO.getCalendarDate(), ((ShiftRestTimeVO) shiftRestTimeVOList.get(j2.intValue())).getRestStartTime().intValue(), ChronoUnit.MINUTES)); productionCalendarDaytime12.setShiftType(ProductionTimeConstant.WORK_TIME); curProductionCalendarDayTimeList.add(productionCalendarDaytime); curProductionCalendarDayTimeList.add(productionCalendarDaytime12); } if (j2.intValue() == shiftRestTimeVOList.size() - 1) { ProductionCalendarDaytime productionCalendarDaytime13 = new ProductionCalendarDaytime(); BeanUtils.copyProperties(productionCalendarDaytime, productionCalendarDaytime13); productionCalendarDaytime13.setStartTime(LocalDateTimeUtils.plus(calendarDateDTO.getCalendarDate(), ((ShiftRestTimeVO) shiftRestTimeVOList.get(j2.intValue())).getRestEndTime().intValue(), ChronoUnit.MINUTES)); productionCalendarDaytime13.setEndTime(LocalDateTimeUtils.plus(calendarDateDTO.getCalendarDate(), endTime.intValue(), ChronoUnit.MINUTES)); productionCalendarDaytime13.setShiftType(ProductionTimeConstant.WORK_TIME); curProductionCalendarDayTimeList.add(productionCalendarDaytime13); } }); } @Override public CalendarVO getCalendar(Long calendarId) { CalendarVO calendarDetail = this.baseMapper.getCalendarDetail(calendarId); List otherCalendarDayVOList = new ArrayList<>(); Integer year = calendarDetail.getYear(); Optional.ofNullable(getOne(new QueryWrapper().lambda() .eq(ProductionCalendar::getCode, calendarDetail.getCode()) .eq(ProductionCalendar::getYear, year+ 1))) .ifPresent(productionCalendar -> { List list = this.calendarDayService.list(new QueryWrapper().lambda().eq(ProductionCalendarDay::getCalendarId, productionCalendar.getId()).in(ProductionCalendarDay::getCalendarDate, new Object[] { LocalDate.of(year.intValue() + 1, 1, 1), LocalDate.of(year.intValue() + 1, 1, 2) })); entityToVo(otherCalendarDayVOList, list); }); /* Optional.ofNullable(getOne((Wrapper) ((LambdaQueryWrapper) new QueryWrapper().lambda().eq((v0) -> { return v0.getCode(); }, calendarDetail.getCode())).eq((v0) -> { return v0.getYear(); }, Integer.valueOf(year.intValue() + 1)))).ifPresent(productionCalendar -> { List list = this.calendarDayService.list((Wrapper) ((LambdaQueryWrapper) new QueryWrapper().lambda().eq((v0) -> { return v0.getCalendarId(); }, productionCalendar.getId())).in((v0) -> { return v0.getCalendarDate(); }, new Object[]{LocalDate.of(year.intValue() + 1, 1, 1), LocalDate.of(year.intValue() + 1, 1, 2)})); entityToVo(otherCalendarDayVOList, list); });*/ Optional.ofNullable(getOne(new QueryWrapper().lambda() .eq(ProductionCalendar::getCode, calendarDetail.getCode()) .eq(ProductionCalendar::getYear, year.intValue() - 1))) .ifPresent(productionCalendar -> { List list = this.calendarDayService.list(new QueryWrapper().lambda().eq(ProductionCalendarDay::getCalendarId, productionCalendar.getId()).in(ProductionCalendarDay::getCalendarDate, new Object[] { LocalDate.of(year.intValue() - 1, 12, 30), LocalDate.of(year.intValue() - 1, 12, 31) })); entityToVo(otherCalendarDayVOList, list); }); /* Optional.ofNullable(getOne((Wrapper) ((LambdaQueryWrapper) new QueryWrapper().lambda().eq((v0) -> { return v0.getCode(); }, calendarDetail.getCode())).eq((v0) -> { return v0.getYear(); }, Integer.valueOf(year.intValue() - 1)))).ifPresent(productionCalendar2 -> { List list = this.calendarDayService.list((Wrapper) ((LambdaQueryWrapper) new QueryWrapper().lambda().eq((v0) -> { return v0.getCalendarId(); }, productionCalendar2.getId())).in((v0) -> { return v0.getCalendarDate(); }, new Object[]{LocalDate.of(year.intValue() - 1, 12, 30), LocalDate.of(year.intValue() - 1, 12, 31)})); entityToVo(otherCalendarDayVOList, list); }); */ calendarDetail.setOtherCalendarDayVOList(otherCalendarDayVOList); return calendarDetail; } private void entityToVo(List otherCalendarDayVOList, List list) { if (Func.isNotEmpty(list)) { otherCalendarDayVOList.addAll(list.stream().map(productionCalendarDay -> { CalendarDayVO calendarDayVO = new CalendarDayVO(); BeanUtils.copyProperties(productionCalendarDay, calendarDayVO); return calendarDayVO; }).collect(Collectors.toList())); } } @Override @Transactional(rollbackFor = {Exception.class}) public ProductionCalendar copyCalendar(CalendarCopyVO calendarCopyVO) { CalendarSaveVO calendarSaveVO = new CalendarSaveVO(); BeanUtils.copyProperties(calendarCopyVO, calendarSaveVO); checkCalendar(calendarSaveVO, AuthUtil.getTenantId()); CalendarVO calendar = getCalendar(calendarCopyVO.getId()); ProductionCalendar productionCalendar = ProductionCalendar.builder().code(calendarCopyVO.getCode()).name(calendar.getName()).status(calendar.getStatus()).year(calendarCopyVO.getYear()).build(); save(productionCalendar); return productionCalendar; } public void checkCalendar(CalendarSaveVO calendarSaveVO, String tenantId) { ProductionCalendar productionCalendar = getOne(Wrappers.lambdaQuery() .eq(ProductionCalendar::getCode, calendarSaveVO.getCode()) .eq(ProductionCalendar::getYear, calendarSaveVO.getYear()) .eq(ProductionCalendar::getTenantId, tenantId)); /* ProductionCalendar productionCalendar = (ProductionCalendar) getOne((Wrapper) ((LambdaQueryWrapper) ((LambdaQueryWrapper) Wrappers.lambdaQuery().eq((v0) -> { return v0.getCode(); }, calendarSaveVO.getCode())).eq((v0) -> { return v0.getYear(); }, calendarSaveVO.getYear())).eq((v0) -> { return v0.getTenantId(); }, tenantId)); */ Optional.ofNullable(productionCalendar).ifPresent(po -> { throw new ServiceException(MessageUtils.message("calendar.code.year.has.be.repeated", new Object[0])); }); } private void saveYearCalendarDayTime(List dateDTOList, ProductionCalendar productionCalendar) { Integer year = productionCalendar.getYear(); Long calendarId = productionCalendar.getId(); Set dateList = dateDTOList.stream().filter(c -> { return !Func.isNull(c.getModelId()) && Func.isNull(c.getOffDayId()); }).map((v0) -> { return v0.getCalendarDate(); }).collect(Collectors.toSet()); Set offDay = dateDTOList.stream().filter(c2 -> { return !Func.isNull(c2.getOffDayId()); }).map((v0) -> { return v0.getCalendarDate(); }).collect(Collectors.toSet()); List productionCalendarDaytimeList = new ArrayList<>(); List productionCalendarDayList = new ArrayList<>(); List modelIds = dateDTOList.stream().map((v0) -> { return v0.getModelId(); }).distinct().collect(Collectors.toList()); Map shiftDetailMap = this.shiftModelService.getShiftDetail(modelIds); Snowflake snowflake = IdUtil.createSnowflake(1L, 1L); List curProductionCalendarDayTimeList = new ArrayList<>(); LocalDate lastDayOfYear = LocalDateTimeUtils.getLastDayOfYear(LocalDate.of(year.intValue(), 1, 1)); LocalDate firstDay = LocalDate.of(LocalDate.now().getYear(), 1, 1); Integer thisYear = Integer.valueOf(firstDay.getYear()); long difference = year.equals(thisYear) ? LocalDateTimeUtils.getDifference(firstDay, lastDayOfYear).intValue() : LocalDateTimeUtils.getDayOfYear(year).intValue() - 1; long j = 0; while (true) { long i = j; if (i <= difference) { LocalDate needHandleDate = year.equals(thisYear) ? firstDay.plus(difference - i, (TemporalUnit) ChronoUnit.DAYS) : LocalDate.of(year.intValue(), 1, 1).plus(difference - i, (TemporalUnit) ChronoUnit.DAYS); LocalDate nextNeedHandleDate = needHandleDate.plus(1L, (TemporalUnit) ChronoUnit.DAYS); if (dateList.contains(needHandleDate)) { handleCalendarDay(dateDTOList, calendarId, year, productionCalendarDayList, shiftDetailMap, snowflake, curProductionCalendarDayTimeList, needHandleDate); } else if (offDay.contains(needHandleDate)) { handleOffDayCalendarDay(dateDTOList, calendarId, year, productionCalendarDayList, shiftDetailMap, snowflake, curProductionCalendarDayTimeList, needHandleDate); } else { handleUndefinedCalendarDay(calendarId, year, curProductionCalendarDayTimeList, needHandleDate); } curProductionCalendarDayTimeList.sort(Comparator.comparing((v0) -> { return v0.getStartTime(); })); productionCalendarDaytimeList.sort(Comparator.comparing((v0) -> { return v0.getStartTime(); })); handleConflictCalendarDay(i, productionCalendar, productionCalendarDaytimeList, curProductionCalendarDayTimeList, needHandleDate, nextNeedHandleDate); if (i == difference && i + 1 == LocalDateTimeUtils.getDayOfYear(year).intValue()) { productionCalendarDaytimeList.sort(Comparator.comparing((v0) -> { return v0.getStartTime(); })); handleFirstDayOfYear(productionCalendar, productionCalendarDaytimeList, needHandleDate); } curProductionCalendarDayTimeList.clear(); j = i + 1; } else { this.calendarDayService.saveBatchDay(productionCalendarDayList); this.calendarDaytimeService.saveBatchDaytime(productionCalendarDaytimeList); return; } } } @Override public List buildProductionTimeCache(CacheBuildDTO cacheBuildDTO) { LocalDate targetDate = cacheBuildDTO.getTargetDate(); Set tenantIds = cacheBuildDTO.getTenantIds(); String code = cacheBuildDTO.getCalendarCode(); List list = list(new QueryWrapper().lambda() .in(ProductionCalendar::getTenantId, tenantIds) .eq(ProductionCalendar::getStatus, ProductionTimeConstant.ENABLE) .eq(StringUtils.isNotBlank(code), ProductionCalendar::getCode, code) .eq(ProductionCalendar::getYear, Integer.valueOf(targetDate.getYear()))); /* List list = list((Wrapper) ((LambdaQueryWrapper) ((LambdaQueryWrapper) new QueryWrapper().lambda().in((v0) -> { return v0.getTenantId(); }, tenantIds)).eq((v0) -> { return v0.getStatus(); }, ProductionTimeConstant.ENABLE)).eq(StringUtils.isNotBlank(code), (v0) -> { return v0.getCode(); }, code).eq((v0) -> { return v0.getYear(); }, Integer.valueOf(targetDate.getYear())));*/ LocalDateTime startTime = LocalDateTime.of(targetDate, LocalTime.MIN); LocalDateTime endTime = LocalDateTime.of(targetDate.plusDays(1L), LocalTime.of(0, 0)); if (Func.isNotEmpty(list)) { return list.stream() .map(productionCalendar -> { TimestampToProductionTimeCacheDto timestampToProductionTimeCacheDto = TimestampToProductionTimeCacheDto.builder().TenantId(productionCalendar.getTenantId()).CalendarCode(productionCalendar.getCode()).NaturalDay(targetDate).build(); List dayTimeList = this.calendarDaytimeService.list(new QueryWrapper().lambda() .eq(ProductionCalendarDaytime::getCalendarId, productionCalendar.getId()) .or().and(i ->{ i.between(ProductionCalendarDaytime::getStartTime, startTime, endTime).or().between(ProductionCalendarDaytime::getEndTime, startTime, endTime); //return i; }) .orderByAsc(ProductionCalendarDaytime::getStartTime)); dayTimeList = dayTimeList.stream().filter(productionCalendarDaytime -> { return (productionCalendarDaytime.getEndTime().equals(startTime) || productionCalendarDaytime.getStartTime().equals(endTime)) ? false : true; }).collect(Collectors.toList()); Map timeSlicesMap = new HashMap<>(1919); for (int i = 0; i <= ProductionTimeConstant.MINUTE_STOP.intValue(); i++) { buildTimeSlicesMap(startTime, endTime, dayTimeList, timeSlicesMap, i, targetDate); } timestampToProductionTimeCacheDto.setTimeSlicesDTOMap(timeSlicesMap); return timestampToProductionTimeCacheDto; }).collect(Collectors.toList()); /* return (List) list.stream().map(productionCalendar -> { TimestampToProductionTimeCacheDto timestampToProductionTimeCacheDto = TimestampToProductionTimeCacheDto.builder().TenantId(productionCalendar.getTenantId()).CalendarCode(productionCalendar.getCode()).NaturalDay(targetDate).build(); List dayTimeList = this.calendarDaytimeService.list((Wrapper) ((LambdaQueryWrapper) ((LambdaQueryWrapper) ((LambdaQueryWrapper) new QueryWrapper().lambda().eq((v0) -> { return v0.getCalendarId(); }, productionCalendar.getId())).or()).and(i -> { LambdaQueryWrapper lambdaQueryWrapper = (LambdaQueryWrapper) ((LambdaQueryWrapper) ((LambdaQueryWrapper) i.between((v0) -> { return v0.getStartTime(); }, startTime, endTime)).or()).between((v0) -> { return v0.getEndTime(); }, startTime, endTime); })).orderByAsc((v0) -> { return v0.getStartTime(); })); List dayTimeList2 = (List) dayTimeList.stream().filter(productionCalendarDaytime -> { return (productionCalendarDaytime.getEndTime().equals(startTime) || productionCalendarDaytime.getStartTime().equals(endTime)) ? false : true; }).collect(Collectors.toList()); Map timeSlicesMap = new HashMap<>(1919); for (int i2 = 0; i2 <= ProductionTimeConstant.MINUTE_STOP.intValue(); i2++) { buildTimeSlicesMap(startTime, endTime, dayTimeList2, timeSlicesMap, i2, targetDate); } timestampToProductionTimeCacheDto.setTimeSlicesDTOMap(timeSlicesMap); return timestampToProductionTimeCacheDto; }).collect(Collectors.toList()); */ } return Lists.newArrayList(); } private void buildTimeSlicesMap(LocalDateTime startTime, LocalDateTime endTime, List dayTimeList, Map timeSlicesDTOMap, int i, LocalDate targetDate) { LocalDateTime localDateTime = LocalDateTime.of(targetDate, LocalTime.MIN).plusMinutes(i).plusSeconds(1L); ProductionCalendarDaytime productionCalendarDaytime = dayTimeList.stream().filter(dayTime -> { return (localDateTime.isAfter(dayTime.getStartTime()) || localDateTime.equals(dayTime.getStartTime())) && (localDateTime.isBefore(dayTime.getEndTime()) || localDateTime.equals(dayTime.getEndTime())); }).findFirst().orElse(null); if (Func.isEmpty(productionCalendarDaytime)) { return; } LocalDateTime shiftStartTime = productionCalendarDaytime.getStartTime(); LocalDateTime shiftEndTime = productionCalendarDaytime.getEndTime(); if (shiftStartTime.isBefore(startTime)) { shiftStartTime = startTime; } if (shiftEndTime.isAfter(endTime)) { shiftEndTime = endTime; } CalendarShiftTimeSlicesDTO calendarShiftTimeSlicesDTO = CalendarShiftTimeSlicesDTO.builder().shiftTimeType(productionCalendarDaytime.getShiftType().toString()).endTime(LocalDateTimeUtils.LocalDateTimeToDate(shiftEndTime)).startTime(LocalDateTimeUtils.LocalDateTimeToDate(shiftStartTime)).shiftIndex(productionCalendarDaytime.getShiftIndex()).shiftTimeType(productionCalendarDaytime.getShiftType().toString()).factoryDate(LocalDateTimeUtils.formatTimeLocalDate(productionCalendarDaytime.getCalendarDate(), DateConstant.PATTERN_DATE)).factoryMonth(productionCalendarDaytime.getMonth()).factoryWeek(productionCalendarDaytime.getWeek()).factoryYear(productionCalendarDaytime.getYear()).build(); timeSlicesDTOMap.put(Integer.valueOf(i), calendarShiftTimeSlicesDTO); } @Override public List getCalendarList(String tenantId) { return list(Wrappers.query().lambda() .eq(ProductionCalendar::getTenantId, tenantId)) .stream() .map(productionCalendar -> { CalendarSimpleVO calendarSimpleVO = new CalendarSimpleVO(); BeanUtils.copyProperties(productionCalendar, calendarSimpleVO); return calendarSimpleVO; }).collect(Collectors.toList()); /* return (List) list((Wrapper) Wrappers.query().lambda().eq((v0) -> { return v0.getTenantId(); }, tenantId)).stream().map(productionCalendar -> { CalendarSimpleVO calendarSimpleVO = new CalendarSimpleVO(); BeanUtils.copyProperties(productionCalendar, calendarSimpleVO); return calendarSimpleVO; }).collect(Collectors.toList()); */ } @Override public List getDaytimeList(String tenantId, String calendarCode, Integer year) { List result = new ArrayList<>(); Optional.ofNullable(getOne(new QueryWrapper().lambda() .eq(ProductionCalendar::getCode, calendarCode) .eq(ProductionCalendar::getTenantId, Long.valueOf(tenantId)) .eq(ProductionCalendar::getYear, year))).ifPresent(calendar -> { List list = this.calendarDaytimeService.list(new QueryWrapper().lambda().eq(ProductionCalendarDaytime::getCalendarId, calendar.getId()).eq(ProductionCalendarDaytime::getCalendarDate, LocalDate.now())); if (Func.isNotEmpty(list)) { list.forEach(productionCalendarDaytime -> { CalendarShiftDetailDTO calendarShiftDetailDTO = new CalendarShiftDetailDTO(); calendarShiftDetailDTO.setCode(calendarCode); calendarShiftDetailDTO.setCalendarDay(DateUtil.format(productionCalendarDaytime.getCalendarDate(), DateConstant.PATTERN_DATE)); calendarShiftDetailDTO.setFactoryDate(DateUtil.format(productionCalendarDaytime.getCalendarDate(), DateConstant.PATTERN_DATE)); calendarShiftDetailDTO.setFactoryMonth(productionCalendarDaytime.getMonth()); calendarShiftDetailDTO.setFactoryWeek(productionCalendarDaytime.getWeek()); calendarShiftDetailDTO.setFactoryYear(productionCalendarDaytime.getYear()); calendarShiftDetailDTO.setShiftIndex(productionCalendarDaytime.getShiftIndex()); calendarShiftDetailDTO.setShiftTimeType(productionCalendarDaytime.getShiftType().toString()); calendarShiftDetailDTO.setStartTime(LocalDateTimeUtils.LocalDateTimeToDate(productionCalendarDaytime.getStartTime())); result.add(calendarShiftDetailDTO); }); } }); return result; } @Override public List buildCalendarShift(CacheBuildDTO cacheBuildDTO) { List calendarCacheDTOList = new LinkedList<>(); LocalDate targetDate = cacheBuildDTO.getTargetDate(); Set tenantIds = cacheBuildDTO.getTenantIds(); List list = list(new QueryWrapper().lambda() .in(ProductionCalendar::getTenantId, tenantIds) .eq(ProductionCalendar::getYear, targetDate.getYear())); /* List list = list((Wrapper) ((LambdaQueryWrapper) new QueryWrapper().lambda().in((v0) -> { return v0.getTenantId(); }, tenantIds)).eq((v0) -> { return v0.getYear(); }, Integer.valueOf(targetDate.getYear()))); */ if (Func.isNotEmpty(list)) { Map> collect = list.stream().collect(Collectors.groupingBy(ProductionCalendar::getTenantId)); /* Map> collect = (Map) list.stream().collect(Collectors.groupingBy((v0) -> { return v0.getTenantId(); }));*/ collect.forEach((tenantId, productionList) -> { productionList.forEach(productionCalendar -> { log.info("处理的租户:{},处理的日历code:{}", tenantId, productionCalendar.getCode()); List dayTimeList = this.calendarDaytimeService.list(new QueryWrapper().lambda() .eq(ProductionCalendarDaytime::getCalendarId, productionCalendar.getId()) .eq(ProductionCalendarDaytime::getCalendarDate, targetDate) .orderByAsc(ProductionCalendarDaytime::getStartTime)); if (Func.isNotEmpty(dayTimeList)) { CalendarCacheDTO calendarCacheDTO = CalendarCacheDTO.builder().tenantId(tenantId).calendarCode(productionCalendar.getCode()).factoryDate(DateUtil.formatDate(targetDate)).factoryMonth(dayTimeList.get(0).getMonth()).factoryWeek(dayTimeList.get(0).getWeek()).factoryYear(dayTimeList.get(0).getYear()).build(); List shiftCacheDTOList = dayTimeList.stream().map(dayTime -> { return ShiftCacheDTO.builder().shiftIndex(dayTime.getShiftIndex()).shiftTimeType(dayTime.getShiftType()).startTime(Long.valueOf(dayTime.getStartTime().toInstant(ZoneOffset.of("+8")).toEpochMilli())).endTime(Long.valueOf(dayTime.getEndTime().toInstant(ZoneOffset.of("+8")).toEpochMilli())).build(); }).collect(Collectors.toList()); calendarCacheDTO.setShiftCacheDTOS(shiftCacheDTOList); calendarCacheDTOList.add(calendarCacheDTO); } }); }); } return calendarCacheDTOList; } @Override public List getCurShift(String tenantId, List calendarCodeList) { List list = list(new QueryWrapper().lambda() .in(ProductionCalendar::getCode, calendarCodeList) .eq(ProductionCalendar::getYear, LocalDateTimeUtils.getYear(LocalDate.now())) .eq(ProductionCalendar::getTenantId, tenantId)); /* List list = list((Wrapper) ((LambdaQueryWrapper) ((LambdaQueryWrapper) new QueryWrapper().lambda().in((v0) -> { return v0.getCode(); }, calendarCodeList)).eq((v0) -> { return v0.getYear(); }, LocalDateTimeUtils.getYear(LocalDate.now()))).eq((v0) -> { return v0.getTenantId(); }, tenantId));*/ List curShiftDTOS = new ArrayList<>(); if (Func.isNotEmpty(list)) { list.forEach(productionCalendar -> { CurShiftDTO curShiftDTO = new CurShiftDTO();//ProductionCalendarDay::getCalendarDate curShiftDTO.setCalendarCode(productionCalendar.getCode()); ProductionCalendarDay one = this.calendarDayService.getOne(new QueryWrapper().lambda() .eq(ProductionCalendarDay::getCalendarId, productionCalendar.getId()) .eq(ProductionCalendarDay::getCalendarDate, LocalDate.now())); if (Func.isNull(one)) { curShiftDTO.setShiftIndex(-1); } else { ShiftVO shiftDetail = this.shiftModelService.getShiftDetail(one.getModelId()); shiftDetail.getShiftDetailVOList().forEach(shiftDetailVO -> { Integer shiftStartTime = shiftDetailVO.getShiftStartTime(); Integer shiftEndTime = shiftDetailVO.getShiftEndTime(); LocalDateTime startTime = LocalDateTimeUtils.plus(LocalDate.now(), shiftStartTime.intValue(), ChronoUnit.MINUTES); LocalDateTime endTime = LocalDateTimeUtils.plus(LocalDate.now(), shiftEndTime.intValue(), ChronoUnit.MINUTES); if (LocalDateTime.now().isBefore(endTime) && LocalDateTime.now().isAfter(startTime)) { curShiftDTO.setShiftIndex(shiftDetailVO.getShiftIndex()); curShiftDTO.setIndexName(shiftDetailVO.getIndexName()); } }); if (curShiftDTO.getShiftIndex() == null) { curShiftDTO.setShiftIndex(-1); } } curShiftDTOS.add(curShiftDTO); }); } return curShiftDTOS; } @Override public Map getFactoryDayInfo(LocalDateTime date, String tenantId, List calendarCodeList) { List factoryDayInfoDTOList = new ArrayList<>(); Integer minute = Integer.valueOf((date.getHour() * 60) + date.getMinute()); DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DateConstant.PATTERN_DATE); String str = LocalDate.now().format(formatter); calendarCodeList.forEach(calendarCode -> { String redisKey = CommonUtil.getReallyCacheName(TimeSliceCache.CALENDAR_CACHE, TimeSliceCache.CALENDARS_CODE.concat(calendarCode).concat(":").concat(TimeSliceCache.MINUTE_POINT), str); CalendarShiftTimeSlicesDTO calendarShiftTimeSlicesDTO = (CalendarShiftTimeSlicesDTO) this.bladeRedis.hGet(redisKey, minute); if (Func.isNotEmpty(calendarShiftTimeSlicesDTO)) { FactoryDayInfoDTO factoryDayInfoDTO = FactoryDayInfoDTO.builder().calendarCode(calendarCode).factoryDay(LocalDate.parse(calendarShiftTimeSlicesDTO.getFactoryDate(), formatter)).build(); factoryDayInfoDTOList.add(factoryDayInfoDTO); } }); if (Func.isEmpty(factoryDayInfoDTOList)) { return new HashMap<>(16); } Map map = list(new QueryWrapper().lambda() .eq(ProductionCalendar::getYear, Integer.valueOf(date.getYear())) .in(ProductionCalendar::getCode, calendarCodeList) .eq(ProductionCalendar::getTenantId, tenantId)) .stream() .collect(Collectors.toMap(ProductionCalendar::getCode, ProductionCalendar::getId)); /* Map map = (Map) list((Wrapper) ((LambdaQueryWrapper) ((LambdaQueryWrapper) new QueryWrapper().lambda().eq((v0) -> { return v0.getYear(); }, Integer.valueOf(date.getYear()))).in((v0) -> { return v0.getCode(); }, calendarCodeList)).eq((v0) -> { return v0.getTenantId(); }, tenantId)).stream().collect(Collectors.toMap((v0) -> { return v0.getCode(); }, (v0) -> { return v0.getId(); }));*/ //ProductionCalendarDay::getCalendarId return factoryDayInfoDTOList.stream().peek(fac -> { Long id = map.get(fac.getCalendarCode()); ProductionCalendarDay productionCalendarDay = this.calendarDayService.getOne(new QueryWrapper().lambda() .eq(ProductionCalendarDay::getCalendarDate, fac.getFactoryDay()) .eq(ProductionCalendarDay::getCalendarId, id)); if (Func.isNotEmpty(productionCalendarDay)) { fac.setStartTime(productionCalendarDay.getStartTime()); fac.setEndTime(productionCalendarDay.getEndTime()); } }).collect(Collectors.toMap(FactoryDayInfoDTO::getCalendarCode, v -> v)); } @Override public ShiftSlicesDTO getShiftSlices(ShiftSlicesClientDTO shiftSlicesClientDTO) { String tenantId = shiftSlicesClientDTO.getTenantId(); String calendarCode = shiftSlicesClientDTO.getCalendarCode(); Integer shiftIndex = shiftSlicesClientDTO.getShiftIndex(); LocalDate localDate = shiftSlicesClientDTO.getLocalDate(); ShiftSlicesDTO shiftSlicesDTO = new ShiftSlicesDTO(); Optional.ofNullable(this.shiftModelService.getShiftTime(tenantId, shiftIndex, localDate, calendarCode)).ifPresent(shiftDetail -> { Integer shiftStartTime = shiftDetail.getShiftStartTime(); Integer shiftEndTime = shiftDetail.getShiftEndTime(); LocalDateTime start = LocalDateTime.of(localDate, LocalTime.of(0, 0, 0)).plus(shiftStartTime.intValue(), (TemporalUnit) ChronoUnit.MINUTES); LocalDateTime end = LocalDateTime.of(localDate, LocalTime.of(0, 0, 0)).plus(shiftEndTime.intValue(), (TemporalUnit) ChronoUnit.MINUTES); shiftSlicesDTO.setStartTime(start); shiftSlicesDTO.setEndTime(end); }); return shiftSlicesDTO; } @Override public List getShiftSlicesList(ShiftSlicesClientCalendarCodesDTO shiftSlicesClientCalendarCodesDTO) { List calendarCodes = shiftSlicesClientCalendarCodesDTO.getCalendarCodes(); ArrayList shiftSlicesCalendarCodeDTOList = new ArrayList<>(); calendarCodes.forEach(calendarCode -> { ShiftSlicesClientDTO shiftSlicesClientDTO = new ShiftSlicesClientDTO(); shiftSlicesClientDTO.setShiftIndex(shiftSlicesClientCalendarCodesDTO.getShiftIndex()); shiftSlicesClientDTO.setCalendarCode(calendarCode); shiftSlicesClientDTO.setTenantId(shiftSlicesClientCalendarCodesDTO.getTenantId()); shiftSlicesClientDTO.setLocalDate(shiftSlicesClientCalendarCodesDTO.getLocalDate()); ShiftSlicesDTO data = getShiftSlices(shiftSlicesClientDTO); ShiftSlicesCalendarCodeDTO shiftSlicesCalendarCodeDTO = new ShiftSlicesCalendarCodeDTO(); shiftSlicesCalendarCodeDTO.setCalendarCode(calendarCode); shiftSlicesCalendarCodeDTO.setStartTime(data.getStartTime()); shiftSlicesCalendarCodeDTO.setEndTime(data.getEndTime()); shiftSlicesCalendarCodeDTOList.add(shiftSlicesCalendarCodeDTO); }); return shiftSlicesCalendarCodeDTOList; } @Override public Long getShiftTimeWithoutRest(String calendarCode) { ProductionCalendar productionCalendar = getOne(Wrappers.lambdaQuery() .eq(ProductionCalendar::getCode, calendarCode) .eq(ProductionCalendar::getYear, Calendar.getInstance().get(1))); /* ProductionCalendar productionCalendar = (ProductionCalendar) getOne((Wrapper) ((LambdaQueryWrapper) Wrappers.lambdaQuery().eq((v0) -> { return v0.getCode(); }, calendarCode)).eq((v0) -> { return v0.getYear(); }, Integer.valueOf(Calendar.getInstance().get(1)))); */ if (Func.isEmpty(productionCalendar)) { return 0L; } List list = this.calendarDayService.list(Wrappers.lambdaQuery() .eq(ProductionCalendarDay::getCalendarId, productionCalendar.getId())); /* List list = this.calendarDayService.list((Wrapper) Wrappers.lambdaQuery().eq((v0) -> { return v0.getCalendarId(); }, productionCalendar.getId()));*/ List productionCalendarDayIds = list.stream().map((v0) -> { return v0.getId(); }).collect(Collectors.toList()); LocalDate now = LocalDate.now(); List productionCalendarDaytimeList = this.calendarDaytimeService.list(Wrappers.lambdaQuery() .in(ProductionCalendarDaytime::getCalendarDayId, productionCalendarDayIds) .eq(ProductionCalendarDaytime::getShiftType, ProductionTimeConstant.WORK_TIME) .eq(ProductionCalendarDaytime::getCalendarDate, now)); return Long.valueOf(productionCalendarDaytimeList.stream().mapToLong(productionCalendarDaytime -> { return LocalDateTimeUtils.betweenTwoTime(productionCalendarDaytime.getStartTime(), productionCalendarDaytime.getEndTime(), ChronoUnit.SECONDS); }).sum()); } @Override public ShiftIndexNameVO getShiftIndexName() { return this.shiftModelService.getShiftIndexName(); } @Override public String getShiftIndexNameByCodeAndYear(String calendarCode, CalendarShiftTimeSlicesDTO calendarShiftTimeSlicesDTO) { return this.baseMapper.getShiftIndexNameByCodeAndYear(calendarCode, calendarShiftTimeSlicesDTO.getFactoryYear(), calendarShiftTimeSlicesDTO.getFactoryDate(), calendarShiftTimeSlicesDTO.getShiftIndex(), AuthUtil.getTenantId()); } @Override public String expireCalendarInfo() { String code = this.baseMapper.expireCalendarInfo(LocalDate.now().getYear()); LocalDate now = LocalDate.now(); LocalDate lastDayOfYear = LocalDate.now().with(TemporalAdjusters.lastDayOfYear()); Integer difference = LocalDateTimeUtils.getDifference(now.minusDays(1L), lastDayOfYear); log.info("当前时间:[{}],最后一天:[{}]", now, lastDayOfYear); if (difference.intValue() <= ProductionTimeConstant.SEVEN_DAYS.intValue()) { return String.format("您的【%s】生产日历将在【%s】天后过期,为了能正常使用,请及时创建新的日历(如果您想更便捷使用,建议创建多年份日历)", code, difference); } return null; } @Override @Transactional(rollbackFor = {Exception.class}) public Boolean deleteCalendar(Long id) { //Workstation::getCalendarCodeWaiting Optional.ofNullable(getById(id)).ifPresent(calendar -> { List list = this.workstationService.list(new QueryWrapper().lambda() .eq(Workstation::getCalendarCode, calendar.getCode()).eq(Workstation::getStatus, CommonConstant.ENABLE).or() .eq(Workstation::getCalendarCodeWaiting, calendar.getCode())); if (Func.isNotEmpty(list)) { throw new ServiceException(MessageUtils.message("calendar.has.be.used", new Object[0])); } removeById(id); this.calendarDayService.remove(Wrappers.lambdaQuery().eq(ProductionCalendarDay::getCalendarId, id)); this.calendarDaytimeService.remove(Wrappers.lambdaQuery().eq(ProductionCalendarDaytime::getCalendarId, id)); }); return true; } @Override public Long getShiftModelByFactoryDay(String calendarCode, LocalDate factoryDay) { return this.baseMapper.getShiftModelByFactoryDay(calendarCode, Integer.valueOf(LocalDate.now().getYear()), factoryDay); } @Override public List getShiftDetailDates(String calendarCode, List dates) { //ProductionCalendar QueryWrapper wrapper = Wrappers.query() .eq("bpc.is_deleted", 0) .eq("bpcd.is_deleted", 0) .eq("bpc.code", calendarCode) .nested(q -> q.in("bpcd.calendar_date", dates)) .orderByAsc("calendar_date", "shift_index"); return this.baseMapper.getShiftDetailDates(wrapper); /* return this.baseMapper.getShiftDetailDates((QueryWrapper) ((QueryWrapper) ((QueryWrapper) ((QueryWrapper) ((QueryWrapper) Wrappers.query().eq("bpc.is_deleted", 0)).eq("bpcd.is_deleted", 0)).eq("bpc.code", calendarCode)).nested(q -> { QueryWrapper queryWrapper = (QueryWrapper) q.in("bpcd.calendar_date", dates); })).orderByAsc("calendar_date", new String[]{"shift_index"}));*/ } @Override public List getTimeShiftAll(String calendarCode, Date startTime, Date endTime) { QueryWrapper wrapper = Wrappers.query().eq("bpc.is_deleted", 0) .eq("bpcd.is_deleted", 0) .ne("bsd.shift_index", ProductionTimeConstant.OUT_OF_SHIFT_TIME) .nested(q -> q.between("bpcd.calendar_date", DateUtil.formatDate(DateUtil.minusDays(startTime, 1L)), DateUtil.formatDate(DateUtil.plusDays(endTime, 1L))) .eq("bpc.code", calendarCode)).orderByAsc("calendar_date", "shift_index" ); return this.baseMapper.getTimeShiftAll(wrapper); /* return this.baseMapper.getTimeShiftAll(Wrappers.query().eq("bpc.is_deleted", 0) .eq("bpcd.is_deleted", 0)) .ne("bsd.shift_index", ProductionTimeConstant.OUT_OF_SHIFT_TIME) .nested(q -> { QueryWrapper queryWrapper = q.between("bpcd.calendar_date", DateUtil.formatDate(DateUtil.minusDays(startTime, 1L)), DateUtil.formatDate(DateUtil.plusDays(endTime, 1L))); })).eq("bpc.code", calendarCode) .orderByAsc("calendar_date", new String[]{"shift_index"})); */ } @Override public List getWorkstationShiftIndexListByDate(Long workstationId, LocalDate localDate) { List result = Lists.newArrayList(); Workstation workstation = (Workstation)this.workstationService.getOne(Wrappers.lambdaQuery() .eq(Workstation::getId, workstationId) .eq(Workstation::getType, WorkstationTypeEnum.MACHINE.getCode()) .isNotNull(Workstation::getCalendarCode)); /* Workstation workstation = (Workstation) this.workstationService.getOne((Wrapper) ((LambdaQueryWrapper) ((LambdaQueryWrapper) Wrappers.lambdaQuery().eq((v0) -> { return v0.getId(); }, workstationId)).eq((v0) -> { return v0.getType(); }, WorkstationTypeEnum.MACHINE.getCode())).isNotNull((v0) -> { return v0.getCalendarCode(); }));*/ if (Func.isNotEmpty(workstation)) { List shiftIndexList = getWorkstationShiftIndexListByDate(workstation.getCalendarCode(), Integer.valueOf(localDate.getYear()), LocalDateTimeUtils.formatTimeLocalDate(localDate, DateConstant.PATTERN_DATE)); shiftIndexList.forEach(x -> { ShiftIndexInfoDTO shiftIndexInfoDTO = new ShiftIndexInfoDTO(); if (x.getCalendarDate().isBefore(localDate)) { if (x.getShiftStartTime().intValue() < CommonConstant.MINUTES_OF_DAY.intValue() && x.getShiftEndTime().intValue() > CommonConstant.MINUTES_OF_DAY.intValue()) { shiftIndexInfoDTO.setShiftIndex(x.getShiftIndex()); shiftIndexInfoDTO.setDateShiftIndex(DateUtil.format(x.getCalendarDate(), "yyyyMMdd") + "/" + x.getShiftIndex()); shiftIndexInfoDTO.setDateShiftIndexName(DateUtil.format(x.getCalendarDate(), "MM/dd") + x.getIndexName()); result.add(shiftIndexInfoDTO); return; } return; } shiftIndexInfoDTO.setShiftIndex(x.getShiftIndex()); shiftIndexInfoDTO.setDateShiftIndex(DateUtil.format(x.getCalendarDate(), "yyyyMMdd") + "/" + x.getShiftIndex()); shiftIndexInfoDTO.setDateShiftIndexName(DateUtil.format(x.getCalendarDate(), "MM/dd") + x.getIndexName()); result.add(shiftIndexInfoDTO); }); } return result; } private List getWorkstationShiftIndexListByDate(String calendarCode, Integer year, String date) { Integer maxShiftIndex; List workstationShiftIndexListByDate = this.baseMapper.getWorkstationShiftIndexListByDate(calendarCode, year, date); String maxShiftIndexStr = ParamCache.getValue("system.shift.max"); if (Func.isEmpty(maxShiftIndexStr)) { maxShiftIndex = CalendarConstant.DEFAULT_MAX_SHIFT_INDEX; } else { maxShiftIndex = Integer.valueOf(maxShiftIndexStr); } Integer num = maxShiftIndex; workstationShiftIndexListByDate.removeIf(s -> { return s.getShiftIndex().intValue() > num.intValue(); }); return workstationShiftIndexListByDate; } @Override @Transactional public Boolean associateWorkstation(CalendarAssociateWorkstationVO calendarAssociateWorkstationVO) { boolean result = this.workstationService.update(Wrappers.lambdaUpdate() .set(Workstation::getCalendarCodeWaiting, calendarAssociateWorkstationVO.getCalendarCode()) .in(Workstation::getId, calendarAssociateWorkstationVO.getWorkstationIdList())); return result; /* return Boolean.valueOf(this.workstationService.update((Wrapper) ((LambdaUpdateWrapper) Wrappers.lambdaUpdate().set((v0) -> { return v0.getCalendarCodeWaiting(); }, calendarAssociateWorkstationVO.getCalendarCode())).in((v0) -> { return v0.getId(); }, calendarAssociateWorkstationVO.getWorkstationIdList())));*/ } @Override public List queryShiftIndexName(String calendarCode, Integer year, String day) { Integer maxShiftIndex; List shiftIndexNameDTOList = this.baseMapper.queryShiftIndexName(calendarCode, year, day); String maxShiftIndexStr = ParamCache.getValue("system.shift.max"); if (Func.isEmpty(maxShiftIndexStr)) { maxShiftIndex = CalendarConstant.DEFAULT_MAX_SHIFT_INDEX; } else { maxShiftIndex = Integer.valueOf(maxShiftIndexStr); } Integer num = maxShiftIndex; shiftIndexNameDTOList.removeIf(s -> { return s.getShiftIndex().intValue() > num.intValue(); }); return shiftIndexNameDTOList; } @Override public List listShiftInfo(List workstationIdList, LocalDate startDay, LocalDate endDay) { Integer maxShiftIndex; List shiftInfoDTOList = this.baseMapper.listShiftInfo(workstationIdList, startDay, endDay); String maxShiftIndexStr = ParamCache.getValue("system.shift.max"); if (Func.isEmpty(maxShiftIndexStr)) { maxShiftIndex = CalendarConstant.DEFAULT_MAX_SHIFT_INDEX; } else { maxShiftIndex = Integer.valueOf(maxShiftIndexStr); } Integer num = maxShiftIndex; shiftInfoDTOList.removeIf(s -> { return s.getShiftIndex().intValue() > num.intValue(); }); return shiftInfoDTOList; } }