/// <summary> /// /// </summary> /// <param name="uploadDTO"></param> /// <returns></returns> public GetInstituteDTO AddInstitute(AddInstDTO uploadDTO) { using (var ctx = new AspodesDB()) { using (var transaction = ctx.Database.BeginTransaction()) { try { //检查联系人、单位是否存在 if (ctx.Institutes.Any(i => i.Code == uploadDTO.Code || i.Name == uploadDTO.Name)) { throw new OtherException("单位已存在"); } if (ctx.Persons.Any(p => p.IDCard == uploadDTO.IDCard || p.Email == uploadDTO.Email)) { throw new OtherException("人员已存在"); } var newInst = AddInst(ctx, uploadDTO); transaction.Commit(); return(Mapper.Map <GetInstituteDTO>(newInst)); } catch (Exception e) { transaction.Rollback(); throw e; } } } }
private Institute AddInst(AspodesDB ctx, AddInstDTO dto) { var inst = Mapper.Map <Institute>(dto); var person = Mapper.Map <Person>(dto); //添加单位 var newInst = ctx.Institutes.Add(inst); ctx.SaveChanges(); //添加联系人 person.InstituteId = newInst.InstituteId; var newPerson = ctx.Persons.Add(person); ctx.SaveChanges(); //给联系人添加账户 User user = ctx.Users.Add(Mapper.Map <User>(newPerson)); ctx.SaveChanges(); //设置单位联系人 newInst.ContactId = user.UserId; //给联系人添加用户角色 Model.Authorize commen = new Model.Authorize { UserId = user.UserId, RoleId = 1 }; ctx.Authorizes.Add(commen); //给联系人添加单位管理员角色 Model.Authorize instAdmin = new Model.Authorize { UserId = user.UserId, RoleId = 2 }; ctx.Authorizes.Add(instAdmin); ctx.SaveChanges(); return(newInst); }
/// <summary> /// 导入单位信息 /// </summary> /// <returns></returns> public HttpResponseMessage UplodInstInfoFile() { IWorkbook workbook = null; ISheet sheet = null; StringBuilder responseMsg = new StringBuilder(); Dictionary <string, int> propertyIndex = new Dictionary <string, int>(); List <AddInstDTO> uploadInstDTOs = new List <AddInstDTO>(); //读取文件 try { string dir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, SystemConfig.UploadFilePathWeb, "InstInfo"); //保存文件 //string newName = DateTime.Now.ToFileTime() + ".xlsx"; string newName = FileHelper.Upload(HttpContext.Current, dir); //获取文件标题映射 var fileTitle = File.ReadAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Config", "InstInfoFileTitle.json"), Encoding.UTF8); Dictionary <string, string> titleMap = JsonConvert.DeserializeObject <Dictionary <string, string> >(fileTitle); //读取上传的Excel workbook = new XSSFWorkbook(Path.Combine(dir, newName)); sheet = workbook.GetSheetAt(0); //读取Excel表格标题 int titleRowIndex = 0; var titleRow = sheet.GetRow(titleRowIndex); foreach (var map in titleMap) { var cell = titleRow.Cells.First(c => !string.IsNullOrEmpty(c.StringCellValue) && c.StringCellValue.Trim().Equals(map.Value)); propertyIndex.Add(map.Key, cell.ColumnIndex); } //删除标题行 sheet.RemoveRow(titleRow); //检查数据是否为空 Type uploadType = typeof(AddInstDTO); using (var ctx = new AspodesDB()) { foreach (IRow row in sheet) { AddInstDTO uploadDTO = new AddInstDTO(); foreach (var map in propertyIndex) { //检查单元格是否为空 ICell cell = row.GetCell(map.Value); var property = uploadType.GetProperty(map.Key); property.SetValue(uploadDTO, cell == null ? null : cell.StringCellValue); } //检查属性值是否合法 var context = new ValidationContext(uploadDTO, null, null); var results = new List <ValidationResult>(); Validator.TryValidateObject(uploadDTO, context, results, true); foreach (var validationResult in results) { responseMsg.Append(string.Format("第{0}行:{1}<br/>", row.RowNum, validationResult.ErrorMessage)); } uploadInstDTOs.Add(uploadDTO); } } } catch (Exception e) { return(ResponseWrapper.ExceptionResponse(e)); } finally { if (null != workbook) { workbook.Close(); } } DbContextTransaction transaction = null; using (var ctx = new AspodesDB()) { try { foreach (var dto in uploadInstDTOs) { //检查联系人、单位是否存在 if (ctx.Institutes.Any(i => i.Code == dto.Code || i.Name == dto.Name)) { responseMsg.Append(string.Format("代码:{0},名称:{1}单位已存在\n", dto.Code, dto.Name)); } if (ctx.Persons.Any(p => p.IDCard == dto.IDCard || p.Email == dto.Email)) { responseMsg.Append(string.Format("身份证:{0},邮箱:{1}人员已存在\n", dto.IDCard, dto.Email)); } } if (responseMsg.Length > 0) { return(ResponseWrapper.ExceptionResponse(new OtherException(responseMsg.ToString()))); } //添加进数据库 transaction = ctx.Database.BeginTransaction(); foreach (var dto in uploadInstDTOs) { AddInst(ctx, dto); } transaction.Commit(); } catch (Exception e) { if (null != transaction) { transaction.Rollback(); } return(ResponseWrapper.ExceptionResponse(e)); } finally { if (null != transaction) { transaction.Dispose(); } } } return(ResponseWrapper.SuccessResponse("上传成功")); }