package com.zy.asrs.task;
|
|
import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.JSONObject;
|
import com.baomidou.mybatisplus.mapper.EntityWrapper;
|
import com.baomidou.mybatisplus.plugins.Page;
|
import com.core.common.Cools;
|
import com.core.exception.CoolException;
|
import com.zy.asrs.entity.ReportData;
|
import com.zy.asrs.service.ApiLogService;
|
import com.zy.asrs.service.ReportDataService;
|
import com.zy.asrs.task.handler.ReportDataHandler;
|
import com.zy.common.utils.HttpHandler;
|
import org.slf4j.Logger;
|
import org.slf4j.LoggerFactory;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.scheduling.annotation.Scheduled;
|
import org.springframework.stereotype.Component;
|
|
import java.util.HashMap;
|
import java.util.Map;
|
import java.util.concurrent.TimeUnit;
|
|
/**
|
* Created by vincent on 2020/7/7
|
*/
|
@Component
|
public class ReportDataScheduler {
|
|
private static final Logger log = LoggerFactory.getLogger(ReportDataScheduler.class);
|
|
@Autowired
|
private ReportDataService reportDataService;
|
|
@Autowired
|
private ApiLogService apiLogService;
|
|
@Autowired
|
private ReportDataHandler reportDataHandler;
|
|
@Value("${mes.appKey}")
|
private String appKey;
|
|
|
@Value("${mes.appSecret}")
|
private String appSecret;
|
|
|
@Value("${mes.report}")
|
private Boolean flag;
|
|
@Value("${mes.getTokenUrl}")
|
private String getTokenUrl;
|
|
@Value("${mes.getTokenPath}")
|
private String getTokenPath;
|
|
@Value("${mes.stock}")
|
private String stock;
|
|
|
@Scheduled(fixedDelay = 30000)
|
public void execute() {
|
if (!flag) {
|
return;
|
}
|
Page<ReportData> objectPage = new Page<>(1, 100);
|
Page<ReportData> reportData = reportDataService.selectPage(objectPage, new EntityWrapper<ReportData>().orderBy("create_time", false));
|
if (reportData != null && !Cools.isEmpty(reportData.getRecords())) {
|
Map<String, Object> mesTokenInfo = getMesTokenInfo();
|
for (ReportData data : reportData.getRecords()) {
|
try {
|
reportDataHandler.start(data, mesTokenInfo);
|
} catch (Exception e) {
|
log.error("数据处理异常,{}", data.getId());
|
}
|
}
|
}
|
}
|
|
//获取mes token信息
|
private Map<String, Object> getMesTokenInfo() {
|
Map<String, String> param = new HashMap<>();
|
param.put("appKey", appKey);
|
param.put("appSecret", appSecret);
|
|
JSONObject jsonObject = doHttpRequest(JSON.toJSONString(param), null, "获取MesToken", getTokenUrl, getTokenPath, null, "127.0.0.1");
|
|
Map<String, Object> headParam = new HashMap<>();
|
headParam.put("X-AUTH", JSON.parseObject(jsonObject.get("data").toString()).get("appAccessToken").toString());
|
|
return headParam;
|
|
}
|
|
private JSONObject doHttpRequest(String requestParam, Map<String, Object> headParam, String namespace, String url, String path, String appkey, String ip) {
|
String response = "";
|
boolean success = false;
|
|
try {
|
response = new HttpHandler.Builder().setUri(url).setPath(path).setTimeout(30, TimeUnit.SECONDS).setHeaders(headParam).setJson(requestParam).setHttps(true).build().doPost();
|
JSONObject jsonObject = JSON.parseObject(response);
|
|
if (Cools.isEmpty(jsonObject.get("code")) || Integer.parseInt(jsonObject.get("code").toString()) != 200) {
|
log.info("mes接口调用失败,返回信息:" + jsonObject);
|
//TODO 张超
|
throw new CoolException("mes接口调用失败,返回信息:" + jsonObject);
|
}
|
success = true;
|
return jsonObject;
|
|
} catch (Exception e) {
|
log.error(e.getMessage());
|
throw new CoolException(e.getMessage());
|
} finally {
|
apiLogService.save(namespace, url + path, appkey, ip, requestParam, response, success);
|
}
|
|
}
|
|
@Scheduled(fixedDelay = 1000 * 60 * 30)
|
public void executeStock() {
|
log.info("Stock定时任务开始执行");
|
// if (!flag) {
|
// return;
|
// }
|
Map<String, Object> mesTokenInfo = getMesTokenInfo();
|
reportDataHandler.stock(mesTokenInfo);
|
log.info("Stock定时任务结束执行");
|
}
|
}
|