Blame view

src/main/java/com/huaheng/pc/task/taskHeader/service/TaskBackQueue.java 9.64 KB
1
2
package com.huaheng.pc.task.taskHeader.service;
tongzhonghao authored
3
import com.alibaba.fastjson.JSON;
tongzhonghao authored
4
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
5
import com.huaheng.common.constant.QuantityConstant;
6
import com.huaheng.common.utils.StringUtils;
tongzhonghao authored
7
import com.huaheng.common.utils.Wrappers;
8
import com.huaheng.framework.web.domain.AjaxResult;
9
import com.huaheng.pc.receipt.receiptDetail.service.ReceiptDetailService;
tongzhonghao authored
10
import com.huaheng.pc.receipt.receiptHeader.domain.ReceiptHeader;
11
import com.huaheng.pc.receipt.receiptHeader.service.ReceiptHeaderService;
tongzhonghao authored
12
import com.huaheng.pc.shipment.shipmentDetail.service.ShipmentDetailService;
tongzhonghao authored
13
import com.huaheng.pc.shipment.shipmentHeader.domain.ShipmentHeader;
tongzhonghao authored
14
import com.huaheng.pc.shipment.shipmentHeader.service.ShipmentHeaderService;
15
16
import com.huaheng.pc.task.taskDetail.domain.TaskDetail;
import com.huaheng.pc.task.taskDetail.service.TaskDetailService;
17
import com.huaheng.pc.task.taskHeader.domain.TaskBackDomain;
18
19
20
21
22
23
24
25
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;
tongzhonghao authored
26
import java.util.stream.Collectors;
27
28
29
30
31
32
33
34
35

@Service
public class TaskBackQueue implements BackQueue{

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

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

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

                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())){
100
101
                        domain.setIsSuc(true).setMsg("无需回传").setBackNum(QuantityConstant.NOT_RETURN);
                        callBack.back(domain);
tongzhonghao authored
102
                        continue;
tongzhonghao authored
103
                    }
tongzhonghao authored
104
105
106
                    if(taskAllComplete(receiptHeader.getCode())){
                        continue;
                    }
tongzhonghao authored
107
                    receiptHeaderId = receiptHeader.getId().toString();
tongzhonghao authored
108
109
110
111
112
113
                    //获取未回传任务集
                    List<TaskDetail> unBackTask = getUnBackTask(receiptHeader.getCode());
                    if(unBackTask.size()==0){
                        continue;
                    }
                    Map<Integer, BigDecimal> taskFinishMaps = getTaskFinishMaps(unBackTask);
tongzhonghao authored
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
                    ajaxResult = receiptHeaderService.postBack(header,receiptHeaderId, taskFinishMaps);
                    switch (receiptHeader.getReceiptType()){
                        case "SI" :
                            receiptHeaderService.postBack2Mes(header,receiptHeaderId, taskFinishMaps);
                            break;
                    }
                    String msg = ajaxResult.getMsg();
                    List<Map> list1 = JSON.parseArray(msg, Map.class);
                    if (!ajaxResult.hasErr()) {
                        switch (receiptHeader.getReceiptType()){
                            case "PI" :
                                receiptHeaderService.postBack2SRM(receiptHeader,list1,taskFinishMaps);
                                break;
                        }
                    }
tongzhonghao authored
129
130
131
132
133
134
                    if(!ajaxResult.hasErr()){
                        for (TaskDetail taskDetail : unBackTask) {
                            taskDetail.setBack(1);
                        }
                        taskDetailService.updateBatchById(unBackTask);
                    }
135
                    backMsg.put(receiptHeader.getCode(),ajaxResult.getMsg());
tongzhonghao authored
136
                    type = 1;
137
138
139
                }
                break;
            case 200 :
tongzhonghao authored
140
                String shipmentHeaderId = "";
tongzhonghao authored
141
142
143
144
145
146
147
148
149
                //需回传的任务
                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())){
150
                        domain.setIsSuc(true).setMsg("无需回传").setBackNum(QuantityConstant.NOT_RETURN);
tongzhonghao authored
151
                        continue;
tongzhonghao authored
152
                    }
tongzhonghao authored
153
154
155
                    if(taskAllComplete(shipmentHeader.getCode())){
                        continue;
                    }
tongzhonghao authored
156
                    shipmentHeaderId = shipmentHeader.getId().toString();
tongzhonghao authored
157
158
159
160
161
162
                    //获取未回传任务集
                    List<TaskDetail> unBackTask = getUnBackTask(shipmentHeader.getCode());
                    if(unBackTask.size()==0){
                        continue;
                    }
                    Map<Integer, BigDecimal> taskFinishMaps = getTaskFinishMaps(unBackTask);
tongzhonghao authored
163
                    ajaxResult = shipmentHeaderService.postBack(header,shipmentHeaderId,taskFinishMaps);
tongzhonghao authored
164
165
166
167
168
169
                    if(!ajaxResult.hasErr()){
                        for (TaskDetail taskDetail : unBackTask) {
                            taskDetail.setBack(1);
                        }
                        taskDetailService.updateBatchById(unBackTask);
                    }
170
                    backMsg.put(shipmentHeader.getCode(),ajaxResult.getMsg());
tongzhonghao authored
171
                    type = 2;
tongzhonghao authored
172
                }
173
174
                break;
        }
tongzhonghao authored
175
176
177
        boolean flag = !ajaxResult.hasErr();
        String  message = ajaxResult.getMsg();
        Integer result = flag ?  QuantityConstant.ISRETURN_SUCC : QuantityConstant.ISRETURN_FAIL;
178
179
        domain.setIsSuc(flag).setMsg(message).setBackNum(result).setType(type).setMsgMaps(backMsg);
        callBack.back(domain);
180
    }
tongzhonghao authored
181
182
183
184
185
186

    private boolean taskAllComplete(String billCode){
        LambdaQueryWrapper<TaskDetail> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(TaskDetail::getBillCode,billCode)
                .lt(TaskDetail::getStatus,QuantityConstant.TASK_STATUS_COMPLETED);
        List<TaskDetail> taskDetails = taskDetailService.list(queryWrapper);
tongzhonghao authored
187
        return !taskDetails.isEmpty();
tongzhonghao authored
188
189
    }
tongzhonghao authored
190
    private Map<Integer, BigDecimal> getTaskFinishMaps(List<TaskDetail> taskDetails){
tongzhonghao authored
191
192
193
194
195
196
197
198
199
200
201
        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
202
203
204
205
206
207
208
209
210
    private List<TaskDetail> getUnBackTask(String billCode){
        LambdaQueryWrapper<TaskDetail> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(TaskDetail::getBillCode,billCode)
                .eq(TaskDetail::getStatus,QuantityConstant.TASK_STATUS_COMPLETED)
                .eq(TaskDetail::getBack,0);
        List<TaskDetail> taskDetails = taskDetailService.list(queryWrapper);
        return taskDetails;
    }
211
}