private SAFEDB AddToContext(SAFEDB context, TH_TRISK01 entity, int count, int commitCount, bool recreateContext) { context.Set <TH_TRISK01>().Add(entity); if (count % commitCount == 0) { context.SaveChanges(); if (recreateContext) { context.Dispose(); context = new SAFEDB(); context.Configuration.AutoDetectChangesEnabled = false; } } return(context); }
/// <summary> /// haza01的文件导入函数,从npoi的sheet转化成list /// </summary> /// <param name="HSSFSheet"></param> /// <returns></returns> public static CSException <List <TH_THAZA01> > BuildListByNPOISheet(NPOI.HSSF.UserModel.HSSFSheet HSSFSheet, string userid) { try { var risk_status_Dic = EP_TEPEP01.GetDicByCName("风险状态"); var rowcount = HSSFSheet.LastRowNum;//有一行是标题,但是还是+1,因为从0开始算。如果没有标题还是+1,因为这个不是统计总行,而是行数的最后一行的代码 var hazaList = new List <TH_THAZA01>(); for (var i = 1; i < rowcount + 1; i++) { try { string force_haza_lvl; var row = HSSFSheet.GetRow(i); if (row.Cells.Count() < 9) { break; } //读取sheet对应行所有数据 //一边读数据一边新建risk var newrisk = new TH_TRISK01("insert"); var haza_loca_deptCode = row.Cells[0].ToString().Trim(); if (haza_loca_deptCode == "END") { //根据要求,加了这个标记的行作为结束行 break; } var haza_loca_name = ""; var haza_name = ""; var risk_status_Cname = ""; try { newrisk.RISK_DEPT = haza_loca_deptCode; haza_loca_name = row.Cells[1].ToString().Trim(); haza_name = row.Cells[2].ToString().Trim(); risk_status_Cname = row.Cells[3].ToString(); newrisk.RISK_STATUS = risk_status_Dic.Where(x => x.Value == risk_status_Cname).First().Key; newrisk.RISK_DECONTENT = row.Cells[4].ToString().Trim(); newrisk.RISK_MOD = row.Cells[5].ToString().Trim(); newrisk.RISK_L = decimal.Parse(row.Cells[6].ToString().Trim()); newrisk.RISK_E = decimal.Parse(row.Cells[7].ToString().Trim()); newrisk.RISK_C = decimal.Parse(row.Cells[8].ToString().Trim()); newrisk.CalcLVL(); newrisk.RISK_SOL = row.Cells[9].ToString().Trim().Replace("、", ""); if (row.PhysicalNumberOfCells > 10) { force_haza_lvl = row.Cells[10].ToString().Trim(); } else { force_haza_lvl = ""; } } catch { throw new Exception("数据中有空白行,不符合规范"); } //看危险源区域是否已经录入 var locare = TH_HAZALOCA.FindLoca(haza_loca_deptCode, haza_loca_name); if (!locare.Flag) { throw new Exception("没有找到危险源区域"); } //危险源区域获得 var hazaloca = locare.Entity; var re_inList = TH_THAZA01.FindInList(hazaloca, haza_name, hazaList); var re_inDB = TH_THAZA01.FindInDB(hazaloca, haza_name); if (re_inDB.Flag && re_inList.Flag)//在数据库和表中同时找到两项,则优先加入临时程序表中 { var hazard = hazaList[(int)re_inList.StatusNum]; //直接添加一个风险进入 hazard.RISKLIST.Add(newrisk); hazard.ReCalcHAZA_LVL(hazard.RISKLIST); if (force_haza_lvl != "") { var lvldic = EP_TEPEP01.GetDicByCName("危险源等级"); hazard.HAZA_LVL = lvldic.Where(x => x.Value == force_haza_lvl).First().Key; } } else if (re_inDB.Flag && !re_inList.Flag)//在数据库中存在 { re_inDB.Entity.ReCalcHAZA_LVL(re_inDB.Entity.HAZA_RISK_String); re_inDB.Entity.RISKLIST.Add(newrisk); re_inDB.Entity.ReCalcHAZA_LVL(re_inDB.Entity.RISKLIST); if (force_haza_lvl != "") { var lvldic = EP_TEPEP01.GetDicByCName("危险源等级"); re_inDB.Entity.HAZA_LVL = lvldic.Where(x => x.Value == force_haza_lvl).First().Key; } hazaList.Add(re_inDB.Entity); } else if (!re_inDB.Flag && re_inList.Flag)//在列表中 { var hazard = hazaList[(int)re_inList.StatusNum]; //直接添加一个风险进入 hazard.RISKLIST.Add(newrisk); hazard.ReCalcHAZA_LVL(hazard.RISKLIST); if (force_haza_lvl != "") { var lvldic = EP_TEPEP01.GetDicByCName("危险源等级"); hazard.HAZA_LVL = lvldic.Where(x => x.Value == force_haza_lvl).First().Key; } } else if (!re_inDB.Flag && !re_inList.Flag)//该危险源在已有资源中不存在,是个完全新危险源 { //新建危险源 var newhaza = new TH_THAZA01("insert"); newhaza.HAZA_LOCA = hazaloca.ID; newhaza.REC_CREATOR = userid; newhaza.HAZA_DEPT = haza_loca_deptCode; newhaza.HAZA_NAME = haza_name; newhaza.RISKLIST = new List <TH_TRISK01>(); //马上添加一个新的风险 newhaza.RISKLIST.Add(newrisk); newhaza.ReCalcHAZA_LVL(newhaza.RISKLIST); if (force_haza_lvl != null && force_haza_lvl != "") { try { var lvldic = EP_TEPEP01.GetDicByCName("危险源等级"); newhaza.HAZA_LVL = lvldic.Where(x => x.Value == force_haza_lvl).First().Key; } catch { throw new Exception("强制危险源等级错误"); } } //新危险源添加入List hazaList.Add(newhaza); } } catch (Exception ex) { var cell = HSSFSheet.GetRow(i).Cells.Select(x => { return(x.ToString()); }); var jsoncell = JsonHelper.SerializeObject(cell); var errmsg = String.Format("至第{0}行录入成功,第{1}行录入失败 原因:{2},\n错误行信息:{3}", i, i + 1, ex.Message, jsoncell); return(new CSException <List <TH_THAZA01> >(false, errmsg, hazaList)); } } //返回加完的hazalist return(new CSException <List <TH_THAZA01> >(true, hazaList)); } catch (Exception ex) { return(new CSException <List <TH_THAZA01> >(false, ex.Message, new List <TH_THAZA01>())); } }