郑永安
2023-06-19 f65443d8abeaedc9d102324565e8368e7c9d90c8
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
package com.gk.firework.Scheduls.DL;
 
import com.alibaba.fastjson.JSONObject;
import com.gk.firework.Domain.CustomerInfo;
import com.gk.firework.Domain.Enterprise;
import com.gk.firework.Domain.SaleOrderDetailInfo;
import com.gk.firework.Domain.SaleOrderInfo;
import com.gk.firework.Domain.Utils.BeanUtils;
import com.gk.firework.Domain.Utils.HttpUtils;
import com.gk.firework.Domain.Utils.StringUtils;
import com.gk.firework.Service.*;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
 
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.io.IOException;
import java.util.*;
 
/**
 * @author : jingjy
 * @date : 2022/1/26 17:15
 */
@Configuration
@EnableScheduling
//@ConditionalOnProperty(prefix = "scheduling",name = "enabled",havingValue = "true")
public class ReportOrderTask {
    private Logger logger = LogManager.getLogger(com.gk.firework.Scheduls.WarningTask.StockWarnTask.class);
    private static final String url = "http://118.190.117.180:8888";
    @Value("${com.gk.firework.schedules.single.switch}")
    private Boolean switchBtn;
    @Autowired
    EnterpriseService enterpriseService;
    @Autowired
    SaleOrderService saleOrderService;
    @Autowired
    SaleOrderDetailService saleOrderDetailService;
    @Autowired
    StockService stockService;
    @Autowired
    CustomerService customerService;
 
    //@Scheduled(cron = "0 0 2 * * ?") //每天凌晨两点执行一次
    @Scheduled(cron = "0/10 * * * * ?") //每隔10秒执行一次
    private void reportOrder() {
        if (!switchBtn) return;
        //1.获取需要上传的企业信息
        List<Enterprise>enterprises = enterpriseService.selectSaleEnterpriseForUpload();
        for (Enterprise enterprise : enterprises){
            if (StringUtils.isBlank(enterprise.getDlcompanycode()) || StringUtils.isBlank(enterprise.getDeviceid())){
                continue;
            }
 
            //2.获取握手密码
            DlResult result = getHandshake(enterprise);
            if (result == null || StringUtils.isBlank(result.getFwdz()) || StringUtils.isBlank(result.getWsmm())){
                continue;
            }
            //获取企业未上传销售数据
            List<SaleOrderInfo> orderInfos = saleOrderService.getWaitUploadOrderByEnterprise(enterprise);
            if (orderInfos == null || orderInfos.size() == 0){
                continue;
            }
 
            uploadOrders(orderInfos,result,enterprise);
        }
 
        //3.上传数据
        //4.处理上传结果
 
    }
 
    private DlResult getHandshake(Enterprise enterprise){
        String respnse = null;
        DlResult result = new DlResult();
        Map params = new HashMap();//请求参数
        params.put("sbid", enterprise.getDeviceid());//
        params.put("dwdm", enterprise.getDlcompanycode());//
 
        try {
            respnse = HttpUtils.net(url+"/lsjk/Sbrz", params, "GET");
            JSONObject jsonResult = JSONObject.parseObject(respnse);
            String wsmm = jsonResult.getString("wsmm");
            String fwdz = jsonResult.getString("fwdz");
            result.setFwdz(fwdz);
            result.setWsmm(wsmm);
            return result;
        }catch (Exception e){
            return result;
        }
    }
 
