| | |
| | | .thenComparing(item -> item.getTemplateRowIndex() == null |
| | | ? Integer.MAX_VALUE : item.getTemplateRowIndex())) |
| | | .collect(Collectors.toList()); |
| | | if (templateItems.isEmpty()) { |
| | | throw new ErrorException("所选原始记录模板未配置检验项或导出值,不能下单"); |
| | | } |
| | | |
| | | StandardMethod standardMethod = standardMethodService.getById(standard.getStandardMethodId()); |
| | | if (standardMethod == null || StringUtils.isBlank(standardMethod.getCode())) { |
| | |
| | | order.setIsExemption(0); |
| | | order.setFactory(null); |
| | | order.setLaboratory(standard.getLaboratory()); |
| | | order.setStandardTreeId(dto.getStandardTreeId()); |
| | | order.setSampleType(null); |
| | | order.setSample(standardCode); |
| | | order.setPrepareUserEn(null); |
| | |
| | | .sorted(Comparator.comparing((TemplateInspectionItemDto item) -> templateSortMap.get(item.getTemplateId())) |
| | | .thenComparing(item -> item.getTemplateRowIndex() == null ? Integer.MAX_VALUE : item.getTemplateRowIndex())) |
| | | .collect(Collectors.toList()); |
| | | if (templateItems.isEmpty()) { |
| | | throw new ErrorException("所选原始记录模板未配置检验项或导出值,不能下单"); |
| | | } |
| | | StandardMethod standardMethod = standardMethodService.getById(standard.getStandardMethodId()); |
| | | if (standardMethod == null || StringUtils.isBlank(standardMethod.getCode())) { |
| | | throw new ErrorException("标准编号不存在"); |
| | |
| | | order.setIsExemption(currentOrder.getIsExemption()); |
| | | order.setFactory(null); |
| | | order.setLaboratory(standard.getLaboratory()); |
| | | order.setStandardTreeId(dto.getStandardTreeId()); |
| | | order.setSampleType(null); |
| | | order.setSample(standardCode); |
| | | order.setPrepareUserEn(null); |
| | |
| | | snapshot.setRemark(template.getRemark()); |
| | | snapshot.setThing(template.getThing()); |
| | | snapshot.setSort(selection.getSort()); |
| | | snapshot.setDeviceDefaultsInitialized(0); |
| | | insOrderStandardTemplateMapper.insert(snapshot); |
| | | |
| | | StandardTreeTemplateBindingVo binding = bindingMap.get(selection.getTemplateId()); |
| | |
| | | .eq(StandardMethod::getCode, insOrder.getSample()) |
| | | .select(StandardMethod::getId)).stream() |
| | | .map(StandardMethod::getId).collect(Collectors.toList()); |
| | | if (!methodIds.isEmpty() && !orderTemplateIds.isEmpty()) { |
| | | if (insOrder.getStandardTreeId() != null) { |
| | | map.put("standardTreeId", insOrder.getStandardTreeId()); |
| | | } else if (!methodIds.isEmpty() && !orderTemplateIds.isEmpty()) { |
| | | List<StandardTree> candidates = standardTreeService.list(Wrappers.<StandardTree>lambdaQuery() |
| | | .in(StandardTree::getStandardMethodId, methodIds) |
| | | .isNull(StandardTree::getSampleType) |
| | |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public Map<String, Object> getInsOrderAndSample(Integer id, String laboratory) { |
| | | Map<String, Object> map = new HashMap<>(); |
| | | initializeTemplateDevicesOnFirstOpen(id); |
| | | // InsOrder insOrder = insOrderMapper.selectById(id); |
| | | IfsOrderVO ifsOrderVO = insOrderMapper.selectOrderInfoById(id); |
| | | Map<Integer, List<Integer>> templateDeviceIds = insOrderTemplateDeviceMapper.selectList( |
| | |
| | | return map; |
| | | } |
| | | |
| | | /** 首次打开检验任务时,将仍为空的模板设备从对应标准补入订单。 */ |
| | | private void initializeTemplateDevicesOnFirstOpen(Integer orderId) { |
| | | InsOrder order = insOrderMapper.selectById(orderId); |
| | | if (order == null || !DIRECT_TEMPLATE_LABORATORY.equals(order.getLaboratory()) |
| | | || !Objects.equals(order.getTypeSource(), -1) |
| | | || !Objects.equals(order.getState(), 1) |
| | | || "QUALITY_MONITOR".equals(order.getTaskSource())) { |
| | | return; |
| | | } |
| | | List<InsOrderStandardTemplate> snapshots = insOrderStandardTemplateMapper.selectList( |
| | | Wrappers.<InsOrderStandardTemplate>lambdaQuery() |
| | | .eq(InsOrderStandardTemplate::getInsOrderId, orderId) |
| | | .orderByAsc(InsOrderStandardTemplate::getSort, InsOrderStandardTemplate::getId) |
| | | .last("FOR UPDATE")); |
| | | if (snapshots.isEmpty() || snapshots.stream().allMatch( |
| | | snapshot -> Objects.equals(snapshot.getDeviceDefaultsInitialized(), 1))) { |
| | | return; |
| | | } |
| | | |
| | | Set<Integer> existingTemplateIds = insOrderTemplateDeviceMapper.selectList( |
| | | Wrappers.<InsOrderTemplateDevice>lambdaQuery() |
| | | .eq(InsOrderTemplateDevice::getInsOrderId, orderId)) |
| | | .stream().map(InsOrderTemplateDevice::getTemplateId) |
| | | .filter(Objects::nonNull).collect(Collectors.toSet()); |
| | | boolean inspectionStarted = order.getFirstInspectDate() != null |
| | | || (order.getInsState() != null && order.getInsState() > 0) |
| | | || snapshots.stream().anyMatch(snapshot -> StringUtils.isNotBlank(snapshot.getRecordValues()) |
| | | || snapshot.getDetectionTime() != null || snapshot.getTemperature() != null |
| | | || snapshot.getDetectionPlace() != null); |
| | | Map<Integer, List<Integer>> defaults = inspectionStarted ? Collections.emptyMap() |
| | | : findStandardDeviceDefaults(order, snapshots); |
| | | |
| | | for (InsOrderStandardTemplate snapshot : snapshots) { |
| | | if (Objects.equals(snapshot.getDeviceDefaultsInitialized(), 1)) { |
| | | continue; |
| | | } |
| | | Integer templateId = snapshot.getTemplateId(); |
| | | if (!inspectionStarted && !existingTemplateIds.contains(templateId) |
| | | && defaults.containsKey(templateId)) { |
| | | List<Integer> deviceIds = defaults.get(templateId); |
| | | for (int index = 0; index < deviceIds.size(); index++) { |
| | | InsOrderTemplateDevice relation = new InsOrderTemplateDevice(); |
| | | relation.setInsOrderId(orderId); |
| | | relation.setTemplateId(templateId); |
| | | relation.setDeviceId(deviceIds.get(index)); |
| | | relation.setSort(index); |
| | | insOrderTemplateDeviceMapper.insert(relation); |
| | | } |
| | | } |
| | | // 无法唯一确定来源时也只尝试这一次,避免以后标准库调整时意外改动订单。 |
| | | insOrderStandardTemplateMapper.update(null, |
| | | Wrappers.<InsOrderStandardTemplate>lambdaUpdate() |
| | | .eq(InsOrderStandardTemplate::getId, snapshot.getId()) |
| | | .set(InsOrderStandardTemplate::getDeviceDefaultsInitialized, 1)); |
| | | } |
| | | } |
| | | |
| | | private Map<Integer, List<Integer>> findStandardDeviceDefaults(InsOrder order, |
| | | List<InsOrderStandardTemplate> snapshots) { |
| | | Set<Integer> templateIds = snapshots.stream().map(InsOrderStandardTemplate::getTemplateId) |
| | | .filter(Objects::nonNull).collect(Collectors.toSet()); |
| | | Integer standardTreeId = order.getStandardTreeId(); |
| | | if (standardTreeId == null && StringUtils.isNotBlank(order.getSample()) && !templateIds.isEmpty()) { |
| | | List<Integer> methodIds = standardMethodService.list(Wrappers.<StandardMethod>lambdaQuery() |
| | | .eq(StandardMethod::getCode, order.getSample()) |
| | | .select(StandardMethod::getId)).stream() |
| | | .map(StandardMethod::getId).collect(Collectors.toList()); |
| | | if (!methodIds.isEmpty()) { |
| | | List<StandardTree> candidates = standardTreeService.list(Wrappers.<StandardTree>lambdaQuery() |
| | | .in(StandardTree::getStandardMethodId, methodIds) |
| | | .eq(StandardTree::getLaboratory, order.getLaboratory()) |
| | | .isNotNull(StandardTree::getSample) |
| | | .isNull(StandardTree::getSampleType) |
| | | .isNull(StandardTree::getModel)); |
| | | List<StandardTree> matches = candidates.stream() |
| | | .filter(tree -> StringUtils.isNotBlank(tree.getSample())) |
| | | .filter(tree -> { |
| | | Set<Integer> boundIds = standardTreeService.selectTemplateBindings(tree.getId()).stream() |
| | | .map(StandardTreeTemplateBindingVo::getTemplateId).collect(Collectors.toSet()); |
| | | return boundIds.containsAll(templateIds); |
| | | }).collect(Collectors.toList()); |
| | | if (matches.size() == 1) { |
| | | standardTreeId = matches.get(0).getId(); |
| | | } else { |
| | | List<StandardTree> exact = matches.stream().filter(tree -> |
| | | standardTreeService.selectTemplateBindings(tree.getId()).stream() |
| | | .map(StandardTreeTemplateBindingVo::getTemplateId) |
| | | .collect(Collectors.toSet()).equals(templateIds)) |
| | | .collect(Collectors.toList()); |
| | | if (exact.size() == 1) { |
| | | standardTreeId = exact.get(0).getId(); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | if (standardTreeId == null) { |
| | | return Collections.emptyMap(); |
| | | } |
| | | StandardTree standard = standardTreeService.getById(standardTreeId); |
| | | if (standard == null || StringUtils.isBlank(standard.getSample()) |
| | | || !Objects.equals(standard.getLaboratory(), order.getLaboratory()) |
| | | || standard.getSampleType() != null || standard.getModel() != null) { |
| | | return Collections.emptyMap(); |
| | | } |
| | | if (order.getStandardTreeId() == null) { |
| | | insOrderMapper.update(null, Wrappers.<InsOrder>lambdaUpdate() |
| | | .eq(InsOrder::getId, order.getId()) |
| | | .isNull(InsOrder::getStandardTreeId) |
| | | .set(InsOrder::getStandardTreeId, standardTreeId)); |
| | | } |
| | | List<StandardTreeTemplateBindingVo> bindings = standardTreeService.selectTemplateBindings(standardTreeId); |
| | | Map<Integer, List<Integer>> defaults = new HashMap<>(); |
| | | for (StandardTreeTemplateBindingVo binding : bindings) { |
| | | if (templateIds.contains(binding.getTemplateId())) { |
| | | defaults.put(binding.getTemplateId(), binding.getDeviceIds() == null |
| | | ? Collections.emptyList() : binding.getDeviceIds()); |
| | | } |
| | | } |
| | | return defaults; |
| | | } |
| | | |
| | | @Override |
| | | public IPage<SampleProductDto2> selectSampleAndProductByOrderId(IPage<SampleProductDto2> page, SampleProductDto2 sampleProductDto) { |
| | | IPage<SampleProductDto2> productDto2IPage = insOrderMapper.selectSampleAndProductByOrderId(page, |