|
@@ -0,0 +1,444 @@
|
|
|
|
+package org.springblade.business.scheduled;
|
|
|
|
+
|
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
|
+import com.alibaba.nacos.common.utils.MD5Utils;
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
+import org.apache.http.HttpResponse;
|
|
|
|
+import org.apache.http.client.methods.HttpPost;
|
|
|
|
+import org.apache.http.entity.StringEntity;
|
|
|
|
+import org.apache.http.impl.client.CloseableHttpClient;
|
|
|
|
+import org.apache.http.impl.client.HttpClientBuilder;
|
|
|
|
+import org.springblade.business.entity.FinishTestReport;
|
|
|
|
+import org.springblade.business.entity.TestType;
|
|
|
|
+import org.springblade.business.entity.ThirdReport;
|
|
|
|
+import org.springblade.business.scheduled.vo.GetFinishTestReport;
|
|
|
|
+import org.springblade.business.scheduled.vo.GetThirdReport;
|
|
|
|
+import org.springblade.business.scheduled.vo.Result;
|
|
|
|
+import org.springblade.business.scheduled.vo.ThirdLogin;
|
|
|
|
+import org.springblade.business.service.TrialCyFinishTestReportService;
|
|
|
|
+import org.springblade.business.service.TrialCyTestTypeService;
|
|
|
|
+import org.springblade.business.service.TrialCyThirdReportService;
|
|
|
|
+import org.springblade.core.redis.cache.BladeRedis;
|
|
|
|
+import org.springblade.core.tool.utils.CollectionUtil;
|
|
|
|
+import org.springblade.core.tool.utils.StringUtil;
|
|
|
|
+import org.springblade.manager.entity.ContractInfo;
|
|
|
|
+import org.springblade.manager.entity.ProjectInfo;
|
|
|
|
+import org.springframework.jdbc.core.JdbcTemplate;
|
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
|
+
|
|
|
|
+import javax.annotation.PostConstruct;
|
|
|
|
+import javax.annotation.Resource;
|
|
|
|
+import java.io.ByteArrayOutputStream;
|
|
|
|
+import java.io.IOException;
|
|
|
|
+import java.io.InputStream;
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
+import java.util.HashMap;
|
|
|
|
+import java.util.List;
|
|
|
|
+import java.util.Map;
|
|
|
|
+import java.util.Set;
|
|
|
|
+import java.util.concurrent.ConcurrentHashMap;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * 成渝试验第三方接口获取数据
|
|
|
|
+ * 定时任务
|
|
|
|
+ *
|
|
|
|
+ * @author LHB
|
|
|
|
+ */
|
|
|
|
+@Component
|
|
|
|
+@Slf4j
|
|
|
|
+public class ChenYuTestScheduled {
|
|
|
|
+
|
|
|
|
+ @Resource
|
|
|
|
+ private JdbcTemplate jdbcTemplate;
|
|
|
|
+
|
|
|
|
+ @Resource
|
|
|
|
+ private BladeRedis bladeRedis;
|
|
|
|
+ /**
|
|
|
|
+ * 成渝-获取标段已完成(已签章完成)的检测报告
|
|
|
|
+ */
|
|
|
|
+ @Resource
|
|
|
|
+ private TrialCyFinishTestReportService trialCyFinishTestReportService;
|
|
|
|
+ /**
|
|
|
|
+ * 成渝-试验检测树
|
|
|
|
+ */
|
|
|
|
+ @Resource
|
|
|
|
+ private TrialCyTestTypeService trialCyTestTypeService;
|
|
|
|
+ /**
|
|
|
|
+ * 成渝-已上传的外委检测报告/ 第三方检测报告/ 上传报告
|
|
|
|
+ */
|
|
|
|
+ @Resource
|
|
|
|
+ private TrialCyThirdReportService trialCyThirdReportService;
|
|
|
|
+
|
|
|
|
+ // HTTP客户端复用
|
|
|
|
+ private CloseableHttpClient httpClient;
|
|
|
|
+
|
|
|
|
+ @PostConstruct
|
|
|
|
+ public void init() {
|
|
|
|
+ this.httpClient = HttpClientBuilder.create().build();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //token缓存key
|
|
|
|
+ private static final String TOKEN_REDIS_KEY = "blade:cqcy:token";
|
|
|
|
+
|
|
|
|
+ //项目编码
|
|
|
|
+ private static final String PROJECT_CODE = "cqcyfx";
|
|
|
|
+
|
|
|
|
+ //项目id
|
|
|
|
+ private static Long PROJECT_ID = null;
|
|
|
|
+
|
|
|
|
+ //接口域名
|
|
|
|
+ private final String URL = "https://apicqcyfx.qdm123.com";
|
|
|
|
+
|
|
|
|
+ //合同编码缓存
|
|
|
|
+ private final Map<String, Long> contractCodeMap = new ConcurrentHashMap<>();
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 登录 获取token
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+ public String thirdLogin() {
|
|
|
|
+ //TODO 从后台管理的配置中心获取
|
|
|
|
+ String url = "/api/ThirdSystem/ThirdLogin";
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ //获取项目id
|
|
|
|
+ String sql = "select id from m_project_info where project_number = ? and is_deleted = 0 limit 1";
|
|
|
|
+ ProjectInfo projectInfo = jdbcTemplate.queryForObject(sql, ProjectInfo.class, PROJECT_CODE);
|
|
|
|
+ PROJECT_ID = projectInfo.getId();
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ String getAppKeySql = "select param_value from blade_param where param_key = 'cy.trial.appKey' and is_deleted = 0";
|
|
|
|
+ String getAppSecretSql = "select param_value from blade_param where param_key = 'cy.trial.appSecret' and is_deleted = 0";
|
|
|
|
+ //后管配置 appKey
|
|
|
|
+ String appKey = jdbcTemplate.queryForObject(getAppKeySql, String.class);
|
|
|
|
+ //后管配置 appSecret
|
|
|
|
+ String appSecret = jdbcTemplate.queryForObject(getAppSecretSql, String.class);
|
|
|
|
+ if (StringUtil.isBlank(appKey) || StringUtil.isBlank(appSecret)) {
|
|
|
|
+ log.error("---------#后管未配置成渝-试验-授权码或私钥");
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+ //当前时间戳
|
|
|
|
+ Long time = System.currentTimeMillis();
|
|
|
|
+
|
|
|
|
+ //加密
|
|
|
|
+ String md5Hex = MD5Utils.md5Hex(time + appKey + appSecret, "UTF-8");
|
|
|
|
+
|
|
|
|
+ ThirdLogin thirdLogin = new ThirdLogin();
|
|
|
|
+ thirdLogin.setAppKey(appKey);
|
|
|
|
+ thirdLogin.setTimestamp(time);
|
|
|
|
+ thirdLogin.setSecret(md5Hex);
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ Result post = this.post(URL + url, thirdLogin);
|
|
|
|
+ if (post == null) {
|
|
|
|
+ log.error("---------#成渝第三方接口访问失败");
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+ Integer state = post.getState();
|
|
|
|
+ //成功
|
|
|
|
+ if (state == 1) {
|
|
|
|
+ JSONObject data = (JSONObject) post.getData();
|
|
|
|
+ bladeRedis.setEx(TOKEN_REDIS_KEY, data.getString("token"), data.getLong("expireSecond"));
|
|
|
|
+ return data.getString("token");
|
|
|
|
+ } else {
|
|
|
|
+ log.error("---------#成渝第三方接口访问失败:" + post.getMessage());
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 获取合同段列表接口
|
|
|
|
+ */
|
|
|
|
+ public void getContractSectByProjCode() {
|
|
|
|
+ //清空缓存
|
|
|
|
+ contractCodeMap.clear();
|
|
|
|
+
|
|
|
|
+ String url = "/api/CQHCZC/MesureSystem/GetContractSectByProjCode";
|
|
|
|
+
|
|
|
|
+ String token = bladeRedis.get(TOKEN_REDIS_KEY);
|
|
|
|
+ if (token == null) {
|
|
|
|
+ token = thirdLogin();
|
|
|
|
+ }
|
|
|
|
+ if (token == null) {
|
|
|
|
+ log.error("---------#成渝第三方接口访问失败:token为空");
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ if (PROJECT_ID == null) {
|
|
|
|
+ log.error("---------#成渝第三方接口访问失败:项目不存在");
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ JSONObject jsonObject = new JSONObject();
|
|
|
|
+ jsonObject.put("token", token);
|
|
|
|
+ jsonObject.put("ProjectCode", PROJECT_CODE);
|
|
|
|
+
|
|
|
|
+ Result post = this.post(URL + url, jsonObject);
|
|
|
|
+
|
|
|
|
+ if (post == null) {
|
|
|
|
+ log.error("---------#成渝第三方接口访问失败");
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ Integer state = post.getState();
|
|
|
|
+ //成功
|
|
|
|
+ if (state == 1) {
|
|
|
|
+ //合同段列表数据
|
|
|
|
+ JSONArray data = (JSONArray) post.getData();
|
|
|
|
+
|
|
|
|
+ List<String> codes = new ArrayList<>();
|
|
|
|
+
|
|
|
|
+ //返回的数据不能为空
|
|
|
|
+ if (CollectionUtil.isNotEmpty(data)) {
|
|
|
|
+ for (Object datum : data) {
|
|
|
|
+ JSONObject jsonObject1 = (JSONObject) datum;
|
|
|
|
+ String code = jsonObject1.getString("Code");
|
|
|
|
+ //编码不能为空
|
|
|
|
+ if (StringUtil.isNotBlank(code)) {
|
|
|
|
+ codes.add(code);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ //封装需要查询的合同段
|
|
|
|
+ if (CollectionUtil.isNotEmpty(codes)) {
|
|
|
|
+ //使用参数化查询防止SQL注入
|
|
|
|
+ StringBuilder sqlBuilder = new StringBuilder("select * from m_contract_info where contract_number in (");
|
|
|
|
+ for (int i = 0; i < codes.size(); i++) {
|
|
|
|
+ sqlBuilder.append(i == 0 ? "?" : ",?");
|
|
|
|
+ }
|
|
|
|
+ sqlBuilder.append(")");
|
|
|
|
+
|
|
|
|
+ //查询数据
|
|
|
|
+ List<ContractInfo> contractInfoList = jdbcTemplate.queryForList(sqlBuilder.toString(), ContractInfo.class, codes.toArray());
|
|
|
|
+ contractInfoList.forEach(f -> {
|
|
|
|
+ contractCodeMap.put(f.getContractNumber(), f.getId());
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ log.error("---------#成渝第三方接口访问失败:" + post.getMessage());
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 获取标段已完成(已签章完成)的检测报告
|
|
|
|
+ */
|
|
|
|
+ public void getFinishTestReport() {
|
|
|
|
+ String url = "/open/TestReport/GetFinishTestReport";
|
|
|
|
+
|
|
|
|
+ String token = bladeRedis.get(TOKEN_REDIS_KEY);
|
|
|
|
+ if (token == null) {
|
|
|
|
+ token = thirdLogin();
|
|
|
|
+ }
|
|
|
|
+ if (token == null) {
|
|
|
|
+ log.error("---------#成渝第三方接口访问失败:token为空");
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ if (PROJECT_ID == null) {
|
|
|
|
+ log.error("---------#成渝第三方接口访问失败:项目不存在");
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ //获取需要同步的合同段信息
|
|
|
|
+ Set<String> contractCodes = contractCodeMap.keySet();
|
|
|
|
+ if (CollectionUtil.isEmpty(contractCodes)) {
|
|
|
|
+ log.error("---------#成渝第三方接口访问失败:合同段编码无数据");
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ for (String contractCode : contractCodes) {
|
|
|
|
+ GetFinishTestReport getFinishTestReport = new GetFinishTestReport(token, PROJECT_CODE, contractCode);
|
|
|
|
+
|
|
|
|
+ Integer pageIndex = 0;
|
|
|
|
+ Integer pageSize = 100;
|
|
|
|
+
|
|
|
|
+ do {
|
|
|
|
+ getFinishTestReport.setPageIndex(pageIndex);
|
|
|
|
+ getFinishTestReport.setPageSize(pageSize);
|
|
|
|
+
|
|
|
|
+ Result post = this.post(URL + url, getFinishTestReport);
|
|
|
|
+ if (post == null) {
|
|
|
|
+ log.error("---------#成渝第三方接口访问失败:接口访问失败, contractCode: {}", contractCode);
|
|
|
|
+ break; // 继续处理下一个合同段
|
|
|
|
+ }
|
|
|
|
+ Integer state = post.getState();
|
|
|
|
+ //成功
|
|
|
|
+ if (state == 1) {
|
|
|
|
+ //检测报告列表
|
|
|
|
+ JSONArray data = (JSONArray) post.getData();
|
|
|
|
+ List<FinishTestReport> list = data.toJavaList(FinishTestReport.class);
|
|
|
|
+ //退出循环
|
|
|
|
+ if (CollectionUtil.isEmpty(list)) {
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ list.forEach(f -> f.setContractId(contractCodeMap.get(contractCode)));
|
|
|
|
+ //处理数据
|
|
|
|
+ trialCyFinishTestReportService.saveBatch(list);
|
|
|
|
+ if (list.size() < pageSize) {
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ pageIndex++;
|
|
|
|
+ } else {
|
|
|
|
+ log.error("---------#成渝第三方接口访问失败:{}, contractCode: {}", post.getMessage(), contractCode);
|
|
|
|
+ break; // 继续处理下一个合同段
|
|
|
|
+ }
|
|
|
|
+ } while (true);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 获取已上传的外委检测报告/第三方检测报告/上传报告
|
|
|
|
+ */
|
|
|
|
+ public void getThirdReport() {
|
|
|
|
+ String url = "/open/ThirdReport/GetThirdReport";
|
|
|
|
+
|
|
|
|
+ String token = bladeRedis.get(TOKEN_REDIS_KEY);
|
|
|
|
+ if (token == null) {
|
|
|
|
+ token = thirdLogin();
|
|
|
|
+ }
|
|
|
|
+ if (token == null) {
|
|
|
|
+ log.error("---------#成渝第三方接口访问失败:token为空");
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ if (PROJECT_ID == null) {
|
|
|
|
+ log.error("---------#成渝第三方接口访问失败:项目不存在");
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ //获取需要同步的合同段信息
|
|
|
|
+ Set<String> contractCodes = contractCodeMap.keySet();
|
|
|
|
+ if (CollectionUtil.isEmpty(contractCodes)) {
|
|
|
|
+ log.error("---------#成渝第三方接口访问失败:合同段编码无数据");
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ for (String contractCode : contractCodes) {
|
|
|
|
+ GetThirdReport getThirdReport = new GetThirdReport(token, PROJECT_CODE, contractCode);
|
|
|
|
+
|
|
|
|
+ Integer pageIndex = 0;
|
|
|
|
+ int pageSize = 100;
|
|
|
|
+
|
|
|
|
+ do {
|
|
|
|
+ getThirdReport.setPageIndex(pageIndex);
|
|
|
|
+ getThirdReport.setPageSize(pageSize);
|
|
|
|
+
|
|
|
|
+ Result post = this.post(URL + url, getThirdReport);
|
|
|
|
+ if (post == null) {
|
|
|
|
+ log.error("---------#成渝第三方接口访问失败:接口访问失败, contractCode: {}", contractCode);
|
|
|
|
+ break; // 继续处理下一个合同段
|
|
|
|
+ }
|
|
|
|
+ Integer state = post.getState();
|
|
|
|
+ //成功
|
|
|
|
+ if (state == 1) {
|
|
|
|
+ //已上传的外委检测报告/第三方检测报告/上传报告
|
|
|
|
+ JSONArray data = (JSONArray) post.getData();
|
|
|
|
+ List<ThirdReport> list = data.toJavaList(ThirdReport.class);
|
|
|
|
+ //退出循环
|
|
|
|
+ if (CollectionUtil.isEmpty(list)) {
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ list.forEach(f -> f.setContractId(contractCodeMap.get(contractCode)));
|
|
|
|
+ //处理数据
|
|
|
|
+ trialCyThirdReportService.saveBatch(list);
|
|
|
|
+
|
|
|
|
+ if (list.size() < pageSize) {
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ pageIndex++;
|
|
|
|
+ } else {
|
|
|
|
+ log.error("---------#成渝第三方接口访问失败:{}, contractCode: {}", post.getMessage(), contractCode);
|
|
|
|
+ break; // 继续处理下一个合同段
|
|
|
|
+ }
|
|
|
|
+ } while (true);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 获取检测项目树(包括自建试验、外委、第三方、上传报告)
|
|
|
|
+ */
|
|
|
|
+ public void GetAllTestType() {
|
|
|
|
+ String url = "/open/ThirdReport/GetAllTestType";
|
|
|
|
+ String token = bladeRedis.get(TOKEN_REDIS_KEY);
|
|
|
|
+ if (token == null) {
|
|
|
|
+ token = thirdLogin();
|
|
|
|
+ }
|
|
|
|
+ if (token == null) {
|
|
|
|
+ log.error("---------#成渝第三方接口访问失败:token为空");
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ if (PROJECT_ID == null) {
|
|
|
|
+ log.error("---------#成渝第三方接口访问失败:项目不存在");
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ JSONObject jsonObject = new JSONObject();
|
|
|
|
+ jsonObject.put("token", token);
|
|
|
|
+ jsonObject.put("ProjectCode", PROJECT_CODE);
|
|
|
|
+
|
|
|
|
+ Result post = this.post(URL + url, jsonObject);
|
|
|
|
+
|
|
|
|
+ if (post == null) {
|
|
|
|
+ log.error("---------#成渝第三方接口访问失败:接口访问失败");
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ Integer state = post.getState();
|
|
|
|
+ //成功
|
|
|
|
+ if (state == 1) {
|
|
|
|
+ //合同段列表数据
|
|
|
|
+ JSONArray data = (JSONArray) post.getData();
|
|
|
|
+ List<TestType> list = data.toJavaList(TestType.class);
|
|
|
|
+ //退出循环
|
|
|
|
+ if (CollectionUtil.isEmpty(list)) {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ list.forEach(f -> f.setProjectId(PROJECT_ID));
|
|
|
|
+ //处理数据
|
|
|
|
+ trialCyTestTypeService.saveOrUpdateBatch(list);
|
|
|
|
+ } else {
|
|
|
|
+ log.error("---------#成渝第三方接口访问失败:" + post.getMessage());
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 封装请求参数
|
|
|
|
+ */
|
|
|
|
+ public Result post(String url, Object object) {
|
|
|
|
+ HttpPost httpPost = new HttpPost(url);
|
|
|
|
+ httpPost.setHeader("Content-Type", "application/json");
|
|
|
|
+
|
|
|
|
+ // 对象转json
|
|
|
|
+ StringEntity json = new StringEntity(JSON.toJSONString(object), "utf-8");
|
|
|
|
+ httpPost.setEntity(json);
|
|
|
|
+
|
|
|
|
+ try {
|
|
|
|
+ HttpResponse httpResponse = httpClient.execute(httpPost);
|
|
|
|
+
|
|
|
|
+ // 检查响应状态码
|
|
|
|
+ int statusCode = httpResponse.getStatusLine().getStatusCode();
|
|
|
|
+ if (statusCode != 200) {
|
|
|
|
+ log.warn("HTTP request failed with status code: {}", statusCode);
|
|
|
|
+ // 可以根据具体业务需求调整错误处理方式
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ try (InputStream inputStream = httpResponse.getEntity().getContent();
|
|
|
|
+ ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
|
|
|
|
+
|
|
|
|
+ byte[] buffer = new byte[1024];
|
|
|
|
+ int index = 0;
|
|
|
|
+ while ((index = inputStream.read(buffer)) != -1) {
|
|
|
|
+ outputStream.write(buffer, 0, index);
|
|
|
|
+ }
|
|
|
|
+ // 响应参数 - 指定明确的字符编码
|
|
|
|
+ Result result = JSON.parseObject(outputStream.toString("utf-8"), Result.class);
|
|
|
|
+ return result;
|
|
|
|
+ }
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ log.error("HTTP request failed: ", e);
|
|
|
|
+ // 返回错误结果或抛出异常,根据业务需求决定
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+}
|