public IActionResult GetCreate(YqfkrybViewModel model) { var response = ResponseModelFactory.CreateInstance; using (_dbContext) { var entity = new HaikanSmartTownCockpit.Api.Entities.Yqfkryb(); entity.Yqfkrybuuid = Guid.NewGuid(); entity.Name = model.Name; entity.Attention = model.Attention; entity.IdCard = model.IdCard; entity.OwnedNetwork = model.OwnedNetwork; entity.ReasonForConcern = model.ReasonForConcern; entity.Sex = model.Sex; entity.EpidemicPreventionAndControlStatus = model.EpidemicPreventionAndControlStatus; entity.ContactPhone = model.ContactPhone; entity.ServicePremises = model.ServicePremises; entity.IsolationTime = model.IsolationTime; entity.CurrentAddress = model.CurrentAddress; entity.Origin = model.Origin; entity.WhereToGo = model.WhereToGo; entity.ToAddress = model.ToAddress; entity.FamilyMemberContactInformation = model.FamilyMemberContactInformation; entity.GoToAddressContact = model.GoToAddressContact; entity.ContactPhoneNumber = model.ContactPhoneNumber; entity.ReturnOrEstimatedReturnTime = model.ReturnOrEstimatedReturnTime; entity.Transportation = model.Transportation; entity.YesNoSuspectedFever = model.YesNoSuspectedFever; entity.CheckMethod = model.CheckMethod; entity.NameOfResponsibleDoctor = model.NameOfResponsibleDoctor; entity.PhoneNumberOfResponsibleDoctor = model.PhoneNumberOfResponsibleDoctor; entity.ServiceMemberInformation = model.ServiceMemberInformation; entity.GuardianInformation = model.GuardianInformation; entity.Remarks = model.Remarks; entity.IsDeleted = 0; _dbContext.Yqfkryb.Add(entity); int res = _dbContext.SaveChanges(); if (res > 0) { ToLog.AddLog("添加", "成功:添加:疫情防控人员信息一条数据", _dbContext); } response.SetSuccess("添加成功"); return(Ok(response)); } }
public IActionResult GetImport(IFormFile excelfile) { var response = ResponseModelFactory.CreateInstance; using (_dbContext) { DateTime beginTime = DateTime.Now; string sWebRootFolder = _hostingEnvironment.WebRootPath + "\\UploadFiles\\KeyPointExcel"; //var schoolinfo = _dbContext.SchoolInforManagement.AsQueryable(); string uploadtitle = "疫情防控人员清单导入" + DateTime.Now.ToString("yyyyMMddHHmmss"); string sFileName = $"{uploadtitle}.xlsx"; FileInfo file = new FileInfo(Path.Combine(sWebRootFolder, sFileName)); //string conStr = ConnectionStrings.DefaultConnection; string responsemsgsuccess = ""; string responsemsgrepeat = ""; string responsemsgdefault = ""; int successcount = 0; int repeatcount = 0; int defaultcount = 0; string today = DateTime.Now.ToString("yyyy-MM-dd"); try { //把excelfile中的数据复制到file中 using (FileStream fs = new FileStream(file.ToString(), FileMode.Create)) //初始化一个指定路径和创建模式的FileStream { excelfile.CopyTo(fs); fs.Flush(); //清空stream的缓存,并且把缓存中的数据输出到file } DataTable dt = Haikan3.Utils.ExcelTools.ExcelToDataTable(file.ToString(), "Sheet1", true); if (dt == null || dt.Rows.Count == 0) { response.SetFailed("表格无数据"); return(Ok(response)); } else { for (int i = 0; i < dt.Rows.Count; i++) { var entity = new HaikanSmartTownCockpit.Api.Entities.Yqfkryb(); entity.Yqfkrybuuid = Guid.NewGuid(); if (!string.IsNullOrEmpty(dt.Rows[i]["姓名"].ToString())) { entity.Name = dt.Rows[i]["姓名"].ToString(); } else { responsemsgdefault += "<p style='color:red'>" + "第" + (i + 2) + "行姓名为空" + "</p></br>"; defaultcount++; continue; } if (!string.IsNullOrEmpty(dt.Rows[i]["疫情防控状态"].ToString())) { entity.EpidemicPreventionAndControlStatus = dt.Rows[i]["疫情防控状态"].ToString(); } if (!string.IsNullOrEmpty(dt.Rows[i]["关注原因"].ToString())) { entity.ReasonForConcern = dt.Rows[i]["关注原因"].ToString(); } if (!string.IsNullOrEmpty(dt.Rows[i]["关注程度"].ToString())) { entity.Attention = dt.Rows[i]["关注程度"].ToString(); } if (!string.IsNullOrEmpty(dt.Rows[i]["所属网格"].ToString())) { entity.OwnedNetwork = dt.Rows[i]["所属网格"].ToString(); } Regex regsfz = new Regex("^(^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$)|(^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])((\\d{4})|\\d{3}[Xx])$)$"); if (regsfz.IsMatch(dt.Rows[i]["身份证"].ToString())) { if (!string.IsNullOrEmpty(dt.Rows[i]["身份证"].ToString())) { string ddd = dt.Rows[i]["身份证"].ToString(); entity.IdCard = dt.Rows[i]["身份证"].ToString(); if (ddd != "" && ddd != null) { string sex = ""; //判断身份证是否18位 if (ddd.Length == 18) { sex = ddd.Substring(14, 3); if (sex != "" && sex != null) { if (int.Parse(sex) % 2 == 0) { entity.Sex = "女"; } else { entity.Sex = "男"; } } } //判断身份证是否15位 if (ddd.Length == 15) { sex = ddd.Substring(12, 3); if (sex != "" && sex != null) { if (int.Parse(sex) % 2 == 0) { entity.Sex = "女"; } else { entity.Sex = "男"; } } } } } } else { responsemsgdefault += "<p style='color:red'>" + "第" + (i + 2) + "行身份证格式不正确" + "</p></br>"; defaultcount++; continue; } if (!string.IsNullOrEmpty(dt.Rows[i]["联系手机"].ToString())) { entity.ContactPhone = dt.Rows[i]["联系手机"].ToString(); } if (!string.IsNullOrEmpty(dt.Rows[i]["现住地址"].ToString())) { entity.CurrentAddress = dt.Rows[i]["现住地址"].ToString(); } if (!string.IsNullOrEmpty(dt.Rows[i]["服务住所"].ToString())) { entity.ServicePremises = dt.Rows[i]["服务住所"].ToString(); } if (!string.IsNullOrEmpty(dt.Rows[i]["隔离时间"].ToString())) { entity.IsolationTime = dt.Rows[i]["隔离时间"].ToString(); } if (!string.IsNullOrEmpty(dt.Rows[i]["来源地"].ToString())) { entity.Origin = dt.Rows[i]["来源地"].ToString(); } if (!string.IsNullOrEmpty(dt.Rows[i]["去往地点"].ToString())) { entity.WhereToGo = dt.Rows[i]["去往地点"].ToString(); } if (!string.IsNullOrEmpty(dt.Rows[i]["去往地址"].ToString())) { entity.ToAddress = dt.Rows[i]["去往地址"].ToString(); } if (!string.IsNullOrEmpty(dt.Rows[i]["家庭成员联系方式"].ToString())) { entity.FamilyMemberContactInformation = dt.Rows[i]["家庭成员联系方式"].ToString(); } if (!string.IsNullOrEmpty(dt.Rows[i]["去往地址联络人"].ToString())) { entity.GoToAddressContact = dt.Rows[i]["去往地址联络人"].ToString(); } if (!string.IsNullOrEmpty(dt.Rows[i]["联络人手机号码"].ToString())) { entity.ContactPhoneNumber = dt.Rows[i]["联络人手机号码"].ToString(); } if (!string.IsNullOrEmpty(dt.Rows[i]["返回或预计返回时间"].ToString())) { entity.ReturnOrEstimatedReturnTime = dt.Rows[i]["返回或预计返回时间"].ToString(); } if (!string.IsNullOrEmpty(dt.Rows[i]["交通方式"].ToString())) { entity.Transportation = dt.Rows[i]["交通方式"].ToString(); } if (!string.IsNullOrEmpty(dt.Rows[i]["有无疑似发热"].ToString())) { entity.YesNoSuspectedFever = dt.Rows[i]["有无疑似发热"].ToString(); } if (!string.IsNullOrEmpty(dt.Rows[i]["排查方式"].ToString())) { entity.CheckMethod = dt.Rows[i]["排查方式"].ToString(); } if (!string.IsNullOrEmpty(dt.Rows[i]["责任医生姓名"].ToString())) { entity.NameOfResponsibleDoctor = dt.Rows[i]["责任医生姓名"].ToString(); } if (!string.IsNullOrEmpty(dt.Rows[i]["责任医生电话"].ToString())) { entity.PhoneNumberOfResponsibleDoctor = dt.Rows[i]["责任医生电话"].ToString(); } if (!string.IsNullOrEmpty(dt.Rows[i]["服务成员信息"].ToString())) { entity.ServiceMemberInformation = dt.Rows[i]["服务成员信息"].ToString(); } if (!string.IsNullOrEmpty(dt.Rows[i]["监护人员信息"].ToString())) { entity.GuardianInformation = dt.Rows[i]["监护人员信息"].ToString(); } if (!string.IsNullOrEmpty(dt.Rows[i]["备注"].ToString())) { entity.Remarks = dt.Rows[i]["备注"].ToString(); } entity.IsDeleted = 0; _dbContext.Yqfkryb.Add(entity); _dbContext.SaveChanges(); successcount++; } } responsemsgsuccess = "<p style='color:green'>导入成功:" + successcount + "条</p></br>" + responsemsgsuccess; responsemsgrepeat = "<p style='color:orange'>重复需手动修改数据:" + repeatcount + "条</p></br>" + responsemsgrepeat; responsemsgdefault = "<p style='color:red'>导入失败:" + defaultcount + "条</p></br>" + responsemsgdefault; ToLog.AddLog("导入", "成功:导入:疫情防控人员信息数据", _dbContext); DateTime endTime = DateTime.Now; TimeSpan useTime = endTime - beginTime; string taketime = "导入时间" + useTime.TotalSeconds.ToString() + "秒 "; response.SetData(JsonConvert.DeserializeObject(JsonConvert.SerializeObject(new { time = taketime, successmsg = responsemsgsuccess , repeatmsg = responsemsgrepeat, defaultmsg = responsemsgdefault }))); return(Ok(response)); } catch (Exception ex) { response.SetFailed(ex.Message); return(Ok(response)); } } }