1
zhang
5 天以前 9f7d850f985cbd1756798329b9a3669dae51ac48
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
package com.zy.acs.manager.core.third;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.Calendar;
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);
            String codes = configService.getVal("CODES", String.class, "1457;1612");
            List<TaskReport> list = taskReportService.list(new LambdaQueryWrapper<TaskReport>().eq(TaskReport::getCompleted, 0).le(TaskReport::getReportTimes, 3));
            for (TaskReport taskReport : list) {
                if (report(taskReport, wmsUrl, wmsPath)) {
                    taskReport.setReportTimes((Cools.isEmpty(taskReport.getReportTimes()) ? 0 : taskReport.getReportTimes()) + 1);
                    taskReport.setUpdateTime(new Date());
                    taskReport.setCompleted(1);
                } else {
                    taskReport.setReportTimes((Cools.isEmpty(taskReport.getReportTimes()) ? 0 : taskReport.getReportTimes()) + 1);
                    taskReport.setUpdateTime(new Date());
                }
                taskReportService.updateById(taskReport);
            }
        }
    }
 
    @Scheduled(fixedDelay = 3000)
    public void execute2() {
        Calendar instance = Calendar.getInstance();
        instance.add(Calendar.DATE, 1);
        List<TaskReport> list = taskReportService.list(new LambdaQueryWrapper<TaskReport>().eq(TaskReport::getCompleted, 1).ge(TaskReport::getUpdateTime, instance.getTime()));
        Integer times = configService.getVal("REPORT_TIMES", Integer.class, 3);
        for (TaskReport taskReport : list) {
            if ((Cools.isEmpty(taskReport.getReportTimes()) ? 0 : taskReport.getReportTimes()) > times) {
                toLog(taskReport);
            }
        }
        list = taskReportService.list(new LambdaQueryWrapper<TaskReport>().eq(TaskReport::getReportTimes, 4).ge(TaskReport::getUpdateTime, instance.getTime()));
        for (TaskReport taskReport : list) {
            if ((Cools.isEmpty(taskReport.getReportTimes()) ? 0 : taskReport.getReportTimes()) > times) {
                toLog(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);
    }
}