| | |
| | | currentMaterialMap.put(key, stat); |
| | | } |
| | | |
| | | // 收集所有需要查询库存的物料(本期有出入库的 + 上一期存在但本期没有出入库的) |
| | | List<String> matnrBatchList = new java.util.ArrayList<>(); |
| | | for (MaterialInOutStatDTO stat : materialStats) { |
| | | String matnr = stat.getMatnr(); |
| | | String batch = stat.getBatch() != null ? stat.getBatch() : ""; |
| | | matnrBatchList.add(matnr + "_" + batch); |
| | | } |
| | | if (previousSettle != null && !previousDetailMap.isEmpty()) { |
| | | for (Map.Entry<String, PreviousSettleEndingQtyDTO> entry : previousDetailMap.entrySet()) { |
| | | String key = entry.getKey(); |
| | | if (!currentMaterialMap.containsKey(key)) { |
| | | PreviousSettleEndingQtyDTO previousDetail = entry.getValue(); |
| | | String matnr = previousDetail.getMatnr(); |
| | | String batch = previousDetail.getBatch() != null ? previousDetail.getBatch() : ""; |
| | | String matnrBatchKey = matnr + "_" + batch; |
| | | if (!matnrBatchList.contains(matnrBatchKey)) { |
| | | matnrBatchList.add(matnrBatchKey); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | // 批量查询库存 |
| | | Map<String, BigDecimal> stockQtyMap = new HashMap<>(); |
| | | if (!matnrBatchList.isEmpty()) { |
| | | List<com.zy.asrs.entity.result.StockQtyDTO> stockQtyList = manLocDetlService.queryStockAnfmeBatch(matnrBatchList); |
| | | if (stockQtyList != null) { |
| | | for (com.zy.asrs.entity.result.StockQtyDTO stockQtyDTO : stockQtyList) { |
| | | String key = stockQtyDTO.getMatnr() + "_" + (stockQtyDTO.getBatch() != null ? stockQtyDTO.getBatch() : ""); |
| | | stockQtyMap.put(key, stockQtyDTO.getStockQty() != null ? stockQtyDTO.getStockQty() : BigDecimal.ZERO); |
| | | } |
| | | } |
| | | } |
| | | |
| | | // 收集所有明细记录,用于批量插入 |
| | | List<MonthlySettleDetail> detailList = new java.util.ArrayList<>(); |
| | | |
| | |
| | | // 3. 计算库存相关数量 |
| | | BigDecimal beginningQty = getBeginningQty(matnr, batch, brand, previousEndingQtyMap); |
| | | BigDecimal endingQty = calculateEndingQty(beginningQty, inQty, outQty); |
| | | // 从批量查询的Map中获取库存 |
| | | String stockKey = matnr + "_" + batch; |
| | | BigDecimal stockQtyDecimal = stockQtyMap.getOrDefault(stockKey, BigDecimal.ZERO); |
| | | BigDecimal diffQty = calculateDiffQty(stockQtyDecimal, endingQty); |
| | | // 差异数量 = 期末库存 - 期初库存(期末大于期初时为正数) |
| | | BigDecimal diffQty = calculateDiffQty(beginningQty, endingQty); |
| | | |
| | | // 4. 创建明细记录(暂不插入) |
| | | MonthlySettleDetail detail = buildMonthlySettleDetail( |
| | | monthlySettle.getId(), settleNo, matnr, batch, maktx, brand, |
| | | beginningQty, inQty, outQty, endingQty, stockQtyDecimal, diffQty |
| | | beginningQty, inQty, outQty, endingQty, diffQty |
| | | ); |
| | | detail.setIsDeleted(0); // 未删除 |
| | | detailList.add(detail); |
| | |
| | | BigDecimal beginningQty = previousEndingQty; |
| | | // 期末库存 = 期初 + 入库 - 出库 = 期初(因为本期没有出入库) |
| | | BigDecimal endingQty = beginningQty; |
| | | // 从批量查询的Map中获取库存 |
| | | String stockKey = matnr + "_" + batch; |
| | | BigDecimal stockQtyDecimal = stockQtyMap.getOrDefault(stockKey, BigDecimal.ZERO); |
| | | // 计算差异 |
| | | BigDecimal diffQty = calculateDiffQty(stockQtyDecimal, endingQty); |
| | | // 差异数量 = 期初库存 - 期末库存(期初大于期末时为正数) |
| | | BigDecimal diffQty = calculateDiffQty(beginningQty, endingQty); |
| | | |
| | | // 创建明细记录(暂不插入) |
| | | MonthlySettleDetail detail = buildMonthlySettleDetail( |
| | | monthlySettle.getId(), settleNo, matnr, batch, maktx, brand, |
| | | beginningQty, inQty, outQty, endingQty, stockQtyDecimal, diffQty |
| | | beginningQty, inQty, outQty, endingQty, diffQty |
| | | ); |
| | | detail.setIsDeleted(0); // 未删除 |
| | | detailList.add(detail); |
| | |
| | | throw new CoolException("月结记录不存在"); |
| | | } |
| | | |
| | | // 关联物料表查询明细 |
| | | // 关联物料表查询明细(汇总) |
| | | List<MonthlySettleDetail> details = monthlySettleDetailMapper.selectDetailWithMat(settleId); |
| | | |
| | | // 查询明细流水(订单明细) |
| | | List<com.zy.asrs.entity.result.MonthlySettleDetailFlowVO> pakinFlows = this.baseMapper.selectDetailFlowFromPakin(settleId); |
| | | List<com.zy.asrs.entity.result.MonthlySettleDetailFlowVO> pakoutFlows = this.baseMapper.selectDetailFlowFromPakout(settleId); |
| | | |
| | | // 合并入库和出库的明细流水 |
| | | List<com.zy.asrs.entity.result.MonthlySettleDetailFlowVO> detailFlows = new java.util.ArrayList<>(); |
| | | if (pakinFlows != null && !pakinFlows.isEmpty()) { |
| | | detailFlows.addAll(pakinFlows); |
| | | } |
| | | if (pakoutFlows != null && !pakoutFlows.isEmpty()) { |
| | | detailFlows.addAll(pakoutFlows); |
| | | } |
| | | |
| | | // 按业务时间和订单编号排序 |
| | | detailFlows.sort((a, b) -> { |
| | | int timeCompare = (a.getOrderTime() != null ? a.getOrderTime() : "").compareTo(b.getOrderTime() != null ? b.getOrderTime() : ""); |
| | | if (timeCompare != 0) { |
| | | return timeCompare; |
| | | } |
| | | return (a.getOrderNo() != null ? a.getOrderNo() : "").compareTo(b.getOrderNo() != null ? b.getOrderNo() : ""); |
| | | }); |
| | | |
| | | MonthlySettleStatisticsVO result = new MonthlySettleStatisticsVO(); |
| | | result.setSettle(settle); |
| | | result.setDetails(details); |
| | | result.setDetailFlows(detailFlows); |
| | | return result; |
| | | } |
| | | |
| | |
| | | } |
| | | |
| | | wrapper.orderBy("create_time", false); |
| | | List<MonthlySettle> list = this.selectList(wrapper); |
| | | |
| | | EntityWrapper<MonthlySettle> countWrapper = new EntityWrapper<>(); |
| | | countWrapper.eq("is_deleted", 0); |
| | | if (condition != null) { |
| | | if (condition.get("settleNo") != null) { |
| | | countWrapper.like("settle_no", condition.get("settleNo").toString()); |
| | | } |
| | | if (condition.get("status") != null) { |
| | | countWrapper.eq("status", condition.get("status")); |
| | | } |
| | | if (condition.get("startDate") != null && condition.get("endDate") != null) { |
| | | countWrapper.ge("start_date", condition.get("startDate")); |
| | | countWrapper.le("end_date", condition.get("endDate")); |
| | | } |
| | | } |
| | | page.setRecords(list); |
| | | page.setTotal(this.selectCount(countWrapper)); |
| | | return page; |
| | | // 使用分页查询,在数据库层面进行分页,而不是查询所有记录 |
| | | Page<MonthlySettle> resultPage = this.selectPage(page, wrapper); |
| | | return resultPage; |
| | | } |
| | | |
| | | /** |
| | |
| | | } |
| | | |
| | | /** |
| | | * 计算差异数量(实际库存-期末库存) |
| | | * 计算差异数量(期末库存-期初库存) |
| | | * 期末大于期初时为正数,表示库存增加 |
| | | */ |
| | | private BigDecimal calculateDiffQty(BigDecimal stockQty, BigDecimal endingQty) { |
| | | return stockQty.subtract(endingQty); |
| | | private BigDecimal calculateDiffQty(BigDecimal beginningQty, BigDecimal endingQty) { |
| | | return endingQty.subtract(beginningQty); |
| | | } |
| | | |
| | | /** |
| | |
| | | private MonthlySettleDetail buildMonthlySettleDetail( |
| | | Long settleId, String settleNo, String matnr, String batch, String maktx, String brand, |
| | | BigDecimal beginningQty, BigDecimal inQty, BigDecimal outQty, BigDecimal endingQty, |
| | | BigDecimal stockQty, BigDecimal diffQty) { |
| | | BigDecimal diffQty) { |
| | | MonthlySettleDetail detail = new MonthlySettleDetail(); |
| | | // 基本信息 |
| | | detail.setSettleId(settleId); |
| | |
| | | detail.setInQty(inQty); |
| | | detail.setOutQty(outQty); |
| | | detail.setEndingQty(endingQty); |
| | | detail.setStockQty(stockQty); |
| | | detail.setDiffQty(diffQty); |
| | | // 时间信息 |
| | | detail.setCreateTime(new Date()); |