    public void uploadOrders(List<SaleOrderInfo>orderInfos, DlResult result, Enterprise enterprise){
 
        String servicesUrl = url+result.getFwdz();
        for (SaleOrderInfo orderInfo : orderInfos){
            Service service = new Service();
            Call call = null;
            /**
             * <?xml version="1.0" encoding="GBK"?>
             * <sb>
             * <sjbs>YH</sjbs><!--数据标注 -->
             * <jylx>XX</jylx><!-- 交易类型(03-运输入库、09-退货、10-零散销售) -->
             * <sjscsj>20100630151617</sjscsj><!--数据生成时间(精确到秒) -->
             * <dwdm>XXXX</dwdm><!--单位代码(和烟花系统确定对应关系) -->
             * <cjsbid>XXXX</cjsbid ><!--条码采集设备ID(定长32,不足补0) -->
             * <zjlx>1</zjlx><!--证件类型(1-身份证、2-其他) -->
             * <zjhm>XXXX</zjhm><!--证件号码,定长18 -->
             * <tms>
             * <bh>XXX</bh><!--条码编号-->
             * <sj>20100630151617</sj><!--采集时间(精确到秒)-->
             * </tms>
             * </sb>
             */
            try {
                List<SaleOrderDetailInfo> detailInfos = saleOrderDetailService.selectByOrderCode(orderInfo.getCode());
                CustomerInfo customerInfo = customerService.getById(orderInfo.getCustomer());
                String date = new DateTime(orderInfo.getCreatedat()).toString("yyyyMMddHHmmss");
                ReportEntity reportEntity = new ReportEntity();
                reportEntity.setSjbs("YH");
                reportEntity.setJylx("10");
                reportEntity.setSjscsj(date);
                reportEntity.setDwdm(enterprise.getDlcompanycode());
                reportEntity.setCjsbid(enterprise.getDeviceid());
                reportEntity.setZjlx("1");
                reportEntity.setZjhm(customerInfo.getIdcardnum());
 
                List<TmsEntity>tmsEntities = new ArrayList<>();
                for (SaleOrderDetailInfo detailInfo : detailInfos){
                    TmsEntity tmsEntity = new TmsEntity(detailInfo.getDirectioncode(),date);
                    tmsEntities.add(tmsEntity);
                }
                reportEntity.setTms(tmsEntities);
                String xml = BeanUtils.beanToXml(reportEntity,ReportEntity.class);
                //String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><sb><sjbs>YH</sjbs><jylx>03</jylx><sjscsj>20160203221055</sjscsj><dwdm>ADW000000008422</dwdm><cjsbid>ADW000000008422652722194006466</cjsbid><zjlx>1</zjlx><zjhm>220283198201021916</zjhm><tms><bh>11230I4WY0F62203824</bh><sj>20160114195153</sj></tms><tms><bh>11230I4WY0F62203819</bh><sj>20160114195153</sj></tms><tms><bh>11230I4WY0F62203822</bh><sj>20160114195153</sj></tms><tms><bh>11230I4WY0F62203826</bh><sj>20160114195153</sj></tms><tms><bh>11230I4WY0F62203820</bh><sj>20160114195153</sj></tms><tms><bh>11230I4WY0F62203823</bh><sj>20160114195153</sj></tms><tms><bh>11230I4WY0F62203818</bh><sj>20160114195153</sj></tms><tms><bh>11230I4WY0F62203817</bh><sj>20160114195153</sj></tms><tms><bh>11230I4WY0F62203821</bh><sj>20160114195153</sj></tms></sb>";
                BASE64Encoder encoder = new BASE64Encoder();
                //String str = encoder.encode(sb.toString().trim().getBytes("utf-8"));
                String str = encoder.encode(xml.getBytes("UTF-8"));
                String cjsbid = enterprise.getDeviceid();//设备ID
                String dwdm = enterprise.getDlcompanycode();//单位代码
                String wsmm = result.getWsmm();//握手密码
                call = (Call) service.createCall();
                call.setTargetEndpointAddress(servicesUrl);
                call.setTimeout(5 * 60 * 1000);// 超时设定5分钟抛出异常
                call.setOperationName("lssjsb");// 调用方法
                call.setReturnType(XMLType.XSD_STRING);// 指定返回类型
                call.addParameter("str", org.apache.axis.encoding.XMLType.XSD_STRING,javax.xml.rpc.ParameterMode.IN);// 接口的参数
                call.addParameter("sbid", org.apache.axis.encoding.XMLType.XSD_STRING,javax.xml.rpc.ParameterMode.IN);// 接口的参数
                call.addParameter("dwdm", org.apache.axis.encoding.XMLType.XSD_STRING,javax.xml.rpc.ParameterMode.IN);// 接口的参数
                call.addParameter("wsmm", org.apache.axis.encoding.XMLType.XSD_STRING,javax.xml.rpc.ParameterMode.IN);// 接口的参数
                String callResult = (String) call.invoke(new Object[] { str,cjsbid, dwdm,wsmm });// 调用服务并返回存在的对应数据
                BASE64Decoder decoder = new BASE64Decoder();
                String xmlCode = null;
                try {
                    xmlCode = new String(decoder.decodeBuffer(callResult));
                    DlReportResult reportResult = (DlReportResult) BeanUtils.xmlToBean(xmlCode,DlReportResult.class);
                    DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern("yyyyMMddHHmmss");
                    DateTime dateTime = dateTimeFormatter.parseDateTime(reportResult.getSjscsj());
                    Date uploadAt = dateTime.toDate();
                    orderInfo.setIsupload((byte)1);
                    orderInfo.setUploadat(uploadAt);
                    orderInfo.setReturncode(reportResult.getFhlx());
                } catch (IOException e1) {
                    e1.printStackTrace();
                    orderInfo.setReturncode("999");
                    orderInfo.setIsupload((byte)2);
                }
            } catch (Exception e) {
                System.out.println("调用webservice失败!" + e.getMessage());
                orderInfo.setReturncode("998");
                orderInfo.setIsupload((byte)2);
            }
            saleOrderService.updateById(orderInfo);
        }
 
 
    }
}
 
