package com.qianwen.smartman.modules.cps.service.impl; import com.baomidou.mybatisplus.core.conditions.Wrapper; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import java.lang.invoke.SerializedLambda; import java.time.LocalDate; import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; import java.util.List; import java.util.Map; import java.util.Optional; import java.util.stream.Collectors; import com.qianwen.smartman.common.constant.ProductionTimeConstant; import com.qianwen.smartman.common.utils.MessageUtils; import com.qianwen.smartman.common.utils.ProductionTimeUtils; import com.qianwen.core.log.exception.ServiceException; import com.qianwen.core.secure.utils.AuthUtil; import com.qianwen.core.tool.utils.CollectionUtil; import com.qianwen.core.tool.utils.Func; import com.qianwen.core.tool.utils.StringUtil; import com.qianwen.smartman.modules.cps.dto.KeyNameDTO; import com.qianwen.smartman.modules.cps.dto.ShiftDetailDTO; import com.qianwen.smartman.modules.cps.dto.ShiftRestTimeDTO; import com.qianwen.smartman.modules.cps.entity.ProductionCalendarDay; import com.qianwen.smartman.modules.cps.entity.ShiftDetail; import com.qianwen.smartman.modules.cps.entity.ShiftModel; import com.qianwen.smartman.modules.cps.entity.ShiftRestTime; import com.qianwen.smartman.modules.cps.mapper.ShiftModelMapper; import com.qianwen.smartman.modules.cps.service.ICalendarDayService; import com.qianwen.smartman.modules.cps.service.IShiftDetailService; import com.qianwen.smartman.modules.cps.service.IShiftModelService; import com.qianwen.smartman.modules.cps.service.IShiftRestTimeService; import com.qianwen.smartman.modules.cps.vo.ShiftDetailVO; import com.qianwen.smartman.modules.cps.vo.ShiftIndexNameVO; import com.qianwen.smartman.modules.cps.vo.ShiftSaveVO; import com.qianwen.smartman.modules.cps.vo.ShiftUpdateBasicVO; import com.qianwen.smartman.modules.cps.vo.ShiftUpdateVO; import com.qianwen.smartman.modules.cps.vo.ShiftVO; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @Service /* loaded from: blade-api.jar:BOOT-INF/classes/org/springblade/modules/cps/service/impl/ShiftModelServiceImpl.class */ public class ShiftModelServiceImpl extends ServiceImpl implements IShiftModelService { @Autowired private IShiftDetailService shiftDetailService; @Autowired private IShiftRestTimeService shiftRestTimeService; @Autowired private ICalendarDayService calendarDayService; @Autowired private ShiftModelMapper shiftModelMapper; @Override // org.springblade.modules.cps.service.IShiftModelService @Transactional(rollbackFor = {Exception.class}) public ShiftModel saveShift(ShiftSaveVO shiftSaveVO) { Integer startTime = shiftSaveVO.getStartTime(); Integer endTime = shiftSaveVO.getEndTime(); List shiftDetailDTOList = shiftSaveVO.getShiftDetailDTOList(); verifyShiftDay(startTime, endTime); verifyShift(shiftSaveVO, shiftDetailDTOList); checkCode(shiftSaveVO, AuthUtil.getTenantId()); ShiftModel shiftModel = new ShiftModel(); BeanUtils.copyProperties(shiftSaveVO, shiftModel); shiftModel.setIsDefault(ProductionTimeConstant.IS_NOT_DEFAULT); save(shiftModel); this.shiftDetailService.saveShiftDetail(shiftDetailDTOList, shiftModel.getId()); return shiftModel; } @Override // org.springblade.modules.cps.service.IShiftModelService @Transactional(rollbackFor = {Exception.class}) public ShiftModel update(ShiftUpdateVO shiftUpdateVO) { long count = this.calendarDayService.count(new QueryWrapper().lambda().eq(ProductionCalendarDay::getModelId, shiftUpdateVO.getId())); if (count > 0) { throw new ServiceException(MessageUtils.message("calendar.time.shift.model.used", new Object[0])); } ShiftSaveVO shiftSaveVO = new ShiftSaveVO(); ShiftModel shiftModel = new ShiftModel(); BeanUtils.copyProperties(shiftUpdateVO, shiftSaveVO); BeanUtils.copyProperties(shiftUpdateVO, shiftModel); List shiftDetailDTOList = shiftSaveVO.getShiftDetailDTOList(); Integer startTime = shiftSaveVO.getStartTime(); Integer endTime = shiftSaveVO.getEndTime(); boolean flag = !(startTime == null && endTime == null) && shiftDetailDTOList.isEmpty(); if (flag) { throw new ServiceException(MessageUtils.message("calendar.shift.day.has.been.modified", new Object[0])); } this.baseMapper.updateById(shiftModel); if (CollectionUtil.isNotEmpty(shiftDetailDTOList)) { verifyShiftDay(startTime, endTime); verifyShift(shiftSaveVO, shiftDetailDTOList); deleteShiftAndRest(shiftUpdateVO.getId()); this.shiftDetailService.saveShiftDetail(shiftDetailDTOList, shiftUpdateVO.getId()); } return shiftModel; } @Override // org.springblade.modules.cps.service.IShiftModelService public ShiftModel updateShiftBasic(ShiftUpdateBasicVO basicDTO) { ShiftModel shiftModel = new ShiftModel(); BeanUtils.copyProperties(basicDTO, shiftModel); this.baseMapper.updateById(shiftModel); return shiftModel; } @Override // org.springblade.modules.cps.service.IShiftModelService public ShiftIndexNameVO getShiftIndexName() { String tenantId = AuthUtil.getTenantId(); ShiftIndexNameVO shiftIndexNameVO = new ShiftIndexNameVO(); List shiftDetailList = this.shiftModelMapper.getShiftIndexNameByTenantId(tenantId); List list = new ArrayList<>(); if (Func.isNotEmpty(shiftDetailList)) { Map map = shiftDetailList.stream().collect(Collectors.groupingBy((v0) -> { return v0.getShiftIndex(); }, Collectors.mapping(shiftDetail -> { return shiftDetail.getIndexName(); }, Collectors.joining("/")))); map.forEach((key, value) -> { if (value.contains("null")) { value = (String) Arrays.stream(value.split("/")).filter(item -> { return !item.equals("null"); }).collect(Collectors.joining("/")); } if (value == null || StringUtil.isEmpty(value)) { value = MessageUtils.message("calendar.page.shift.model.shift", new Object[0]) + key.toString(); } if (value.endsWith("/")) { value = value.substring(0, value.length() - 1); } if (value.startsWith("/")) { value = value.substring(1, value.length()); } KeyNameDTO keyNameDTO = new KeyNameDTO(String.valueOf(key), value); list.add(keyNameDTO); }); } shiftIndexNameVO.setKeyNameDTOList(list); return shiftIndexNameVO; } @Override // org.springblade.modules.cps.service.IShiftModelService public ShiftDetail getShiftTime(String tenantId, Integer shiftIndex, LocalDate localDate, String calendarCode) { ShiftDetail shiftDetail = this.shiftModelMapper.getShiftTime(tenantId, shiftIndex, localDate.getYear(), localDate, calendarCode); return shiftDetail; } @Override // org.springblade.modules.cps.service.IShiftModelService public List getShiftIndex(Long modelId) { ShiftVO shiftDetailR = getShiftDetail(modelId); List shiftDetailVOList = shiftDetailR.getShiftDetailVOList(); return shiftDetailVOList.stream().map((v0) -> { return v0.getShiftIndex(); }).distinct().collect(Collectors.toList()); } @Override // org.springblade.modules.cps.service.IShiftModelService @Transactional(rollbackFor = {Exception.class}) public void deleteShift(String id) { long count = this.calendarDayService.count(new QueryWrapper().lambda().eq(ProductionCalendarDay::getModelId, Long.valueOf(id))); if (count > 0) { throw new ServiceException(MessageUtils.message("calendar.shift.model.cannot.be.deleted", new Object[0])); } this.baseMapper.deleteById(Long.valueOf(id)); deleteShiftAndRest(Long.valueOf(id)); } private void deleteShiftAndRest(Long modelId) { this.shiftRestTimeService.remove(new QueryWrapper().lambda().eq(ShiftRestTime::getModelId, modelId)); this.shiftDetailService.remove(new QueryWrapper().lambda().eq(ShiftDetail::getModelId, modelId)); /* this.shiftRestTimeService.remove((Wrapper) new QueryWrapper().lambda().eq((v0) -> { return v0.getModelId(); }, modelId)); this.shiftDetailService.remove((Wrapper) new QueryWrapper().lambda().eq((v0) -> { return v0.getModelId(); }, modelId));*/ } @Override // org.springblade.modules.cps.service.IShiftModelService public ShiftVO getShiftDetail(Long modelId) { ShiftVO shiftDetail = this.shiftModelMapper.getShiftDetail(modelId); long count = this.calendarDayService.count(new QueryWrapper().lambda().eq(ProductionCalendarDay::getModelId, modelId)); if (count > 0) { shiftDetail.setIsUsed(ProductionTimeConstant.IS_USED); } List vos = shiftDetail.getShiftDetailVOList(); vos.sort(Comparator.comparing((v0) -> { return v0.getShiftIndex(); })); return shiftDetail; } @Override // org.springblade.modules.cps.service.IShiftModelService public Map getShiftDetail(List modelIds) { return modelIds.stream().map(id -> this.baseMapper.getShiftDetail(id)).collect(Collectors.toMap(ShiftVO::getId, v -> v)); /* return (Map) modelIds.stream().map(id -> { return this.baseMapper.getShiftDetail(id); }).collect(Collectors.toMap((v0) -> { return v0.getId(); }, v -> { return v; }));*/ } private void verifyShiftRestTime(ShiftDetailDTO shiftDetailDTO) { List shiftRestTimeDTOList = shiftDetailDTO.getShiftRestTimeDTOList(); if (!shiftRestTimeDTOList.isEmpty()) { if (shiftRestTimeDTOList.size() > ProductionTimeConstant.REST_TIME_UPPER.intValue()) { throw new ServiceException(MessageUtils.message("calendar.shift.rest.time.top", new Object[0])); } shiftRestTimeDTOList.stream().forEach(shiftRestTimeDTO -> { if (shiftRestTimeDTO.getRestEndTime().intValue() <= shiftRestTimeDTO.getRestStartTime().intValue()) { throw new ServiceException(MessageUtils.message("calendar.shift.rest.time.end.time.must.be.after.start.time", new Object[0])); } }); List restStartTimeList = shiftRestTimeDTOList.stream().map((v0) -> { return v0.getRestStartTime(); }).sorted().collect(Collectors.toList()); List restEndTimeList = shiftRestTimeDTOList.stream().map((v0) -> { return v0.getRestEndTime(); }).sorted().collect(Collectors.toList()); if (ProductionTimeUtils.isOverlap(restStartTimeList, restEndTimeList)) { throw new ServiceException(MessageUtils.message("calendar.shift.rest.time.overlapped", new Object[0])); } if (restStartTimeList.get(0).intValue() < shiftDetailDTO.getShiftStartTime().intValue() || restEndTimeList.get(restEndTimeList.size() - 1).intValue() > shiftDetailDTO.getShiftEndTime().intValue()) { throw new ServiceException(MessageUtils.message("calendar.shift.rest.time.not.in.shift.time", new Object[0])); } } } private void verifyShift(ShiftSaveVO shiftSaveVO, List shiftDetailDTOList) { if (shiftDetailDTOList.size() > ProductionTimeConstant.SHIFT_UPPER.intValue()) { throw new ServiceException(MessageUtils.message("calendar.no.more.than.4.shifts.per.day", new Object[0])); } List shiftStartTimeList = shiftDetailDTOList.stream().map((v0) -> { return v0.getShiftStartTime(); }).sorted().collect(Collectors.toList()); List shiftEndTimeList = shiftDetailDTOList.stream().map((v0) -> { return v0.getShiftEndTime(); }).sorted().collect(Collectors.toList()); if (ProductionTimeUtils.isOverlap(shiftStartTimeList, shiftEndTimeList)) { throw new ServiceException(MessageUtils.message("calendar.shift.time.overlapped", new Object[0])); } if (shiftStartTimeList.get(0).intValue() < shiftSaveVO.getStartTime().intValue() || shiftEndTimeList.get(shiftEndTimeList.size() - 1).intValue() > shiftSaveVO.getEndTime().intValue()) { throw new ServiceException(MessageUtils.message("calendar.shift.time.not.in.shift.day", new Object[0])); } shiftDetailDTOList.stream().forEach(shiftDetailDTO -> { if (shiftDetailDTO.getShiftEndTime().intValue() <= shiftDetailDTO.getShiftStartTime().intValue()) { throw new ServiceException(MessageUtils.message("calendar.shift.day.end.time.must.be.after.start.time", new Object[0])); } verifyShiftRestTime(shiftDetailDTO); }); } private void verifyShiftDay(Integer startTime, Integer endTime) { if (endTime.intValue() < startTime.intValue()) { throw new ServiceException(MessageUtils.message("calendar.shift.time.end.time.must.be.after.start.time", new Object[0])); } if (endTime.intValue() - startTime.intValue() != ProductionTimeConstant.SHIFT_DAY_UPPER.intValue()) { throw new ServiceException(MessageUtils.message("calendar.shift.daily.span.must.be.greater.than.24h", new Object[0])); } } public void checkCode(ShiftSaveVO shiftSaveVO, String tenantId) { QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.lambda().eq(ShiftModel::getCode, shiftSaveVO.getCode()).eq(ShiftModel::getTenantId, tenantId); /* ((LambdaQueryWrapper) queryWrapper.lambda().eq((v0) -> { return v0.getCode(); }, shiftSaveVO.getCode())).eq((v0) -> { return v0.getTenantId(); }, tenantId);*/ Optional.ofNullable(this.baseMapper.selectOne(queryWrapper)).ifPresent(po -> { if (!po.getId().equals(shiftSaveVO.getId())) { throw new ServiceException(MessageUtils.message("calendar.code.has.be.repeated", new Object[0])); } }); } }