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);
|
}
|
}
|