Blame view

src/main/java/com/huaheng/pc/task/taskHeader/service/TaskBackQueue.java 10.7 KB
1
2
package com.huaheng.pc.task.taskHeader.service;
tongzhonghao authored
3
import com.alibaba.fastjson.JSON;
tongzhonghao authored
4
5
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
tongzhonghao authored
6
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
7
import com.huaheng.common.constant.QuantityConstant;
8
import com.huaheng.common.utils.StringUtils;
tongzhonghao authored
9
import com.huaheng.common.utils.Wrappers;
10
import com.huaheng.framework.web.domain.AjaxResult;
11
import com.huaheng.pc.receipt.receiptDetail.service.ReceiptDetailService;
tongzhonghao authored
12
import com.huaheng.pc.receipt.receiptHeader.domain.ReceiptHeader;
13
import com.huaheng.pc.receipt.receiptHeader.service.ReceiptHeaderService;
tongzhonghao authored
14
import com.huaheng.pc.shipment.shipmentDetail.service.ShipmentDetailService;
tongzhonghao authored
15
import com.huaheng.pc.shipment.shipmentHeader.domain.ShipmentHeader;
tongzhonghao authored
16
import com.huaheng.pc.shipment.shipmentHeader.service.ShipmentHeaderService;
17
18
import com.huaheng.pc.task.taskDetail.domain.TaskDetail;
import com.huaheng.pc.task.taskDetail.service.TaskDetailService;
19
import com.huaheng.pc.task.taskHeader.domain.TaskBackDomain;
20
21
22
23
24
25
26
27
import com.huaheng.pc.task.taskHeader.domain.TaskHeader;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
童宗豪 authored
28
import java.util.Set;
tongzhonghao authored
29
import java.util.stream.Collectors;
30
31
32
33
34
35
36
37
38

@Service
public class TaskBackQueue implements BackQueue{

    @Resource
    private  ReceiptHeaderService receiptHeaderService;
    @Resource
    private ReceiptDetailService receiptDetailService;
    @Resource
tongzhonghao authored
39
40
41
42
    private ShipmentHeaderService shipmentHeaderService;
    @Resource
    private ShipmentDetailService shipmentDetailService;
    @Resource
43
44
45
46
47
48
    private  TaskHeaderService taskHeaderService;
    @Resource
    private TaskDetailService taskDetailService;

