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.stereotype.Component; import java.sql.Timestamp; import java.time.LocalDateTime; 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; 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> rerunTestList) { List> existList = ibpsRepository.qryExistData(); rerunTestList = cleanExistData(rerunTestList,existList); if (rerunTestList.size() > 0) { String fieldName = null; String fieldValue = null; List> insertList = new ArrayList>(); for (Map entityMap : rerunTestList) { 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() : null; if (fieldName != null) { insertMap.put(fieldName, fieldValue); } } //补充其他字段信息 insertMap.put("id_", entityMap.get("SAMPLECODE")+"_"+entityMap.get("ITEMCODE")); insertMap.put("shi_fou_guo_shen_", "已完成"); String userInfo = ibpsRepository.getUserInfoByName(String.valueOf(entityMap.get("CREATEBY"))); if (userInfo.split("\\^")[0].equals("-1")) { continue; } LocalDateTime currentDateTime = LocalDateTime.now(); DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); String formattedDateTime = currentDateTime.format(formatter2); insertMap.put("create_by_", userInfo.split("@")[0]); insertMap.put("create_time_", Timestamp.valueOf(String.valueOf(formattedDateTime))); insertMap.put("ri_qi_", String.valueOf(entityMap.get("CREATETIME")).split(" ")[0]); insertMap.put("bian_zhi_bu_men_", userInfo.split("@")[1]); insertList.add(insertMap); } //ibpsRepository.saveToTable(insertList, "t_fjbbjlb"); } } public List> cleanExistData(List> rerunTestList, List> existList) { // 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 : rerunTestList) { Object idObj = testMap.get("SAMPLECODE")+"_"+testMap.get("ITEMCODE"); String idStr = idObj == null ? null : idObj.toString(); // 如果当前id不在existIdSet中,则保留 if (!existIdSet.contains(idStr)) { resultList.add(testMap); } } return resultList; } }