public ActionResult SaveForm(KeyBoxEntity entity) { Operator user = OperatorProvider.Provider.Current(); WriteLog.AddLog($"用户:{user.UserName} id:{user.UserId}开始编辑钥匙管理数据 \r\n {JsonConvert.SerializeObject(entity)}", "KeyBox"); try { if (string.IsNullOrEmpty(entity.CreateUserId)) { entity.CreateDate = DateTime.Now; entity.CreateUserId = user.UserId; entity.CreateUserName = user.UserName; } entity.ModifyDate = DateTime.Now; entity.ModifyUserId = user.UserId; entity.ModifyUserName = user.UserName; List <KeyBoxEntity> dataList = new List <KeyBoxEntity>(); dataList.Add(entity); _bll.operateKeyBox(dataList); return(Success("操作成功")); } catch (Exception ex) { WriteLog.AddLog($"错误 \r\n用户:{user.UserName} id:{user.UserId}编辑钥匙管理数据失败 \r\n 错误信息:{JsonConvert.SerializeObject(ex)}", "SafetyScore"); return(Error(ex.Message)); } }
public JsonResult Importxlsx() { DepartmentBLL departmentBLL = new DepartmentBLL(); UserBLL userBLL = new UserBLL(); Operator user = OperatorProvider.Provider.Current(); var success = true; var message = string.Empty; try { if (this.Request.Files.Count == 0) { throw new Exception("请上传文件"); } if (!this.Request.Files[0].FileName.EndsWith(".xlsx")) { throw new Exception("请上传 Excel 文件"); } //读取文件 var book = new Workbook(this.Request.Files[0].InputStream); //获取第一个sheet var sheet = book.Worksheets[0]; //列表实体 var templates = new List <KeyBoxEntity>(); //主表 //DateTime dtDate; var userList = userBLL.GetUserList(); var deptList = departmentBLL.GetList(); //获取CATEGORY var valueStr = string.Format("\"userid\":\"{0}\"", user.UserId); var DataStr = string.Format("\"code\":\"{0}\"", "HidMajorClassify"); DataStr = "{" + DataStr + "}"; valueStr = "{" + valueStr + ",\"data\":" + DataStr + "}"; var CategoryData = webClientERCHTMS <BaseResultData>("BaseData/GetCodeList", valueStr); var CategoryList = CategoryData.data; //获取keycode var KeycodeList = _bll.getKeyBoxData(); for (int i = 1; i <= sheet.Cells.MaxDataRow; i++) { var entity = new KeyBoxEntity(); entity.ID = Guid.NewGuid().ToString(); if (!string.IsNullOrEmpty(sheet.Cells[i, 0].StringValue)) { entity.KeyCode = sheet.Cells[i, 0].StringValue.Trim(); entity.Category = sheet.Cells[i, 1].StringValue.Trim(); var CkCategory = CategoryList.FirstOrDefault(row => row.ItemName == entity.Category); if (CkCategory == null) { throw new Exception("第" + (i + 1) + "行,不存在该专业类型!"); } entity.CategoryId = CkCategory.ItemValue; var CkCode = KeycodeList.FirstOrDefault(row => row.KeyCode == entity.KeyCode && row.Category == entity.Category); var EntityCode = templates.Where(row => row.KeyCode == entity.KeyCode && row.Category == entity.Category); if (EntityCode.Count() > 0) { throw new Exception("第" + (i + 1) + "行,钥匙编码重复!"); } if (CkCode != null) { throw new Exception("第" + (i + 1) + "行,钥匙编码重复!"); } //编码规则 try { var Sort = new List <int>(); for (int j = 0; j < entity.KeyCode.Length; j++) { var ckSort = Util.Str.ContainsNumber(entity.KeyCode[j].ToString()); if (ckSort) { Sort.Add(j); } } if (Sort.Count == 1) { throw new Exception("第" + (i + 1) + "行,钥匙编码格式错误!"); } var sortStr = string.Empty; int m = 0; for (int n = Sort[0]; n <= Sort[Sort.Count - 1]; n++) { sortStr += entity.KeyCode[n]; if (m != Sort.Count - 1) { if (Sort[m] + 1 != Sort[m + 1]) { throw new Exception("第" + (i + 1) + "行,钥匙编码格式错误!"); } } m++; } entity.Sort = sortStr; } catch (Exception ex) { throw new Exception("第" + (i + 1) + "行,钥匙编码格式错误!"); } entity.KeyPlace = sheet.Cells[i, 2].StringValue.Trim(); var depatName = sheet.Cells[i, 3].StringValue.Trim(); var CkDept = deptList.FirstOrDefault(row => row.FullName == depatName); if (CkDept == null) { throw new Exception("第" + (i + 1) + "行,系统不存在该部门!"); } entity.DeptCode = CkDept.EnCode; entity.DeptId = CkDept.DepartmentId; entity.DeptName = CkDept.FullName; entity.CreateDate = DateTime.Now; entity.CreateUserId = user.UserId; entity.CreateUserName = user.UserName; entity.ModifyDate = DateTime.Now; entity.ModifyUserId = user.UserId; entity.ModifyUserName = user.UserName; entity.State = false; //if (!string.IsNullOrEmpty(sheet.Cells[i, 9].StringValue)) //{ // if (DateTime.TryParse(sheet.Cells[i, 9].StringValue, out dtDate)) // { // } // else // { // throw new Exception("第" + (i + 1) + "行,演练时间格式错误!"); // } //} templates.Add(entity); } } _bll.operateKeyBox(templates); } catch (Exception ex) { success = false; message = HttpUtility.JavaScriptStringEncode(ex.Message); } return(Json(new { success, message })); }