wisdomisite-java/src/main/java/com/zhgd/xmgl/task/StableWaterMixStationTask.java

148 lines
7.0 KiB
Java
Raw Normal View History

2023-12-02 11:10:52 +08:00
package com.zhgd.xmgl.task;
2023-12-04 16:34:08 +08:00
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
2023-12-11 18:11:56 +08:00
import com.gexin.fastjson.JSON;
2023-12-04 16:34:08 +08:00
import com.glodon.cloudt.rest.client.RestServiceClient;
import com.glodon.cloudt.rest.client.data.HmacRestAuthInfo;
import com.glodon.cloudt.rest.client.data.RestResponseInfo;
import com.glodon.cloudt.rest.client.exception.AuthenticateException;
import com.glodon.cloudt.rest.client.exception.InvalidUriException;
import com.glodon.cloudt.rest.client.exception.NoAuthenticateException;
import com.glodon.cloudt.rest.client.impl.HmacRestServiceClient;
import com.zhgd.xmgl.modules.project.entity.Project;
import com.zhgd.xmgl.modules.project.mapper.ProjectMapper;
2023-12-02 17:16:10 +08:00
import com.zhgd.xmgl.modules.stablewater.service.IStableWaterMixStationDataService;
2023-12-02 11:10:52 +08:00
import com.zhgd.xmgl.util.ThirdPartRequestUtil;
import lombok.extern.log4j.Log4j;
2023-12-11 18:11:56 +08:00
import lombok.extern.slf4j.Slf4j;
2023-12-02 11:10:52 +08:00
import net.javacrumbs.shedlock.core.SchedulerLock;
import org.springframework.beans.factory.annotation.Autowired;
2023-12-04 16:34:08 +08:00
import org.springframework.beans.factory.annotation.Value;
2023-12-02 11:10:52 +08:00
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
2023-12-04 16:34:08 +08:00
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
2023-12-02 11:10:52 +08:00
2023-12-02 17:16:10 +08:00
import java.util.List;
2023-12-02 11:10:52 +08:00
/**
* 水稳拌合站任务
*/
2023-12-11 18:11:56 +08:00
@Slf4j
2023-12-02 11:10:52 +08:00
@Component
2023-12-04 16:34:08 +08:00
@RestController
2023-12-02 11:10:52 +08:00
public class StableWaterMixStationTask {
@Autowired
ThirdPartRequestUtil thirdPartRequestUtil;
2023-12-02 17:16:10 +08:00
@Autowired
IStableWaterMixStationDataService stableWaterMixStationDataService;
2023-12-04 16:34:08 +08:00
@Autowired
ProjectMapper projectMapper;
2023-12-02 17:16:10 +08:00
2023-12-02 11:10:52 +08:00
/**
* 定时拉取广联达的搅拌站数据https://xmgl.glodon.com/wl/docs/third_help/part2/openapi/pull/mixTotal.html
*/
@Scheduled(cron = "0 2 0 * * ?")
@SchedulerLock(name = "executeStableWaterMixStationTask", lockAtMostFor = 1000 * 60 * 2, lockAtLeastFor = 1000 * 60 * 1)
2023-12-06 14:04:04 +08:00
@GetMapping("1204") // to do
2023-12-02 11:10:52 +08:00
public void executeTask() {
log.info("定时拉取广联达的搅拌站数据任务开始");
2023-12-06 14:04:04 +08:00
List<Project> projects = projectMapper.selectList(new LambdaQueryWrapper<Project>()
.isNotNull(Project::getGldProjectId)
.ne(Project::getGldProjectId, "")
);
//for (Project project : projects) {
2023-12-06 14:04:04 +08:00
sendHttp(null);
//}
2023-12-04 16:34:08 +08:00
}
private void sendHttp(Project project) {
try {
/**----------------准备 -------------------*/
//第1步下载授权文件
//第2步获取SDK
/** ------------ 授权认证 --------------*/
//第3步创建客户端实例
RestServiceClient serviceClient = HmacRestServiceClient.getInstance();
//第4步加载/验证授权文件
//4.1构建认证信息
HmacRestAuthInfo restAuthInfo = new HmacRestAuthInfo();
//4.2设置授权文件路径
2023-12-11 18:11:56 +08:00
restAuthInfo.setLicPath("C:\\Users\\Administrator\\Desktop\\t\\auth.lic");
//restAuthInfo.setLicPath(project.getGldLicPath());
2023-12-04 16:34:08 +08:00
//4.3权限认证
serviceClient.authenticate(restAuthInfo);
/** ------------ 拼接请求地址 --------------*/
//第5步获取授权文件关联的GYS系统地址hostAddress
String hostAddress = serviceClient.getRestRootAddress();
//第6步设置请求接口的URI地址apiURI(以获取集成项目列表信息为例)
2023-12-11 18:11:56 +08:00
String apiURI = "/api/inspection/v1.0/project/getTenantHasCodeProject";
//String apiURI = String.format("/api/mix/v1.0/mix/product/getMachineProductList?projectId=%s&beginTimestamp=20170710101010000", "672804614860800");
//String apiURI = String.format("/api/mix/v1.0/mix/product/getMachineProductList?projectId=%s&beginTimestamp=20170710101010000", project.getGldProjectId());
2023-12-04 16:34:08 +08:00
//第7步拼装完整的请求网址
String fullURL = hostAddress + apiURI;
//第8步请求数据准备本例中为从GYS系统获取数据无需进行请求数据的准备
2023-12-11 18:11:56 +08:00
log.info("url:{}", fullURL);
2023-12-04 16:34:08 +08:00
//第9步发送请求用来推送/获取数据
RestResponseInfo restResponseInfo = serviceClient.get(fullURL);
2023-12-11 18:11:56 +08:00
log.info("rs:{}", JSON.toJSONString(restResponseInfo));
2023-12-04 16:34:08 +08:00
/** ------------ 处理请求返回结果 --------------*/
if (restResponseInfo.isSuccess()) {
//请求成功:
//第10步处理请求结果第三方系统自行处理
System.out.println(restResponseInfo.getStringContent());
save(restResponseInfo.getStringContent(), project);
} else {
//请求失败:
//第11步根据返回结果排查失败原因数据原因第三方系统自行处理
}
} catch (AuthenticateException e) {
e.printStackTrace();
} catch (InvalidUriException e) {
e.printStackTrace();
} catch (NoAuthenticateException e) {
e.printStackTrace();
}
}
private void save(String stringContent, Project project) {
//String projectId = "672804614860800";
//String url = String.format("/api/mix/v1.0/mix/product/getMachineProductList?projectId=%s&beginTimestamp=20170710101010000", projectId);
2023-12-02 11:10:52 +08:00
//String rs = thirdPartRequestUtil.get(url);
//JSONObject jo = JSON.parseObject(rs);
//if (Objects.equals(jo.getBoolean("success"), true)) {
2023-12-02 17:16:10 +08:00
// JSONObject dataJson = jo.getJSONObject("data");
// JSONArray bills = dataJson.getJSONArray("Bills");
// for (Object bill : bills) {
// StableWaterMixStationData data = new StableWaterMixStationData();
// List<StableWaterMixStationSetData> setDataList = new ArrayList<>();
// data.setSetDataList(setDataList);
// BeanUtil.copyProperties(bill, data, true);
// JSONArray pcList = ((JSONObject) bill).getJSONArray("PCList");
// for (Object pc : pcList) {
// StableWaterMixStationSetData setData = new StableWaterMixStationSetData();
// BeanUtil.copyProperties(pc, setData, true);
// setDataList.add(setData);
// List<StableWaterMixStationRawMaterialData> rawMaterialDataList = new ArrayList<>();
// setData.setRawMaterialDataList(rawMaterialDataList);
// JSONArray ycList = ((JSONObject) pc).getJSONArray("YCList");
// for (Object yc : ycList) {
// StableWaterMixStationRawMaterialData rawMaterialData = new StableWaterMixStationRawMaterialData();
// BeanUtil.copyProperties(yc, rawMaterialData, true);
// rawMaterialDataList.add(rawMaterialData);
// }
// }
// stableWaterMixStationDataService.add(data);
// }
2023-12-02 11:10:52 +08:00
//}
2023-12-04 16:34:08 +08:00
2023-12-02 11:10:52 +08:00
}
2023-12-04 16:34:08 +08:00
2023-12-02 11:10:52 +08:00
}