1
zhang
昨天 2ddf6fc24333df35bd1ac15848b917336d533d53
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
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.third.dto.TaskEvent;
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.enums.TaskReportStsType;
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 wcsUrl = configService.getVal("WCS_URL", String.class);
            String wcsPath = configService.getVal("WCS_TASK_REPORT", String.class);
            List<TaskReport> list = taskReportService.list(new LambdaQueryWrapper<TaskReport>().eq(TaskReport::getCompleted, 0).le(TaskReport::getReportTimes, 3));
            for (TaskReport taskReport : list) {
                if (taskReport.getEventType().equalsIgnoreCase(TaskReportStsType.COMPLETED.name)) {
                    String response = null;
                    try {
                        response = new HttpHandler.Builder()
                                .setUri(wcsUrl)
                                .setPath(wcsPath)
                                .setJson(JSON.toJSONString(taskReport))
                                .build()
                                .doPost();
                        log.info("返回参数:{}", response);
                        taskReport.setReportTimes((Cools.isEmpty(taskReport.getReportTimes()) ? 0 : taskReport.getReportTimes()) + 1);
                        taskReport.setUpdateTime(new Date());
                        taskReport.setCompleted(1);
                    } catch (IOException e) {
                        taskReport.setReportTimes((Cools.isEmpty(taskReport.getReportTimes()) ? 0 : taskReport.getReportTimes()) + 1);
                        taskReport.setUpdateTime(new Date());
                        e.printStackTrace();
                    }
                    taskReportService.updateById(taskReport);
                } else {
                    TaskEvent taskEvent = new TaskEvent(taskReport.getSeqNum(), taskReport.getEventType(), taskReport.getAgvId() + "");
                    log.info("开始上报:{}", taskEvent);
                    if (report(taskEvent, 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() {
        Boolean report = configService.getVal("REPORT", Boolean.class, false);
        if (report) {
            Calendar instance = Calendar.getInstance();
            instance.add(Calendar.DATE, 7);
            List<TaskReport> list = taskReportService.list(new LambdaQueryWrapper<TaskReport>().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(TaskEvent 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 != null && jsonObject.get("code") != null && jsonObject.getInteger("code").equals(200)) {
                return true;
            }
        } catch (IOException e) {
            log.info("报错了,{}", e);
            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);
    }
}