| | |
| | | } |
| | | |
| | | @Override |
| | | public ErpPurchaseOrderImportRespVO importPurchaseOrderList(List<ErpPurchaseOrderImportExcelVO> importOrders, |
| | | ErpPurchaseOrderImportReqVO importReqVO) { |
| | | // 1. 校验非空(过滤完全空白的行) |
| | | importOrders = filterList(importOrders, item -> StrUtil.isNotBlank(item.getProductCode())); |
| | | if (CollUtil.isEmpty(importOrders)) { |
| | | throw exception(PURCHASE_ORDER_IMPORT_LIST_IS_EMPTY); |
| | | } |
| | | // 2. 按「单号」分组:相同单号的多行合并成一张采购订单;单号为空时每行各自成一张订单 |
| | | public ErpPurchaseOrderImportRespVO importPurchaseOrderList(List<ErpPurchaseOrderImportExcelVO> rows, |
| | | ErpPurchaseOrderImportReqVO req) { |
| | | if (CollUtil.isEmpty(rows)) throw exception(PURCHASE_ORDER_IMPORT_LIST_IS_EMPTY); |
| | | if (Boolean.TRUE.equals(req.getUpdateSupport())) throw exception(PURCHASE_ORDER_IMPORT_INVALID, "仅支持新增,不支持覆盖"); |
| | | ErpPurchaseOrderImportRespVO result = ErpPurchaseOrderImportRespVO.builder() |
| | | .createPurchaseOrderNos(new ArrayList<>()).failurePurchaseOrderNos(new LinkedHashMap<>()).build(); |
| | | List<List<ErpPurchaseOrderImportExcelVO>> groups = new ArrayList<>(); |
| | | Map<String, Integer> groupIndexMap = new LinkedHashMap<>(); |
| | | int rowIndex = 0; |
| | | for (ErpPurchaseOrderImportExcelVO row : importOrders) { |
| | | rowIndex++; |
| | | String groupKey = StrUtil.isNotBlank(row.getNo()) ? row.getNo() : ("__row_" + rowIndex); |
| | | Integer index = groupIndexMap.get(groupKey); |
| | | List<String> labels = new ArrayList<>(); |
| | | Map<String, Integer> indexes = new LinkedHashMap<>(); |
| | | for (int i = 0; i < rows.size(); i++) { |
| | | ErpPurchaseOrderImportExcelVO row = rows.get(i); |
| | | if (row == null || cn.hutool.core.bean.BeanUtil.beanToMap(row).values().stream() |
| | | .noneMatch(value -> value != null && (!(value instanceof String) || StrUtil.isNotBlank((String) value)))) continue; |
| | | if (StrUtil.isBlank(row.getNo())) { |
| | | groups.add(new ArrayList<>(Collections.singletonList(row))); |
| | | labels.add("第" + (i + 2) + "行"); |
| | | continue; |
| | | } |
| | | String key = row.getNo().trim(); |
| | | Integer index = indexes.get(key); |
| | | if (index == null) { |
| | | index = groups.size(); |
| | | groupIndexMap.put(groupKey, index); |
| | | groups.add(new ArrayList<>()); |
| | | index = groups.size(); indexes.put(key, index); |
| | | groups.add(new ArrayList<>()); labels.add(key); |
| | | } |
| | | groups.get(index).add(row); |
| | | } |
| | | // 3. 逐组合并创建采购订单 |
| | | ErpPurchaseOrderImportRespVO respVO = ErpPurchaseOrderImportRespVO.builder() |
| | | .createPurchaseOrderNos(new ArrayList<>()) |
| | | .failurePurchaseOrderNos(new LinkedHashMap<>()) |
| | | .build(); |
| | | for (List<ErpPurchaseOrderImportExcelVO> group : groups) { |
| | | String groupKey = StrUtil.isNotBlank(group.get(0).getNo()) ? group.get(0).getNo() |
| | | : "第" + (importOrders.indexOf(group.get(0)) + 1) + "行"; |
| | | if (groups.isEmpty()) throw exception(PURCHASE_ORDER_IMPORT_LIST_IS_EMPTY); |
| | | for (int i = 0; i < groups.size(); i++) { |
| | | try { |
| | | ErpPurchaseOrderSaveReqVO createReqVO = buildPurchaseOrderImportSaveReqVO(group); |
| | | Long orderId = createPurchaseOrder(createReqVO); |
| | | respVO.getCreatePurchaseOrderNos().add(purchaseOrderMapper.selectById(orderId).getNo()); |
| | | Long id = cn.hutool.extra.spring.SpringUtil.getBean(ErpPurchaseOrderServiceImpl.class) |
| | | .createImportedPurchaseOrder(groups.get(i)); |
| | | result.getCreatePurchaseOrderNos().add(purchaseOrderMapper.selectById(id).getNo()); |
| | | } catch (ServiceException ex) { |
| | | respVO.getFailurePurchaseOrderNos().put(groupKey, ex.getMessage()); |
| | | result.getFailurePurchaseOrderNos().put(labels.get(i), ex.getMessage()); |
| | | } catch (RuntimeException ex) { |
| | | log.error("[importPurchaseOrderList] 采购订单导入失败,分组={}", labels.get(i), ex); |
| | | result.getFailurePurchaseOrderNos().put(labels.get(i), "导入失败,请检查基础资料和订单明细"); |
| | | } |
| | | } |
| | | return respVO; |
| | | return result; |
| | | } |
| | | |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public Long createImportedPurchaseOrder(List<ErpPurchaseOrderImportExcelVO> group) { |
| | | return createPurchaseOrder(buildPurchaseOrderImportSaveReqVO(group)); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 将同一张采购订单的多行明细转换成创建 Request VO |
| | |
| | | if (first.getOrderTime() == null) { |
| | | throw exception(PURCHASE_ORDER_IMPORT_ORDER_TIME_REQUIRED); |
| | | } |
| | | if (first.getDiscountPercent() != null && (first.getDiscountPercent().signum() < 0 || first.getDiscountPercent().compareTo(new BigDecimal("100")) > 0) |
| | | || first.getDepositPrice() != null && first.getDepositPrice().signum() < 0) { |
| | | throw exception(PURCHASE_ORDER_IMPORT_INVALID, "优惠率须在 0 至 100 之间,定金不能为负数"); |
| | | } |
| | | for (ErpPurchaseOrderImportExcelVO row : group) { |
| | | if (StrUtil.isNotBlank(row.getSupplierName()) && !StrUtil.equals(first.getSupplierName(), row.getSupplierName()) |
| | | || row.getOrderTime() != null && !row.getOrderTime().equals(first.getOrderTime()) |
| | | || StrUtil.isNotBlank(row.getAccountName()) && !StrUtil.equals(first.getAccountName(), row.getAccountName()) |
| | | || row.getDiscountPercent() != null && (first.getDiscountPercent() == null || row.getDiscountPercent().compareTo(first.getDiscountPercent()) != 0) |
| | | || row.getDepositPrice() != null && (first.getDepositPrice() == null || row.getDepositPrice().compareTo(first.getDepositPrice()) != 0) |
| | | || StrUtil.isNotBlank(row.getRemark()) && !StrUtil.equals(first.getRemark(), row.getRemark())) { |
| | | throw exception(PURCHASE_ORDER_IMPORT_INVALID, "同一采购订单的主表信息不一致"); |
| | | } |
| | | if (StrUtil.isBlank(row.getProductCode()) || StrUtil.isBlank(row.getProductUnitCode()) || row.getCount() == null || row.getCount().signum() <= 0 |
| | | || row.getProductPrice() == null || row.getProductPrice().signum() < 0 |
| | | || row.getTaxPercent() != null && (row.getTaxPercent().signum() < 0 || row.getTaxPercent().compareTo(new BigDecimal("100")) > 0)) { |
| | | throw exception(PURCHASE_ORDER_IMPORT_INVALID, "每行须填写物料、单位、正数数量、非负单价;税率须在 0 至 100 之间"); |
| | | } |
| | | } |
| | | // 2. 解析结算账户(可选) |
| | | Long accountId = null; |
| | | if (StrUtil.isNotBlank(first.getAccountName())) { |
| | |
| | | // 3. 组装明细 |
| | | List<ErpPurchaseOrderSaveReqVO.Item> items = new ArrayList<>(); |
| | | for (ErpPurchaseOrderImportExcelVO row : group) { |
| | | MdmItemRespDTO product = mdmItemApi.getItemByCode(row.getProductCode()).getCheckedData(); |
| | | MdmItemRespDTO product = mdmItemApi.getItemByCode(row.getProductCode().trim()).getCheckedData(); |
| | | if (product == null) { |
| | | throw exception(PURCHASE_ORDER_IMPORT_PRODUCT_NOT_EXISTS, row.getProductCode()); |
| | | } |
| | | MdmUnitMeasureRespDTO unit = mdmUnitMeasureApi.getUnitMeasureByCode(row.getProductUnitCode()).getCheckedData(); |
| | | MdmUnitMeasureRespDTO unit = mdmUnitMeasureApi.getUnitMeasureByCode(row.getProductUnitCode().trim()).getCheckedData(); |
| | | if (unit == null) { |
| | | throw exception(PURCHASE_ORDER_IMPORT_UNIT_NOT_EXISTS, row.getProductUnitCode()); |
| | | } |
| | | if (product.getStatus() != null && product.getStatus() != 0 || unit.getStatus() != null && unit.getStatus() != 0) { |
| | | throw exception(PURCHASE_ORDER_IMPORT_INVALID, "物料或计量单位已停用"); |
| | | } |
| | | ErpPurchaseOrderSaveReqVO.Item item = new ErpPurchaseOrderSaveReqVO.Item() |
| | | .setProductId(product.getId()) |
| | | .setProductUnitId(unit.getId()) |