luqingyang
2023-11-01 57f7298c3d870f0992444991cee670dfc1c51f42
consum-base/src/main/java/com/consum/base/service/LWhFormOutputCoreService.java
@@ -8,14 +8,13 @@
import com.consum.model.po.*;
import com.iplatform.model.po.S_user_core;
import com.walker.infrastructure.utils.CollectionUtils;
import com.walker.infrastructure.utils.DateUtils;
import com.walker.infrastructure.utils.NumberGenerator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
@@ -29,20 +28,38 @@
@Slf4j
@Service
public class LWhFormOutputCoreService {
    @Resource
    private BaseGoodsTemplateServiceImpl baseGoodsTemplateService;
    @Resource
    private LWhGoodsService lWhGoodsService;
    @Resource
    private CodeGeneratorService codeGeneratorService;
    @Resource
    private LWhProcureModelService lWhProcureModelService;
    @Resource
    private LWhFormTransferService lWhFormTransferService;
    @Resource
    private LWhFormOutputService lWhFormOutputService;
    @Resource
    private LWarehouseFlowService lWarehouseFlowService;
    private LWhGoodsRecordDetailsService lWhGoodsRecordDetailsService;
    private LWhGoodsRecordService lWhGoodsRecordService;
    @Autowired
    public LWhFormOutputCoreService(BaseGoodsTemplateServiceImpl baseGoodsTemplateService,
                                    LWhGoodsService lWhGoodsService,
                                    CodeGeneratorService codeGeneratorService,
                                    LWhProcureModelService lWhProcureModelService,
                                    LWhFormTransferService lWhFormTransferService,
                                    LWhFormOutputService lWhFormOutputService,
                                    LWarehouseFlowService lWarehouseFlowService,
                                    LWhGoodsRecordDetailsService lWhGoodsRecordDetailsService,
                                    LWhGoodsRecordService lWhGoodsRecordService) {
        this.baseGoodsTemplateService = baseGoodsTemplateService;
        this.lWhGoodsService = lWhGoodsService;
        this.codeGeneratorService = codeGeneratorService;
        this.lWhProcureModelService = lWhProcureModelService;
        this.lWhFormTransferService = lWhFormTransferService;
        this.lWhFormOutputService = lWhFormOutputService;
        this.lWarehouseFlowService = lWarehouseFlowService;
        this.lWhGoodsRecordDetailsService = lWhGoodsRecordDetailsService;
        this.lWhGoodsRecordService = lWhGoodsRecordService;
    }
    /**
     * 根据 调拨单 生成 出库单
@@ -51,7 +68,7 @@
     * @return 出库单Id
     */
    @Transactional(rollbackFor = Exception.class)
    public Long createOutFormByTransId(Long whFormTransferId, S_user_core currentUser) {
    public Long createOutFormByTransId(Long whFormTransferId, S_user_core currentUser, Long dealTime) {
        if (whFormTransferId == null) {
            return null;
        }
@@ -61,6 +78,12 @@
            log.error("调拨失败!请检查调拨单状态");
            return null;
        }
        // 查询该调拨单是否已经生成了出库单
        Long outWarehouseFormIdT = lWhFormOutputService.queyrIdByTransferId(whFormTransferId);
        if (outWarehouseFormIdT != null) {
            return outWarehouseFormIdT;
        }
        //申请调拨的物品
        List<LWhProcureModel> goodsModelNumList = lWhProcureModelService.getModelByForm(WhBusinessEnum.DIAOBO, whFormTransferId);
        if (CollectionUtils.isEmpty(goodsModelNumList)) {
@@ -71,36 +94,18 @@
        String outWarehouseName = lWhFormTransfer.getOutWarehouseName();
        // 出库单ID
        Long outWarehouseFormId = NumberGenerator.getLongSequenceNumberNano();
        outWarehouseFormIdT = NumberGenerator.getLongSequenceNumberNano();
        Long outWarehouseFormId = outWarehouseFormIdT;
        // 调拨单物品 已经按型号分好了
        goodsModelNumList.forEach(itemModelInfo -> {
            // 需要调拨的物品的某个型号
            Long baseGoodsModelsId = itemModelInfo.getBaseGoodsModelsId();
            // 需要调拨的物品某个型号的数量
            Integer counts = itemModelInfo.getCounts();
            //通过LockManage获得锁
            //因为是根据调拨生成的 所以此处不用校验库存
//            Object warehouseModelLockObj = LockManage.acquireLock(outWarehouseId, baseGoodsModelsId);
            //            synchronized (warehouseModelLockObj) {
            // 获得锁后查询该型号的期初数量
//                int goodsModelNum = lWhGoodsService.queryGoodsModelNum(outWarehouseId, baseGoodsModelsId, queryModelStatus, buyType);
//                if (goodsModelNum < counts) {
//                    log.error("出库失败!型号{} 的数量不足,需要调拨{}个,仓库中现存{}个", baseGoodsModelsId, counts, goodsModelNum);
//                    // 手动回滚
//                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
//                    throw new RuntimeException("出库失败!型号数量不足");
//                }
            // 复用对象
            itemModelInfo.setId(NumberGenerator.getLongSequenceNumber());
            itemModelInfo.setFromProcureGoodsId(null);
            itemModelInfo.setBusinessType(3);
            itemModelInfo.setBusinessId(outWarehouseFormId);
            itemModelInfo.setPrice(null);
//                itemModelInfo.setCounts(counts);
//                itemModelInfo.setWorehouseCount(goodsModelNum);
            itemModelInfo.setSupplier(null);
//            }
        });
        lWhProcureModelService.insertBatch(goodsModelNumList);
@@ -123,7 +128,6 @@
        whFormOutput.setAgencyName(null);
        whFormOutput.setOperatorId(currentUser.getId());
        whFormOutput.setOperatorName(currentUser.getNick_name());
        long dealTime = DateUtils.getDateTimeNumber(System.currentTimeMillis());
        whFormOutput.setDealTime(dealTime);
        whFormOutput.setStates(1);
@@ -141,10 +145,10 @@
     * 根据 出库单 出库
     *
     * @param outWarehouseFormId 出库单Id
     * @return
     * @return 流水记录总表ID
     */
    @Transactional(rollbackFor = Exception.class)
    public Long outFormByTransId(Long outWarehouseFormId, S_user_core currentUser) {
    public Long outFormByTransId(Long outWarehouseFormId, S_user_core currentUser, Long dealTime) {
        if (outWarehouseFormId == null) {
            return null;
        }
@@ -161,12 +165,13 @@
            return null;
        }
        // 要出物品的仓库id
        Integer outWarehouseType = lWhFormOutput.getOutWarehouseType();
        Long outWarehouseId = lWhFormOutput.getWarehouseId();
        String warehouseName = lWhFormOutput.getWarehouseName();
        Long userId = currentUser.getId();
        String nickName = currentUser.getNick_name();
        long dealTime = DateUtils.getDateTimeNumber(System.currentTimeMillis());
        // 流水记录总表ID
        long lWarehouseFlowId = NumberGenerator.getLongSequenceNumberNano();
        // 流水记录总表中 业务ID 。调拨单时  保存调拨单id  出库单时 保存出库单id
@@ -176,18 +181,30 @@
        short queryModelStatusT = 1;
        //采购方式(1:集采;2=自采)
        Short buyTypeT = null;
        Short modGoodsTransferingStatusT = null;
        String outputCode = lWhFormOutput.getOutputCode();
        if ((BaseWarehouseParam1.In_OutPutTypeEnum.Transfer_Output.getValue() + "").equals(outputCode)) {
            //调拨出库
            queryModelStatusT = 1;
            buyTypeT = 1;
            lWarehouseFlowBusinessId = lWhFormOutput.getTransBusinessId();
            modGoodsTransferingStatusT = 0;
        } else if ((BaseWarehouseParam1.In_OutPutTypeEnum.Dept_Lending_Out.getValue() + "").equals(outputCode)) {
            //  部门仓库 分发出库
//            queryModelStatusT = 2;
        } else if ((BaseWarehouseParam1.In_OutPutTypeEnum.Fragmentary_Output.getValue() + "").equals(outputCode)) {
            //  零星出库
            queryModelStatusT = 1;
            buyTypeT = null;
            modGoodsTransferingStatusT = 4;
        }
        short queryModelStatus = queryModelStatusT;
        Short buyType = buyTypeT;
        Short modGoodsTransferingStatus = modGoodsTransferingStatusT;
        // 接收方仓库信息
        Long inWarehouseId = lWhFormOutput.getInWarehouseId();
        String inWarehouseName = lWhFormOutput.getInWarehouseName();
        goodsModelNumList.forEach(itemModelInfo -> {
            // 需要调拨的物品的某个型号
@@ -225,10 +242,10 @@
            whGoodsRecord.setThisCount(counts);
            //通过LockManage获得锁
            Object warehouseModelLockObj = LockManage.acquireLock(outWarehouseId, baseGoodsModelsId);
            Object warehouseModelLockObj = LockManage.acquireLock(outWarehouseType, outWarehouseId, baseGoodsModelsId);
            synchronized (warehouseModelLockObj) {
                // 获得锁后查询该型号的期初数量
                int goodsModelNum = lWhGoodsService.queryGoodsModelNum(outWarehouseId, baseGoodsModelsId, queryModelStatus, buyType);
                int goodsModelNum = lWhGoodsService.queryGoodsModelNum(outWarehouseType, outWarehouseId, baseGoodsModelsId, queryModelStatus, buyType);
                if (goodsModelNum < counts) {
                    log.error("出库失败!型号{} 的数量不足,需要调拨{}个,仓库中现存{}个", baseGoodsModelsId, counts, goodsModelNum);
                    // 手动回滚
@@ -239,13 +256,12 @@
                whGoodsRecord.setEndCount(goodsModelNum - counts);
                //出库时候,按 先入库的先出库,同时入库的,按价格高的先出库
                List<Long> outGoodsId = lWhGoodsService.queryOutGoodsId(outWarehouseId, baseGoodsModelsId, queryModelStatus, buyType, goodsModelNum);
                outGoodsId.forEach(whGoodsId -> {
                });
                List<Long> outGoodsId = lWhGoodsService.queryOutGoodsId(outWarehouseType, outWarehouseId, baseGoodsModelsId, queryModelStatus, buyType, goodsModelNum);
                lWhGoodsService.modGoodsTransfering(outGoodsId, outWarehouseType, inWarehouseId, inWarehouseName, modGoodsTransferingStatus);
                // 批量插入 进出库流水明细[L_WH_GOODS_RECORD_DETAILS]
                lWhGoodsRecordDetailsService.sameGoodsInsertMore(outGoodsId, whGoodsRecordId, (short) 0);
                lWhGoodsRecordService.insert(whGoodsRecord);
            }
        });
        LWarehouseFlow warehouseFlow = new LWarehouseFlow();
        warehouseFlow.setId(lWarehouseFlowId);
@@ -261,8 +277,10 @@
        // 插入流水总表
        int flowInsertFlag = lWarehouseFlowService.insert(warehouseFlow);
        if (flowInsertFlag == 0) {
            return 0l;
            return null;
        }
        return 1l;
        return lWarehouseFlowId;
    }
}