WangHan
2025-04-02 a8ba678a3fe5a39da2c732014cebbb66e408e97c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
package com.consum.base.service.core;
 
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
 
import com.consum.base.core.CodeGeneratorEnum;
import com.consum.base.core.CodeGeneratorService;
import com.consum.base.core.WhBusinessEnum;
import com.consum.base.core.type.InAndOutPutType;
import com.consum.base.core.type.OutPutStatesType;
import com.consum.base.core.utils.IdUtil;
import com.consum.base.core.utils.LockManage;
import com.consum.base.service.BaseGoodsTemplateService;
import com.consum.base.service.LWarehouseFlowService;
import com.consum.base.service.LWhFormOutputService;
import com.consum.base.service.LWhGoodsRecordService;
import com.consum.base.service.LWhGoodsService;
import com.consum.base.service.LWhProcureModelService;
import com.consum.base.service.impl.LWhGoodsRecordDetailsService;
import com.consum.model.po.LWarehouseFlow;
import com.consum.model.po.LWhFormOutput;
import com.consum.model.po.LWhFormTransfer;
import com.consum.model.po.LWhGoodsRecord;
import com.consum.model.po.LWhProcureModel;
import com.iplatform.model.po.S_user_core;
import com.walker.infrastructure.utils.CollectionUtils;
import com.walker.infrastructure.utils.NumberGenerator;
 
import cn.hutool.core.convert.Convert;
import lombok.extern.slf4j.Slf4j;
 
/**
 * @ClassName WhFormOutputCoreService
 * @Date 2023/10/26
 * @Description
 * @Version 1.0
 **/
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
public class LWhFormOutputCoreService {
 
    private BaseGoodsTemplateService baseGoodsTemplateService;
    private LWhGoodsService lWhGoodsService;
    private CodeGeneratorService codeGeneratorService;
    private LWhProcureModelService lWhProcureModelService;
    private LWhFormOutputService lWhFormOutputService;
    private LWhWarningCoreService lWhWarningCoreService;
    private LWarehouseFlowService lWarehouseFlowServiceImpl;
    private LWhGoodsRecordDetailsService lWhGoodsRecordDetailsService;
    private LWhGoodsRecordService lWhGoodsRecordServiceImpl;
 
    @Autowired
    public LWhFormOutputCoreService(BaseGoodsTemplateService baseGoodsTemplateService, LWhGoodsService lWhGoodsService,
        CodeGeneratorService codeGeneratorService, LWhProcureModelService lWhProcureModelService,
        LWhFormOutputService lWhFormOutputService, LWarehouseFlowService lWarehouseFlowServiceImpl,
        LWhGoodsRecordDetailsService lWhGoodsRecordDetailsService, LWhGoodsRecordService lWhGoodsRecordServiceImpl,
        LWhWarningCoreService lWhWarningCoreService) {
        this.baseGoodsTemplateService = baseGoodsTemplateService;
        this.lWhGoodsService = lWhGoodsService;
        this.codeGeneratorService = codeGeneratorService;
        this.lWhProcureModelService = lWhProcureModelService;
        this.lWhFormOutputService = lWhFormOutputService;
        this.lWarehouseFlowServiceImpl = lWarehouseFlowServiceImpl;
        this.lWhGoodsRecordDetailsService = lWhGoodsRecordDetailsService;
        this.lWhGoodsRecordServiceImpl = lWhGoodsRecordServiceImpl;
        this.lWhWarningCoreService = lWhWarningCoreService;
    }
 
