|
@@ -0,0 +1,628 @@
|
|
|
|
+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 com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
+import org.apache.http.Consts;
|
|
|
|
+import org.apache.http.HttpEntity;
|
|
|
|
+import org.apache.http.HttpResponse;
|
|
|
|
+import org.apache.http.NameValuePair;
|
|
|
|
+import org.apache.http.client.entity.UrlEncodedFormEntity;
|
|
|
|
+import org.apache.http.client.methods.CloseableHttpResponse;
|
|
|
|
+import org.apache.http.client.methods.HttpGet;
|
|
|
|
+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.apache.http.impl.client.HttpClients;
|
|
|
|
+import org.apache.http.message.BasicNameValuePair;
|
|
|
|
+import org.apache.http.util.EntityUtils;
|
|
|
|
+import org.springblade.business.entity.TrialCyAccessories;
|
|
|
|
+import org.springblade.business.entity.TrialCyFinishTestReport;
|
|
|
|
+import org.springblade.business.entity.TrialCyTestType;
|
|
|
|
+import org.springblade.business.entity.TrialCyThirdReport;
|
|
|
|
+import org.springblade.business.scheduled.vo.*;
|
|
|
|
+import org.springblade.business.service.TrialCyAccessoriesService;
|
|
|
|
+import org.springblade.business.service.TrialCyFinishTestReportService;
|
|
|
|
+import org.springblade.business.service.TrialCyTestTypeService;
|
|
|
|
+import org.springblade.business.service.TrialCyThirdReportService;
|
|
|
|
+import org.springblade.common.utils.SnowFlakeUtil;
|
|
|
|
+import org.springblade.common.utils.SystemUtils;
|
|
|
|
+import org.springblade.core.redis.cache.BladeRedis;
|
|
|
|
+import org.springblade.core.tool.api.R;
|
|
|
|
+import org.springblade.core.tool.utils.CollectionUtil;
|
|
|
|
+import org.springblade.core.tool.utils.DigestUtil;
|
|
|
|
+import org.springblade.core.tool.utils.StringUtil;
|
|
|
|
+import org.springblade.manager.entity.ContractInfo;
|
|
|
|
+import org.springblade.manager.entity.ProjectInfo;
|
|
|
|
+import org.springframework.jdbc.core.BeanPropertyRowMapper;
|
|
|
|
+import org.springframework.jdbc.core.JdbcTemplate;
|
|
|
|
+import org.springframework.scheduling.annotation.Scheduled;
|
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
|
+
|
|
|
|
+import javax.annotation.PostConstruct;
|
|
|
|
+import javax.annotation.PreDestroy;
|
|
|
|
+import javax.annotation.Resource;
|
|
|
|
+import java.io.ByteArrayOutputStream;
|
|
|
|
+import java.io.IOException;
|
|
|
|
+import java.io.InputStream;
|
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
|
+import java.util.*;
|
|
|
|
+import java.util.concurrent.ConcurrentHashMap;
|
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * 成渝试验第三方接口获取数据
|
|
|
|
+ * 定时任务
|
|
|
|
+ *
|
|
|
|
+ * @author LHB
|
|
|
|
+ */
|
|
|
|
+@Component
|
|
|
|
+@Slf4j
|
|
|
|
+public class ChenYuTestScheduled {
|
|
|
|
+
|
|
|
|
+ // 接口URL常量
|
|
|
|
+ private static final String THIRD_LOGIN_URL = "/api/ThirdSystem/ThirdLogin";
|
|
|
|
+ private static final String CONTRACT_SECT_URL = "/api/CQHCZC/MesureSystem/GetContractSectByProjCode";
|
|
|
|
+ private static final String FINISH_TEST_REPORT_URL = "/open/TestReport/GetFinishTestReport";
|
|
|
|
+ private static final String THIRD_REPORT_URL = "/open/ThirdReport/GetThirdReport";
|
|
|
|
+ private static final String ALL_TEST_TYPE_URL = "/open/ThirdReport/GetAllTestType";
|
|
|
|
+
|
|
|
|
+ // SQL常量
|
|
|
|
+ private static final String PROJECT_ID_SQL = "select id from m_project_info where project_number = ? and is_deleted = 0 limit 1";
|
|
|
|
+ private static final String APP_KEY_SQL = "select param_value from blade_param where param_key = 'cy.trial.appKey' and is_deleted = 0";
|
|
|
|
+ private static final String APP_SECRET_SQL = "select param_value from blade_param where param_key = 'cy.trial.appSecret' and is_deleted = 0";
|
|
|
|
+
|
|
|
|
+ @Resource
|
|
|
|
+ private JdbcTemplate jdbcTemplate;
|
|
|
|
+
|
|
|
|
+ @Resource
|
|
|
|
+ private BladeRedis bladeRedis;
|
|
|
|
+
|
|
|
|
+ @Resource
|
|
|
|
+ private TrialCyFinishTestReportService trialCyFinishTestReportService;
|
|
|
|
+
|
|
|
|
+ @Resource
|
|
|
|
+ private TrialCyTestTypeService trialCyTestTypeService;
|
|
|
|
+
|
|
|
|
+ @Resource
|
|
|
|
+ private TrialCyThirdReportService trialCyThirdReportService;
|
|
|
|
+
|
|
|
|
+ @Resource
|
|
|
|
+ private TrialCyAccessoriesService trialCyAccessoriesService;
|
|
|
|
+
|
|
|
|
+ private CloseableHttpClient httpClient;
|
|
|
|
+ private static final String TOKEN_REDIS_KEY = "blade:cqcy:token";
|
|
|
|
+ private static final String PROJECT_CODE = "cqcyfx";
|
|
|
|
+ private static Long PROJECT_ID = null;
|
|
|
|
+ private static final String BASE_URL = "https://apicqcyfx.qdm123.com";
|
|
|
|
+ private final Map<String, Long> contractCodeMap = new ConcurrentHashMap<>();
|
|
|
|
+
|
|
|
|
+ @PostConstruct
|
|
|
|
+ public void init() {
|
|
|
|
+ this.httpClient = HttpClientBuilder.create().build();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @PreDestroy
|
|
|
|
+ public void destroy() {
|
|
|
|
+ if (httpClient != null) {
|
|
|
|
+ try {
|
|
|
|
+ httpClient.close();
|
|
|
|
+ log.info("HTTP client closed successfully");
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ log.error("Error closing HTTP client", e);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 确保项目ID已初始化
|
|
|
|
+ */
|
|
|
|
+ private synchronized void ensureProjectIdInitialized() {
|
|
|
|
+ if (PROJECT_ID != null) return;
|
|
|
|
+
|
|
|
|
+ try {
|
|
|
|
+ List<ProjectInfo> projectList = jdbcTemplate.query(
|
|
|
|
+ PROJECT_ID_SQL,
|
|
|
|
+ new Object[]{PROJECT_CODE},
|
|
|
|
+ new BeanPropertyRowMapper<>(ProjectInfo.class)
|
|
|
|
+ );
|
|
|
|
+
|
|
|
|
+ if (projectList != null && !projectList.isEmpty()) {
|
|
|
|
+ PROJECT_ID = projectList.get(0).getId();
|
|
|
|
+ log.info("Project ID initialized: {}", PROJECT_ID);
|
|
|
|
+ } else {
|
|
|
|
+ log.error("Project not found for code: {}", PROJECT_CODE);
|
|
|
|
+ }
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ log.error("Error initializing project ID", e);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 获取有效的Token
|
|
|
|
+ */
|
|
|
|
+ private String getToken() {
|
|
|
|
+ String token = bladeRedis.get(TOKEN_REDIS_KEY);
|
|
|
|
+ if (token != null) {
|
|
|
|
+ return token;
|
|
|
|
+ }
|
|
|
|
+ return thirdLogin();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 登录获取Token
|
|
|
|
+ */
|
|
|
|
+ private String thirdLogin() {
|
|
|
|
+ ensureProjectIdInitialized();
|
|
|
|
+ if (PROJECT_ID == null) {
|
|
|
|
+ log.error("Project ID not available for login");
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ try {
|
|
|
|
+ String appKey = jdbcTemplate.queryForObject(APP_KEY_SQL, String.class);
|
|
|
|
+ String appSecret = jdbcTemplate.queryForObject(APP_SECRET_SQL, String.class);
|
|
|
|
+
|
|
|
|
+ if (StringUtil.isBlank(appKey) || StringUtil.isBlank(appSecret)) {
|
|
|
|
+ log.error("Missing appKey or appSecret configuration");
|
|
|
|
+ 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 result = postToken(BASE_URL + THIRD_LOGIN_URL, thirdLogin);
|
|
|
|
+ if (result == null || result.getState() != 1) {
|
|
|
|
+ log.error("Login failed: {}", result != null ? result.getMessage() : "No response");
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ JSONObject data = (JSONObject) result.getData();
|
|
|
|
+ String newToken = data.getString("token");
|
|
|
|
+ long expire = data.getLong("expireSecond") - 60;
|
|
|
|
+ bladeRedis.setEx(TOKEN_REDIS_KEY, newToken, expire);
|
|
|
|
+ return newToken;
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ log.error("Login process failed", e);
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 获取合同段列表
|
|
|
|
+ */
|
|
|
|
+ @Scheduled(cron = "0 0 * * * ?")
|
|
|
|
+ public void getContractSectByProjCode() {
|
|
|
|
+ if (!SystemUtils.isLinux()) {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ contractCodeMap.clear();
|
|
|
|
+ String token = getToken();
|
|
|
|
+ ensureProjectIdInitialized();
|
|
|
|
+
|
|
|
|
+ if (token == null || PROJECT_ID == null) {
|
|
|
|
+ log.error("Invalid token or project ID");
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ try {
|
|
|
|
+ //获取合同段列表接口
|
|
|
|
+ Result result = getContractList();
|
|
|
|
+ if (result == null || result.getState() != 1) {
|
|
|
|
+ log.error("Failed to get contract sections: {}",
|
|
|
|
+ result != null ? result.getMessage() : "No response");
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ JSONArray data = (JSONArray) result.getData();
|
|
|
|
+ if (CollectionUtil.isEmpty(data)) {
|
|
|
|
+ log.warn("No contract sections found");
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ List<String> codes = new ArrayList<>();
|
|
|
|
+ for (int i = 0; i < data.size(); i++) {
|
|
|
|
+ JSONObject obj = data.getJSONObject(i);
|
|
|
|
+ String name = obj.getString("Name");
|
|
|
|
+ if (StringUtil.isNotBlank(name)) {
|
|
|
|
+ codes.add(name);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (CollectionUtil.isEmpty(codes)) {
|
|
|
|
+ log.info("No valid contract codes found");
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 构建参数化查询
|
|
|
|
+ String placeholders = String.join(",", Collections.nCopies(codes.size(), "?"));
|
|
|
|
+ String sql = "SELECT * FROM m_contract_info WHERE contract_number IN (" + placeholders + ")";
|
|
|
|
+
|
|
|
|
+ List<ContractInfo> contracts = jdbcTemplate.query(
|
|
|
|
+ sql,
|
|
|
|
+ codes.toArray(),
|
|
|
|
+ new BeanPropertyRowMapper<>(ContractInfo.class)
|
|
|
|
+ );
|
|
|
|
+
|
|
|
|
+ for (ContractInfo contract : contracts) {
|
|
|
|
+ contractCodeMap.put(contract.getContractNumber(), contract.getId());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ log.info("Loaded {} contract mappings", contractCodeMap.size());
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ log.error("Error processing contract sections", e);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 获取已完成的检测报告
|
|
|
|
+ */
|
|
|
|
+ @Scheduled(cron = "0 0 * * * ?")
|
|
|
|
+ public void getFinishTestReport() {
|
|
|
|
+ if (!SystemUtils.isLinux()) {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ String token = getToken();
|
|
|
|
+ ensureProjectIdInitialized();
|
|
|
|
+
|
|
|
|
+ if (token == null || PROJECT_ID == null || contractCodeMap.isEmpty()) {
|
|
|
|
+ log.error("Preconditions not met for finish test report");
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ for (Map.Entry<String, Long> entry : contractCodeMap.entrySet()) {
|
|
|
|
+ String contractCode = entry.getKey();
|
|
|
|
+ Long contractId = entry.getValue();
|
|
|
|
+
|
|
|
|
+ //查询旧数据
|
|
|
|
+ List<TrialCyFinishTestReport> list = trialCyFinishTestReportService.list(Wrappers.<TrialCyFinishTestReport>lambdaQuery()
|
|
|
|
+ .eq(TrialCyFinishTestReport::getContractId, contractId));
|
|
|
|
+ //等会准备删除的历史数据
|
|
|
|
+ List<Long> ids = list.stream().map(TrialCyFinishTestReport::getId).collect(Collectors.toList());
|
|
|
|
+
|
|
|
|
+ int pageIndex = 0;
|
|
|
|
+ final int pageSize = 100;
|
|
|
|
+ boolean hasMore = true;
|
|
|
|
+
|
|
|
|
+ while (hasMore) {
|
|
|
|
+ try {
|
|
|
|
+ List<NameValuePair> params = new ArrayList<NameValuePair>();
|
|
|
|
+ params.add(new BasicNameValuePair("ProjectCode", PROJECT_CODE));
|
|
|
|
+ params.add(new BasicNameValuePair("ContractsectCode", contractCode));
|
|
|
|
+ //这些参数未找到传递的值
|
|
|
|
+ if (false) {
|
|
|
|
+ params.add(new BasicNameValuePair("BeginTime", null));
|
|
|
|
+ params.add(new BasicNameValuePair("EndTime", null));
|
|
|
|
+ params.add(new BasicNameValuePair("FinishBeginTime", null));
|
|
|
|
+ params.add(new BasicNameValuePair("FinishEndTime", null));
|
|
|
|
+ }
|
|
|
|
+ params.add(new BasicNameValuePair("PageIndex", String.valueOf(pageIndex)));
|
|
|
|
+ params.add(new BasicNameValuePair("PageSize", String.valueOf(pageSize)));
|
|
|
|
+
|
|
|
|
+ ResultPost result = post(BASE_URL + FINISH_TEST_REPORT_URL, params);
|
|
|
|
+
|
|
|
|
+ if (result == null) {
|
|
|
|
+ log.error("Request failed for contract: {}", contractCode);
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (result.getCode() != 200) {
|
|
|
|
+ log.error("API error for contract {}: {}", contractCode, result.getMsg());
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ JSONArray data = (JSONArray) result.getResult();
|
|
|
|
+ if (CollectionUtil.isEmpty(data)) {
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ List<TrialCyFinishTestReport> reports = data.toJavaList(TrialCyFinishTestReport.class);
|
|
|
|
+ for (TrialCyFinishTestReport report : reports) {
|
|
|
|
+ Long id = SnowFlakeUtil.getId();
|
|
|
|
+ report.setId(id);
|
|
|
|
+ report.setContractId(contractId);
|
|
|
|
+ //处理boolean值
|
|
|
|
+ report.setExperimentResult(String.valueOf(report.getExperimentResult() == null ? 0 : "True".equals(report.getExperimentResult()) ? 1 : 0));
|
|
|
|
+
|
|
|
|
+ //处理附件
|
|
|
|
+ List<TrialCyAccessories> accessories = report.getAccessories();
|
|
|
|
+ if (CollectionUtil.isNotEmpty(accessories)) {
|
|
|
|
+ accessories.forEach(f -> {
|
|
|
|
+ f.setId(SnowFlakeUtil.getId());
|
|
|
|
+ f.setReportId(id);
|
|
|
|
+ });
|
|
|
|
+
|
|
|
|
+ if (CollectionUtil.isNotEmpty(ids)) {
|
|
|
|
+ trialCyAccessoriesService.remove(Wrappers.<TrialCyAccessories>lambdaQuery()
|
|
|
|
+ .in(TrialCyAccessories::getReportId, ids));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ trialCyAccessoriesService.saveBatch(accessories);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ if (CollectionUtil.isNotEmpty(ids)) {
|
|
|
|
+ trialCyFinishTestReportService.removeBatchByIds(ids);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ trialCyFinishTestReportService.saveBatch(reports);
|
|
|
|
+ hasMore = reports.size() >= pageSize;
|
|
|
|
+ pageIndex++;
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ log.error("Error processing contract {} page {}", contractCode, pageIndex, e);
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 获取第三方报告
|
|
|
|
+ */
|
|
|
|
+ @Scheduled(cron = "0 0 * * * ?")
|
|
|
|
+ public void getThirdReport() {
|
|
|
|
+ if (!SystemUtils.isLinux()) {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ Integer[] thirdTypes = {0,1,2};
|
|
|
|
+
|
|
|
|
+ String token = getToken();
|
|
|
|
+ ensureProjectIdInitialized();
|
|
|
|
+
|
|
|
|
+ if (token == null || PROJECT_ID == null || contractCodeMap.isEmpty()) {
|
|
|
|
+ log.error("Preconditions not met for third report");
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ for (Map.Entry<String, Long> entry : contractCodeMap.entrySet()) {
|
|
|
|
+ String contractCode = entry.getKey();
|
|
|
|
+ Long contractId = entry.getValue();
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ //获取旧id
|
|
|
|
+ List<TrialCyThirdReport> list = trialCyThirdReportService.list(Wrappers.<TrialCyThirdReport>lambdaQuery()
|
|
|
|
+ .eq(TrialCyThirdReport::getContractId, contractId));
|
|
|
|
+ //等会要删除的id
|
|
|
|
+ List<Long> ids = list.stream().map(TrialCyThirdReport::getPKeyId).collect(Collectors.toList());
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ int pageIndex = 0;
|
|
|
|
+ final int pageSize = 100;
|
|
|
|
+ boolean hasMore = true;
|
|
|
|
+
|
|
|
|
+ for (Integer thirdType : thirdTypes) {
|
|
|
|
+ while (hasMore) {
|
|
|
|
+ try {
|
|
|
|
+ List<NameValuePair> params = new ArrayList<NameValuePair>();
|
|
|
|
+ params.add(new BasicNameValuePair("ProjectCode", PROJECT_CODE));
|
|
|
|
+ params.add(new BasicNameValuePair("ContractsectCode", contractCode));
|
|
|
|
+ params.add(new BasicNameValuePair("ThirdType", String.valueOf(thirdType)));
|
|
|
|
+ //这些参数为找到传递值
|
|
|
|
+ if (false) {
|
|
|
|
+ params.add(new BasicNameValuePair("BeginTime", null));
|
|
|
|
+ params.add(new BasicNameValuePair("EndTime", null));
|
|
|
|
+ }
|
|
|
|
+ params.add(new BasicNameValuePair("PageIndex", String.valueOf(pageIndex)));
|
|
|
|
+ params.add(new BasicNameValuePair("PageSize", String.valueOf(pageSize)));
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ ResultPost result = post(BASE_URL + THIRD_REPORT_URL, params);
|
|
|
|
+
|
|
|
|
+ if (result == null) {
|
|
|
|
+ log.error("Request failed for contract: {}", contractCode);
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (result.getCode() != 200) {
|
|
|
|
+ log.error("API error for contract {}: {}", contractCode, result.getMsg());
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ JSONArray data = (JSONArray) result.getResult();
|
|
|
|
+ if (CollectionUtil.isEmpty(data)) {
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ List<TrialCyThirdReport> reports = data.toJavaList(TrialCyThirdReport.class);
|
|
|
|
+ for (TrialCyThirdReport report : reports) {
|
|
|
|
+ Long id = SnowFlakeUtil.getId();
|
|
|
|
+ report.setPKeyId(id);
|
|
|
|
+ report.setContractId(contractId);
|
|
|
|
+ //报告类型
|
|
|
|
+ report.setThirdType(thirdType);
|
|
|
|
+ //处理boolean值
|
|
|
|
+ report.setIsDeleted(String.valueOf(report.getIsDeleted() == null ? '1' : "True".equals(report.getIsDeleted()) ? '1' : '0'));
|
|
|
|
+ report.setIsQualified(String.valueOf(report.getIsQualified() == null ? '0' : "True".equals(report.getIsQualified()) ? '1' : '0'));
|
|
|
|
+
|
|
|
|
+ //处理附件
|
|
|
|
+ List<TrialCyAccessories> accessories = report.getAccessories();
|
|
|
|
+ if (CollectionUtil.isNotEmpty(accessories)) {
|
|
|
|
+ accessories.forEach(f -> {
|
|
|
|
+ f.setId(SnowFlakeUtil.getId());
|
|
|
|
+ f.setReportId(id);
|
|
|
|
+ });
|
|
|
|
+ //删除旧数据
|
|
|
|
+ if (CollectionUtil.isNotEmpty(ids)) {
|
|
|
|
+ trialCyAccessoriesService.remove(Wrappers.<TrialCyAccessories>lambdaQuery()
|
|
|
|
+ .in(TrialCyAccessories::getReportId, ids));
|
|
|
|
+ }
|
|
|
|
+ //添加新数据
|
|
|
|
+ trialCyAccessoriesService.saveBatch(accessories);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ //删除旧数据
|
|
|
|
+ if (CollectionUtil.isNotEmpty(ids)) {
|
|
|
|
+ trialCyThirdReportService.removeBatchByIds(ids);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //添加新数据
|
|
|
|
+ trialCyThirdReportService.saveBatch(reports);
|
|
|
|
+ hasMore = reports.size() >= pageSize;
|
|
|
|
+ pageIndex++;
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ log.error("Error processing contract {} page {}", contractCode, pageIndex, e);
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 获取检测项目树 TODO 第三方接口未开发完(待调试)
|
|
|
|
+ */
|
|
|
|
+ @Scheduled(cron = "0 0 * * * ?")
|
|
|
|
+ public void getAllTestType() {
|
|
|
|
+ if (!SystemUtils.isLinux()) {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ try {
|
|
|
|
+ String token = getToken();
|
|
|
|
+ ensureProjectIdInitialized();
|
|
|
|
+
|
|
|
|
+ if (token == null || PROJECT_ID == null) {
|
|
|
|
+ log.error("Invalid token or project ID");
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ List<NameValuePair> params = new ArrayList<NameValuePair>();
|
|
|
|
+ params.add(new BasicNameValuePair("ProjectCode", PROJECT_CODE));
|
|
|
|
+
|
|
|
|
+ ResultPost result = post(BASE_URL + ALL_TEST_TYPE_URL, params);
|
|
|
|
+ if (result == null || result.getCode() != 200) {
|
|
|
|
+ log.error("Failed to get test types: {}",
|
|
|
|
+ result != null ? result.getMsg() : "No response");
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ JSONArray data = (JSONArray) result.getResult();
|
|
|
|
+ if (CollectionUtil.isEmpty(data)) {
|
|
|
|
+ log.info("No test types found");
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ List<TrialCyTestType> testTypes = data.toJavaList(TrialCyTestType.class);
|
|
|
|
+ for (TrialCyTestType type : testTypes) {
|
|
|
|
+ type.setPKeyId(SnowFlakeUtil.getId());
|
|
|
|
+ type.setProjectId(PROJECT_ID);
|
|
|
|
+ type.setFatherID(type.getFatherID() == null || type.getFatherID().isEmpty() ? "0" : type.getFatherID());
|
|
|
|
+ }
|
|
|
|
+ //先删除旧数据
|
|
|
|
+ trialCyTestTypeService.remove(Wrappers.<TrialCyTestType>lambdaQuery()
|
|
|
|
+ .eq(TrialCyTestType::getProjectId, PROJECT_ID));
|
|
|
|
+ trialCyTestTypeService.saveOrUpdateBatch(testTypes);
|
|
|
|
+ log.info("Saved {} test types", testTypes.size());
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ log.error("Error processing test types", e);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * HTTP POST请求 获取token
|
|
|
|
+ */
|
|
|
|
+ private Result postToken(String url, ThirdLogin payload) {
|
|
|
|
+ HttpPost httpPost = new HttpPost(url);
|
|
|
|
+ httpPost.setHeader("Content-Type", "application/json");
|
|
|
|
+ try {
|
|
|
|
+ httpPost.setEntity(new StringEntity(JSON.toJSONString(payload), StandardCharsets.UTF_8));
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ log.error("Failed to set request entity", e);
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ try (ByteArrayOutputStream output = new ByteArrayOutputStream()) {
|
|
|
|
+ HttpResponse response = httpClient.execute(httpPost);
|
|
|
|
+ int status = response.getStatusLine().getStatusCode();
|
|
|
|
+
|
|
|
|
+ if (status != 200) {
|
|
|
|
+ log.warn("HTTP error: {} - {}", status, response.getStatusLine().getReasonPhrase());
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ try (InputStream input = response.getEntity().getContent()) {
|
|
|
|
+ byte[] buffer = new byte[1024];
|
|
|
|
+ int bytesRead;
|
|
|
|
+ while ((bytesRead = input.read(buffer)) != -1) {
|
|
|
|
+ output.write(buffer, 0, bytesRead);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ String responseString = new String(output.toByteArray(), StandardCharsets.UTF_8);
|
|
|
|
+ return JSON.parseObject(responseString, Result.class);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ log.error("HTTP request failed: {}", url, e);
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * HTTP POST请求
|
|
|
|
+ */
|
|
|
|
+ private ResultPost post(String url, List<NameValuePair> params) {
|
|
|
|
+ HttpPost httpPost = new HttpPost(url);
|
|
|
|
+ httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
|
|
|
|
+ httpPost.setHeader("token", getToken());
|
|
|
|
+ try {
|
|
|
|
+ httpPost.setEntity(new UrlEncodedFormEntity(params, Consts.UTF_8));
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ log.error("Failed to set request entity", e);
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ try (ByteArrayOutputStream output = new ByteArrayOutputStream()) {
|
|
|
|
+ HttpResponse response = httpClient.execute(httpPost);
|
|
|
|
+ int status = response.getStatusLine().getStatusCode();
|
|
|
|
+
|
|
|
|
+ if (status != 200) {
|
|
|
|
+ log.warn("HTTP error: {} - {}", status, response.getStatusLine().getReasonPhrase());
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ try (InputStream input = response.getEntity().getContent()) {
|
|
|
|
+ byte[] buffer = new byte[1024];
|
|
|
|
+ int bytesRead;
|
|
|
|
+ while ((bytesRead = input.read(buffer)) != -1) {
|
|
|
|
+ output.write(buffer, 0, bytesRead);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ String responseString = new String(output.toByteArray(), StandardCharsets.UTF_8);
|
|
|
|
+ return JSON.parseObject(responseString, ResultPost.class);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ log.error("HTTP request failed: {}", url, e);
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * HTTP get 请求 获取合同段数据
|
|
|
|
+ */
|
|
|
|
+ private Result getContractList() {
|
|
|
|
+ try {
|
|
|
|
+ CloseableHttpClient client = HttpClients.createDefault();
|
|
|
|
+ HttpGet httpGet = new HttpGet(BASE_URL + CONTRACT_SECT_URL + "?token=" + getToken() + "&ProjectCode=" + PROJECT_CODE);
|
|
|
|
+ CloseableHttpResponse response = client.execute(httpGet);
|
|
|
|
+
|
|
|
|
+ //5、获取实体
|
|
|
|
+ HttpEntity entity = response.getEntity();
|
|
|
|
+ //将实体装成字符串
|
|
|
|
+ String string = EntityUtils.toString(entity);
|
|
|
|
+ return JSON.parseObject(string, Result.class);
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ log.error("HTTP request failed: {}", BASE_URL + CONTRACT_SECT_URL, e);
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+}
|