package com.jyxt.getdatabyview; import com.jyxt.getdatabyview.view.repository.IBPSRepository; import com.jyxt.getdatabyview.view.repository.LISViewRepository; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import java.sql.Timestamp; import java.time.LocalDateTime; import java.time.YearMonth; import java.time.format.DateTimeFormatter; import java.util.*; @Component public class HandleData { private static final Logger log = LoggerFactory.getLogger(GetDataByViewApplication.class); @Autowired private LISViewRepository lisViewRepository; @Autowired private IBPSRepository ibpsRepository; @Value("${showTestCodeName}") private String showTestCodeName; @Value("${manualQryMonth}") private String manualQryMonth; public static final Map RerunSampleField = new HashMap<>(); static { // 静态初始化块中填充数据 RerunSampleField.put("CREATEBY", "bian_zhi_ren_"); RerunSampleField.put("CREATETIME", "bian_zhi_shi_jian"); RerunSampleField.put("SAMPLECODE", "biao_ben_bian_hao"); // RerunSampleField.put("TESTCODE", "xiang_mu_id_"); RerunSampleField.put("TESTCODENAME", "fu_jian_xiang_mu_"); RerunSampleField.put("INITIALRESULT", "ce_ding_zhi_chu_j"); RerunSampleField.put("RERUNRESULT", "ce_ding_zhi_fu_ji"); RerunSampleField.put("REPORTRESULT", "bao_gao_zhi_"); RerunSampleField.put("AUDITUSER", "jian_yan_zhe_"); RerunSampleField.put("REPORTTIME", "bao_gao_shi_jian_"); RerunSampleField.put("RERUNMETHODNOTE", "bei_zhu_fu_jian_f"); RerunSampleField.put("PATNAME", "xing_ming_"); RerunSampleField.put("RERUNMETHOD", "fu_jian_fang_shi_"); RerunSampleField.put("UNIT", "dan_wei_"); } public void startHandleData(List> retList, String lisTable) { List> existList = ibpsRepository.qryExistData(lisTable); retList = cleanExistData(lisTable,retList,existList); log.info("table:"+lisTable+" get data:"+retList.size()); if (lisTable.equals("V_JT_TestCodeRerunRecord")){ log.info("ready to get rerun testcode"); HandleRerunSample(retList); } if (lisTable.equals("V_JT_QCMonthReport")){ log.info("ready to get QC month report"); HandleMonthReport(retList); } } public List> cleanExistData(String lisTable, List> lisList, List> existList) { if (lisTable.equals("V_JT_TestCodeRerunRecord")){ // 1. 创建Set存储existList中所有id_的值(转换为字符串形式) Set existIdSet = new HashSet<>(); for (Map existMap : existList) { Object idObj = existMap.get("biao_ben_bian_hao"); existIdSet.add(idObj == null ? null : idObj.toString()); } // 2. 过滤rerunTestList:只保留id不在existIdSet中的元素 List> resultList = new ArrayList<>(); for (Map testMap : lisList) { Object idObj = testMap.get("SAMPLECODE"); String idStr = idObj == null ? null : idObj.toString(); // 如果当前条码不在existIdSet中,则保留 if (!existIdSet.contains(idStr)) { resultList.add(testMap); } } return resultList; } if (lisTable.equals("V_JT_QCMonthReport")){ // 1. 创建Set存储existList中所有id_的值(转换为字符串形式) Set existIdSet = new HashSet<>(); for (Map existMap : existList) { Object idObj = existMap.get("id_"); existIdSet.add(idObj == null ? null : idObj.toString()); } // 2. 过滤rerunTestList:只保留id不在existIdSet中的元素 List> resultList = new ArrayList<>(); for (Map testMap : lisList) { Object idObj = testMap.get("REPORTID"); String idStr = idObj == null ? null : idObj.toString(); // 如果当前条码不在existIdSet中,则保留 if (!existIdSet.contains(idStr)) { resultList.add(testMap); } } return resultList; } return null; } public void HandleRerunSample(List> retList){ if (retList.size() > 0) { String fieldName = null; String fieldValue = null; List> insertList = new ArrayList>(); for (Map entityMap : retList) { Map insertMap = new HashMap<>(); for (Object entry : entityMap.keySet()) { fieldName = null; fieldValue = null; fieldName = RerunSampleField.get(entry.toString()); Object value = entityMap.get(entry.toString()); fieldValue = value != null ? value.toString() : ""; if (fieldName != null) { insertMap.put(fieldName, fieldValue); } } //补充其他字段信息 insertMap.put("id_", UUID.randomUUID().toString()); insertMap.put("shi_fou_guo_shen_", "已完成"); String createUserInfo = ibpsRepository.getUserInfoByName(String.valueOf(entityMap.get("CREATEBY"))); String auditUserInfo = ibpsRepository.getUserInfoByName(String.valueOf(entityMap.get("AUDITUSER"))); if (createUserInfo.split("\\^")[0].equals("-1")) { log.info("error userInfo,skip to insert:"+createUserInfo); continue; } if (auditUserInfo.split("\\^")[0].equals("-1")) { log.info("error auditUserInfo,skip to insert:"+auditUserInfo); continue; } LocalDateTime currentDateTime = LocalDateTime.now(); DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); String formattedDateTime = currentDateTime.format(formatter2); insertMap.put("create_by_", createUserInfo.trim().split("@")[0]); insertMap.put("create_time_", Timestamp.valueOf(String.valueOf(formattedDateTime))); insertMap.put("bian_zhi_ren_", createUserInfo.trim().split("@")[0]); insertMap.put("jian_yan_zhe_", String.valueOf(entityMap.get("AUDITUSER"))); insertMap.put("jian_yan_yuan_", auditUserInfo.trim().split("@")[0]); insertMap.put("ri_qi_", String.valueOf(entityMap.get("CREATETIME")).split(" ")[0]); insertMap.put("bian_zhi_bu_men_", createUserInfo.split("@")[1]); insertList.add(insertMap); } // insertList = lisViewRepository.query2("ss"); String res = ibpsRepository.saveToTable(insertList, "t_fjbbjlb","1"); if(res.equals("success")){ log.info("success insert:"+insertList.size()+" datas"); } else { log.info("fail insert"); } } } public void HandleMonthReport(List> retList){ if (retList.size() > 0) { String fieldName = null; String fieldValue = null; List> insertList = new ArrayList>(); for (Map entityMap : retList) { Map insertReportMap = new HashMap<>(); insertReportMap.put("id_", entityMap.get("REPORTID")); insertReportMap.put("shi_fou_guo_shen_", "待推送"); LocalDateTime currentDateTime = LocalDateTime.now(); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); String formattedDateTime = currentDateTime.format(formatter); insertReportMap.put("create_time_", Timestamp.valueOf(String.valueOf(formattedDateTime))); String editUser = entityMap.get("CREATEBY").toString(); String editUserInfo = ibpsRepository.getUserInfoByName(editUser); //insertReportMap.put("bian_zhi_shi_jian", String.valueOf(formattedDateTime)); String posID = ibpsRepository.getPosiByCode(entityMap.get("GROUPCODE").toString()); if ((posID != "-1")&&(!editUserInfo.split("\\^")[0].equals("-1"))) { insertReportMap.put("di_dian_", posID); insertReportMap.put("bian_zhi_bu_men_", posID); insertReportMap.put("bian_zhi_ren_", editUserInfo.split("@")[0]); } else{ log.info("error editUserInfo or posID,skip to insert,posID:"+posID+" ,editUserInfo:"+editUserInfo); continue; } insertReportMap.put("xi_tong_ming_chen", entityMap.get("GROUPNAME")+"全组仪器"); insertReportMap.put("ji_lu_bian_hao_", entityMap.get("CREATETIME").toString().substring(0, 4) + "-" + entityMap.get("CREATETIME").toString().substring(4)); insertList.add(insertReportMap); } String res = ibpsRepository.saveToTable(insertList, "t_dlxmsnzkyfx", "1"); if(res.equals("success")){ log.info("table:t_dlxmsnzkyfx success insert:"+insertList.size()+" datas"); handQCTestCode(insertList); } else { log.info("fail insert t_dlxmsnzkyfx"); } } }; public String handQCTestCode(List> reportList) { LocalDateTime currentDateTime = LocalDateTime.now(); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); String formattedDateTime = currentDateTime.format(formatter); Timestamp currentTimestamp = Timestamp.valueOf(currentDateTime); long sequenceCounter = 0; for (Map reportMap : reportList) { String reportID = getStringValue(reportMap, "id_"); if (reportID == null || reportID.isEmpty()) { continue; // 跳过无效的报告ID } List> retList = lisViewRepository.getQCTestCodeList(reportID); if (retList == null || retList.isEmpty()) { continue; // 跳过空结果 } List> insertList = new ArrayList<>(); // 在循环外部定义序列号计数器 for (Map entityMap : retList) { Map insertMap = new HashMap<>(); // 生成有序ID:时间戳+自增序列号 String orderedId = String.format("%d-%06d", currentTimestamp.getTime(), // 使用时间戳 sequenceCounter++ // 自增序列号 ); insertMap.put("id_", orderedId); insertMap.put("parent_id_", getStringValue(entityMap, "REPORTID")); insertMap.put("create_time_", currentTimestamp); // 直接使用Timestamp,避免字符串转换 // 简化条件判断 String testCodeValue = showTestCodeName.equals("1") ? getStringValue(entityMap, "TESTCODENAME") : getStringValue(entityMap, "TESTCODE"); insertMap.put("xiang_mu_", testCodeValue); // 使用辅助方法简化代码 putIfNotNull(insertMap, "zhi_kong_pin_pi_h", entityMap, "LOTNUMBER"); putIfNotNull(insertMap, "pi_hao_kai_shi_sh", entityMap, "LOTSTARTTIME"); putIfNotNull(insertMap, "zhi_kong_tu_dan_w", entityMap, "QCCHARTUNIT"); putIfNotNull(insertMap, "zhi_kong_tu_shui_", entityMap, "QCCHARTLEVEL"); putIfNotNull(insertMap, "zhi_kong_tu_jun_z", entityMap, "QCCHARTAVG"); putIfNotNull(insertMap, "zhi_kong_tu_sd_", entityMap, "QCCHARTSD"); putIfNotNull(insertMap, "zhi_kong_tu_cv_", entityMap, "QCCHARTCV"); putIfNotNull(insertMap, "yuan_shi_jun_zhi_", entityMap, "RAWDATAAVG"); putIfNotNull(insertMap, "yuan_shi_sd_", entityMap, "RAWDATASD"); putIfNotNull(insertMap, "yuan_shi_cv_", entityMap, "RAWDATACV"); putIfNotNull(insertMap, "yuan_shi_n_", entityMap, "RAWDATAN"); putIfNotNull(insertMap, "shi_kong_shu_", entityMap, "ROWDATAOC"); putIfNotNull(insertMap, "chu_jun_zhi_", entityMap, "CONTROLDATAAVG"); putIfNotNull(insertMap, "chu_sd_", entityMap, "CONTROLDATASD"); putIfNotNull(insertMap, "chu_cv_", entityMap, "CONTROLDATACV"); putIfNotNull(insertMap, "lei_jun_zhi_", entityMap, "ACCUMDATAAVG"); putIfNotNull(insertMap, "lei_sd_", entityMap, "ACCUMDATASD"); putIfNotNull(insertMap, "lei_cv_", entityMap, "ACCUMDATACV"); putIfNotNull(insertMap, "lei_n_", entityMap, "ACCUMDATAN"); putIfNotNull(insertMap, "zai_kong_lv_", entityMap, "ACCUMDATAPER"); putIfNotNull(insertMap, "cv_kong_zhi_fan_w", entityMap, "CVRANGE"); putIfNotNull(insertMap, "zhi_kong_chang_ji", entityMap, "QCMFG"); putIfNotNull(insertMap, "shi_kong_gui_ze_", entityMap, "QCRule"); putIfNotNull(insertMap, "shi_fou_he_ge_", entityMap, "PASS"); putIfNotNull(insertMap, "she_bei_ming_chen", entityMap, "DEVICENAME"); insertList.add(insertMap); } if (!insertList.isEmpty()) { String res = ibpsRepository.saveToTable(insertList, "t_dlxmsnzkyfxzb", "1"); if ("success".equals(res)) { log.info("table:t_dlxmsnzkyfxzb success insert: {} datas", insertList.size()); } else { log.info("fail insert t_dlxmsnzkyfxzb"); } } } return "success"; } // 辅助方法:安全获取字符串值,避免空指针异常 private String getStringValue(Map map, String key) { Object value = map.get(key); return value == null ? "" : value.toString(); } // 辅助方法:简化条件插入操作 private void putIfNotNull(Map targetMap, String targetKey, Map sourceMap, String sourceKey) { Object value = sourceMap.get(sourceKey); if (value != null) { targetMap.put(targetKey, value.toString()); if(targetKey.equals("pi_hao_kai_shi_sh")){ targetMap.put(targetKey, value.toString().substring(0,10)); } if(targetKey.equals("shi_fou_he_ge_")){ if(value.toString().equals("Y")){ targetMap.put(targetKey, "合格"); } if(value.toString().equals("N")){ targetMap.put(targetKey, "不合格"); } } } else { targetMap.put(targetKey, ""); } } public void HandQIData(List>qualityIndicatorList){ // 创建用于存储更新数据的列表 List> updateDataList = new ArrayList<>(); if (qualityIndicatorList == null || qualityIndicatorList.isEmpty()) { // 执行退出操作 return; } // Set distinctSet = new HashSet<>(); // for (Map item : qualityIndicatorList) { // Object value = item.get("bian_zhi_shi_jian"); // if (value != null) { // distinctSet.add(value.toString()); // } // } // // 转换为List // List distinctList = new ArrayList<>(distinctSet); LocalDateTime currentDateTime = LocalDateTime.now(); DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); String formattedDateTime = currentDateTime.format(formatter2); YearMonth currentYearMonth = YearMonth.now(); YearMonth previousMonth = currentYearMonth.minusMonths(1); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年M月份", Locale.CHINA); String DateRange = DateRangeUtil.getDateRange(previousMonth.format(formatter)); if(!manualQryMonth.equals("")){ DateRange = DateRangeUtil.getDateRange(manualQryMonth); } else { manualQryMonth = previousMonth.format(formatter); } List> lisRetList = lisViewRepository.GetLISRes(DateRange); List> matchList = new ArrayList<>(); for (Map undoQIMap : qualityIndicatorList) { String editTime = undoQIMap.get("bian_zhi_shi_jian").toString(); //如果不是当前月份退出 if(!editTime.equals(manualQryMonth)){ continue; } matchList = MatchLISRes(undoQIMap, lisRetList, matchList, formattedDateTime); } if (matchList == null) { log.info("no match data found"); return; } //测试输出matchList StringBuilder outString = new StringBuilder("current matchList:\n"); for(Map retMap : matchList){ outString.append(retMap.toString()).append("\n"); } log.info(outString.toString()); String retValue = ibpsRepository.saveToTable(matchList,"t_zlzbpjzb","2"); if(retValue.equals("success")){ log.info("success update:"+matchList.size()+" datas"); } else { log.info("fail update"); } // for (Map qualityIndicatorMap : qualityIndicatorList) { // log.info(qualityIndicatorMap.toString()); // String DateRange = DateRangeUtil.getDateRange(qualityIndicatorMap.get("bian_zhi_shi_jian").toString()); // String QryItem = qualityIndicatorMap.get("bei_zhu_").toString().split(":")[1]; // // String id = qualityIndicatorMap.get("id_").toString(); // String compareRes = ValueEvaluator.evaluateValue(retVal,qualityIndicatorMap.get("yuan_shi_shu_ju_").toString()); // // 将数据添加到更新列表 // Map updateData = new HashMap<>(); // updateData.put("id", id); // updateData.put("retVal", retVal); // updateData.put("compareRes", compareRes); // updateDataList.add(updateData); // } // // 调用抽离的保存方法 // ibpsRepository.updateQualityIndicatorData(updateDataList); } public List> MatchLISRes(Map undoQIMap, List> lisRetList, List> matchList, String formattedDateTime) { // 如果 matchList 为 null,则初始化 if (matchList == null) { matchList = new ArrayList<>(); } // 获取 bei_zhu_ 字段值 String beiZhu = (String) undoQIMap.get("bei_zhu_"); // 在 lisRetList 中查找匹配的记录 for (Map lisMap : lisRetList) { String currentLisFlag = (String) lisMap.get("lisFlag"); if (beiZhu.equals(currentLisFlag)) { // 找到匹配项,创建新的匹配记录 Map matchRecord = new HashMap<>(); matchRecord.put("id_", undoQIMap.get("id_")); // 从 undoQIMap 取 id_ matchRecord.put("shi_ji_shu_zhi_", lisMap.get("percentage")); // 从 lisRetList 取 percentage //计算是否达标 String curValue = lisMap.get("percentage").toString(); String compChar = undoQIMap.get("yuan_shi_shu_ju_").toString(); String compRes = ValueEvaluator.evaluateValue(curValue, compChar); matchRecord.put("da_biao_qing_kuan", compRes); matchRecord.put("zhuang_tai_", "已完成"); matchRecord.put("update_time_", Timestamp.valueOf(String.valueOf(formattedDateTime))); log.info("curValue:"+curValue+", compChar:"+compChar+", cpmpare result:"+compRes); matchList.add(matchRecord); break; // 找到匹配后跳出内层循环 } } return matchList; } }