Esempio n. 1
0
        public async Task <ActionResultResponse> Update(string tenantId, string id, CustomerMeta customerMeta)
        {
            var customer = await _customerRepository.GetInfo(id);

            if (customer == null)
            {
                return(new ActionResultResponse(-1, _customerResourceService.GetString("Customer does not exists.")));
            }

            if (customer.ConcurrencyStamp != customerMeta.ConcurrencyStamp)
            {
                return(new ActionResultResponse(-2,
                                                _customerResourceService.GetString("The Customer already updated by other people. You can not update this Customer.")));
            }

            if (customer.TenantId != tenantId)
            {
                return(new ActionResultResponse(-3,
                                                _sharedResourceService.GetString(ErrorMessage.NotHavePermission)));
            }

            var oldProvinceId = customer.ProvinceId;
            var oldDistrictId = customer.DistrictId;
            var oldNationalId = customer.NationalId;
            var oldReligionId = customer.ReligionId;
            var oldEthnicId   = customer.EthnicId;

            if (oldProvinceId != customerMeta.ProvinceId)
            {
                if (customerMeta.ProvinceId.HasValue)
                {
                    var provinceInfo = await GetProvinceInfo(customerMeta.ProvinceId.Value);

                    if (provinceInfo == null)
                    {
                        return(new ActionResultResponse(-4,
                                                        _sharedResourceService.GetString("Province does not exists. Please try again.")));
                    }

                    customer.ProvinceId   = provinceInfo.Id;
                    customer.ProvinceName = provinceInfo.Name;
                }
                else
                {
                    customer.ProvinceId   = null;
                    customer.ProvinceName = null;
                }
            }

            if (oldDistrictId != customerMeta.DistrictId)
            {
                if (customerMeta.DistrictId.HasValue)
                {
                    var districtInfo = await GetDistrictInfo(customerMeta.DistrictId.Value);

                    if (districtInfo == null)
                    {
                        return(new ActionResultResponse(-5,
                                                        _sharedResourceService.GetString("District does not exists. Please try again.")));
                    }

                    customer.DistrictId   = districtInfo.Id;
                    customer.DistrictName = districtInfo.Name;
                }
                else
                {
                    customer.DistrictId   = null;
                    customer.DistrictName = null;
                }
            }

            if (oldNationalId != customerMeta.NationalId)
            {
                if (customerMeta.NationalId.HasValue)
                {
                    var nationalInfo = await GetNationalInfo(customerMeta.NationalId.Value);

                    if (nationalInfo == null)
                    {
                        return(new ActionResultResponse(-6,
                                                        _sharedResourceService.GetString("Nation does not exists. Please try again.")));
                    }

                    customer.NationalId   = nationalInfo.Id;
                    customer.NationalName = nationalInfo.Name;
                }
                else
                {
                    customer.NationalId   = null;
                    customer.NationalName = null;
                }
            }

            if (oldReligionId != customerMeta.ReligionId)
            {
                if (customerMeta.ReligionId.HasValue)
                {
                    var religionInfo = await GetReligionInfo(customerMeta.ReligionId.Value);

                    if (religionInfo == null)
                    {
                        return(new ActionResultResponse(-7,
                                                        _sharedResourceService.GetString("Religion does not exists. Please try again.")));
                    }

                    customer.ReligionId   = religionInfo.Id;
                    customer.ReligionName = religionInfo.Name;
                }
                else
                {
                    customer.ReligionId   = null;
                    customer.ReligionName = null;
                }
            }
            if (oldEthnicId != customerMeta.EthnicId)
            {
                if (customerMeta.EthnicId.HasValue)
                {
                    var ethnicInfo = await GetEthnicInfo(customerMeta.EthnicId.Value);

                    if (ethnicInfo == null)
                    {
                        return(new ActionResultResponse(-8, _sharedResourceService.GetString("Ethnic does not exists.")));
                    }

                    customer.EthnicId   = ethnicInfo.Id;
                    customer.EthnicName = ethnicInfo.Name;
                }
                else
                {
                    customer.EthnicId   = null;
                    customer.EthnicName = null;
                }
            }

            customer.ConcurrencyStamp   = Guid.NewGuid().ToString();
            customer.FullName           = customerMeta.FullName;
            customer.UnsignName         = customerMeta.FullName.StripVietnameseChars().ToUpper();
            customer.Birthday           = customerMeta.Birthday;
            customer.Gender             = customerMeta.Gender;
            customer.CustomerResourceId = customerMeta.CustomerResourceId;
            customer.IdCardNumber       = customerMeta.IdCardNumber;
            customer.JobId   = customerMeta.JobId;
            customer.Address = customerMeta.Address;

            await _customerRepository.Update(customer);

            await UpdateContacts();
            await UpdateRelativesContacts();

            return(new ActionResultResponse(1, _customerResourceService.GetString("Update Customer successful.")));

            async Task UpdateContacts()
            {
                // Delete all existing contacts.
                await _customerContactRepository.DeleteByCustomerId(customer.Id);

                // Add new contacts.
                await AddCustomerContacts(customer.Id, customerMeta.CustomerContacts);
            }

            async Task UpdateRelativesContacts()
            {
                // Delete all relatives contacts.
                await _contactCustomerRepository.DeleteByCustomerId(customer.Id);

                // Add new relatives contacts.
                await AddRelativesContacts(customer.Id, customerMeta.CustomerRelativesContacts);
            }
        }