|  |  | 
 |  |  | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; | 
 |  |  | import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; | 
 |  |  | import com.baomidou.mybatisplus.core.metadata.IPage; | 
 |  |  | import com.baomidou.mybatisplus.core.toolkit.ObjectUtils; | 
 |  |  | import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; | 
 |  |  | import com.baomidou.mybatisplus.core.toolkit.Wrappers; | 
 |  |  | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; | 
 |  |  | import com.ruoyi.basic.mapper.IfsInventoryQuantityMapper; | 
 |  |  | import com.ruoyi.basic.mapper.StandardProductListMapper; | 
 |  |  | import com.ruoyi.basic.mapper.StructureItemParameterMapper; | 
 |  |  | import com.ruoyi.basic.pojo.IfsInventoryQuantity; | 
 |  |  | import com.ruoyi.basic.pojo.StandardProductList; | 
 |  |  | import com.ruoyi.basic.pojo.StructureItemParameter; | 
 |  |  | import com.ruoyi.basic.pojo.StructureTestObject; | 
 |  |  | import com.ruoyi.common.constant.InsOrderTypeConstants; | 
 |  |  | import com.ruoyi.common.core.domain.Result; | 
 |  |  | import com.ruoyi.common.core.domain.entity.User; | 
 |  |  | import com.ruoyi.common.numgen.NumberGenerator; | 
 |  |  | import com.ruoyi.common.utils.*; | 
 |  |  | 
 |  |  | import com.ruoyi.inspect.service.InsProductService; | 
 |  |  | import com.ruoyi.inspect.service.InsSampleService; | 
 |  |  | import com.ruoyi.inspect.vo.InsOrderPrintingVo; | 
 |  |  | import com.ruoyi.performance.pojo.AuxiliaryOutputWorkingHoursTemporary; | 
 |  |  | import com.ruoyi.performance.service.AuxiliaryOutputWorkingHoursTemporaryService; | 
 |  |  | import com.ruoyi.system.mapper.UserMapper; | 
 |  |  | import lombok.AllArgsConstructor; | 
 |  |  | import org.apache.commons.lang3.StringUtils; | 
 |  |  | 
 |  |  |     private IfsApiUtils ifsApiUtils; | 
 |  |  |     private SpotCheckQuarterItemMapper spotCheckQuarterItemMapper; | 
 |  |  |     private StandardProductListMapper standardProductListMapper; | 
 |  |  |     private AuxiliaryOutputWorkingHoursTemporaryService auxiliaryOutputWorkingHoursTemporaryService; | 
 |  |  |     private StructureItemParameterMapper structureItemParameterMapper; | 
 |  |  |  | 
 |  |  |  | 
 |  |  |  | 
 |  |  |     //获取检验下单数据 | 
 |  |  |     @Override | 
 |  |  |     public IPage<SampleOrderDto> selectInsOrderParameter(IPage<InsOrder> page, SampleOrderDto sampleOrderDto) { | 
 |  |  |         //todo: 只看我判断全部,个人,组织的权限 | 
 |  |  |         String laboratory = null; | 
 |  |  |         // 判断是否是全部 | 
 |  |  |         String isOrderAll = null; | 
 |  |  | 
 |  |  |             spotCheckQuarterItemMapper.updateById(spotCheckQuarterItem); | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         // 创建工时暂存 | 
 |  |  |         // 缓存样品id, 编号map | 
 |  |  | //        addWorkingHoursTemporary(userId, insSamples, ids, order); | 
 |  |  |  | 
 |  |  |         return 1; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 创建工时暂存 | 
 |  |  |      * @param userId | 
 |  |  |      * @param insSamples | 
 |  |  |      * @param ids | 
 |  |  |      * @param order | 
 |  |  |      */ | 
 |  |  |     private void addWorkingHoursTemporary(Integer userId, List<InsSample> insSamples, List<Integer> ids, InsOrder order) { | 
 |  |  |         Map<Integer, String> sampleMap = insSamples.stream().collect(Collectors.toMap(InsSample::getId, InsSample::getSampleCode)); | 
 |  |  |         List<InsProduct> insProductList = insProductMapper.selectList(Wrappers.<InsProduct>lambdaQuery() | 
 |  |  |                 .in(InsProduct::getInsSampleId, ids) | 
 |  |  |                 .eq(InsProduct::getState, 1)); | 
 |  |  |         List<AuxiliaryOutputWorkingHoursTemporary> outputWorkingHours = insProductList.stream().map(insProduct -> { | 
 |  |  |             AuxiliaryOutputWorkingHoursTemporary auxiliaryOutputWorkingHours = new AuxiliaryOutputWorkingHoursTemporary(); | 
 |  |  |             auxiliaryOutputWorkingHours.setInspectionItemClass(insProduct.getInspectionItemClass());//检测项分类 | 
 |  |  |             auxiliaryOutputWorkingHours.setInspectionItem(insProduct.getInspectionItem());//检测父项 | 
 |  |  |             auxiliaryOutputWorkingHours.setInspectionItemSubclass(insProduct.getInspectionItemSubclass());//检测子项 | 
 |  |  |             auxiliaryOutputWorkingHours.setSample(sampleMap.get(insProduct.getInsSampleId()));//样品编号 | 
 |  |  |             auxiliaryOutputWorkingHours.setOrderId(order.getId());//订单id | 
 |  |  |             auxiliaryOutputWorkingHours.setOrderNo(order.getEntrustCode());//非加班委托单号 | 
 |  |  |             auxiliaryOutputWorkingHours.setWorkTime(insProduct.getManHour());//非加班工时 | 
 |  |  |             auxiliaryOutputWorkingHours.setAmount(1);//非加班数量 | 
 |  |  |             auxiliaryOutputWorkingHours.setOutputWorkTime((ObjectUtils.isNotEmpty(auxiliaryOutputWorkingHours.getOvertimeWorkTime()) ? auxiliaryOutputWorkingHours.getOvertimeWorkTime() : BigDecimal.ZERO).add(ObjectUtils.isNotEmpty(auxiliaryOutputWorkingHours.getWorkTime()) ? auxiliaryOutputWorkingHours.getWorkTime() : BigDecimal.ZERO));//产量工时 | 
 |  |  |             auxiliaryOutputWorkingHours.setManHourGroup(insProduct.getManHourGroup());//工时分组 | 
 |  |  |             DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); | 
 |  |  |             DateTimeFormatter formatters = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); | 
 |  |  |             auxiliaryOutputWorkingHours.setDateTime(LocalDateTime.now().toLocalDate().atStartOfDay().format(formatters));//日期 | 
 |  |  |             LocalDateTime localDateTime = LocalDateTime.now(); | 
 |  |  |             DateTime parse = DateUtil.parse(localDateTime.format(formatter)); | 
 |  |  |             auxiliaryOutputWorkingHours.setWeekDay(getWeek(localDateTime.format(formatters)));//星期 | 
 |  |  |             auxiliaryOutputWorkingHours.setWeek(String.valueOf(DateUtil.weekOfYear(DateUtil.offsetDay(parse, 1))));//周次 | 
 |  |  |             auxiliaryOutputWorkingHours.setCheck(userId);//检测人 | 
 |  |  |             auxiliaryOutputWorkingHours.setPrice(insProduct.getPrice());//单价 | 
 |  |  |             auxiliaryOutputWorkingHours.setSampleId(insProduct.getInsSampleId());//样品id | 
 |  |  |             auxiliaryOutputWorkingHours.setInsProductId(insProduct.getId());//检验项id | 
 |  |  |  | 
 |  |  |             return auxiliaryOutputWorkingHours; | 
 |  |  |         }).collect(Collectors.toList()); | 
 |  |  |         auxiliaryOutputWorkingHoursTemporaryService.saveBatch(outputWorkingHours); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     public static String getWeek(String dayStr) { | 
 |  |  |         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); | 
 |  |  | 
 |  |  |             default: | 
 |  |  |                 return "未知"; | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |  | 
 |  |  |     // 如果你使用 Java 8+,也可以用 Stream API 简化代码: | 
 |  |  |     public List<StandardProductList> findMissingItemsWithStream(List<StandardProductList> standardList, | 
 |  |  |                                                                 List<InsProduct> insList) { | 
 |  |  |         Set<Integer> insIds = insList.stream() | 
 |  |  |                 .map(InsProduct::getStructureItemParameterId) | 
 |  |  |                 .collect(Collectors.toSet()); | 
 |  |  |  | 
 |  |  |         return standardList.stream() | 
 |  |  |                 .filter(product -> !insIds.contains(product.getStructureItemParameterId())) | 
 |  |  |                 .collect(Collectors.toList()); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     @Override | 
 |  |  | 
 |  |  |                 .set(InsSample::getModel, insSample.getModel())); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 查询当前时间是否没有该检测项的抽样计划 | 
 |  |  |      * @param sampleList | 
 |  |  |      * @param insOrder | 
 |  |  |      * @return | 
 |  |  |      */ | 
 |  |  |     @Override | 
 |  |  |     public Result judgeNotSpotCheckOrder(List<SampleProductDto> sampleList, InsOrder insOrder) { | 
 |  |  |         // todo: 下单判断抽样计划的唯一性 | 
 |  |  |         if (insOrder.getQuarterItemId() != null) { | 
 |  |  |             Long quarterItemCount = insOrderMapper.selectCount(Wrappers.<InsOrder>lambdaQuery() | 
 |  |  |                     .eq(InsOrder::getQuarterItemId, insOrder.getQuarterItemId()) | 
 |  |  |                     .notIn(InsOrder::getState, -1 ,2 ,3)); | 
 |  |  |             if (quarterItemCount > 0) { | 
 |  |  |                 throw new ErrorException("该抽样计划已被绑定过"); | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         // 判断是否是抽样检测 | 
 |  |  |         if (insOrder.getOrderType().equals(InsOrderTypeConstants.SPOT_CHECK)) { | 
 |  |  |             Set<String> monthMessageSet = new HashSet<>(); | 
 |  |  |             Set<String> quarterMessageSet = new HashSet<>(); | 
 |  |  |             Set<String> yearMessageSet = new HashSet<>(); | 
 |  |  |  | 
 |  |  |             String formatTime = "yyyy-MM-dd HH:mm:ss"; | 
 |  |  |             // 查询当前所有样品的检验项 | 
 |  |  |             for (SampleProductDto sampleProductDto : sampleList) { | 
 |  |  |                 List<StandardProductList> standardProductList = getProductTreeBySampleId(sampleProductDto.getId()); | 
 |  |  |  | 
 |  |  |                 Set<Integer> insIds = sampleProductDto.getInsProduct().stream() | 
 |  |  |                         .map(InsProduct::getStructureItemParameterId) | 
 |  |  |                         .collect(Collectors.toSet()); | 
 |  |  |  | 
 |  |  |                 List<StandardProductList> productLists = standardProductList.stream() | 
 |  |  |                         .filter(product -> !insIds.contains(product.getStructureItemParameterId())) | 
 |  |  |                         .collect(Collectors.toList()); | 
 |  |  |  | 
 |  |  |                 // 查询检验项基础表, 查询是否有月度, 季度, 年度的检验项 | 
 |  |  |                 List<StructureItemParameter> itemParameterList = structureItemParameterMapper.selectList(Wrappers.<StructureItemParameter>lambdaQuery() | 
 |  |  |                         .in(StructureItemParameter::getId, productLists.stream().map(StandardProductList::getStructureItemParameterId).collect(Collectors.toList()))); | 
 |  |  |  | 
 |  |  |                 for (StructureItemParameter item : itemParameterList) { | 
 |  |  |                     if (StringUtils.isNotBlank(item.getSpotCheckType())) { | 
 |  |  |                         switch (item.getSpotCheckType()) { | 
 |  |  |                             case "1": // 月度 | 
 |  |  |                                 // 查询当月是否有该检测项的抽样计划 | 
 |  |  |                                 // 当月开始时间 | 
 |  |  |                                 DateTime monthStart = DateUtil.beginOfMonth(DateUtil.date()); | 
 |  |  |                                 String monthStartTime = monthStart.toString(formatTime); | 
 |  |  |                                 // 当月结束时间 | 
 |  |  |                                 DateTime monthEnd = DateUtil.endOfMonth(DateUtil.date()); | 
 |  |  |                                 String monthEndTime = monthEnd.toString(formatTime); | 
 |  |  |                                 Integer count = insOrderMapper.selectNotSpotCheckOrder(item.getId(), monthStartTime, monthEndTime); | 
 |  |  |                                 if (count == 0) { | 
 |  |  |                                     String monthMessage = item.getInspectionItemClass() + | 
 |  |  |                                             item.getInspectionItem() + | 
 |  |  |                                             item.getInspectionItemSubclass(); | 
 |  |  |                                     monthMessageSet.add(monthMessage); | 
 |  |  |                                 } | 
 |  |  |                                 break; | 
 |  |  |                             case "2": // 季度 | 
 |  |  |                                 DateTime quarterStart = DateUtil.beginOfQuarter(DateUtil.date()); | 
 |  |  |                                 String quarterStartTime = quarterStart.toString(formatTime); | 
 |  |  |                                 // 当月结束时间 | 
 |  |  |                                 DateTime quarterEnd = DateUtil.endOfQuarter(DateUtil.date()); | 
 |  |  |                                 String quarterEndTime = quarterEnd.toString(formatTime); | 
 |  |  |                                 Integer quarterCount = insOrderMapper.selectNotSpotCheckOrder(item.getId(), quarterStartTime, quarterEndTime); | 
 |  |  |                                 if (quarterCount == 0) { | 
 |  |  |                                     String quarterMessage = item.getInspectionItemClass() + | 
 |  |  |                                             item.getInspectionItem() + | 
 |  |  |                                             item.getInspectionItemSubclass(); | 
 |  |  |                                     quarterMessageSet.add(quarterMessage); | 
 |  |  |                                 } | 
 |  |  |  | 
 |  |  |                                 break; | 
 |  |  |                             case "3": // 年度 | 
 |  |  |                                 DateTime yearStart = DateUtil.beginOfQuarter(DateUtil.date()); | 
 |  |  |                                 String yearStartTime = yearStart.toString(formatTime); | 
 |  |  |                                 // 当月结束时间 | 
 |  |  |                                 DateTime yearEnd = DateUtil.endOfQuarter(DateUtil.date()); | 
 |  |  |                                 String yearEndTime = yearEnd.toString(formatTime); | 
 |  |  |                                 Integer yearCount = insOrderMapper.selectNotSpotCheckOrder(item.getId(), yearStartTime, yearEndTime); | 
 |  |  |                                 if (yearCount == 0) { | 
 |  |  |                                     String yearMessage = item.getInspectionItemClass() + | 
 |  |  |                                             item.getInspectionItem() + | 
 |  |  |                                             item.getInspectionItemSubclass(); | 
 |  |  |                                     yearMessageSet.add(yearMessage); | 
 |  |  |                                 } | 
 |  |  |  | 
 |  |  |                                 break; | 
 |  |  |                         } | 
 |  |  |                     } | 
 |  |  |                 } | 
 |  |  |             } | 
 |  |  |  | 
 |  |  |             String message = ""; | 
 |  |  |             if (CollectionUtils.isNotEmpty(monthMessageSet)) { | 
 |  |  |                 message += StrUtil.format("<p>检验项<span style=\"color: red\">{}</span>当月还未进行抽样检测, 请查看是否需要添加当前检验项</p>", CollUtil.join(monthMessageSet, ", ")); | 
 |  |  |             } | 
 |  |  |  | 
 |  |  |             if (CollectionUtils.isNotEmpty(quarterMessageSet)) { | 
 |  |  |                 message += StrUtil.format("<p>检验项<span style=\"color: red\">{}</span>当前季度还未进行抽样检测, 请看是否需要添加当前检验项</p>", CollUtil.join(quarterMessageSet, ", ")); | 
 |  |  |             } | 
 |  |  |  | 
 |  |  |             if (CollectionUtils.isNotEmpty(yearMessageSet)) { | 
 |  |  |                 message += StrUtil.format("<p>检验项<span style=\"color: red\">{}</span>今年还未进行抽样检测, 请看是否需要添加当前检验项</p>", CollUtil.join(yearMessageSet, ", ")); | 
 |  |  |             } | 
 |  |  |  | 
 |  |  |             if (StringUtils.isNotBlank(message)) { | 
 |  |  |                 return Result.success(false, message); | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |         return Result.success(true); | 
 |  |  |  | 
 |  |  |     } | 
 |  |  |  | 
 |  |  | } | 
 |  |  |  | 
 |  |  |  |