    /**
     * 根据 调拨单 生成 出库单
     *
     * @param whFormTransferId 调拨单id
     * @return 出库单Id
     */
    @Transactional(rollbackFor = Exception.class)
    public Long createOutFormByTransId(Long whFormTransferId, WhBusinessEnum businessEnum, S_user_core currentUser,
        Long dealTime) {
        if (whFormTransferId == null) {
            return null;
        }
        LWhFormTransfer lWhFormTransfer = new LWhFormTransfer(whFormTransferId);
        lWhFormTransfer = lWhFormOutputService.get(lWhFormTransfer);
        if (lWhFormTransfer == null || lWhFormTransfer.getStates() != 0) {
            log.error("调拨失败!请检查调拨单状态");
            return null;
        }
        // 查询该调拨单是否已经生成了出库单
        Long outWarehouseFormId = lWhFormOutputService.queyrIdByTransferId(whFormTransferId);
        if (outWarehouseFormId != null) {
            return outWarehouseFormId;
        }
        // 出库单ID
        outWarehouseFormId = IdUtil.generateId();
 
        // 申请调拨的物品
        List<LWhProcureModel> goodsModelNumList = lWhProcureModelService.getModelByForm(businessEnum, whFormTransferId);
        if (CollectionUtils.isEmpty(goodsModelNumList)) {
            log.error("没有要入库的物品");
            throw new RuntimeException("没有要入库的物品");
        }
        Long outWarehouseId = lWhFormTransfer.getOutWarehouseId();
        String outWarehouseName = lWhFormTransfer.getOutWarehouseName();
 
        // 调拨单物品 已经按型号分好了
        for (LWhProcureModel itemModelInfo : goodsModelNumList) {
            // 复用对象
            itemModelInfo.setId(IdUtil.generateId());
            itemModelInfo.setFromProcureGoodsId(null);
            itemModelInfo.setBusinessType(3);
            itemModelInfo.setBusinessId(outWarehouseFormId);
            itemModelInfo.setPrice(null);
            itemModelInfo.setSupplier(null);
        }
        lWhProcureModelService.insertBatch(goodsModelNumList);
 
        // //创建出库单
        LWhFormOutput whFormOutput = new LWhFormOutput();
        whFormOutput.setId(outWarehouseFormId);
        // 此时并没有出库
        whFormOutput.setWarehouseFlowId(null);
        whFormOutput
            .setBusinessFormCode(codeGeneratorService.createBusinessFormCode(CodeGeneratorEnum.OutPut_Warehouse));
        // 进出库流水总表 入库单ID
        whFormOutput.setInWarehouseFlowId(null);
        // 调拨单ID(调拨时需要)
        whFormOutput.setTransBusinessId(whFormTransferId);
        whFormOutput.setOutputCode(InAndOutPutType.Transfer_Output.getValue() + "");
        whFormOutput.setOutputName("调拨出库");
        whFormOutput.setWarehouseId(outWarehouseId);
        whFormOutput.setWarehouseName(outWarehouseName);
        // 这样就查询不到该条记录
        whFormOutput.setAgencyId(null);
        whFormOutput.setAgencyName(null);
        whFormOutput.setOperatorId(currentUser.getId());
        whFormOutput.setOperatorName(currentUser.getNick_name());
 
        whFormOutput.setDealTime(dealTime);
        whFormOutput.setStates(OutPutStatesType.OUT_PENDING.getValue());
        int insert = lWhFormOutputService.insert(whFormOutput);
        if (insert == 0) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return null;
        }
 
