李林
2023-10-07 658d4927d468c47208fd012d9128b09249c07eff
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
/*
 *    Copyright (c) 2018-2025, ztt All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: ztt
 */
package com.chinaztt.mes.warehouse.service.impl;
 
import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.druid.sql.visitor.functions.Insert;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.chinaztt.mes.common.numgen.NumberGenerator;
import com.chinaztt.mes.warehouse.dto.StockAddDTO;
import com.chinaztt.mes.warehouse.entity.ReturnDetail;
import com.chinaztt.mes.warehouse.entity.ReturnMain;
import com.chinaztt.mes.warehouse.entity.Stock;
import com.chinaztt.mes.warehouse.entity.StockInCode;
import com.chinaztt.mes.warehouse.mapper.ReturnDetailMapper;
import com.chinaztt.mes.warehouse.mapper.ReturnMainMapper;
import com.chinaztt.mes.warehouse.mapper.StockInCodeMapper;
import com.chinaztt.mes.warehouse.mapper.StockMapper;
import com.chinaztt.mes.warehouse.service.ReturnMainService;
import com.chinaztt.mes.warehouse.util.StockUtils;
import lombok.AllArgsConstructor;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.math.BigDecimal;
import java.util.List;
 
/**
 * 退库申请主表
 *
 * @author cxf
 * @date 2020-10-21 10:46:42
 */
@Service
@AllArgsConstructor
@Transactional(rollbackFor = Exception.class)
public class ReturnMainServiceImpl extends ServiceImpl<ReturnMainMapper, ReturnMain> implements ReturnMainService {
    private NumberGenerator<ReturnMain> numberGenerator;
    private ReturnDetailMapper returnDetailMapper;
    private StockMapper stockMapper;
    private StockUtils stockUtils;
    private ReturnMainMapper returnMainMapper;
    private StockInCodeMapper stockInCodeMapper;
 
    @Override
    public boolean save(ReturnMain returnMain) {
        returnMain.setReturnNo(numberGenerator.generateNumberWithPrefix(ReturnMain.DIGIT, ReturnMain.PREFIX, ReturnMain::getReturnNo));
        return SqlHelper.retBool(baseMapper.insert(returnMain));
    }
 
    @Override
    public boolean submit(Long mainId) {
        // 1.修改主表状态为已下发(状态机之后再调整)
        ReturnMain returnMain = new ReturnMain();
        returnMain.setId(mainId);
        returnMain.setStatus("已下发");
        baseMapper.updateById(returnMain);
        // 2.退库明细的确认数量默认为退库数量
        returnDetailMapper.updateDefaultConfirmQtyByMainId(mainId);
        return true;
    }
 
    @Override
    public boolean accomplish(Long mainId) {
        // 1.修改主表状态为已处理(状态机之后再调整)
        ReturnMain returnMain = new ReturnMain();
        returnMain.setId(mainId);
        returnMain.setStatus("已处理");
        baseMapper.updateById(returnMain);
        // 2.产生退库接收事务,库存数量增加
        List<ReturnDetail> returnDetails = returnDetailMapper.selectList(Wrappers.<ReturnDetail>query().lambda().eq(ReturnDetail::getReturnMainId, mainId));
        if (CollectionUtil.isNotEmpty(returnDetails)) {
            for (ReturnDetail returnDetail : returnDetails) {
                ReturnMain newReturnMain = returnMainMapper.selectById(returnDetail.getReturnMainId());
                StockInCode stockInCode = stockInCodeMapper.selectById(newReturnMain.getReturnType());
                Stock stock = stockMapper.selectById(returnDetail.getStockId());
                // 如果退库的零件不在原来的库位
                StockAddDTO stockAdd = new StockAddDTO();
                stockAdd.setPartsId(stock.getPartId());
                stockAdd.setNewLocationId(returnDetail.getLocationId());
                stockAdd.setNewPartBatchNo(stock.getPartBatchNo());
                stockAdd.setNewSystemNo(stock.getSystemNo());
                stockAdd.setReelNumber(stock.getReelNumber());
 
                Stock newStock = stockUtils.query(stockAdd);
                stockUtils.updateById(newStock.getId(), returnDetail.getConfirmQty(), BigDecimal.ZERO, BigDecimal.ZERO, BigDecimal.ZERO, newReturnMain.getReturnNo(), stockInCode.getStockCodeName());
            }
        }
        return true;
    }
}