//public async Task<Account> CreateAccountAsync(AccountDto accountDto)
        //{
        //    //باز کردن موقتی حساب
        //    accountDto.AccountStatusID = ConstAccountStatuses.Open;

        //    Branch branch = await branchAppService._branchService.GetBranchAsync(accountDto.BranchID);

        //    accountDto.BranchCode = branch.BranchCode;
        //    var account = mapper.Map<Account>(accountDto);
        //    await accountService.CreateAccountAsync(account);

        //    var result = await transactionManager.SaveAllAsync();

        //    return account;
        //}


        public async Task <ResultObject> UpdateCustomerAsync(CustomerHeadDto customerDto)
        {
            ResultObject resultObject = new ResultObject();

            customerDto.FillCustomerDetail();
            var customerHead = _mapper.Map <CustomerHead>(customerDto);

            // بروز رسانی جزئیات مشتری
            foreach (var detail in customerHead.CustomerDetails)
            {
                if (detail.ID != 0)
                {
                    var customerDetailTask = await _customerService.UpdateCustomerDetailsAsync(detail);
                }
                else
                {
                    await _customerService.AddCustomerDetailsAsync(detail);
                }
            }

            foreach (var skill in customerDto.Skills)
            {
                customerHead.CustomerSkills.Add(new CustomerSkill()
                {
                    CustomerID = customerHead.ID, SkillID = skill
                });
            }
            await _customerService.UpdateCustomerSkills(customerHead.CustomerSkills, customerHead.ID);

            var customerHeadTask = await _customerService.UpdateCustomerAsync(customerHead);

            await _transactionManager.SaveAllAsync();

            resultObject.Result       = customerHeadTask.Serial;
            resultObject.ServerErrors = null;
            return(resultObject);
        }
        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);
        }