class DlResult{
    private String wsmm;
    private String fwdz;
 
    DlResult() {
    }
 
    public DlResult(String wsmm, String fwdz) {
        this.wsmm = wsmm;
        this.fwdz = fwdz;
    }
 
    public String getWsmm() {
        return wsmm;
    }
 
    public void setWsmm(String wsmm) {
        this.wsmm = wsmm;
    }
 
    public String getFwdz() {
        return fwdz;
    }
 
    public void setFwdz(String fwdz) {
        this.fwdz = fwdz;
    }
 
}
 
@XmlRootElement(name="fh")
class ReportEntity{
    private String sjbs;
    private String jylx;
    private String sjscsj;
    private String dwdm;
    private String cjsbid;
    private String zjlx;
    private String zjhm;
    private List<TmsEntity> tms;
 
    public String getSjbs() {
        return sjbs;
    }
 
    public void setSjbs(String sjbs) {
        this.sjbs = sjbs;
    }
 
    public String getJylx() {
        return jylx;
    }
 
    public void setJylx(String jylx) {
        this.jylx = jylx;
    }
 
    public String getSjscsj() {
        return sjscsj;
    }
 
    public void setSjscsj(String sjscsj) {
        this.sjscsj = sjscsj;
    }
 
    public String getDwdm() {
        return dwdm;
    }
 
    public void setDwdm(String dwdm) {
        this.dwdm = dwdm;
    }
 
    public String getCjsbid() {
        return cjsbid;
    }
 
    public void setCjsbid(String cjsbid) {
        this.cjsbid = cjsbid;
    }
 
    public String getZjlx() {
        return zjlx;
    }
 
    public void setZjlx(String zjlx) {
        this.zjlx = zjlx;
    }
 
    public String getZjhm() {
        return zjhm;
    }
 
    public void setZjhm(String zjhm) {
        this.zjhm = zjhm;
    }
 
    @XmlElement(name="tms")
    public List<TmsEntity> getTms() {
        return tms;
    }
 
    public void setTms(List<TmsEntity> tms) {
        this.tms = tms;
    }
}
 
@XmlRootElement(name="tms")
class TmsEntity{
    private String bh;
 
    private String sj;
 
    public String getBh() {
        return bh;
    }
 
    public void setBh(String bh) {
        this.bh = bh;
    }
 
    public TmsEntity() {
    }
 
    public TmsEntity(String bh, String sj) {
        this.bh = bh;
        this.sj = sj;
    }
 
    public String getSj() {
        return sj;
    }
 
    public void setSj(String sj) {
        this.sj = sj;
    }
}
 
@XmlRootElement(name="fh")
class DlReportResult{
    private String sjbs;
 
    private String cjsbid;
 
    private String sjscsj;
 
    private String fhlx;
 
    private String fhsj;
 
    public String getSjbs() {
        return sjbs;
    }
 
    public void setSjbs(String sjbs) {
        this.sjbs = sjbs;
    }
 
    public String getCjsbid() {
        return cjsbid;
    }
 
    public void setCjsbid(String cjsbid) {
        this.cjsbid = cjsbid;
    }
 
    public String getSjscsj() {
        return sjscsj;
    }
 
    public void setSjscsj(String sjscsj) {
        this.sjscsj = sjscsj;
    }
 
    public String getFhlx() {
        return fhlx;
    }
 
    public void setFhlx(String fhlx) {
        this.fhlx = fhlx;
    }
 
    public String getFhsj() {
        return fhsj;
    }
 
    public void setFhsj(String fhsj) {
        this.fhsj = fhsj;
    }
}