园区定时数据上传
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

177 lines
8.7 KiB

package com.hxjt.dataupload.jobhandler.doubleprevent;
import com.alibaba.fastjson.JSONObject;
import com.hxjt.dataupload.model.entity.doubleprevent.Fxfxdx;
import com.hxjt.dataupload.model.entity.doubleprevent.Fxgkcs;
import com.hxjt.dataupload.model.entity.doubleprevent.Yhpcjh;
import com.hxjt.dataupload.service.FxfxdxService;
import com.hxjt.dataupload.utils.HttpUtils;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.protocol.HTTP;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;
@Component
public class YhpcjhJobHandler {
private Logger log = LoggerFactory.getLogger(YhpcjhJobHandler.class);
@Autowired
FxfxdxService fxfxdxService;
@XxlJob(value = "YhpcjhJobHandler", init = "init", destroy = "destroy")
public void execute(String param) {
String jobParam = XxlJobHelper.getJobParam();
ArrayList<Yhpcjh> fxgkcs = new ArrayList<>();
if (jobParam.equals("全量")) {
fxgkcs = fxfxdxService.getYhpcjhs("N");
}else if(jobParam.equals("每日")){
fxgkcs = fxfxdxService.getYhpcjhs("Y");
}
ArrayList<Fxfxdx> zdwxys = fxfxdxService.getZdwxys();
for (int i = 0; i < fxgkcs.size(); i++) {
String HAZARD_NAME = fxgkcs.get(i).getFengxianfenxiduixian();
for (int j = 0; j < zdwxys.size(); j++) {
String DANGESOURCE_NAME = zdwxys.get(j).getZhongdaweixianyuanmi();
switch (HAZARD_NAME) {
case "重大危险源甲醇中间储存":
if (DANGESOURCE_NAME.equals("甲醇中间储存单元")) {
fxgkcs.get(i).setZhongdaweixianyuanmi(DANGESOURCE_NAME);
fxgkcs.get(i).setZhongdaweixianyuande(zdwxys.get(j).getZhongdaweixianyuande());
}
break;
case "重大危险源化产油库":
if (DANGESOURCE_NAME.equals("化产油库区")) {
fxgkcs.get(i).setZhongdaweixianyuanmi(DANGESOURCE_NAME);
fxgkcs.get(i).setZhongdaweixianyuande(zdwxys.get(j).getZhongdaweixianyuande());
}
break;
case "重大危险源甲醇储罐":
if (DANGESOURCE_NAME.equals("甲醇储存单元")) {
fxgkcs.get(i).setZhongdaweixianyuanmi(DANGESOURCE_NAME);
fxgkcs.get(i).setZhongdaweixianyuande(zdwxys.get(j).getZhongdaweixianyuande());
}
break;
case "重大危险源空分站":
if (DANGESOURCE_NAME.equals("空分站单元")) {
fxgkcs.get(i).setZhongdaweixianyuanmi(DANGESOURCE_NAME);
fxgkcs.get(i).setZhongdaweixianyuande(zdwxys.get(j).getZhongdaweixianyuande());
}
break;
case "重大危险源气柜储存":
if (DANGESOURCE_NAME.equals("气柜")) {
fxgkcs.get(i).setZhongdaweixianyuanmi(DANGESOURCE_NAME);
fxgkcs.get(i).setZhongdaweixianyuande(zdwxys.get(j).getZhongdaweixianyuande());
}
break;
case "重大危险源粗苯蒸馏":
if (DANGESOURCE_NAME.equals("粗苯蒸馏单元")) {
fxgkcs.get(i).setZhongdaweixianyuanmi(DANGESOURCE_NAME);
fxgkcs.get(i).setZhongdaweixianyuande(zdwxys.get(j).getZhongdaweixianyuande());
}
break;
}
}
if((!StringUtils.isBlank(fxgkcs.get(i).getZhongdaweixianyuande()))&&fxgkcs.get(i).getZhongdaweixianyuande().length()>2){
fxgkcs.get(i).setZhongdaweixianyuande(fxgkcs.get(i).getZhongdaweixianyuande().substring(0,2));
}
//System.out.println(fxfxdxs.get(i).toString());
}
int success = 0;
int fail = 0;
for (int i = 0; i < fxgkcs.size(); i++) {
System.out.print(i + 1+",");
String company = fxgkcs.get(i).getQiyemingcheng();
String url = "https://ny-fxfk.com/space-ningyanghuagongchany/sapi/qpaas/tiga/dys/keyAuthentication/yinhuanpaicharenwuqi/v1/yinhuanpaicharenwuqi";
switch (company) {
case "山东恒信高科能源有限公司":
case "恒信中碳氢能源科技(山东)有限公司":
JSONObject jsonObject = (JSONObject) JSONObject.toJSON(fxgkcs.get(i));
String post = null;
try {
post = HttpUtils.sendDataUpload(url, jsonObject, HTTP.UTF_8, "5JzJFKkwF5aHJ0GQUz3Y3D76035B58D84048");
boolean thisFailFlag = false;
boolean postIsNull = false;
boolean postNoMsgs = false;
if(!StringUtils.isBlank(post)){
JSONObject postJsonObj = JSONObject.parseObject(post);
if(postJsonObj.get("msg")!=null && postJsonObj.get("code")!=null && postJsonObj.get("status")!=null && !StringUtils.isBlank(postJsonObj.get("msg").toString())&&!StringUtils.isBlank(postJsonObj.get("code").toString())&&!StringUtils.isBlank(postJsonObj.get("status").toString())){
if(postJsonObj.get("msg").equals("成功")&&postJsonObj.get("code").toString().equals("1000000")&&postJsonObj.get("status").toString().equals("200")){//status
//成功
System.out.println("成功"+(i+1)+",");
//System.out.println(jsonObject);
//System.out.println("post:" + post.toString());
//XxlJobHelper.log("执行日志:" + company + post.toString());
success++;
}else{
thisFailFlag = true;
}
}else{
thisFailFlag = true;
postNoMsgs = true;
}
}else{
postIsNull = true;
thisFailFlag = true;
}
if(thisFailFlag){//异常情况
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String formattedDate = LocalDateTime.now().format(formatter);
System.out.println(" 当前时间: " + formattedDate); // 示例输出:2025-03-12 08:29:25
System.out.println("失败"+(i+1)+",隐患排查任务");
if(!postIsNull){
System.out.println("post:" + post.toString());
XxlJobHelper.log("执行日志:" + company + post.toString());
}
System.out.println(jsonObject);
fail++;
Thread.sleep(120000);
}
} catch (Exception e) {
e.printStackTrace();
XxlJobHelper.log("错误信息:" + e);
XxlJobHelper.log("请求体:" + jsonObject);
try {
TimeUnit.MINUTES.sleep(2); // 直接指定分钟
} catch (InterruptedException ie) {
Thread.currentThread().interrupt();
}
}
break;
}
}
if(fxgkcs.size()>0){
XxlJobHelper.log("执行日志:本次有"+fxgkcs.size()+"条需要上传,成功上传"+success+"条,失败"+fail+"条");
}else{
XxlJobHelper.log("执行日志:本次无新数据需上传");
}
}
private void init() {
log.info("init 方法调用成功");
}
private void destroy() {
log.info("destroy 方法调用成功");
}
}