public async Task <string> TaoTaiKhoanDoanhNghiep(CreateOrUpdateDoanhNghiepInfoInput input) { try { if (!string.IsNullOrEmpty(input.DoanhNghiep.TenDangNhap)) { if (checkExistTenDangNhap(input.DoanhNghiep.TenDangNhap)) { return("ten_dang_nhap_da_co"); } if (checkExistEmail(input.DoanhNghiep.EmailDoanhNghiep)) { return("email_da_co"); } if (IsValidEmail(input.DoanhNghiep.EmailXacNhan)) { if (checkExistEmail(input.DoanhNghiep.EmailXacNhan)) { return("email_da_co"); } CreateOrUpdateUserInput userDoanhNghiep = new CreateOrUpdateUserInput(); UserEditDto userdto = new UserEditDto(); userdto.Surname = input.DoanhNghiep.SurName; userdto.Name = input.DoanhNghiep.Name; userdto.EmailAddress = input.DoanhNghiep.EmailXacNhan; userdto.IsActive = false; userdto.IsLockoutEnabled = true; userdto.IsTwoFactorEnabled = false; userdto.UserName = input.DoanhNghiep.TenDangNhap.Trim(); userdto.PhoneNumber = input.DoanhNghiep.SoDienThoai; userdto.PhongBanId = null; userdto.RoleLevel = (int)CommonENum.ROLE_LEVEL.DOANH_NGHIEP; userdto.ShouldChangePasswordOnNextLogin = true; userDoanhNghiep.SendActivationEmail = true; userDoanhNghiep.SetRandomPassword = true; userDoanhNghiep.User = userdto; List <string> roleNames = new List <string>(); int tenancyDoanhNghiepId = 1; if (!ConfigurationManager.AppSettings["TENANTCY_ID_DOANH_NGHIEP"].IsNullOrEmpty()) { tenancyDoanhNghiepId = Convert.ToInt32(ConfigurationManager.AppSettings["TENANTCY_ID_DOANH_NGHIEP"]); } using (CurrentUnitOfWork.DisableFilter(AbpDataFilters.MayHaveTenant)) { var roleDoanhNghiep = _roleManager.Roles.Where(x => x.IsDefault == true && x.TenantId == tenancyDoanhNghiepId).ToList(); if (roleDoanhNghiep != null && roleDoanhNghiep.Count() > 0) { foreach (var role in roleDoanhNghiep) { roleNames.Add(role.Name); } } } userDoanhNghiep.AssignedRoleNames = roleNames.ToArray(); if (userDoanhNghiep.AssignedRoleNames.Length > 0) { using (var unitOfWork = _unitOfWorkManager.Begin()) { var userId = await _userService.CreateUserFrontEndAsync(userDoanhNghiep); if (input.DoanhNghiep.TinhId != null) { var tinh = _tinhRepos.Get(input.DoanhNghiep.TinhId.Value); input.DoanhNghiep.Tinh = tinh != null ? tinh.Ten : ""; } if (input.DoanhNghiep.HuyenId.HasValue) { var huyen = _huyenRepos.Get(input.DoanhNghiep.HuyenId.Value); input.DoanhNghiep.Huyen = huyen != null ? huyen.Ten : ""; } if (input.DoanhNghiep.XaId.HasValue) { var xa = _xaRepos.Get(input.DoanhNghiep.XaId.Value); input.DoanhNghiep.Xa = xa != null ? xa.Ten : ""; } if (input.DoanhNghiep.LoaiHinhDoanhNghiepId != null) { var loaihinh = _loaiHinhRepos.Get(input.DoanhNghiep.LoaiHinhDoanhNghiepId.Value); input.DoanhNghiep.TenLoaiHinh = loaihinh != null ? loaihinh.TenLoaiHinh : ""; } input.DoanhNghiep.IsDaXuLy = false; input.DoanhNghiep.MaSoThue = input.DoanhNghiep.MaSoThue.Trim(); long id = await CreateOrUpdateDoanhNghiepAsync(input); UpdateUserDoanhNghiepId(userId, id); unitOfWork.Complete(); } } else { return("vai_tro_doanh_nghiep_khong_ton_tai"); } return("ok"); } else { return("email_khong_ton_tai"); } } else { return("ten_dang_nhap_khong_hop_le"); } } catch (Exception ex) { Logger.Error($"{DateTime.Now.ToString("HH:mm:ss dd/MM/yyyy")} TaoTaiKhoanDoanhNghiep {ex.Message} {JsonConvert.SerializeObject(ex)}"); return(ex.Message); } }
public async Task <long> CreateOrUpdateDoanhNghiepAsync(CreateOrUpdateDoanhNghiepInfoInput input) { if (input.DoanhNghiep.Id != 0) { // update var updateData = await _doanhNghiepRepos.GetAsync(input.DoanhNghiep.Id); input.DoanhNghiep.MapTo(updateData); await _doanhNghiepRepos.UpdateAsync(updateData); return(updateData.Id); } else { try { var insertInput = input.DoanhNghiep.MapTo <DoanhNghiep>(); long id = await _doanhNghiepRepos.InsertAndGetIdAsync(insertInput); #region HoSoPhapLy var giayPheps = _giayPhepPhapLyRepos.GetAll().Where(a => a.DoanhNghiepId == id).ToList(); foreach (var item in giayPheps) { _giayPhepPhapLyRepos.Delete(item); } var path = SetPath(insertInput); var temp = ""; foreach (var item in input.DoanhNghiep.GiayPhepPhapLys) { item.DoanhNghiepId = id; item.DaTaiLen = true; item.IsActive = true; if (item.IsNew == true) { temp = item.DuongDanTep; } item.DuongDanTep = item.IsNew == true?CopyTo(item.DuongDanTep, path) : item.DuongDanTep; _giayPhepPhapLyRepos.Insert(item); } if (!string.IsNullOrWhiteSpace(temp)) { var index = temp.LastIndexOf("\\"); temp = temp.Substring(0, index); EmptyFolder(temp); } #endregion return(id); } catch (Exception ex) { throw new Exception(ex.Message); } } }