李林
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
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
/*
 *    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.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chinaztt.mes.basic.mapper.PartMapper;
import com.chinaztt.mes.common.gsm.in.InStorageService;
import com.chinaztt.mes.common.gsm.in.InStorageSingleAddModel;
import com.chinaztt.mes.common.gsm.out.OutStorageService;
import com.chinaztt.mes.common.gsm.out.OutStorageSingleAddModel;
import com.chinaztt.mes.warehouse.dto.EscortDTO;
import com.chinaztt.mes.warehouse.dto.SendBackDTO;
import com.chinaztt.mes.warehouse.entity.Escort;
import com.chinaztt.mes.warehouse.entity.EscortDetail;
import com.chinaztt.mes.warehouse.entity.SendBack;
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.SendBackService;
import com.chinaztt.mes.warehouse.util.StockUtils;
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 org.springframework.transaction.interceptor.TransactionAspectSupport;
 
import java.math.BigDecimal;
import java.util.List;
 
/**
 * 退货
 *
 * @author cxf
 * @date 2021-07-29 11:07:45
 */
@Slf4j
@Service
@AllArgsConstructor
@Transactional(rollbackFor = Exception.class)
public class SendBackServiceImpl extends ServiceImpl<SendBackMapper, SendBack> implements SendBackService {
    private EscortServiceImpl escortService;
    private EscortDetailMapper escortDetailMapper;
    private EscortMapper escortMapper;
    private StockUtils stockUtils;
    private StockMapper stockMapper;
    private PartMapper partMapper;
//    private ApplyService applyService;
 
 
 
//    @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(escortDetailDTO.getLocationId());
//                sendBackDTO.setStatus("02completed");
//                baseMapper.insert(sendBackDTO);
//
//                //进行移库  库位一样就增加数量   不然在这个库位新增数量  库位号 零件批号 系统编号确定唯一
//                Stock stock = stockMapper.selectById(escortDetailDTO.getStockId());
//                Long stockId = escortDetailDTO.getStockId();
//                if(!stock.getLocationId().equals(sendBackDTO.getSendBackLocationId())){
//                    stockUtils.updateById(escortDetailDTO.getStockId(), sendBackDTO.getSendBackQuantity(), BigDecimal.ZERO
//                            , escortDetailDTO.getEscortNo(), TransactionType.SALES_ORDER_RETURNS.getValue());
////                }else{ //新增库存记录
//                    Part  part  = partMapper.selectOne(Wrappers.<Part>lambdaQuery()
//                            .eq(Part::getPartNo,escortDetailDTO.getPartNo()));
//                    Stock stockNew = stockUtils.query(part.getId(), sendBackDTO.getSendBackLocationId(), escortDetailDTO.getPartBatchNo()
//                            ,escortDetailDTO.getSystemNo());
//                    stockId = stockNew.getId();
//                }
//                stockUtils.updateById(stockId, sendBackDTO.getSendBackQuantity(), BigDecimal.ZERO
//                        , escortDetailDTO.getEscortNo(), TransactionType.SALES_ORDER_RETURNS.getValue());
//
//                //生成 检测申请
//                ApplyDTO applyDTO = new ApplyDTO();
//                applyDTO.setApplyType(Apply.BACK_APPLY);
//                applyDTO.setRemark("退货报检:" + sendBackDTO.getEscortNo());
//
//                List<ApplyPartDTO> applyPartList = new ArrayList<ApplyPartDTO>();
//                ApplyPartDTO applyPartDTO = new ApplyPartDTO();
//                applyPartDTO.setSystemNo(escortDetailDTO.getSystemNo());
//                applyPartDTO.setPartNo(escortDetailDTO.getPartNo());
//                applyPartDTO.setPartName(escortDetailDTO.getPartName());
//                applyPartDTO.setOutBatchNo(escortDetailDTO.getPartBatchNo());
//                applyPartDTO.setQtyArrived(sendBackDTO.getSendBackQuantity().toPlainString());
//                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();
//    }
 