        return outWarehouseFormId;
    }
 
    /**
     * 根据 出库单 出库
     *
     * @param outWarehouseFormId 出库单Id
     * @return 流水记录总表ID
     */
    @Transactional(rollbackFor = Exception.class)
    public Long outFormByTransId(Long outWarehouseFormId, WhBusinessEnum businessType, S_user_core currentUser,
        Long dealTime, Long whFormTransferId) {
        if (outWarehouseFormId == null) {
            return null;
        }
        LWhFormOutput lWhFormOutput = new LWhFormOutput(outWarehouseFormId);
        lWhFormOutput = lWhFormOutputService.get(lWhFormOutput);
        if (lWhFormOutput == null || lWhFormOutput.getStates() != 1) {
            log.error("出库失败!请检查出库单状态");
            return null;
        }
        // 申请调拨的物品
        List<LWhProcureModel> goodsModelNumList =
            lWhProcureModelService.getModelByForm(WhBusinessEnum.CHUKU, outWarehouseFormId);
        if (CollectionUtils.isEmpty(goodsModelNumList)) {
            log.error("没有要出库的物品");
            return null;
        }
        // 要出物品的仓库id
        Integer outWarehouseType = lWhFormOutput.getOutWarehouseType();
        Long outWarehouseId = lWhFormOutput.getWarehouseId();
        String warehouseName = lWhFormOutput.getWarehouseName();
        Integer inWarehouseType = lWhFormOutput.getInWarehouseType();
        Long inWarehouseId = lWhFormOutput.getInWarehouseId();
        String inWarehouseName = lWhFormOutput.getInWarehouseName();
 
        Long userId = currentUser.getId();
        String nickName = currentUser.getNick_name();
 
        // 流水记录总表ID
        long lWarehouseFlowId = IdUtil.generateId();
        long lWarehouseFlowBusinessId = outWarehouseFormId;
        Integer queryModelStatus = 1;
        Integer buyType = null;
        Integer modGoodsTransferStatus = null;
 
        Integer businessTypeValue = businessType.getValue();
        if (businessTypeValue == InAndOutPutType.Transfer_Output.getValue()) {
            // 调拨出库
            queryModelStatus = 0;
            buyType = 1;
            lWarehouseFlowBusinessId = lWhFormOutput.getTransBusinessId();
            modGoodsTransferStatus = 0;
        } else if (businessTypeValue == InAndOutPutType.Dept_Lending_Out.getValue()) {
            // 部门仓库 分发出库
            lWarehouseFlowBusinessId = lWhFormOutput.getTransBusinessId();
        } else if (businessTypeValue == InAndOutPutType.Fragmentary_Output.getValue()
            || businessTypeValue == InAndOutPutType.Inventory_Output.getValue()) {
            // 零星出库 或 盘点出库
            queryModelStatus = 1;
            modGoodsTransferStatus = 4;
        }
 
        ArrayList<Long> allChangModelList = new ArrayList<>();
        for (LWhProcureModel itemModelInfo : goodsModelNumList) {
            // 需要调拨的物品的某个型号
            Long baseGoodsModelsId = itemModelInfo.getBaseGoodsModelsId();
 
            // TODO 为空时不加入更新列表
            if (baseGoodsModelsId != null) {
                allChangModelList.add(baseGoodsModelsId);
            }
            // 需要调拨的物品某个型号的数量
            Integer counts = itemModelInfo.getCounts();
 
            // 查询型号信息
            Map<String, Object> tempGoodsInfo = baseGoodsTemplateService.queryGoodsInfoByModelId(baseGoodsModelsId);
 
            Long tempGoodsId = (Long)tempGoodsInfo.get("goodsid");
            String goodsName = (String)tempGoodsInfo.get("goodsname");
            String modelName = (String)tempGoodsInfo.get("modelname");
 
            // 插入 各规格物品的进出库记录 L_WH_GOODS_RECORD
            long whGoodsRecordId = IdUtil.generateId();
            LWhGoodsRecord whGoodsRecord = new LWhGoodsRecord();
            whGoodsRecord.setId(whGoodsRecordId);
            whGoodsRecord.setWarehouseId(outWarehouseId);
            whGoodsRecord.setWarehouseFlowId(lWarehouseFlowId);
            whGoodsRecord.setBaseGoodsTemplateId(tempGoodsId);
            whGoodsRecord.setGoodsTemplateName(goodsName);
            whGoodsRecord.setBaseGoodsModelsId(baseGoodsModelsId);
            whGoodsRecord.setBaseGoodsModelsName(modelName);
            whGoodsRecord.setDealTime(dealTime);
            // 本次调整类型 1=调增;2=调减
            whGoodsRecord.setThisType(2);
            whGoodsRecord.setThisCount(counts);
 
            // 通过LockManage获得锁
            Object warehouseModelLockObj = LockManage.acquireLock(outWarehouseType, outWarehouseId, baseGoodsModelsId);
            List<Long> outGoodsId = null;
            synchronized (warehouseModelLockObj) {
                // 获得锁后查询该型号的期初数量
                List<Long> warehouseIds = new ArrayList<>(Arrays.asList(outWarehouseId));
                int goodsModelNum = lWhGoodsService.queryGoodsModelNum(outWarehouseType, warehouseIds,
                    baseGoodsModelsId, queryModelStatus, buyType);
                if (goodsModelNum < counts) {
                    log.error("出库失败!型号{} 的数量不足,需要调拨{}个,仓库中现存{}个", baseGoodsModelsId, counts, goodsModelNum);
                    // 手动回滚
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    throw new RuntimeException("出库失败!型号数量不足");
                }
                whGoodsRecord.setInitialCount(goodsModelNum);
                whGoodsRecord.setEndCount(goodsModelNum - counts);
 
                // 出库时候,按 先入库的先出库,同时入库的,按价格高的先出库
                outGoodsId = lWhGoodsService.queryOutGoodsId(outWarehouseType, outWarehouseId, baseGoodsModelsId,
                    queryModelStatus, buyType, counts);
                lWhGoodsService.modGoodsTransfering(outGoodsId, inWarehouseType, inWarehouseId, inWarehouseName,
                    modGoodsTransferStatus);
                // 批量插入 进出库流水明细[L_WH_GOODS_RECORD_DETAILS]
                lWhGoodsRecordDetailsService.sameGoodsInsertMore(outGoodsId, whGoodsRecordId, (short)0);
            }
            long totalAmount = lWhGoodsService.queryGoodsPriceById(outGoodsId);
            LWhProcureModel lWhProcureModel = new LWhProcureModel(itemModelInfo.getId());
            whGoodsRecord.setTotalPrice(totalAmount);
            lWhGoodsRecordServiceImpl.insert(whGoodsRecord);
            lWhProcureModel.setTotalAmount(totalAmount);
            lWhProcureModelService.update(lWhProcureModel);
        }
        LWarehouseFlow warehouseFlow = new LWarehouseFlow();
        warehouseFlow.setId(lWarehouseFlowId);
        warehouseFlow.setWarehouseId(outWarehouseId);
        warehouseFlow.setWarehouseName(warehouseName);
        warehouseFlow.setThisType(2);
        warehouseFlow.setBusinessType(businessType.getValue());
 
        warehouseFlow.setBusinessFormId(whFormTransferId == null ? lWarehouseFlowBusinessId : whFormTransferId);
        warehouseFlow.setOperatorId(userId);
        warehouseFlow.setOperatorName(nickName);
        warehouseFlow.setDealTime(dealTime);
        // 插入流水总表
        int flowInsertFlag = lWarehouseFlowServiceImpl.insert(warehouseFlow);
        if (flowInsertFlag == 0) {
            return null;
        }
        // 向出库单 插入 进出库流水总表ID
        lWhFormOutput = new LWhFormOutput(outWarehouseFormId);
        lWhFormOutput.setStates(OutPutStatesType.OUT_SUCCESS.getValue());
        lWhFormOutput.setWarehouseFlowId(lWarehouseFlowId);
        lWhFormOutputService.update(lWhFormOutput);
 
        // 当库存变动时调用该方法
        if (outWarehouseType == 0) {
            lWhWarningCoreService.updateKuCun(Convert.toShort(outWarehouseType, (short)0), outWarehouseId,
                allChangModelList, null, dealTime);
        }
        return lWarehouseFlowId;
    }
 
}