HandleData.java 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. package com.jyxt.getdatabyview;
  2. import com.jyxt.getdatabyview.view.repository.IBPSRepository;
  3. import com.jyxt.getdatabyview.view.repository.LISViewRepository;
  4. import org.slf4j.Logger;
  5. import org.slf4j.LoggerFactory;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.beans.factory.annotation.Value;
  8. import org.springframework.stereotype.Component;
  9. import java.sql.Timestamp;
  10. import java.time.LocalDateTime;
  11. import java.time.YearMonth;
  12. import java.time.format.DateTimeFormatter;
  13. import java.util.*;
  14. @Component
  15. public class HandleData {
  16. private static final Logger log = LoggerFactory.getLogger(GetDataByViewApplication.class);
  17. @Autowired
  18. private LISViewRepository lisViewRepository;
  19. @Autowired
  20. private IBPSRepository ibpsRepository;
  21. @Value("${showTestCodeName}")
  22. private String showTestCodeName;
  23. @Value("${manualQryMonth}")
  24. private String manualQryMonth;
  25. public static final Map<String, String> RerunSampleField = new HashMap<>();
  26. static {
  27. // 静态初始化块中填充数据
  28. RerunSampleField.put("CREATEBY", "bian_zhi_ren_");
  29. RerunSampleField.put("CREATETIME", "bian_zhi_shi_jian");
  30. RerunSampleField.put("SAMPLECODE", "biao_ben_bian_hao");
  31. // RerunSampleField.put("TESTCODE", "xiang_mu_id_");
  32. RerunSampleField.put("TESTCODENAME", "fu_jian_xiang_mu_");
  33. RerunSampleField.put("INITIALRESULT", "ce_ding_zhi_chu_j");
  34. RerunSampleField.put("RERUNRESULT", "ce_ding_zhi_fu_ji");
  35. RerunSampleField.put("REPORTRESULT", "bao_gao_zhi_");
  36. RerunSampleField.put("AUDITUSER", "jian_yan_zhe_");
  37. RerunSampleField.put("REPORTTIME", "bao_gao_shi_jian_");
  38. RerunSampleField.put("RERUNMETHODNOTE", "bei_zhu_fu_jian_f");
  39. RerunSampleField.put("PATNAME", "xing_ming_");
  40. RerunSampleField.put("RERUNMETHOD", "fu_jian_fang_shi_");
  41. RerunSampleField.put("UNIT", "dan_wei_");
  42. }
  43. public void startHandleData(List<Map<String, Object>> retList, String lisTable) {
  44. List<Map<String, Object>> existList = ibpsRepository.qryExistData(lisTable);
  45. retList = cleanExistData(lisTable,retList,existList);
  46. log.info("table:"+lisTable+" get data:"+retList.size());
  47. if (lisTable.equals("V_JT_TestCodeRerunRecord")){
  48. log.info("ready to get rerun testcode");
  49. HandleRerunSample(retList);
  50. }
  51. if (lisTable.equals("V_JT_QCMonthReport")){
  52. log.info("ready to get QC month report");
  53. HandleMonthReport(retList);
  54. }
  55. }
  56. public List<Map<String, Object>> cleanExistData(String lisTable, List<Map<String, Object>> lisList, List<Map<String, Object>> existList) {
  57. if (lisTable.equals("V_JT_TestCodeRerunRecord")){
  58. // 1. 创建Set存储existList中所有id_的值(转换为字符串形式)
  59. Set<String> existIdSet = new HashSet<>();
  60. for (Map<String, Object> existMap : existList) {
  61. Object idObj = existMap.get("biao_ben_bian_hao");
  62. existIdSet.add(idObj == null ? null : idObj.toString());
  63. }
  64. // 2. 过滤rerunTestList:只保留id不在existIdSet中的元素
  65. List<Map<String, Object>> resultList = new ArrayList<>();
  66. for (Map<String, Object> testMap : lisList) {
  67. Object idObj = testMap.get("SAMPLECODE");
  68. String idStr = idObj == null ? null : idObj.toString();
  69. // 如果当前条码不在existIdSet中,则保留
  70. if (!existIdSet.contains(idStr)) {
  71. resultList.add(testMap);
  72. }
  73. }
  74. return resultList;
  75. }
  76. if (lisTable.equals("V_JT_QCMonthReport")){
  77. // 1. 创建Set存储existList中所有id_的值(转换为字符串形式)
  78. Set<String> existIdSet = new HashSet<>();
  79. for (Map<String, Object> existMap : existList) {
  80. Object idObj = existMap.get("id_");
  81. existIdSet.add(idObj == null ? null : idObj.toString());
  82. }
  83. // 2. 过滤rerunTestList:只保留id不在existIdSet中的元素
  84. List<Map<String, Object>> resultList = new ArrayList<>();
  85. for (Map<String, Object> testMap : lisList) {
  86. Object idObj = testMap.get("REPORTID");
  87. String idStr = idObj == null ? null : idObj.toString();
  88. // 如果当前条码不在existIdSet中,则保留
  89. if (!existIdSet.contains(idStr)) {
  90. resultList.add(testMap);
  91. }
  92. }
  93. return resultList;
  94. }
  95. return null;
  96. }
  97. public void HandleRerunSample(List<Map<String, Object>> retList){
  98. if (retList.size() > 0) {
  99. String fieldName = null;
  100. String fieldValue = null;
  101. List<Map<String, Object>> insertList = new ArrayList<Map<String, Object>>();
  102. for (Map entityMap : retList) {
  103. Map<String, Object> insertMap = new HashMap<>();
  104. for (Object entry : entityMap.keySet()) {
  105. fieldName = null;
  106. fieldValue = null;
  107. fieldName = RerunSampleField.get(entry.toString());
  108. Object value = entityMap.get(entry.toString());
  109. fieldValue = value != null ? value.toString() : "";
  110. if (fieldName != null) {
  111. insertMap.put(fieldName, fieldValue);
  112. }
  113. }
  114. //补充其他字段信息
  115. insertMap.put("id_", UUID.randomUUID().toString());
  116. insertMap.put("shi_fou_guo_shen_", "已完成");
  117. String createUserInfo = ibpsRepository.getUserInfoByName(String.valueOf(entityMap.get("CREATEBY")));
  118. String auditUserInfo = ibpsRepository.getUserInfoByName(String.valueOf(entityMap.get("AUDITUSER")));
  119. if (createUserInfo.split("\\^")[0].equals("-1")) {
  120. log.info("error userInfo,skip to insert:"+createUserInfo);
  121. continue;
  122. }
  123. if (auditUserInfo.split("\\^")[0].equals("-1")) {
  124. log.info("error auditUserInfo,skip to insert:"+auditUserInfo);
  125. continue;
  126. }
  127. LocalDateTime currentDateTime = LocalDateTime.now();
  128. DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
  129. String formattedDateTime = currentDateTime.format(formatter2);
  130. insertMap.put("create_by_", createUserInfo.trim().split("@")[0]);
  131. insertMap.put("create_time_", Timestamp.valueOf(String.valueOf(formattedDateTime)));
  132. insertMap.put("bian_zhi_ren_", createUserInfo.trim().split("@")[0]);
  133. insertMap.put("jian_yan_zhe_", String.valueOf(entityMap.get("AUDITUSER")));
  134. insertMap.put("jian_yan_yuan_", auditUserInfo.trim().split("@")[0]);
  135. insertMap.put("ri_qi_", String.valueOf(entityMap.get("CREATETIME")).split(" ")[0]);
  136. insertMap.put("bian_zhi_bu_men_", createUserInfo.split("@")[1]);
  137. insertList.add(insertMap);
  138. }
  139. // insertList = lisViewRepository.query2("ss");
  140. String res = ibpsRepository.saveToTable(insertList, "t_fjbbjlb","1");
  141. if(res.equals("success")){
  142. log.info("success insert:"+insertList.size()+" datas");
  143. } else {
  144. log.info("fail insert");
  145. }
  146. }
  147. }
  148. public void HandleMonthReport(List<Map<String, Object>> retList){
  149. if (retList.size() > 0) {
  150. String fieldName = null;
  151. String fieldValue = null;
  152. List<Map<String, Object>> insertList = new ArrayList<Map<String, Object>>();
  153. for (Map entityMap : retList) {
  154. Map<String, Object> insertReportMap = new HashMap<>();
  155. insertReportMap.put("id_", entityMap.get("REPORTID"));
  156. insertReportMap.put("shi_fou_guo_shen_", "待推送");
  157. LocalDateTime currentDateTime = LocalDateTime.now();
  158. DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
  159. String formattedDateTime = currentDateTime.format(formatter);
  160. insertReportMap.put("create_time_", Timestamp.valueOf(String.valueOf(formattedDateTime)));
  161. String editUser = entityMap.get("CREATEBY").toString();
  162. String editUserInfo = ibpsRepository.getUserInfoByName(editUser);
  163. //insertReportMap.put("bian_zhi_shi_jian", String.valueOf(formattedDateTime));
  164. String posID = ibpsRepository.getPosiByCode(entityMap.get("GROUPCODE").toString());
  165. if ((posID != "-1")&&(!editUserInfo.split("\\^")[0].equals("-1"))) {
  166. insertReportMap.put("di_dian_", posID);
  167. insertReportMap.put("bian_zhi_bu_men_", posID);
  168. insertReportMap.put("bian_zhi_ren_", editUserInfo.split("@")[0]);
  169. } else{
  170. log.info("error editUserInfo or posID,skip to insert,posID:"+posID+" ,editUserInfo:"+editUserInfo);
  171. continue;
  172. }
  173. insertReportMap.put("xi_tong_ming_chen", entityMap.get("GROUPNAME")+"全组仪器");
  174. insertReportMap.put("ji_lu_bian_hao_", entityMap.get("CREATETIME").toString().substring(0, 4) + "-" + entityMap.get("CREATETIME").toString().substring(4));
  175. insertList.add(insertReportMap);
  176. }
  177. String res = ibpsRepository.saveToTable(insertList, "t_dlxmsnzkyfx", "1");
  178. if(res.equals("success")){
  179. log.info("table:t_dlxmsnzkyfx success insert:"+insertList.size()+" datas");
  180. handQCTestCode(insertList);
  181. } else {
  182. log.info("fail insert t_dlxmsnzkyfx");
  183. }
  184. }
  185. };
  186. public String handQCTestCode(List<Map<String, Object>> reportList) {
  187. LocalDateTime currentDateTime = LocalDateTime.now();
  188. DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
  189. String formattedDateTime = currentDateTime.format(formatter);
  190. Timestamp currentTimestamp = Timestamp.valueOf(currentDateTime);
  191. long sequenceCounter = 0;
  192. for (Map<String, Object> reportMap : reportList) {
  193. String reportID = getStringValue(reportMap, "id_");
  194. if (reportID == null || reportID.isEmpty()) {
  195. continue; // 跳过无效的报告ID
  196. }
  197. List<Map<String, Object>> retList = lisViewRepository.getQCTestCodeList(reportID);
  198. if (retList == null || retList.isEmpty()) {
  199. continue; // 跳过空结果
  200. }
  201. List<Map<String, Object>> insertList = new ArrayList<>();
  202. // 在循环外部定义序列号计数器
  203. for (Map<String, Object> entityMap : retList) {
  204. Map<String, Object> insertMap = new HashMap<>();
  205. // 生成有序ID:时间戳+自增序列号
  206. String orderedId = String.format("%d-%06d",
  207. currentTimestamp.getTime(), // 使用时间戳
  208. sequenceCounter++ // 自增序列号
  209. );
  210. insertMap.put("id_", orderedId);
  211. insertMap.put("parent_id_", getStringValue(entityMap, "REPORTID"));
  212. insertMap.put("create_time_", currentTimestamp); // 直接使用Timestamp,避免字符串转换
  213. // 简化条件判断
  214. String testCodeValue = showTestCodeName.equals("1") ?
  215. getStringValue(entityMap, "TESTCODENAME") :
  216. getStringValue(entityMap, "TESTCODE");
  217. insertMap.put("xiang_mu_", testCodeValue);
  218. // 使用辅助方法简化代码
  219. putIfNotNull(insertMap, "zhi_kong_pin_pi_h", entityMap, "LOTNUMBER");
  220. putIfNotNull(insertMap, "pi_hao_kai_shi_sh", entityMap, "LOTSTARTTIME");
  221. putIfNotNull(insertMap, "zhi_kong_tu_dan_w", entityMap, "QCCHARTUNIT");
  222. putIfNotNull(insertMap, "zhi_kong_tu_shui_", entityMap, "QCCHARTLEVEL");
  223. putIfNotNull(insertMap, "zhi_kong_tu_jun_z", entityMap, "QCCHARTAVG");
  224. putIfNotNull(insertMap, "zhi_kong_tu_sd_", entityMap, "QCCHARTSD");
  225. putIfNotNull(insertMap, "zhi_kong_tu_cv_", entityMap, "QCCHARTCV");
  226. putIfNotNull(insertMap, "yuan_shi_jun_zhi_", entityMap, "RAWDATAAVG");
  227. putIfNotNull(insertMap, "yuan_shi_sd_", entityMap, "RAWDATASD");
  228. putIfNotNull(insertMap, "yuan_shi_cv_", entityMap, "RAWDATACV");
  229. putIfNotNull(insertMap, "yuan_shi_n_", entityMap, "RAWDATAN");
  230. putIfNotNull(insertMap, "shi_kong_shu_", entityMap, "ROWDATAOC");
  231. putIfNotNull(insertMap, "chu_jun_zhi_", entityMap, "CONTROLDATAAVG");
  232. putIfNotNull(insertMap, "chu_sd_", entityMap, "CONTROLDATASD");
  233. putIfNotNull(insertMap, "chu_cv_", entityMap, "CONTROLDATACV");
  234. putIfNotNull(insertMap, "lei_jun_zhi_", entityMap, "ACCUMDATAAVG");
  235. putIfNotNull(insertMap, "lei_sd_", entityMap, "ACCUMDATASD");
  236. putIfNotNull(insertMap, "lei_cv_", entityMap, "ACCUMDATACV");
  237. putIfNotNull(insertMap, "lei_n_", entityMap, "ACCUMDATAN");
  238. putIfNotNull(insertMap, "zai_kong_lv_", entityMap, "ACCUMDATAPER");
  239. putIfNotNull(insertMap, "cv_kong_zhi_fan_w", entityMap, "CVRANGE");
  240. putIfNotNull(insertMap, "zhi_kong_chang_ji", entityMap, "QCMFG");
  241. putIfNotNull(insertMap, "shi_kong_gui_ze_", entityMap, "QCRule");
  242. putIfNotNull(insertMap, "shi_fou_he_ge_", entityMap, "PASS");
  243. putIfNotNull(insertMap, "she_bei_ming_chen", entityMap, "DEVICENAME");
  244. insertList.add(insertMap);
  245. }
  246. if (!insertList.isEmpty()) {
  247. String res = ibpsRepository.saveToTable(insertList, "t_dlxmsnzkyfxzb", "1");
  248. if ("success".equals(res)) {
  249. log.info("table:t_dlxmsnzkyfxzb success insert: {} datas", insertList.size());
  250. } else {
  251. log.info("fail insert t_dlxmsnzkyfxzb");
  252. }
  253. }
  254. }
  255. return "success";
  256. }
  257. // 辅助方法:安全获取字符串值,避免空指针异常
  258. private String getStringValue(Map<String, Object> map, String key) {
  259. Object value = map.get(key);
  260. return value == null ? "" : value.toString();
  261. }
  262. // 辅助方法:简化条件插入操作
  263. private void putIfNotNull(Map<String, Object> targetMap, String targetKey,
  264. Map<String, Object> sourceMap, String sourceKey) {
  265. Object value = sourceMap.get(sourceKey);
  266. if (value != null) {
  267. targetMap.put(targetKey, value.toString());
  268. if(targetKey.equals("pi_hao_kai_shi_sh")){
  269. targetMap.put(targetKey, value.toString().substring(0,10));
  270. }
  271. if(targetKey.equals("shi_fou_he_ge_")){
  272. if(value.toString().equals("Y")){
  273. targetMap.put(targetKey, "合格");
  274. }
  275. if(value.toString().equals("N")){
  276. targetMap.put(targetKey, "不合格");
  277. }
  278. }
  279. } else {
  280. targetMap.put(targetKey, "");
  281. }
  282. }
  283. public void HandQIData(List<Map<String, Object>>qualityIndicatorList){
  284. // 创建用于存储更新数据的列表
  285. List<Map<String, Object>> updateDataList = new ArrayList<>();
  286. if (qualityIndicatorList == null || qualityIndicatorList.isEmpty()) {
  287. // 执行退出操作
  288. return;
  289. }
  290. // Set<String> distinctSet = new HashSet<>();
  291. // for (Map<String, Object> item : qualityIndicatorList) {
  292. // Object value = item.get("bian_zhi_shi_jian");
  293. // if (value != null) {
  294. // distinctSet.add(value.toString());
  295. // }
  296. // }
  297. // // 转换为List
  298. // List<String> distinctList = new ArrayList<>(distinctSet);
  299. LocalDateTime currentDateTime = LocalDateTime.now();
  300. DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
  301. String formattedDateTime = currentDateTime.format(formatter2);
  302. YearMonth currentYearMonth = YearMonth.now();
  303. YearMonth previousMonth = currentYearMonth.minusMonths(1);
  304. DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年M月份", Locale.CHINA);
  305. String DateRange = DateRangeUtil.getDateRange(previousMonth.format(formatter));
  306. if(!manualQryMonth.equals("")){
  307. DateRange = DateRangeUtil.getDateRange(manualQryMonth);
  308. } else {
  309. manualQryMonth = previousMonth.format(formatter);
  310. }
  311. List<Map<String, Object>> lisRetList = lisViewRepository.GetLISRes(DateRange);
  312. List<Map<String, Object>> matchList = new ArrayList<>();
  313. for (Map<String, Object> undoQIMap : qualityIndicatorList) {
  314. String editTime = undoQIMap.get("bian_zhi_shi_jian").toString();
  315. //如果不是当前月份退出
  316. if(!editTime.equals(manualQryMonth)){
  317. continue;
  318. }
  319. matchList = MatchLISRes(undoQIMap, lisRetList, matchList, formattedDateTime);
  320. }
  321. if (matchList == null) {
  322. log.info("no match data found");
  323. return;
  324. }
  325. //测试输出matchList
  326. StringBuilder outString = new StringBuilder("current matchList:\n");
  327. for(Map<String, Object> retMap : matchList){
  328. outString.append(retMap.toString()).append("\n");
  329. }
  330. log.info(outString.toString());
  331. String retValue = ibpsRepository.saveToTable(matchList,"t_zlzbpjzb","2");
  332. if(retValue.equals("success")){
  333. log.info("success update:"+matchList.size()+" datas");
  334. } else {
  335. log.info("fail update");
  336. }
  337. // for (Map<String, Object> qualityIndicatorMap : qualityIndicatorList) {
  338. // log.info(qualityIndicatorMap.toString());
  339. // String DateRange = DateRangeUtil.getDateRange(qualityIndicatorMap.get("bian_zhi_shi_jian").toString());
  340. // String QryItem = qualityIndicatorMap.get("bei_zhu_").toString().split(":")[1];
  341. //
  342. // String id = qualityIndicatorMap.get("id_").toString();
  343. // String compareRes = ValueEvaluator.evaluateValue(retVal,qualityIndicatorMap.get("yuan_shi_shu_ju_").toString());
  344. // // 将数据添加到更新列表
  345. // Map<String, Object> updateData = new HashMap<>();
  346. // updateData.put("id", id);
  347. // updateData.put("retVal", retVal);
  348. // updateData.put("compareRes", compareRes);
  349. // updateDataList.add(updateData);
  350. // }
  351. // // 调用抽离的保存方法
  352. // ibpsRepository.updateQualityIndicatorData(updateDataList);
  353. }
  354. public List<Map<String, Object>> MatchLISRes(Map<String, Object> undoQIMap, List<Map<String, Object>> lisRetList, List<Map<String, Object>> matchList, String formattedDateTime) {
  355. // 如果 matchList 为 null,则初始化
  356. if (matchList == null) {
  357. matchList = new ArrayList<>();
  358. }
  359. // 获取 bei_zhu_ 字段值
  360. String beiZhu = (String) undoQIMap.get("bei_zhu_");
  361. // 在 lisRetList 中查找匹配的记录
  362. for (Map<String, Object> lisMap : lisRetList) {
  363. String currentLisFlag = (String) lisMap.get("lisFlag");
  364. if (beiZhu.equals(currentLisFlag)) {
  365. // 找到匹配项,创建新的匹配记录
  366. Map<String, Object> matchRecord = new HashMap<>();
  367. matchRecord.put("id_", undoQIMap.get("id_")); // 从 undoQIMap 取 id_
  368. matchRecord.put("shi_ji_shu_zhi_", lisMap.get("percentage")); // 从 lisRetList 取 percentage
  369. //计算是否达标
  370. String curValue = lisMap.get("percentage").toString();
  371. String compChar = undoQIMap.get("yuan_shi_shu_ju_").toString();
  372. String compRes = ValueEvaluator.evaluateValue(curValue, compChar);
  373. matchRecord.put("da_biao_qing_kuan", compRes);
  374. matchRecord.put("zhuang_tai_", "已完成");
  375. matchRecord.put("update_time_", Timestamp.valueOf(String.valueOf(formattedDateTime)));
  376. log.info("curValue:"+curValue+", compChar:"+compChar+", cpmpare result:"+compRes);
  377. matchList.add(matchRecord);
  378. break; // 找到匹配后跳出内层循环
  379. }
  380. }
  381. return matchList;
  382. }
  383. }