HandleData.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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.format.DateTimeFormatter;
  12. import java.util.*;
  13. @Component
  14. public class HandleData {
  15. private static final Logger log = LoggerFactory.getLogger(GetDataByViewApplication.class);
  16. @Autowired
  17. private LISViewRepository lisViewRepository;
  18. @Autowired
  19. private IBPSRepository ibpsRepository;
  20. @Value("${showTestCodeName}")
  21. private String showTestCodeName;
  22. public static final Map<String, String> RerunSampleField = new HashMap<>();
  23. static {
  24. // 静态初始化块中填充数据
  25. RerunSampleField.put("CREATEBY", "bian_zhi_ren_");
  26. RerunSampleField.put("CREATETIME", "bian_zhi_shi_jian");
  27. RerunSampleField.put("SAMPLECODE", "biao_ben_bian_hao");
  28. // RerunSampleField.put("TESTCODE", "xiang_mu_id_");
  29. RerunSampleField.put("TESTCODENAME", "fu_jian_xiang_mu_");
  30. RerunSampleField.put("INITIALRESULT", "ce_ding_zhi_chu_j");
  31. RerunSampleField.put("RERUNRESULT", "ce_ding_zhi_fu_ji");
  32. RerunSampleField.put("REPORTRESULT", "bao_gao_zhi_");
  33. RerunSampleField.put("AUDITUSER", "jian_yan_zhe_");
  34. RerunSampleField.put("REPORTTIME", "bao_gao_shi_jian_");
  35. RerunSampleField.put("RERUNMETHODNOTE", "bei_zhu_fu_jian_f");
  36. RerunSampleField.put("PATNAME", "xing_ming_");
  37. RerunSampleField.put("RERUNMETHOD", "fu_jian_fang_shi_");
  38. RerunSampleField.put("UNIT", "dan_wei_");
  39. }
  40. public void startHandleData(List<Map<String, Object>> retList, String lisTable) {
  41. List<Map<String, Object>> existList = ibpsRepository.qryExistData(lisTable);
  42. retList = cleanExistData(lisTable,retList,existList);
  43. log.info("table:"+lisTable+" get data:"+retList.size());
  44. if (lisTable.equals("V_JT_TestCodeRerunRecord")){
  45. log.info("ready to get rerun testcode");
  46. HandleRerunSample(retList);
  47. }
  48. if (lisTable.equals("V_JT_QCMonthReport")){
  49. log.info("ready to get QC month report");
  50. HandleMonthReport(retList);
  51. }
  52. }
  53. public List<Map<String, Object>> cleanExistData(String lisTable, List<Map<String, Object>> lisList, List<Map<String, Object>> existList) {
  54. if (lisTable.equals("V_JT_TestCodeRerunRecord")){
  55. // 1. 创建Set存储existList中所有id_的值(转换为字符串形式)
  56. Set<String> existIdSet = new HashSet<>();
  57. for (Map<String, Object> existMap : existList) {
  58. Object idObj = existMap.get("biao_ben_bian_hao");
  59. existIdSet.add(idObj == null ? null : idObj.toString());
  60. }
  61. // 2. 过滤rerunTestList:只保留id不在existIdSet中的元素
  62. List<Map<String, Object>> resultList = new ArrayList<>();
  63. for (Map<String, Object> testMap : lisList) {
  64. Object idObj = testMap.get("SAMPLECODE");
  65. String idStr = idObj == null ? null : idObj.toString();
  66. // 如果当前条码不在existIdSet中,则保留
  67. if (!existIdSet.contains(idStr)) {
  68. resultList.add(testMap);
  69. }
  70. }
  71. return resultList;
  72. }
  73. if (lisTable.equals("V_JT_QCMonthReport")){
  74. // 1. 创建Set存储existList中所有id_的值(转换为字符串形式)
  75. Set<String> existIdSet = new HashSet<>();
  76. for (Map<String, Object> existMap : existList) {
  77. Object idObj = existMap.get("id_");
  78. existIdSet.add(idObj == null ? null : idObj.toString());
  79. }
  80. // 2. 过滤rerunTestList:只保留id不在existIdSet中的元素
  81. List<Map<String, Object>> resultList = new ArrayList<>();
  82. for (Map<String, Object> testMap : lisList) {
  83. Object idObj = testMap.get("REPORTID");
  84. String idStr = idObj == null ? null : idObj.toString();
  85. // 如果当前条码不在existIdSet中,则保留
  86. if (!existIdSet.contains(idStr)) {
  87. resultList.add(testMap);
  88. }
  89. }
  90. return resultList;
  91. }
  92. return null;
  93. }
  94. public void HandleRerunSample(List<Map<String, Object>> retList){
  95. if (retList.size() > 0) {
  96. String fieldName = null;
  97. String fieldValue = null;
  98. List<Map<String, Object>> insertList = new ArrayList<Map<String, Object>>();
  99. for (Map entityMap : retList) {
  100. Map<String, Object> insertMap = new HashMap<>();
  101. for (Object entry : entityMap.keySet()) {
  102. fieldName = null;
  103. fieldValue = null;
  104. fieldName = RerunSampleField.get(entry.toString());
  105. Object value = entityMap.get(entry.toString());
  106. fieldValue = value != null ? value.toString() : "";
  107. if (fieldName != null) {
  108. insertMap.put(fieldName, fieldValue);
  109. }
  110. }
  111. //补充其他字段信息
  112. insertMap.put("id_", UUID.randomUUID().toString());
  113. insertMap.put("shi_fou_guo_shen_", "已完成");
  114. String createUserInfo = ibpsRepository.getUserInfoByName(String.valueOf(entityMap.get("CREATEBY")));
  115. String auditUserInfo = ibpsRepository.getUserInfoByName(String.valueOf(entityMap.get("AUDITUSER")));
  116. if (createUserInfo.split("\\^")[0].equals("-1")) {
  117. log.info("error userInfo,skip to insert:"+createUserInfo);
  118. continue;
  119. }
  120. if (auditUserInfo.split("\\^")[0].equals("-1")) {
  121. log.info("error auditUserInfo,skip to insert:"+auditUserInfo);
  122. continue;
  123. }
  124. LocalDateTime currentDateTime = LocalDateTime.now();
  125. DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
  126. String formattedDateTime = currentDateTime.format(formatter2);
  127. insertMap.put("create_by_", createUserInfo.trim().split("@")[0]);
  128. insertMap.put("create_time_", Timestamp.valueOf(String.valueOf(formattedDateTime)));
  129. insertMap.put("bian_zhi_ren_", createUserInfo.trim().split("@")[0]);
  130. insertMap.put("jian_yan_zhe_", String.valueOf(entityMap.get("AUDITUSER")));
  131. insertMap.put("jian_yan_yuan_", auditUserInfo.trim().split("@")[0]);
  132. insertMap.put("ri_qi_", String.valueOf(entityMap.get("CREATETIME")).split(" ")[0]);
  133. insertMap.put("bian_zhi_bu_men_", createUserInfo.split("@")[1]);
  134. insertList.add(insertMap);
  135. }
  136. // insertList = lisViewRepository.query2("ss");
  137. String res = ibpsRepository.saveToTable(insertList, "t_fjbbjlb");
  138. if(res.equals("success")){
  139. log.info("success insert:"+insertList.size()+" datas");
  140. } else {
  141. log.info("fail insert");
  142. }
  143. }
  144. }
  145. public void HandleMonthReport(List<Map<String, Object>> retList){
  146. if (retList.size() > 0) {
  147. String fieldName = null;
  148. String fieldValue = null;
  149. List<Map<String, Object>> insertList = new ArrayList<Map<String, Object>>();
  150. for (Map entityMap : retList) {
  151. Map<String, Object> insertReportMap = new HashMap<>();
  152. insertReportMap.put("id_", entityMap.get("REPORTID"));
  153. insertReportMap.put("shi_fou_guo_shen_", "待推送");
  154. LocalDateTime currentDateTime = LocalDateTime.now();
  155. DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
  156. String formattedDateTime = currentDateTime.format(formatter);
  157. insertReportMap.put("create_time_", Timestamp.valueOf(String.valueOf(formattedDateTime)));
  158. String editUser = entityMap.get("CREATEBY").toString();
  159. String editUserInfo = ibpsRepository.getUserInfoByName(editUser);
  160. //insertReportMap.put("bian_zhi_shi_jian", String.valueOf(formattedDateTime));
  161. String posID = ibpsRepository.getPosiByCode(entityMap.get("GROUPCODE").toString());
  162. if ((posID != "-1")&&(!editUserInfo.split("\\^")[0].equals("-1"))) {
  163. insertReportMap.put("di_dian_", posID);
  164. insertReportMap.put("bian_zhi_bu_men_", posID);
  165. insertReportMap.put("bian_zhi_ren_", editUserInfo.split("@")[0]);
  166. } else{
  167. log.info("error editUserInfo or posID,skip to insert,posID:"+posID+" ,editUserInfo:"+editUserInfo);
  168. continue;
  169. }
  170. insertReportMap.put("xi_tong_ming_chen", entityMap.get("GROUPNAME")+"全组仪器");
  171. insertReportMap.put("ji_lu_bian_hao_", entityMap.get("CREATETIME").toString().substring(0, 4) + "-" + entityMap.get("CREATETIME").toString().substring(4));
  172. insertList.add(insertReportMap);
  173. }
  174. String res = ibpsRepository.saveToTable(insertList, "t_dlxmsnzkyfx");
  175. if(res.equals("success")){
  176. log.info("table:t_dlxmsnzkyfx success insert:"+insertList.size()+" datas");
  177. handQCTestCode(insertList);
  178. } else {
  179. log.info("fail insert t_dlxmsnzkyfx");
  180. }
  181. }
  182. };
  183. public String handQCTestCode(List<Map<String, Object>> reportList) {
  184. LocalDateTime currentDateTime = LocalDateTime.now();
  185. DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
  186. String formattedDateTime = currentDateTime.format(formatter);
  187. Timestamp currentTimestamp = Timestamp.valueOf(currentDateTime);
  188. long sequenceCounter = 0;
  189. for (Map<String, Object> reportMap : reportList) {
  190. String reportID = getStringValue(reportMap, "id_");
  191. if (reportID == null || reportID.isEmpty()) {
  192. continue; // 跳过无效的报告ID
  193. }
  194. List<Map<String, Object>> retList = lisViewRepository.getQCTestCodeList(reportID);
  195. if (retList == null || retList.isEmpty()) {
  196. continue; // 跳过空结果
  197. }
  198. List<Map<String, Object>> insertList = new ArrayList<>();
  199. // 在循环外部定义序列号计数器
  200. for (Map<String, Object> entityMap : retList) {
  201. Map<String, Object> insertMap = new HashMap<>();
  202. // 生成有序ID:时间戳+自增序列号
  203. String orderedId = String.format("%d-%06d",
  204. currentTimestamp.getTime(), // 使用时间戳
  205. sequenceCounter++ // 自增序列号
  206. );
  207. insertMap.put("id_", orderedId);
  208. insertMap.put("parent_id_", getStringValue(entityMap, "REPORTID"));
  209. insertMap.put("create_time_", currentTimestamp); // 直接使用Timestamp,避免字符串转换
  210. // 简化条件判断
  211. String testCodeValue = showTestCodeName.equals("1") ?
  212. getStringValue(entityMap, "TESTCODENAME") :
  213. getStringValue(entityMap, "TESTCODE");
  214. insertMap.put("xiang_mu_", testCodeValue);
  215. // 使用辅助方法简化代码
  216. putIfNotNull(insertMap, "zhi_kong_pin_pi_h", entityMap, "LOTNUMBER");
  217. putIfNotNull(insertMap, "pi_hao_kai_shi_sh", entityMap, "LOTSTARTTIME");
  218. putIfNotNull(insertMap, "zhi_kong_tu_dan_w", entityMap, "QCCHARTUNIT");
  219. putIfNotNull(insertMap, "zhi_kong_tu_shui_", entityMap, "QCCHARTLEVEL");
  220. putIfNotNull(insertMap, "zhi_kong_tu_jun_z", entityMap, "QCCHARTAVG");
  221. putIfNotNull(insertMap, "zhi_kong_tu_sd_", entityMap, "QCCHARTSD");
  222. putIfNotNull(insertMap, "zhi_kong_tu_cv_", entityMap, "QCCHARTCV");
  223. putIfNotNull(insertMap, "yuan_shi_jun_zhi_", entityMap, "RAWDATAAVG");
  224. putIfNotNull(insertMap, "yuan_shi_sd_", entityMap, "RAWDATASD");
  225. putIfNotNull(insertMap, "yuan_shi_cv_", entityMap, "RAWDATACV");
  226. putIfNotNull(insertMap, "yuan_shi_n_", entityMap, "RAWDATAN");
  227. putIfNotNull(insertMap, "shi_kong_shu_", entityMap, "ROWDATAOC");
  228. putIfNotNull(insertMap, "chu_jun_zhi_", entityMap, "CONTROLDATAAVG");
  229. putIfNotNull(insertMap, "chu_sd_", entityMap, "CONTROLDATASD");
  230. putIfNotNull(insertMap, "chu_cv_", entityMap, "CONTROLDATACV");
  231. putIfNotNull(insertMap, "lei_jun_zhi_", entityMap, "ACCUMDATAAVG");
  232. putIfNotNull(insertMap, "lei_sd_", entityMap, "ACCUMDATASD");
  233. putIfNotNull(insertMap, "lei_cv_", entityMap, "ACCUMDATACV");
  234. putIfNotNull(insertMap, "lei_n_", entityMap, "ACCUMDATAN");
  235. putIfNotNull(insertMap, "zai_kong_lv_", entityMap, "ACCUMDATAPER");
  236. putIfNotNull(insertMap, "cv_kong_zhi_fan_w", entityMap, "CVRANGE");
  237. putIfNotNull(insertMap, "zhi_kong_chang_ji", entityMap, "QCMFG");
  238. putIfNotNull(insertMap, "shi_kong_gui_ze_", entityMap, "QCRule");
  239. putIfNotNull(insertMap, "shi_fou_he_ge_", entityMap, "PASS");
  240. putIfNotNull(insertMap, "she_bei_ming_chen", entityMap, "DEVICENAME");
  241. insertList.add(insertMap);
  242. }
  243. if (!insertList.isEmpty()) {
  244. String res = ibpsRepository.saveToTable(insertList, "t_dlxmsnzkyfxzb");
  245. if ("success".equals(res)) {
  246. log.info("table:t_dlxmsnzkyfxzb success insert: {} datas", insertList.size());
  247. } else {
  248. log.info("fail insert t_dlxmsnzkyfxzb");
  249. }
  250. }
  251. }
  252. return "success";
  253. }
  254. // 辅助方法:安全获取字符串值,避免空指针异常
  255. private String getStringValue(Map<String, Object> map, String key) {
  256. Object value = map.get(key);
  257. return value == null ? "" : value.toString();
  258. }
  259. // 辅助方法:简化条件插入操作
  260. private void putIfNotNull(Map<String, Object> targetMap, String targetKey,
  261. Map<String, Object> sourceMap, String sourceKey) {
  262. Object value = sourceMap.get(sourceKey);
  263. if (value != null) {
  264. targetMap.put(targetKey, value.toString());
  265. if(targetKey.equals("pi_hao_kai_shi_sh")){
  266. targetMap.put(targetKey, value.toString().substring(0,10));
  267. }
  268. if(targetKey.equals("shi_fou_he_ge_")){
  269. if(value.toString().equals("Y")){
  270. targetMap.put(targetKey, "合格");
  271. }
  272. if(value.toString().equals("N")){
  273. targetMap.put(targetKey, "不合格");
  274. }
  275. }
  276. } else {
  277. targetMap.put(targetKey, "");
  278. }
  279. }
  280. public void HandQIData(List<Map<String, Object>>qualityIndicatorList){
  281. // 创建用于存储更新数据的列表
  282. List<Map<String, Object>> updateDataList = new ArrayList<>();
  283. for (Map<String, Object> qualityIndicatorMap : qualityIndicatorList) {
  284. log.info(qualityIndicatorMap.toString());
  285. String DateRange = DateRangeUtil.getDateRange(qualityIndicatorMap.get("bian_zhi_shi_jian").toString());
  286. String QryItem = qualityIndicatorMap.get("bei_zhu_").toString().split(":")[1];
  287. String retVal = lisViewRepository.GetLISRes(DateRange, QryItem);
  288. String id = qualityIndicatorMap.get("id_").toString();
  289. String compareRes = ValueEvaluator.evaluateValue(retVal,qualityIndicatorMap.get("yuan_shi_shu_ju_").toString());
  290. // 将数据添加到更新列表
  291. Map<String, Object> updateData = new HashMap<>();
  292. updateData.put("id", id);
  293. updateData.put("retVal", retVal);
  294. updateData.put("compareRes", compareRes);
  295. updateDataList.add(updateData);
  296. }
  297. // 调用抽离的保存方法
  298. ibpsRepository.updateQualityIndicatorData(updateDataList);
  299. }
  300. }