自动化立体仓库 - WMS系统
bfwms
2025-07-07 2fc66774335bfc603aa36b1ca41e5f1be19442b0
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
package com.zy.asrs.task;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.core.common.Cools;
import com.core.common.DateUtils;
import com.core.exception.CoolException;
import com.zy.asrs.entity.*;
import com.zy.asrs.entity.result.OrderResult;
import com.zy.asrs.service.*;
import com.zy.common.utils.HttpHandler;
import com.zy.system.entity.Config;
import com.zy.system.service.ConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
 
import java.util.*;
 
@Service
@Slf4j
public class ERPReportScheduler extends AbstractHandler<String> {
 
    @Value("${erp.address.URL}")
    private String URL;
    @Value("${erp.address.outReportAddress}")
    private String outReportAddress;
    @Value("${erp.address.inReportAddress}")
    private String inReportAddress;
    @Autowired
    private OrderDetlService orderDetlService;
    @Autowired
    private ApiLogService apiLogService;
    @Autowired
    private WrkMastService wrkMastService;
    @Autowired
    private ConfigService configService;
    @Autowired
    private WrkDetlService wrkDetlService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private DocTypeService docTypeService;
 
 
    /**
     * 单个任务上报erp
     */
    @Scheduled(cron = "0/10 * * * * ? ")
    private synchronized void execute() {
        //查找所有任务档任务状态为40ERP上报中的任务
        List<WrkMast> wrkMasts = wrkMastService.selectList(new EntityWrapper<WrkMast>().eq("wrk_sts", 40));
 
        //是否需要上报ERP
        Config config = configService.selectOne(new EntityWrapper<Config>().eq("code", "newErpReport"));
        if(!Cools.isEmpty(config)&&config.getValue().equals("Y")&&!Cools.isEmpty(wrkMasts)){
 
            for(WrkMast wrkMast:wrkMasts){
                List<WrkDetl> wrkDetls = wrkDetlService.selectByWrkNo(wrkMast.getWrkNo());
                if(wrkDetls!=null&&wrkDetls.size()>0){
                    List<LinkedHashMap<String,Object>> datas=new ArrayList<>();
                    for(WrkDetl wrkDetl:wrkDetls){
                        OrderDetl orderDetl= orderDetlService.selectItem(wrkDetl.getOrderNo(),wrkDetl.getMatnr(),wrkDetl.getBatch());
                        if(orderDetl==null){
                            log.error("orderNo={},matnr={},batch={},没有查询到订单明细",wrkDetl.getOrderNo(),wrkDetl.getMatnr(),wrkDetl.getBatch());
                            continue;
                        }
                        Order order= orderService.selectByNo(wrkDetl.getOrderNo());
                        if(order==null){
                            log.error("orderNo={},matnr={},batch={},没有查询到订单",wrkDetl.getOrderNo(),wrkDetl.getMatnr(),wrkDetl.getBatch());
                            continue;
                        }
                        DocType docType = docTypeService.selectById(order.getDocType());//单据编号
                        LinkedHashMap<String,Object> map=new LinkedHashMap<>();
                        map.put("orgNo",orderDetl.getManu());//组织编码
                        map.put("docNo",orderDetl.getOrderNo());
                        map.put("docType",docType.getMemo());
                        map.put("docSeqNo",orderDetl.getModel());
                        map.put("itemNo",orderDetl.getMatnr());
                        map.put("qty",wrkDetl.getAnfme());
                        map.put("unitNo",orderDetl.getUnit());
                        map.put("warehouseNo","02201");
                        map.put("cellNo",orderDetl.getBrand());
                        map.put("combinationLotNo",String.valueOf(orderDetl.getBatch()));
                        map.put("barcode",wrkDetl.getMatnr());
                        datas.add(map);
                    }
                    String path= "";
                    String work= "";
                    if(wrkMast.getIoType()<100){
                        path=inReportAddress;
                        work="入库";
                    }else{
                        path=outReportAddress;
                        work="出库";
                    }
                    path=outReportAddress;
                    HashMap<String,Object> map=new HashMap<>();
                    map.put("data",datas);
                    //上报ERP
                    String response = "";
                    boolean success = false;
                    try {
                        response = new HttpHandler.Builder()
                                .setUri(URL)
                                .setPath(path)
                                .setJson(JSON.toJSONString(map))
                                .build()
                                .doPost();
                        JSONObject jsonObject = JSON.parseObject(response);
                        if (jsonObject.getInteger("status") == 200) {
                            if(wrkMast.getIoType()<100){
                                wrkMast.setWrkSts(10L);//入库转历史档
                            }else{
                                wrkMast.setWrkSts(18L);//出库转历史档
                            }
                            wrkMastService.updateById(wrkMast);
                        } else {
                            log.error("任务号={},上报失败",wrkMast.getWrkNo());
                        }
                    } catch (Exception e) {
                        log.error("fail", e);
                    } finally {
                        try {
                            // 保存接口日志
                            apiLogService.save(
                                    "上报"+work+"任务结果给ERP",
                                    URL + path,
                                    null,
                                    "127.0.0.1",
                                    map.toString(),
                                    response,
                                    success
                            );
                        } catch (Exception e) {
                            log.error("", e);
                        }
                    }
                }else {
                    log.error("任务号:{},没有任务明细",wrkMast.getWrkNo());
                }
            }
        }
 
    }
}