李林
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
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
package com.chinaztt.mes.production.service.impl;
 
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.chinaztt.mes.basic.entity.Part;
import com.chinaztt.mes.basic.mapper.PartMapper;
import com.chinaztt.mes.production.service.CommonService;
import com.chinaztt.mes.quality.dto.ApplyDTO;
import com.chinaztt.mes.quality.dto.ApplyPartDTO;
import com.chinaztt.mes.quality.entity.Apply;
import com.chinaztt.mes.quality.service.ApplyService;
import com.chinaztt.mes.warehouse.dto.EscortDTO;
import com.chinaztt.mes.warehouse.dto.EscortDetailDTO;
import com.chinaztt.mes.warehouse.dto.SendBackDTO;
import com.chinaztt.mes.warehouse.dto.StockAddDTO;
import com.chinaztt.mes.warehouse.entity.Escort;
import com.chinaztt.mes.warehouse.entity.EscortDetail;
import com.chinaztt.mes.warehouse.entity.Stock;
import com.chinaztt.mes.warehouse.mapper.EscortDetailMapper;
import com.chinaztt.mes.warehouse.mapper.EscortMapper;
import com.chinaztt.mes.warehouse.mapper.SendBackMapper;
import com.chinaztt.mes.warehouse.mapper.StockMapper;
import com.chinaztt.mes.warehouse.service.impl.EscortServiceImpl;
import com.chinaztt.mes.warehouse.util.StockUtils;
import com.chinaztt.mes.warehouse.util.TransactionType;
import com.chinaztt.ztt.common.core.util.R;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
 
 
/**
 * @author zhangxy
 */
@Slf4j
@Service
@AllArgsConstructor
@Transactional(rollbackFor = Exception.class)
public class CommonServiceImpl implements CommonService {
 
    private EscortServiceImpl escortService;
    private EscortDetailMapper escortDetailMapper;
    private EscortMapper escortMapper;
    private StockUtils stockUtils;
    private StockMapper stockMapper;
    private PartMapper partMapper;
    private ApplyService applyService;
    private SendBackMapper sendBackMapper;
 
 
    @Override
    public R fullSave(SendBackDTO sendBackDTO) {
        if (CollectionUtil.isNotEmpty(sendBackDTO.getEscortList())) {
            for (EscortDetailDTO escortDetailDTO : sendBackDTO.getEscortList()) {
                sendBackDTO.setEscortDetailId(escortDetailDTO.getId());
                sendBackDTO.setPartNo(escortDetailDTO.getPartNo());
                sendBackDTO.setPartName(escortDetailDTO.getPartName());
                sendBackDTO.setPartBatchNo(escortDetailDTO.getPartBatchNo());
                sendBackDTO.setSystemNo(escortDetailDTO.getSystemNo());
                sendBackDTO.setPartUnit(escortDetailDTO.getPartUnit());
                sendBackDTO.setSendBackLocationId(sendBackDTO.getSendBackLocationId());
                sendBackDTO.setSendBackLocationName(sendBackDTO.getSendBackLocationName());
                sendBackDTO.setStatus("02completed");
                sendBackMapper.insert(sendBackDTO);
 
                //进行移库  库位一样就增加数量   不然在这个库位新增数量  库位号 零件批号 系统编号确定唯一
                Stock stock = stockMapper.selectById(escortDetailDTO.getStockId());
                Long stockId = escortDetailDTO.getStockId();
                List<Part> parts = partMapper.selectList(Wrappers.<Part>lambdaQuery()
                        .eq(Part::getPartNo, escortDetailDTO.getPartNo()).orderByDesc(Part::getId));
                if (CollectionUtil.isEmpty(parts)) {
                    throw new RuntimeException("缺少" + escortDetailDTO.getPartNo() + "的零件");
                }
                Part part = parts.get(0);
                if (null == sendBackDTO.getSendBackLocationId()) {
                    throw new RuntimeException("缺少库位");
                }
                if (!stock.getLocationId().equals(sendBackDTO.getSendBackLocationId())) {
//                    stockUtils.updateById(escortDetailDTO.getStockId(), sendBackDTO.getSendBackQuantity(), BigDecimal.ZERO
//                            , escortDetailDTO.getEscortNo(), TransactionType.SALES_ORDER_RETURNS.getValue());
//                }else{ //新增库存记录
                    StockAddDTO stockAdd = new StockAddDTO();
                    stockAdd.setPartsId(part.getId());
                    stockAdd.setNewLocationId(sendBackDTO.getSendBackLocationId());
                    stockAdd.setNewPartBatchNo(escortDetailDTO.getPartBatchNo());
                    stockAdd.setNewSystemNo(escortDetailDTO.getSystemNo());
                    stockAdd.setReelNumber(stock.getReelNumber());
 
                    Stock stockNew = stockUtils.query(stockAdd);
                    stockId = stockNew.getId();
                }
                stockUtils.updateById(stockId, sendBackDTO.getSendBackQuantity(), BigDecimal.ZERO, BigDecimal.ZERO, BigDecimal.ZERO
                        , escortDetailDTO.getEscortNo(), TransactionType.SALES_ORDER_RETURNS.getValue());
 
                //生成 检测申请
                ApplyDTO applyDTO = new ApplyDTO();
                applyDTO.setApplyType(Apply.BACK_APPLY);
                applyDTO.setRemark("退货报检");
 
                List<ApplyPartDTO> applyPartList = new ArrayList<ApplyPartDTO>();
                ApplyPartDTO applyPartDTO = new ApplyPartDTO();
                applyPartDTO.setSystemNo(escortDetailDTO.getSystemNo());
                applyPartDTO.setPartNo(escortDetailDTO.getPartNo());
                applyPartDTO.setPartName(escortDetailDTO.getPartName());
                applyPartDTO.setQtyArrived(sendBackDTO.getSendBackQuantity().toPlainString());
                applyPartDTO.setLotBatchNo(escortDetailDTO.getPartBatchNo());
                applyPartDTO.setPartId(part.getId());
                applyPartList.add(applyPartDTO);
                applyDTO.setApplyPartList(applyPartList);
                applyService.saveDto(applyDTO);
 
 
                //同步gsm 删除发货 入库
                EscortDetail escortDetail = escortDetailMapper.selectById(sendBackDTO.getEscortDetailId());
                EscortDTO escortDTO = new EscortDTO();
                if (escortDetail != null) {
                    Escort escort = escortMapper.selectById(escortDetail.getEscortId());
                    BeanUtils.copyProperties(escort, escortDTO);
                }
                try {
//                    escortService.deleteOutStorage(escortDTO, escortDetail);
                    escortService.deleteInStorage(escortDTO, escortDetail);
                } catch (Exception e) {
                    log.error("退货失败:" + e.getMessage());
                    log.error("退货失败:sendBackDTO" + sendBackDTO);
                    e.printStackTrace();
                }
            }
        } else {
            return R.failed("没有选择已完成发货的明细");
        }
        return R.ok();
    }
}