    @Override
    public void receiveBack(int taskId, CallBack callBack) {
49
50
51
52
53
        Map<String,String> backMsg = new HashMap<>();
        Integer type = null;
        TaskBackDomain domain = new TaskBackDomain().setIsSuc(false)
                .setBackNum(QuantityConstant.ISRETURN_FAIL);
54
        AjaxResult ajaxResult = AjaxResult.success();
55
        if(StringUtils.isNull(taskId)){
56
57

            callBack.back(domain.setMsg("任务id不能为空"));
58
59
60
61
            return;
        }
        TaskHeader header = taskHeaderService.getById(taskId);
        if(header == null){
62
            callBack.back(domain.setMsg("找不到任务id为:"+taskId+"的任务"));
63
64
            return;
        }
65
66
67
68
69
70
71
72
        switch (header.getTaskType()){
            case 500 :
            case 600 :
            case 800 :
            case 900 :
            case 1000 :
            case 1100 :
            case 1200 :
73
74
                domain.setBackNum(QuantityConstant.NOT_RETURN).setMsg("无需回传").setIsSuc(true);
                callBack.back(domain);
75
76
                return;
        }
77
78
        List<TaskDetail> taskDetails = taskDetailService.findByTaskId(taskId);
        if(taskDetails==null || taskDetails.size()==0){
79
            callBack.back(domain.setMsg("找不到任务id为:"+taskId+"的任务,没有任务明细"));
80
81
            return;
        }
tongzhonghao authored
82
83
84
        List<String> billCodes = taskDetails.stream().map(TaskDetail::getBillCode).collect(Collectors.toList());
        Map<Integer, BigDecimal> detailsQtyMap =new HashMap<>();
        for (TaskDetail taskDetail : taskDetails) {
tongzhonghao authored
85
86
87
88
89
90
            BigDecimal bigDecimal = detailsQtyMap.get(taskDetail.getBillDetailId());
            if(bigDecimal!=null){
                detailsQtyMap.put(taskDetail.getBillDetailId(),bigDecimal.add(taskDetail.getQty()));
            }else{
                detailsQtyMap.put(taskDetail.getBillDetailId(),taskDetail.getQty());
            }
tongzhonghao authored
91
        }
92
93
        switch (header.getInternalTaskType()){
            case 100 :
tongzhonghao authored
94
                String receiptHeaderId = "";
tongzhonghao authored
95
96
97
98
99
100
101
102

                LambdaQueryWrapper<ReceiptHeader> queryWrapper = Wrappers.lambdaQuery();
                queryWrapper.in(ReceiptHeader::getCode,billCodes)
                        .eq(ReceiptHeader::getWarehouseCode,header.getWarehouseCode());
                //获取任务中的入库单
                List<ReceiptHeader> list = receiptHeaderService.list(queryWrapper);
                for (ReceiptHeader receiptHeader : list) {
                    if(StringUtils.isEmpty(receiptHeader.getReferCode())){
103
104
                        domain.setIsSuc(true).setMsg("无需回传").setBackNum(QuantityConstant.NOT_RETURN);
                        callBack.back(domain);
tongzhonghao authored
105
                        continue;
tongzhonghao authored
106
                    }
xumiao authored
107
108
109
                    if(taskAllComplete(receiptHeader.getCode(),receiptHeader.getWarehouseCode())){
                        continue;
                    }
童宗豪 authored
110
                    receiptHeaderId = receiptHeader.getId().toString();
童宗豪 authored
111
//                    //获取未回传任务集
xumiao authored
112
113
114
115
116
117
                    List<TaskDetail> unBackTask = getUnBackTask(receiptHeader.getCode(),receiptHeader.getWarehouseCode());
                    if(unBackTask.size()==0){
                        continue;
                    }
                    Map<Integer, BigDecimal> taskFinishMaps = getTaskFinishMaps(unBackTask);
                    ajaxResult = receiptHeaderService.postBack(header,receiptHeaderId, taskFinishMaps);
tongzhonghao authored
118
119
                    switch (receiptHeader.getReceiptType()){
                        case "SI" :
xumiao authored
120
                            receiptHeaderService.postBack2Mes(header,receiptHeaderId, taskFinishMaps);
tongzhonghao authored
121
122
                            break;
                    }
tongzhonghao authored
123
                    if (!ajaxResult.hasErr() && QuantityConstant.PLATFORM_SRM.equals(receiptHeader.getSourcePlatform())) {
tongzhonghao authored
124
125
                        switch (receiptHeader.getReceiptType()){
                            case "PI" :
126
                            case "PII" :
tongzhonghao authored
127
128
129
130
                                String msg = ajaxResult.getMsg();
                                Object jsonObject = JSON.parseObject(msg);
                                 if (jsonObject instanceof JSONArray) {
                                     List<Map> list1 = JSON.parseArray(msg, Map.class);
xumiao authored
131
                                    receiptHeaderService.postBack2SRM(receiptHeader,list1,taskFinishMaps);
tongzhonghao authored
132
                                }
tongzhonghao authored
133
134
135
                                break;
                        }
                    }
tongzhonghao authored
136
                    if(!ajaxResult.hasErr()){
童宗豪 authored
137
                        //获取任务明细中当前包含当前入库编码的 任务明细
xumiao authored
138
                        for (TaskDetail taskDetail : unBackTask) {
tongzhonghao authored
139
140
                            taskDetail.setBack(1);
                        }
141
xumiao authored
142
                        taskDetailService.updateBatchById(unBackTask);
tongzhonghao authored
143
                    }
144
                    backMsg.put(receiptHeader.getCode(),ajaxResult.getMsg());
tongzhonghao authored
145
                    type = 1;
146
147
148
                }
                break;
            case 200 :
tongzhonghao authored
149
                String shipmentHeaderId = "";
tongzhonghao authored
150
151
152
153
154
155
156
157
158
                //需回传的任务
                LambdaQueryWrapper<ShipmentHeader> queryWrapper1 = Wrappers.lambdaQuery();
                queryWrapper1.in(ShipmentHeader::getCode,billCodes)
                                .eq(ShipmentHeader::getWarehouseCode,header.getWarehouseCode());
                //获取任务中的出库单
                List<ShipmentHeader> list1 = shipmentHeaderService.list(queryWrapper1);
                //遍历回传每个出库单
                for (ShipmentHeader shipmentHeader : list1) {
                    if(StringUtils.isEmpty(shipmentHeader.getReferCode())){
159
                        domain.setIsSuc(true).setMsg("无需回传").setBackNum(QuantityConstant.NOT_RETURN);
tongzhonghao authored
160
                        continue;
tongzhonghao authored
161
                    }
xumiao authored
162
163
164
                    if(taskAllComplete(shipmentHeader.getCode(),shipmentHeader.getWarehouseCode())){
                        continue;
                    }
童宗豪 authored
165
                    shipmentHeaderId = shipmentHeader.getId().toString();
童宗豪 authored
166
//                    //获取未回传任务集
xumiao authored
167
168
169
170
171
172
                    List<TaskDetail> unBackTask = getUnBackTask(shipmentHeader.getCode(),shipmentHeader.getWarehouseCode());
                    if(unBackTask.size()==0){
                        continue;
                    }
                    Map<Integer, BigDecimal> taskFinishMaps = getTaskFinishMaps(unBackTask);
                    ajaxResult = shipmentHeaderService.postBack(header,shipmentHeaderId,taskFinishMaps);
童宗豪 authored
173
                    //获取任务明细中当前包含当前入库编码的 任务明细
xumiao authored
174
//                    List<TaskDetail> details = taskDetails.stream().filter(t -> shipmentHeader.getCode().equals(t.getBillCode())).collect(Collectors.toList());
tongzhonghao authored
175
                    if(!ajaxResult.hasErr()){
xumiao authored
176
                        for (TaskDetail taskDetail : unBackTask) {
tongzhonghao authored
177
178
                            taskDetail.setBack(1);
                        }
xumiao authored
179
                        taskDetailService.updateBatchById(unBackTask);
tongzhonghao authored
180
                    }
181
                    backMsg.put(shipmentHeader.getCode(),ajaxResult.getMsg());
tongzhonghao authored
182
                    type = 2;
tongzhonghao authored
183
                }
184
                break;
185
            default:
186
        }
tongzhonghao authored
187
188
189
        boolean flag = !ajaxResult.hasErr();
        String  message = ajaxResult.getMsg();
        Integer result = flag ?  QuantityConstant.ISRETURN_SUCC : QuantityConstant.ISRETURN_FAIL;
190
191
        domain.setIsSuc(flag).setMsg(message).setBackNum(result).setType(type).setMsgMaps(backMsg);
        callBack.back(domain);
192
    }
tongzhonghao authored
193
tongzhonghao authored
194
    private boolean taskAllComplete(String billCode,String warehouseCode){
tongzhonghao authored
195
196
        LambdaQueryWrapper<TaskDetail> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(TaskDetail::getBillCode,billCode)
tongzhonghao authored
197
                .eq(TaskDetail::getWarehouseCode,warehouseCode)
tongzhonghao authored
198
199
                .lt(TaskDetail::getStatus,QuantityConstant.TASK_STATUS_COMPLETED);
        List<TaskDetail> taskDetails = taskDetailService.list(queryWrapper);
tongzhonghao authored
200
        return !taskDetails.isEmpty();
tongzhonghao authored
201
202
    }
tongzhonghao authored
203
    private Map<Integer, BigDecimal> getTaskFinishMaps(List<TaskDetail> taskDetails){
tongzhonghao authored
204
205
206
207
208
209
210
211
212
213
214
        Map<Integer, BigDecimal> map = new HashMap<>();
        for (TaskDetail taskDetail : taskDetails) {
            if(StringUtils.isNull(map.get(taskDetail.getBillDetailId()))){
                map.put(taskDetail.getBillDetailId(),taskDetail.getQty());
            }else{
                map.put(taskDetail.getBillDetailId(),map.get(taskDetail.getBillDetailId()).add(taskDetail.getQty()));
            }
        }
        return map;
    }
tongzhonghao authored
215
    private List<TaskDetail> getUnBackTask(String billCode,String warehouseCode){
tongzhonghao authored
216
217
        LambdaQueryWrapper<TaskDetail> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(TaskDetail::getBillCode,billCode)
tongzhonghao authored
218
                .eq(TaskDetail::getWarehouseCode,warehouseCode)
tongzhonghao authored
219
220
221
222
223
224
                .eq(TaskDetail::getStatus,QuantityConstant.TASK_STATUS_COMPLETED)
                .eq(TaskDetail::getBack,0);
        List<TaskDetail> taskDetails = taskDetailService.list(queryWrapper);
        return taskDetails;
    }
225
}