    @Override
    public IPage<SendBackDTO> getSendBackPage(Page page, QueryWrapper<SendBackDTO> gen) {
        return baseMapper.getSendBackPage(page,gen);
    }
    /**
     * 同步GSM入库单
     *
     * @param escort
     * @return
     */
    public long syncGsmIn(EscortDTO escort,BigDecimal number) {
        //补充入库记录
        InStorageService inStorageService = new InStorageService();
        InStorageSingleAddModel singleAddModel = new InStorageSingleAddModel();
        singleAddModel.setSalesServiceKey("5CEa1840tB4D8e83E1BA886FE42a60C7");
        singleAddModel.setContractid(escort.getEscortNo());
        singleAddModel.setContractInfoProductAutoID(escort.getEscortAutoId().toString());
        singleAddModel.setEntryPeople(escort.getSalesman());
        singleAddModel.setEntryPeopleWorkCode(escort.getSalesmanWorkCode());
        singleAddModel.setQuantity(number.toString());
        singleAddModel.setIsJosn("1");
 
        String res = inStorageService.getInStorageServiceSoap().singleAdd(singleAddModel);
        JSONObject obj = JSONObject.parseObject(res);
        int errorcode = obj.getJSONObject("miap_header").getIntValue("errorcode");
        if (errorcode == 0) {
            return obj.getJSONObject("miap_Body").getLong("AutoID");
        } else {
            String msg = obj.getJSONObject("miap_header").getString("errormsg");
            throw new RuntimeException("GSM入库记录创建失败,原因:" + msg);
        }
    }
    /**
     * 同步GSM出库单
     *
     * @param escort
     * @param escortDetail
     * @return
     */
    public long syncGsmOut(EscortDTO escort,EscortDetail escortDetail,BigDecimal number) {
        OutStorageService outStorageService = new OutStorageService();
        OutStorageSingleAddModel outSingleAddModel = new OutStorageSingleAddModel();
        outSingleAddModel.setSalesServiceKey("5CEa1840tB4D8e83E1BA886FE42a60C7");
        outSingleAddModel.setContractid(escort.getEscortNo());
        outSingleAddModel.setContractInfoProductAutoID(escort.getId().toString());
        outSingleAddModel.setQualityQueryID(null);
        outSingleAddModel.setInstorageAutoID(escortDetail.getEscortInAutoId().toString());
        outSingleAddModel.setEscortID(escort.getEscortAutoId().toString());
        outSingleAddModel.setEntryPeople(escort.getSalesman());
        outSingleAddModel.setEntryPeopleWorkCode(escort.getSalesmanWorkCode());
        outSingleAddModel.setQuantity(number.toString());
        outSingleAddModel.setIsJosn("1");
 
        String res = outStorageService.getOutStorageServiceSoap().singleAdd(outSingleAddModel);
        JSONObject obj = JSONObject.parseObject(res);
        int errorcode = obj.getJSONObject("miap_header").getIntValue("errorcode");
        if (errorcode == 0) {
            return obj.getJSONObject("miap_Body").getLong("AutoID");
        } else {
            String msg = obj.getJSONObject("miap_header").getString("errormsg");
            throw new RuntimeException("GSM出库记录创建失败,原因:" + msg);
        }
    }
    @Override
    public R updateSendBack(List<SendBack> sendBackList, String event) {
        String state = "CONFIRM";
        if (CollectionUtil.isNotEmpty(sendBackList)){
            for (SendBack sendBack:sendBackList){
                if (state.equals(event)){
                    if ("01draft".equals(sendBack.getStatus())){
                        sendBack.setStatus("02completed");
                        baseMapper.updateById(sendBack);
                    }
                }else {
                    if ("02completed".equals(sendBack.getStatus())){
                        sendBack.setStatus("01draft");
                        baseMapper.updateById(sendBack);
                    }
                }
            }
            try {
                //先根据退货明细查询发货明细和押运单
                EscortDetail escortDetail = escortDetailMapper.selectById(sendBackList.get(0).getEscortDetailId());
                EscortDTO escortDTO = new EscortDTO();
                if (escortDetail!=null){
                    Escort escort = escortMapper.selectById(escortDetail.getEscortId());
                    BeanUtils.copyProperties(escort,escortDTO);
                }
//                escortService.deleteOutStorage(escortDTO,escortDetail);
                escortService.deleteInStorage(escortDTO,escortDetail);
                BigDecimal number = BigDecimal.ZERO;
                for (SendBack sendBack:sendBackList){
                    if ("01draft".equals(sendBack.getStatus())){
                        number = number.add(sendBack.getSendBackQuantity());
                    }
                }
                if (number.compareTo(BigDecimal.ZERO)>0 && number.compareTo(escortDetail.getDeliveryQty())<0){
                    //调用GSM入库并同步到mes系统中
                    long inAutoId =syncGsmIn(escortDTO,number);
                    escortDetail.setEscortInAutoId(inAutoId);
                    escortDetailMapper.updateById(escortDetail);
                    //调用GSM的发货新增并把发货的id同步到mes系统中
                    long outAutoId = syncGsmOut(escortDTO,escortDetail,number);
                    escortDetail.setEscortOutAutoId(outAutoId);
                    escortDetailMapper.updateById(escortDetail);
                }
            } catch (Exception ex){
                log.error("updateSendBack error  :" + ex.getMessage());
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return R.failed(ex.getLocalizedMessage());
            }
        }
        return R.ok();
    }
}