public JsonResult DoImport() { var success = true; var message = "保存成功!"; var user = OperatorProvider.Provider.Current(); var company = new DepartmentBLL().GetCompany(user.DeptId); var baseUrl = Config.GetValue("ErchtmsApiUrl"); var client = new HttpClient(); var param1 = new { Data = new { companyId = company.DepartmentId } }; var requestContent = new StringContent(Newtonsoft.Json.JsonConvert.SerializeObject(param1)); requestContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"); var json = client.PostAsync($"{baseUrl}District/GetDistrict", requestContent).Result.Content.ReadAsStringAsync().Result; var districts = Newtonsoft.Json.JsonConvert.DeserializeObject <List <DistrictModel> >(json); var departments = new DepartmentBLL().GetAll(); var userbll = new UserBLL(); var categories = checkCategoryBLL.GetCategories(); if (this.Request.Files.Count > 0) { var book = new Workbook(this.Request.Files[0].InputStream); var sheet = book.Worksheets[0]; try { var data = new List <CheckTemplateEntity>(); for (int i = 2; i <= sheet.Cells.MaxDataRow; i++) { if (string.IsNullOrEmpty(sheet.Cells[i, 0].StringValue) && string.IsNullOrEmpty(sheet.Cells[i, 1].StringValue) && string.IsNullOrEmpty(sheet.Cells[i, 4].StringValue)) { continue; } var item = new CheckTemplateEntity(); item.CategoryName = sheet.Cells[i, 0].StringValue; item.ProblemContent = sheet.Cells[i, 1].StringValue; item.ProblemMeasure = sheet.Cells[i, 2].StringValue; item.DutyDepartmentName = sheet.Cells[i, 3].StringValue; item.DistrictName = sheet.Cells[i, 4].StringValue; //验证 if (string.IsNullOrEmpty(item.CategoryName)) { throw new Exception($"行 {i + 1} 问题类别不能为空!"); } if (string.IsNullOrEmpty(item.ProblemContent)) { throw new Exception($"行 {i + 1} 问题描述不能为空!"); } if (string.IsNullOrEmpty(item.DistrictName)) { throw new Exception($"行 {i + 1} 区域范围不能为空!"); } var category = categories.Find(x => x.CategoryName == item.CategoryName); if (category == null) { throw new Exception($"行 {i + 1} 问题类别不存在!"); } else { item.CategoryId = category.CategoryId; } var district = districts.Find(x => x.DistrictName == item.DistrictName); if (district == null) { throw new Exception($"行 {i + 1} 区域名称不存在!"); } else { item.DistrictId = district.DistrictID; } var department = departments.Find(x => x.FullName == item.DutyDepartmentName); if (department != null) { item.DutyDepartmentId = department.DepartmentId; } item.CreateDate = DateTime.Now; item.CreateUserId = item.ModifyUserId = user.UserId; item.TemplateId = Guid.NewGuid().ToString(); data.Add(item); } checkTemplateBLL.Save(data); } catch (Exception e) { success = false; message = e.Message; } } else { success = false; message = "请选择导入文件!"; } return(Json(new AjaxResult() { type = success ? ResultType.success : ResultType.error, message = message })); }
public JsonResult DoImport() { var success = true; var message = "保存成功!"; var user = OperatorProvider.Provider.Current(); var company = new DepartmentBLL().GetCompany(user.DeptId); var baseUrl = Config.GetValue("ErchtmsApiUrl"); var client = new HttpClient(); var param1 = new { Data = new { companyId = company.DepartmentId } }; var requestContent = new StringContent(Newtonsoft.Json.JsonConvert.SerializeObject(param1)); requestContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"); var json = client.PostAsync($"{baseUrl}District/GetDistrict", requestContent).Result.Content.ReadAsStringAsync().Result; var districts = Newtonsoft.Json.JsonConvert.DeserializeObject <List <DistrictModel> >(json); var param2 = new { Data = "区域责任人设置" }; requestContent = new StringContent(Newtonsoft.Json.JsonConvert.SerializeObject(param2)); requestContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"); json = client.PostAsync($"{baseUrl}KbsDeviceManage/GetElements", requestContent).Result.Content.ReadAsStringAsync().Result; var elements = Newtonsoft.Json.JsonConvert.DeserializeObject <ListModel <DataItemDetailEntity> >(json); var departments = new DepartmentBLL().GetAll(); var userbll = new UserBLL(); if (this.Request.Files.Count > 0) { var book = new Workbook(this.Request.Files[0].InputStream); var sheet = book.Worksheets[0]; try { var data = new List <DistrictPersonEntity>(); for (int i = 3; i <= sheet.Cells.MaxDataRow; i++) { if (string.IsNullOrEmpty(sheet.Cells[i, 0].StringValue) && string.IsNullOrEmpty(sheet.Cells[i, 1].StringValue) && string.IsNullOrEmpty(sheet.Cells[i, 2].StringValue) && string.IsNullOrEmpty(sheet.Cells[i, 3].StringValue) && string.IsNullOrEmpty(sheet.Cells[i, 5].StringValue)) { continue; } var item = new DistrictPersonEntity(); item.DistrictName = sheet.Cells[i, 0].StringValue; item.CategoryName = sheet.Cells[i, 1].StringValue; item.DutyDepartmentName = sheet.Cells[i, 2].StringValue; item.DutyUser = sheet.Cells[i, 3].StringValue; item.Phone = sheet.Cells[i, 4].StringValue; item.Cycle = sheet.Cells[i, 5].StringValue; //验证 if (string.IsNullOrEmpty(item.DistrictName)) { throw new Exception($"行 {i + 1} 区域名称不能为空!"); } if (string.IsNullOrEmpty(item.CategoryName)) { throw new Exception($"行 {i + 1} 责任人类别不能为空!"); } if (string.IsNullOrEmpty(item.DutyDepartmentName)) { throw new Exception($"行 {i + 1} 部门不能为空!"); } if (string.IsNullOrEmpty(item.DutyUser)) { throw new Exception($"行 {i + 1} 责任人不能为空!"); } if (string.IsNullOrEmpty(item.Cycle)) { throw new Exception($"行 {i + 1} 周期不能为空!"); } var district = districts.Find(x => x.DistrictName == item.DistrictName); if (district == null) { throw new Exception($"行 {i + 1} 区域名称不存在!"); } else { item.DistrictId = district.DistrictID; item.DistrictCode = district.DistrictCode; } var category = elements.Data.Find(x => x.ItemName == item.CategoryName); if (category == null) { throw new Exception($"行 {i + 1} 责任人类别不存在!"); } else { item.CategoryId = category.ItemDetailId; } var department = departments.Find(x => x.FullName == item.DutyDepartmentName); if (department == null) { throw new Exception($"行 {i + 1} 部门不存在!"); } else { item.DutyDepartmentId = department.DepartmentId; } //var dutyUser = userbll.Get(item.DutyDepartmentName, item.DutyUser); //if (dutyUser == null) throw new Exception($"行 {i + 1} 责任人不存在!"); //else // item.DutyUserId = dutyUser.UserId; item.CompanyId = company.DepartmentId; item.CompanyName = company.FullName; item.CreateDate = DateTime.Now; item.UpdateDate = DateTime.Now; item.CreateUserId = item.UpdateUserId = user.UserId; item.DistrictPersonId = Guid.NewGuid().ToString(); data.Add(item); } districtPersonBLL.Save(data); } catch (Exception e) { success = false; message = e.Message; } } else { success = false; message = "请选择导入文件!"; } return(Json(new AjaxResult() { type = success ? ResultType.success : ResultType.error, message = message })); }