private ActionResult HandleUploadedFile_20160513(List <UploadedBlacklist> list, UploadedFile uploadedFile) { using (var scope = this.unitOfWork.CreateTransactionScope()) { var repository = this.unitOfWork.Repository <Blacklist>(); var successCnt = 0; foreach (var model in list) { if (string.IsNullOrEmpty(model.Mobile)) { continue; } var entity = new Blacklist(); entity.Name = model.Name; entity.Mobile = model.Mobile; entity.E164Mobile = MobileUtil.GetE164PhoneNumber(model.Mobile); entity.Region = MobileUtil.GetRegionName(model.Mobile); entity.Enabled = true; entity.Remark = model.Remark; entity.UpdatedTime = uploadedFile.CreatedTime; entity.CreatedUserId = CurrentUserId; entity.UpdatedUserName = CurrentUserName; entity.UploadedFile = uploadedFile; var error = string.Empty; var isValid = this.validationService.Validate(entity, out error); if (isValid) { entity = repository.Insert(entity); successCnt++; } } scope.Complete(); string message = successCnt == list.Count ? string.Format("上傳黑名單成功,總共上傳{0}筆資料", list.Count) : string.Format("上傳黑名單成功,總共上傳{0}筆資料({1}筆成功,{2}筆失敗)", list.Count, successCnt, list.Count - successCnt); var result = new FileUploadResult { FileName = uploadedFile.FileName, Message = message, }; return(Json(result, JsonRequestBehavior.AllowGet)); } }
private ActionResult HandleUploadedFile(List <UploadedBlacklist> list, UploadedFile uploadedFile) { using (var scope = this.unitOfWork.CreateTransactionScope()) { var entities = list.Select((model, i) => new Blacklist { Name = model.Name, Mobile = model.Mobile, E164Mobile = MobileUtil.GetE164PhoneNumber(model.Mobile), Region = MobileUtil.GetRegionName(model.Mobile), Enabled = true, Remark = model.Remark, UpdatedTime = uploadedFile.CreatedTime, CreatedUserId = CurrentUserId, UpdatedUserName = CurrentUserName, UploadedFile = uploadedFile, }).ToList(); // 要加上 ToList,否則會檢驗呈無效名單,目前不知道為什麼 var error = string.Empty; entities = entities.Where(entity => this.validationService.Validate(entity, out error)).ToList(); var successCnt = entities.Count; context.BulkInsert(entities); context.MySaveChanges(); scope.Complete(); string message = successCnt == list.Count ? string.Format("上傳黑名單成功,總共上傳{0}筆資料", list.Count) : string.Format("上傳黑名單成功,總共上傳{0}筆資料({1}筆成功,{2}筆失敗)", list.Count, successCnt, list.Count - successCnt); var result = new FileUploadResult { FileName = uploadedFile.FileName, Message = message, }; return(Json(result, JsonRequestBehavior.AllowGet)); } }
private ActionResult HandleUploadedFile(List <UploadedContact> list, UploadedFile uploadedFile) { // TODO: 批次新增 using (var scope = this.unitOfWork.CreateTransactionScope()) { var repository = this.unitOfWork.Repository <Contact>(); var successCnt = 0; foreach (var model in list) { // 姓名以及行動電話必填 if (string.IsNullOrEmpty(model.Name)) { continue; } if (string.IsNullOrEmpty(model.Mobile)) { continue; } var entity = new Contact(); entity.Name = model.Name; entity.Mobile = model.Mobile; entity.E164Mobile = MobileUtil.GetE164PhoneNumber(model.Mobile); entity.Region = MobileUtil.GetRegionName(model.Mobile); entity.HomePhone = model.HomePhone; entity.CompanyPhone = model.CompanyPhone; entity.Email = model.Email; entity.Msn = model.Msn; entity.Description = model.Description; entity.Birthday = model.Birthday; entity.ImportantDay = model.ImportantDay; entity.Gender = model.Gender == "2" ? Gender.Female : model.Gender == "1" ? Gender.Male : Gender.Unknown; entity.CreatedUserId = CurrentUserId; var error = string.Empty; var isValid = this.validationService.Validate(entity, out error); if (isValid) { entity = repository.Insert(entity); string groupDescription = model.Group.Trim(); if (!string.IsNullOrEmpty(groupDescription)) { var group = this.unitOfWork.Repository <Group>().DbSet .Where(p => p.CreatedUserId == CurrentUserId && p.Name == model.Group.Trim()) .FirstOrDefault(); if (group != null) { this.unitOfWork.Repository <GroupContact>().Insert(new GroupContact { GroupId = group.Id, ContactId = entity.Id }); } } successCnt++; } } scope.Complete(); string message = successCnt == list.Count ? string.Format("上傳聯絡人成功,總共上傳{0}筆資料", list.Count) : string.Format("上傳聯絡人成功,總共上傳{0}筆資料({1}筆成功,{2}筆失敗)", list.Count, successCnt, list.Count - successCnt); var result = new FileUploadResult { FileName = uploadedFile.FileName, Message = message, }; return(Json(result, JsonRequestBehavior.AllowGet)); } }