zhang
2 天以前 d95047d4d0e212896d1a1ed1f4528b46553d4e09
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
package com.zy.acs.manager.core.third;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zy.acs.framework.common.Cools;
import com.zy.acs.manager.core.utils.HttpHandler;
import com.zy.acs.manager.manager.entity.TaskReport;
import com.zy.acs.manager.manager.entity.TaskReportLog;
import com.zy.acs.manager.manager.service.TaskReportLogService;
import com.zy.acs.manager.manager.service.TaskReportService;
import com.zy.acs.manager.system.service.ConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
 
import java.io.IOException;
import java.util.Date;
import java.util.List;
 
@Slf4j
@Component
public class ReportThirdScheduler {
 
 
    @Autowired
    private TaskReportService taskReportService;
 
    @Autowired
    private TaskReportLogService taskReportLogService;
 
    @Autowired
    private ConfigService configService;
 
 
    @Scheduled(fixedDelay = 3000)
    public void execute() {
        Boolean report = configService.getVal("REPORT", Boolean.class, false);
        if (report) {
            String wmsUrl = configService.getVal("WMS_URL", String.class);
            String wmsPath = configService.getVal("WMS_PATH", String.class);
            Integer times = configService.getVal("REPORT_TIMES", Integer.class, 3);
            List<TaskReport> list = taskReportService.list();
            for (TaskReport taskReport : list) {
                if ((Cools.isEmpty(taskReport.getReportTimes()) ? 0 : taskReport.getReportTimes()) > times) {
                    toLog(taskReport);
                    continue;
                }
                if (report(taskReport, wmsUrl, wmsPath)) {
                    toLog(taskReport);
                } else {
                    taskReport.setReportTimes((Cools.isEmpty(taskReport.getReportTimes()) ? 0 : taskReport.getReportTimes()) + 1);
                    taskReport.setUpdateTime(new Date());
                    taskReportService.updateById(taskReport);
                }
            }
        }
    }
 
    @Transactional
    public boolean report(TaskReport taskReport, String wmsUrl, String wmsPath) {
        String response = null;
        try {
            response = new HttpHandler.Builder()
                    .setUri(wmsUrl)
                    .setPath(wmsPath)
                    .setJson(JSON.toJSONString(taskReport))
                    .build()
                    .doPost();
            log.info("返回参数:{}", response);
            JSONObject jsonObject = JSON.parseObject(response);
            if (jsonObject.getInteger("code").equals(200)) {
                return true;
            }
        } catch (IOException e) {
            log.info("报错了,{}", e);
            taskReport.setMemo(e.getMessage());
            e.printStackTrace();
        }
 
        return false;
    }
 
    @Transactional
    public void toLog(TaskReport taskReport) {
        TaskReportLog taskReportLog = new TaskReportLog();
        BeanUtils.copyProperties(taskReport, taskReportLog);
        taskReportService.removeById(taskReport.getId());
        taskReportLog.setId(null);
        taskReportLogService.save(taskReportLog);
    }
}