自动化立体仓库 - WMS系统
zwl
2025-07-08 3ad65283c54059e0c61110589fb19398ac3525fe
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
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.common.SnowflakeIdWorker;
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.asrs.service.impl.DocTypeServiceImpl;
import com.zy.common.utils.HttpHandler;
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.Component;
import org.springframework.stereotype.Service;
 
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;
 
@Service
@Slf4j
public class CollectOrdersScheduler extends AbstractHandler<String> {
    @Value("${erp.address.URL}")
    private String URL;
    @Value("${erp.address.inaddress}")
    private String inaddress;
 
    @Autowired
    private OrderService orderService;
    @Autowired
    private DocTypeService docTypeService;
    @Autowired
    private SnowflakeIdWorker snowflakeIdWorker;
    @Autowired
    private MatService matService;
    @Autowired
    private OrderDetlService orderDetlService;
    @Autowired
    private ApiLogService apiLogService;
 
    /**
     * 轮询获取入库订单
     */
    @Scheduled(cron = "0/30 * * * * ? ")
    private synchronized void execute() {
        // 获取当前日期时间
        LocalDateTime now = LocalDateTime.now();
 
        // 减去一个月
        LocalDateTime oneMonthAgo = now.minusMonths(1);
 
        // 定义日期时间格式(HH表示24小时制)
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
 
        // 格式化日期时间
        String formattedDateTime = oneMonthAgo.format(formatter);
 
        HashMap<String ,Object> hashMap=new HashMap<>();
 
        HashMap<String, Object> map = new HashMap<>();
 
        map.put("orgNo", "2");//组织编号
//        map.put("docNo",0);
        map.put("startTime", formattedDateTime);//开始时间
 
        String format = now.format(formatter);
        map.put("endTime", format);//结束时间
 
        hashMap.put("condition",map);
 
        //查询订单
        String response = "";
        boolean success = false;
        try {
            response = new HttpHandler.Builder()
                    .setUri(URL)
                    .setPath(inaddress)
                    .setJson(JSON.toJSONString(hashMap))
                    .build()
                    .doPost();
            JSONObject jsonObject = JSON.parseObject(response);
            if (jsonObject.getInteger("status") == 200) {
                JSONArray response1 = JSON.parseArray(jsonObject.get("response").toString());
                for (int j = 0; j < response1.size(); j++) {
                    OrderResult orderResult = response1.getObject(j, OrderResult.class);
                    Order order = orderService.selectByNo(orderResult.getDocNo());//单据编号
                    if (Cools.isEmpty(order)) {
                        Date now1 = new Date();
                        DocType docType = docTypeService.selectOne(new EntityWrapper<DocType>().eq("memo",orderResult.getDocType()));//单据编号
                        // 单据主档
                        order = new Order(
                                String.valueOf(snowflakeIdWorker.nextId()),    // 编号[非空]
                                orderResult.getDocNo(),    // 订单编号
                                DateUtils.convert(now1),    // 单据日期
                                docType.getDocId(),    // 单据类型
                                null,    // 项目编号
                                null,    //
                                null,    // 调拨项目编号
                                null,    // 初始票据号
                                null,    // 票据号
                                null,    // 客户编号
                                null,    // 客户
                                null,    // 联系方式
                                null,    // 操作人员
                                null,    // 合计金额
                                null,    // 优惠率
                                null,    // 优惠金额
                                null,    // 销售或采购费用合计
                                null,    // 实付金额
                                null,    // 付款类型
                                null,    // 业务员
                                null,    // 结算天数
                                null,    // 邮费支付类型
                                null,    // 邮费
                                null,    // 付款时间
                                null,    // 发货时间
                                null,    // 物流名称
                                null,    // 物流单号
                                1L,    // 订单状态
                                1,    // 状态
                                9527L,    // 添加人员
                                now1,    // 添加时间
                                9527L,    // 修改人员
                                now1,    // 修改时间
                                null    // 备注
                        );
                        if (!orderService.insert(order)) {
                            throw new CoolException("生成单据失败,请联系管理员");
                        }
                    }
                    //物料编码
                    Mat mat = matService.selectByMatnr(orderResult.getItemNo());
                    if (Cools.isEmpty(mat)) {
                        Mat mat1=new Mat();
                        mat1.setTagId(1L);
                        mat1.setMatnr(orderResult.getItemNo());
                        mat1.setMaktx(orderResult.getItemName());
                        mat1.setSpecs(orderResult.getItemSpec());
                        matService.insert(mat1);
//                        throw new CoolException(orderResult.getItemNo() + "编号商品检索失败,请先添加商品");
                    }
                    OrderDetl orderDetl1 = orderDetlService.selectOne(new EntityWrapper<OrderDetl>()
                            .eq("order_no", orderResult.getDocNo())
                            .eq("matnr", orderResult.getItemNo())
                            .eq("batch", orderResult.getDocLotNo()));//批号
                    if(Cools.isEmpty(orderResult.getDocLotNo())){
                        orderDetl1 = orderDetlService.selectOne(new EntityWrapper<OrderDetl>()
                                .eq("order_no", orderResult.getDocNo())
                                .eq("matnr", orderResult.getItemNo()));
                    }
 
                    if (!Cools.isEmpty(orderDetl1)) {
                        continue;
                    }
                    OrderDetl orderDetl = new OrderDetl();
                    orderDetl.setMatnr(orderResult.getItemNo());
                    orderDetl.setMaktx(orderResult.getItemName());
                    orderDetl.setSpecs(orderResult.getItemSpec());
                    orderDetl.setBatch(orderResult.getDocLotNo());
                    orderDetl.setAnfme(orderResult.getApplyQty());
                    orderDetl.setOrderId(order.getId());
                    orderDetl.setOrderNo(order.getOrderNo());
                    orderDetl.setCreateBy(9527L);
                    orderDetl.setCreateTime(new Date());
                    orderDetl.setUpdateBy(9527L);
                    orderDetl.setUpdateTime(new Date());
                    orderDetl.setStatus(1);
                    orderDetl.setQty(0.0D);
 
                    //保存erp传过来的字段
                    orderDetl.setManu((orderResult.getOrgNo()));//组织编码
                    orderDetl.setModel(orderResult.getDocSeqNo());//项次--行号
                    orderDetl.setColor(orderResult.getDocWarehouseNo());//任务仓库
                    orderDetl.setBrand(orderResult.getDocCellNo());//任务储位
                    orderDetl.setUnit(orderResult.getUnitNo());//单位编号
                    orderDetl.setSku(orderResult.getUnitName());//单位名称
                    orderDetl.setItemNum(orderResult.getSupplierNo());//供应商编码
                    orderDetl.setOrigin(orderResult.getSupplierName());//供应商名称
 
                    if (!orderDetlService.insert(orderDetl)) {
                        throw new CoolException("生成单据明细失败,请联系管理员");
                    }
                }
            } else {
            }
        } catch (Exception e) {
            log.error("fail", e);
        } finally {
            try {
                // 保存接口日志
                apiLogService.save(
                        "轮询数据",
                        URL + inaddress,
                        null,
                        "127.0.0.1",
                        map.toString(),
                        response,
                        success
                );
            } catch (Exception e) {
                log.error("", e);
            }
        }
    }
 
}