public ActionResult SaveCustomer(CustomerModel model) { if (model.IsNew && !CanAdd) { return GetAddDeniedResult(); } if (!model.IsNew && !CanUpdate) { return GetUpdateDeniedResult(); } ModelState.Remove("City"); ModelState.Remove("District"); if (!ModelState.IsValid && GetModelStateErrorList().Any()) { return JsonObject(false, GetModelStateErrors()); } #region fill working hours DateTime dateTime; if (!string.IsNullOrWhiteSpace(model.SundayStartStr)) { dateTime = DateTime.Parse(model.SundayStartStr, CultureInfo.CurrentCulture); model.SundayStart = new DateTime(ConstKeys.YearForTime, ConstKeys.MonthForTime, ConstKeys.DayForTime, dateTime.Hour, dateTime.Minute, 0); } if (!string.IsNullOrWhiteSpace(model.SundayEndStr)) { dateTime = DateTime.Parse(model.SundayEndStr, CultureInfo.CurrentCulture); model.SundayEnd = new DateTime(ConstKeys.YearForTime, ConstKeys.MonthForTime, ConstKeys.DayForTime, dateTime.Hour, dateTime.Minute, 0); } //--- if (!string.IsNullOrWhiteSpace(model.MondayStartStr)) { dateTime = DateTime.Parse(model.MondayStartStr, CultureInfo.CurrentCulture); model.MondayStart = new DateTime(ConstKeys.YearForTime, ConstKeys.MonthForTime, ConstKeys.DayForTime, dateTime.Hour, dateTime.Minute, 0); } if (!string.IsNullOrWhiteSpace(model.MondayEndStr)) { dateTime = DateTime.Parse(model.MondayEndStr, CultureInfo.CurrentCulture); model.MondayEnd = new DateTime(ConstKeys.YearForTime, ConstKeys.MonthForTime, ConstKeys.DayForTime, dateTime.Hour, dateTime.Minute, 0); } //-- if (!string.IsNullOrWhiteSpace(model.TuesdayStartStr)) { dateTime = DateTime.Parse(model.TuesdayStartStr, CultureInfo.CurrentCulture); model.TuesdayStart = new DateTime(ConstKeys.YearForTime, ConstKeys.MonthForTime, ConstKeys.DayForTime, dateTime.Hour, dateTime.Minute, 0); } if (!string.IsNullOrWhiteSpace(model.TuesdayEndStr)) { dateTime = DateTime.Parse(model.TuesdayEndStr, CultureInfo.CurrentCulture); model.TuesdayEnd = new DateTime(ConstKeys.YearForTime, ConstKeys.MonthForTime, ConstKeys.DayForTime, dateTime.Hour, dateTime.Minute, 0); } //--- if (!string.IsNullOrWhiteSpace(model.WednesdayStartStr)) { dateTime = DateTime.Parse(model.WednesdayStartStr, CultureInfo.CurrentCulture); model.WednesdayStart = new DateTime(ConstKeys.YearForTime, ConstKeys.MonthForTime, ConstKeys.DayForTime, dateTime.Hour, dateTime.Minute, 0); } if (!string.IsNullOrWhiteSpace(model.WednesdayEndStr)) { dateTime = DateTime.Parse(model.WednesdayEndStr, CultureInfo.CurrentCulture); model.WednesdayEnd = new DateTime(ConstKeys.YearForTime, ConstKeys.MonthForTime, ConstKeys.DayForTime, dateTime.Hour, dateTime.Minute, 0); } //--- if (!string.IsNullOrWhiteSpace(model.ThursdayStartStr)) { dateTime = DateTime.Parse(model.ThursdayStartStr, CultureInfo.CurrentCulture); model.ThursdayStart = new DateTime(ConstKeys.YearForTime, ConstKeys.MonthForTime, ConstKeys.DayForTime, dateTime.Hour, dateTime.Minute, 0); } if (!string.IsNullOrWhiteSpace(model.ThursdayEndStr)) { dateTime = DateTime.Parse(model.ThursdayEndStr, CultureInfo.CurrentCulture); model.ThursdayEnd = new DateTime(ConstKeys.YearForTime, ConstKeys.MonthForTime, ConstKeys.DayForTime, dateTime.Hour, dateTime.Minute, 0); } //--- if (!string.IsNullOrWhiteSpace(model.FridayStartStr)) { dateTime = DateTime.Parse(model.FridayStartStr, CultureInfo.CurrentCulture); model.FridayStart = new DateTime(ConstKeys.YearForTime, ConstKeys.MonthForTime, ConstKeys.DayForTime, dateTime.Hour, dateTime.Minute, 0); } if (!string.IsNullOrWhiteSpace(model.FridayEndStr)) { dateTime = DateTime.Parse(model.FridayEndStr, CultureInfo.CurrentCulture); model.FridayEnd = new DateTime(ConstKeys.YearForTime, ConstKeys.MonthForTime, ConstKeys.DayForTime, dateTime.Hour, dateTime.Minute, 0); } //--- if (!string.IsNullOrWhiteSpace(model.SaturdayStartStr)) { dateTime = DateTime.Parse(model.SaturdayStartStr, CultureInfo.CurrentCulture); model.SaturdayStart = new DateTime(ConstKeys.YearForTime, ConstKeys.MonthForTime, ConstKeys.DayForTime, dateTime.Hour, dateTime.Minute, 0); } if (!string.IsNullOrWhiteSpace(model.SaturdayEndStr)) { dateTime = DateTime.Parse(model.SaturdayEndStr, CultureInfo.CurrentCulture); model.SaturdayEnd = new DateTime(ConstKeys.YearForTime, ConstKeys.MonthForTime, ConstKeys.DayForTime, dateTime.Hour, dateTime.Minute, 0); } #endregion #region visit var jsonSerializer = new JavaScriptSerializer(); model.Visits = jsonSerializer.Deserialize<List<CustomerVisitModel>>(model.VisitJsonString); model.CustomerServices = jsonSerializer.Deserialize<List<CustomerServiceModel>>(model.CustomerServiceJsonString); #endregion #region save list image var listImagePath = new List<string>(); for (var i = 0; i < Request.Files.Count; ++i) { var file = Request.Files[i]; if (file != null && !string.IsNullOrWhiteSpace(file.FileName)) { var extension = Path.GetExtension(file.FileName).ToStr(); if (!SiteUtils.IsImageFile(file.FileName)) { return JsonObject(false, BackendMessage.FileTypeIsInvalid); } if (!SiteUtils.ImageSizeIsValid(file.ContentLength)) { return JsonObject(false, BackendMessage.FileMaxSize5MB); } var imagePath = Guid.NewGuid() + extension; listImagePath.Add(imagePath); var filePath = PhysicalDataFilePath(imagePath); file.SaveAs(filePath); } } #endregion var entity = model.Map<CustomerModel, Customer>(); var oldData = new Customer(); entity.UpdatedDate = DateTime.UtcNow; entity.UpdatedBy = CurrentUserId; if (entity.IsNew) { entity.InitId(); entity.CreatedDate = DateTime.UtcNow; entity.CreatedBy = CurrentUserId; } else { oldData = ServiceHelper.Customer.ExecuteDispose(s => s.GetCustomer(entity.Id)); if (oldData == null) { return JsonObject(false, BackendMessage.CannotLoadData); } entity.CreatedDate = oldData.CreatedDate; entity.CreatedBy = oldData.CreatedBy; } entity.Visits.ForEach(i => i.CustomerId = entity.Id); entity.CustomerServices.ForEach(i => i.CustomerId = entity.Id); var response = ServiceHelper.Customer.ExecuteDispose(s => s.SaveCustomer(new SaveRequest { Entity = entity })); if (response.Success) { if (listImagePath.Any()) { var listImages = new List<CustomerImage>(); listImagePath.ForEach(imagePath => { var image = new CustomerImage { ImagePath = imagePath, CustomerId = entity.Id, CreatedDate = DateTime.UtcNow }; image.InitId(); listImages.Add(image); }); ServiceHelper.Customer.ExecuteDispose(s => s.SaveListImage(listImages)); } SendNotification(NotifyType.Success, BackendMessage.SaveDataSuccess); #region Log var dataLog = new DataLog { BeUserId = CurrentUserId, Table = TableLog.Customer, Action = model.IsNew ? ActionLog.Insert : ActionLog.Update, LogDate = DateTime.UtcNow, NewData = ObjectToLog(entity), OldData = model.IsNew ? string.Empty : ObjectToLog(oldData), ItemId = entity.Id }; ServiceHelper.DataLog.ExecuteDispose(s => s.Insert(new Data.DataContract.DataLogDC.SaveRequest { Entity = dataLog })); #endregion return JsonObject(true, string.Empty); } if (listImagePath.Any()) { listImagePath.ForEach(DeleteImageFile); } return JsonObject(false, response.Messages.FirstOrDefault().GetServiceMessageRes()); }
private void PopulateSaveImportData(List<Customer> listCustomer, DataTable table) { List<EnumModel> listCity = SiteUtils.GetVietNamProvines(); var cache = ObjectFactory.GetInstance<ICacheHelper>(); var listNumberOfDentist = cache.GetCategories(CategoryType.NumberOfDentist); var listNumberOfStaff = cache.GetCategories(CategoryType.NumberOfStaff); var listNumberOfChair = cache.GetCategories(CategoryType.NumberOfChair); var listUsingRC = cache.GetCategories(CategoryType.UsingRC); var listProducts = cache.GetCategories(CategoryType.Product); var age = cache.GetCategories(CategoryType.Age); var specialization = cache.GetCategories(CategoryType.Specialization); foreach (DataRow row in table.Rows) { EnumModel city = listCity.Find(c => c.Name == row[6].ToStr()); var districts = SiteUtils.GetVietNamDistricts(city.Value).Find(d => d.Name == row[7].ToStr()); var numberOfDentist = listNumberOfDentist.Find(n => n.Name == row[8].ToStr()); var numberOfStaff = listNumberOfStaff.Find(n => n.Name == row[9].ToStr()); var numberOfChair = listNumberOfChair.Find(n => n.Name == row[10].ToStr()); var usingRC = listUsingRC.Find(n => n.Name == row[11].ToStr()); string[] arr_usingDevice = row[12].ToStr().Split(';'); var usingDevice = new List<Category>(); foreach (var itemUsingDevice in arr_usingDevice) { var device = listProducts.Find(n => n.Name.Contains(itemUsingDevice.Trim())); if (device != null) { usingDevice.Add(device); } } var v_age = age.Find(a => a.Name == row[17].ToStr()); string[] arr_specialization = row[18].ToStr().Split(';'); var usingSpecialization = new List<Category>(); foreach (var itemUsingDevice in arr_specialization) { var n_specialization = listProducts.Find(n => n.Name.Contains(itemUsingDevice.Trim())); if (n_specialization != null) { usingSpecialization.Add(n_specialization); } } var item = new Customer { CustomerType = CustomerType, ClinicId = row[0].ToStr(), ClinicName = row[1].ToStr(), ClinicEmail = row[2].ToStr(), Website = row[3].ToStr(), ClinicPhone = row[4].ToStr(), Address = row[5].ToStr(), City = city == null ? (short)0 : city.Value, District = districts == null ? (short)0 : districts.Value, NumberOfDentist = numberOfDentist == null ? (Guid?)null : numberOfDentist.Id, NumberOfStaff = numberOfStaff == null ? (Guid?)null : numberOfStaff.Id, NumberOfChair = numberOfChair == null ? (Guid?)null : numberOfChair.Id, UsingRC = usingRC == null ? (Guid?)null : usingRC.Id, UsingDevices = this.GetIdCategories(usingDevice), DentistName = row[13].ToStr(), Gender = row[14].ToStr() == "Male" ? Gender.Nam : row[14].ToStr() == "Female" ? Gender.Nu : (Gender?)null, DentistPhone = row[15].ToStr(), DentistEmail = row[16].ToStr(), Age = v_age == null ? (Guid?)null : v_age.Id, Specialization = this.GetIdCategories(usingSpecialization), MaritalStatus = row[19].ToStr() == "Single" ? MaritalStatus.Single : row[19].ToStr() == "Married" ? MaritalStatus.Married : MaritalStatus.Divorced, AssignTo = row[20].ToStr(), Remark = row[21].ToStr(), CreatedBy = CurrentUserId, CreatedDate = DateTime.UtcNow, UpdatedBy = CurrentUserId, UpdatedDate = DateTime.UtcNow }; item.InitId(); listCustomer.Add(item); } }