|
@@ -0,0 +1,172 @@
|
|
|
|
+package com.fjhx.stock.service.impl;
|
|
|
|
+
|
|
|
|
+import cn.hutool.core.util.ObjectUtil;
|
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.IdWorker;
|
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
|
+import com.fjhx.entity.stock.Stock;
|
|
|
|
+import com.fjhx.entity.stock.StockDetail;
|
|
|
|
+import com.fjhx.entity.stock.StockWater;
|
|
|
|
+import com.fjhx.entity.stock.StockWaterdetial;
|
|
|
|
+import com.fjhx.params.stock.StockOutDto;
|
|
|
|
+import com.fjhx.stock.mapper.StockMapper;
|
|
|
|
+import com.fjhx.stock.service.StockDetailService;
|
|
|
|
+import com.fjhx.stock.service.StockService;
|
|
|
|
+import com.fjhx.stock.service.StockWaterService;
|
|
|
|
+import com.fjhx.stock.service.StockWaterdetialService;
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
|
+
|
|
|
|
+import java.math.BigDecimal;
|
|
|
|
+import java.util.*;
|
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * <p>
|
|
|
|
+ * 物料库存 服务实现类
|
|
|
|
+ * </p>
|
|
|
|
+ *
|
|
|
|
+ * @author ${author}
|
|
|
|
+ * @since 2023-02-20
|
|
|
|
+ */
|
|
|
|
+@Service
|
|
|
|
+public class StockServiceImpl extends ServiceImpl<StockMapper, Stock> implements StockService {
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ private StockDetailService stockDetailService;
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ private StockWaterService stockWaterService;
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ private StockWaterdetialService stockWaterdetialService;
|
|
|
|
+
|
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
|
+ @Override
|
|
|
|
+ public void out(StockOutDto dto) {
|
|
|
|
+
|
|
|
|
+ List<StockOutDto.Detail> detailList = dto.getDetailList();
|
|
|
|
+
|
|
|
|
+ // 物料编码集合
|
|
|
|
+ Set<String> materialCodeList = detailList.stream().map(StockOutDto.Detail::getMaterialCode).collect(Collectors.toSet());
|
|
|
|
+
|
|
|
|
+ // 物料编码 出库数量 map
|
|
|
|
+ Map<String, BigDecimal> warehousingQuantityMap = detailList.stream().collect(Collectors.toMap(
|
|
|
|
+ StockOutDto.Detail::getMaterialCode,
|
|
|
|
+ StockOutDto.Detail::getQuantity,
|
|
|
|
+ BigDecimal::add
|
|
|
|
+ ));
|
|
|
|
+
|
|
|
|
+ // rfid集合
|
|
|
|
+ Set<String> rfidSet = detailList.stream().map(StockOutDto.Detail::getRfidCode).collect(Collectors.toSet());
|
|
|
|
+
|
|
|
|
+ // 查询原有库存
|
|
|
|
+ List<Stock> stockList = list(Stock::getMaterialCode, materialCodeList);
|
|
|
|
+
|
|
|
|
+ // 扣减库存
|
|
|
|
+ inventoryDeduction(stockList, warehousingQuantityMap);
|
|
|
|
+
|
|
|
|
+ // 添加出入库流水
|
|
|
|
+ addStockWater(dto, stockList, warehousingQuantityMap, rfidSet);
|
|
|
|
+
|
|
|
|
+ // 删除库存明细
|
|
|
|
+ stockDetailService.remove(q -> q.in(StockDetail::getRfidcode, rfidSet));
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 扣减库存
|
|
|
|
+ */
|
|
|
|
+ private void inventoryDeduction(List<Stock> stockList, Map<String, BigDecimal> map) {
|
|
|
|
+
|
|
|
|
+ for (Stock stock : stockList) {
|
|
|
|
+ // 出库数量
|
|
|
|
+ BigDecimal quantity = map.get(stock.getMaterialCode());
|
|
|
|
+ // 扣减库存后的剩余库存
|
|
|
|
+ BigDecimal surplusQuantity = stock.getQuantity().subtract(quantity);
|
|
|
|
+ // 赋值剩余库存
|
|
|
|
+ stock.setQuantity(surplusQuantity.compareTo(BigDecimal.ZERO) > 0 ? surplusQuantity : BigDecimal.ZERO);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 修改库存数量
|
|
|
|
+ updateBatchById(stockList);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 添加出入库流水
|
|
|
|
+ */
|
|
|
|
+ private void addStockWater(StockOutDto dto, List<Stock> stockList, Map<String, BigDecimal> warehousingQuantityMap, Set<String> rfidSet) {
|
|
|
|
+ // 原库存map
|
|
|
|
+ Map<String, BigDecimal> oldStockQyantityMap = stockList.stream()
|
|
|
|
+ .collect(Collectors.toMap(Stock::getMaterialCode, Stock::getQuantity));
|
|
|
|
+
|
|
|
|
+ // 当前时间
|
|
|
|
+ Date date = new Date();
|
|
|
|
+
|
|
|
|
+ Map<String, List<StockOutDto.Detail>> map = dto.getDetailList().stream()
|
|
|
|
+ .collect(Collectors.groupingBy(StockOutDto.Detail::getMaterialCode));
|
|
|
|
+
|
|
|
|
+ // 库存明细
|
|
|
|
+ List<StockDetail> list = stockDetailService.list(q -> q.in(StockDetail::getRfidcode, rfidSet).eq(StockDetail::getIsdelete, 0));
|
|
|
|
+ Map<String, StockDetail> stockDetailMap = list.stream().collect(Collectors.toMap(StockDetail::getRfidcode, item -> item, (v1, v2) -> v1));
|
|
|
|
+
|
|
|
|
+ List<StockWater> stockWaterList = new ArrayList<>();
|
|
|
|
+ List<StockWaterdetial> stockWaterdetialList = new ArrayList<>();
|
|
|
|
+ warehousingQuantityMap.forEach((k, v) -> {
|
|
|
|
+ Long waterNo = IdWorker.getId();
|
|
|
|
+
|
|
|
|
+ StockWater stockWater = new StockWater();
|
|
|
|
+ stockWater.setId(waterNo);
|
|
|
|
+ stockWater.setIsdelete(0);
|
|
|
|
+ stockWater.setCreatedtime(date);
|
|
|
|
+ stockWater.setUpdatedtime(date);
|
|
|
|
+ stockWater.setWaterno(waterNo + "");
|
|
|
|
+ stockWater.setOperuserid(dto.getOperUserId());
|
|
|
|
+ stockWater.setTotalqty(v);
|
|
|
|
+ stockWater.setStockhouseid(dto.getHouseId());
|
|
|
|
+ stockWater.setMaterialcode(k);
|
|
|
|
+ stockWater.setStockchangetype(dto.getChangeType());
|
|
|
|
+ stockWater.setRemark(dto.getRemark());
|
|
|
|
+ stockWater.setOldquantity(ObjectUtil.defaultIfNull(oldStockQyantityMap.get(k), BigDecimal.ZERO));
|
|
|
|
+ stockWater.setAbnormaloutstate(1);
|
|
|
|
+ stockWater.setNewInterface(1);
|
|
|
|
+ stockWaterList.add(stockWater);
|
|
|
|
+
|
|
|
|
+ List<StockOutDto.Detail> details = map.get(k);
|
|
|
|
+ List<StockWaterdetial> waterDetailList = getWaterDetailList(dto, details, date, waterNo, stockDetailMap);
|
|
|
|
+ stockWaterdetialList.addAll(waterDetailList);
|
|
|
|
+ });
|
|
|
|
+
|
|
|
|
+ stockWaterService.saveBatch(stockWaterList);
|
|
|
|
+ stockWaterdetialService.saveBatch(stockWaterdetialList);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private List<StockWaterdetial> getWaterDetailList(StockOutDto dto, List<StockOutDto.Detail> details,
|
|
|
|
+ Date date, Long waterNo, Map<String, StockDetail> stockDetailMap) {
|
|
|
|
+ List<StockWaterdetial> stockWaterdetialList = new ArrayList<>();
|
|
|
|
+ for (StockOutDto.Detail detail : details) {
|
|
|
|
+ StockDetail stockDetail = stockDetailMap.get(detail.getRfidCode());
|
|
|
|
+
|
|
|
|
+ StockWaterdetial stockWaterdetial = new StockWaterdetial();
|
|
|
|
+ stockWaterdetial.setIsDelete(0);
|
|
|
|
+ stockWaterdetial.setCreatedTime(date);
|
|
|
|
+ stockWaterdetial.setUpdatedTime(date);
|
|
|
|
+ stockWaterdetial.setWaterId(waterNo + "");
|
|
|
|
+ stockWaterdetial.setOperUserId(dto.getOperUserId());
|
|
|
|
+ stockWaterdetial.setMaterialCode(detail.getMaterialCode());
|
|
|
|
+ stockWaterdetial.setChangeNum(detail.getQuantity());
|
|
|
|
+ stockWaterdetial.setRfidCode(detail.getRfidCode());
|
|
|
|
+ stockWaterdetial.setStockChangeType(dto.getChangeType());
|
|
|
|
+ stockWaterdetial.setInOutDevice(dto.getDevice());
|
|
|
|
+ stockWaterdetial.setNewInterface(1);
|
|
|
|
+
|
|
|
|
+ if (stockDetail != null) {
|
|
|
|
+ stockWaterdetial.setQRCode(stockDetail.getQrcode());
|
|
|
|
+ stockWaterdetial.setPrice(stockDetail.getPrice());
|
|
|
|
+ }
|
|
|
|
+ stockWaterdetialList.add(stockWaterdetial);
|
|
|
|
+ }
|
|
|
|
+ return stockWaterdetialList;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+}
|