Exemple #1
0
        /// <summary>
        /// 地址转换
        /// </summary>
        /// <param name="orderImport"></param>
        /// <returns></returns>
        private async Task <Address> PrepareShippingAddress(OrderImport orderImport)
        {
            var address = await _addressManager.FindByPhoneNumerAsync(orderImport.Telephone);

            if (address == null)
            {
                address = new Address();
            }

            var province = _stateCache.GetProvinceByNameOrNull(orderImport.Province);

            address.ProvinceId = province?.Id ?? 0;
            address.Province   = province?.Name ?? string.Empty;

            CityCacheItem city;
            var           citys = await _stateManager.Cities.Where(c => c.ProvinceId == province.Id).ToListAsync();

            if (citys.Count() == 1)
            {
                city = _stateCache.GetCityByNameOrNull(citys.FirstOrDefault().Name);
            }
            else
            {
                city = _stateCache.GetCityByNameOrNull(orderImport.City);

                if (city == null)
                {
                    var cityEntity = new City()
                    {
                        ProvinceId = province.Id,
                        Name       = orderImport.City,
                        IsActive   = true
                    };

                    await _stateManager.CreateCityAsync(cityEntity);

                    await CurrentUnitOfWork.SaveChangesAsync();

                    city = _stateCache.GetCityByNameOrNull(orderImport.City);
                }
            }

            address.CityId = city?.Id ?? 0;
            address.City   = city?.Name ?? string.Empty;

            var destrictName = orderImport.District.Replace(" ", "");

            if (!destrictName.IsNullOrEmpty())
            {
                var destrict = _stateCache.GetDistrictByNameOrNull(destrictName);
                if (destrict == null)
                {
                    var destrictEntity = new District()
                    {
                        CityId   = city.Id,
                        Name     = destrictName,
                        IsActive = true
                    };

                    await _stateManager.CreateDistrictAsync(destrictEntity);

                    await CurrentUnitOfWork.SaveChangesAsync();

                    destrict = _stateCache.GetDistrictByNameOrNull(orderImport.City);
                }

                address.DistrictId = destrict?.Id ?? 0;
                address.District   = destrict?.Name ?? string.Empty;
            }

            address.DetailAddress = RemoveReplaceAddress(address, orderImport.Address);
            address.PhoneNumber   = orderImport.Telephone;
            address.FullName      = orderImport.ReceiverName;

            if (address.Id == 0)
            {
                await _addressManager.CreateAsync(address);
            }
            else
            {
                await _addressManager.UpdateAsync(address);
            }

            return(address);
        }