public async Task <ResultObject> CreateCustomerAsync(CustomerHeadDto customerDto) { ResultObject result = new ResultObject(); var cu = await _customerService.GetCustomerByNationalCodeAndBranchIDAsync(customerDto.NationalCode); if (cu != null) { result.ServerErrors.Add(new ServerErr() { Hint = "کد ملی مشتری نمیتواند تکراری باشد" }); return(result); } customerDto.FillCustomerDetail(); result = ValidateCustomerDetail(customerDto, result); if (result.ServerErrors.Count > 0) { return(result); } var customer = _mapper.Map <CustomerHead>(customerDto); if (customerDto.Skills != null && customerDto.Skills.Count > 0) { foreach (var skillID in customerDto.Skills) { customer.CustomerSkills.Add(new CustomerSkill() { SkillID = skillID }); } } var mainUser = await _userAppService.GetUserByNationalCodeAsync(customerDto.NationalCode); User user = new User(); IDbContextTransaction transaction; if (mainUser == null) { user.FirstName = customerDto.FirstName; user.LastName = customerDto.LastName; user.UserName = customerDto.NationalCode; user.NationalCode = customerDto.NationalCode; user.IsActive = true; user.IsRequireChangePass = true; user.Password = "******"; UserBranches userBranch = new UserBranches() { BranchId = customerDto.BranchID, UserId = user.ID }; UserBranchRole userBranchRole = new UserBranchRole() { BranchId = customerDto.BranchID, UserId = user.ID, RoleId = ConstRoles.Customer }; user.UserBranches.Add(userBranch); user.UserBranchRoles.Add(userBranchRole); transaction = await _userService.CreateUserByUserBranchAsync(user); await _transactionManager.SaveAllAsync(); customer.CustomerUserID = user.ID; await _customerService.CreateCustomerAsync(customer); var res = await _transactionManager.SaveAllAsync(); transaction.Commit(); } else { if (result.ServerErrors.Count > 0) { return(result); } customer.CustomerUserID = mainUser.ID; await _customerService.CreateCustomerAsync(customer); var res = await _transactionManager.SaveAllAsync(); } if (customer.IsMale) { _notificationAppService.SendMessage($"جناب آقای {customer.FirstName + " " + customer.LastName} " + $"{Environment.NewLine} اطلاعات شما با شماره مشتری {customer.Serial} در سامانه صندوق های خرد محلی {customerDto.BranchName} ثبت گردید {Environment.NewLine} نام کاربری {user.UserName}", customerDto.MobileCustomerDetail.Value); } else { _notificationAppService.SendMessage($"سرکار خانم {customer.FirstName + " " + customer.LastName} {Environment.NewLine} اطلاعات شما با شماره مشتری {customer.Serial} در سامانه صندوق های خرد محلی {customerDto.BranchName} ثبت گردید {Environment.NewLine} نام کاربری {user.UserName}", customerDto.MobileCustomerDetail.Value); } result.Result = customer.Serial; result.ServerErrors = null; return(result); }