Exemple #1
0
 public static StateOrProvinceGetResult FromStateOrProvince(StateOrProvinceDto model)
 {
     if (model == null)
     {
         return(null);
     }
     return(new StateOrProvinceGetResult()
     {
         Id = model.Id,
         Level = (int)model.Level,
         Name = model.Name,
         ParentId = model.ParentId ?? 0
     });
 }
Exemple #2
0
        public async Task <IList <UserAddressShippingResult> > GetList(int?userAddressId = null)
        {
            var user = await _workContext.GetCurrentUserAsync();

            var countryId = (int)CountryWithId.China;
            var provinces = await _countryService.GetProvinceByCache(countryId);

            if (provinces == null || provinces.Count <= 0)
            {
                throw new Exception("省市区数据异常,请联系管理员");
            }

            var query = _userAddressRepository
                        .Query()
                        .Include(c => c.Address).ThenInclude(c => c.Country)
                        .Where(x => x.AddressType == AddressType.Shipping && x.UserId == user.Id);

            if (userAddressId.HasValue && userAddressId.Value > 0)
            {
                query = query.Where(c => c.Id == userAddressId.Value);
            }

            var list = await query.Select(x => new UserAddressShippingResult
            {
                UserAddressId     = x.Id,
                AddressId         = x.AddressId,
                ContactName       = x.Address.ContactName,
                Phone             = x.Address.Phone,
                AddressLine1      = x.Address.AddressLine1,
                ZipCode           = x.Address.ZipCode,
                CountryId         = x.Address.CountryId,
                CountryName       = x.Address.Country.Name,
                StateOrProvinceId = x.Address.StateOrProvinceId,
                UpdatedOn         = x.Address.UpdatedOn
            }).ToListAsync();

            foreach (var item in list)
            {
                item.IsDefault = item.UserAddressId == user.DefaultShippingAddressId;

                var first = provinces.FirstOrDefault(c => c.Id == item.StateOrProvinceId);
                if (first != null)
                {
                    StateOrProvinceDto pro = null, city = null, district = null;
                    if (first.Level == StateOrProvinceLevel.Default)
                    {
                        pro = first;
                    }
                    else if (first.Level == StateOrProvinceLevel.City)
                    {
                        city = first;
                        pro  = provinces.FirstOrDefault(c => c.Id == city?.ParentId);
                    }
                    else if (first.Level == StateOrProvinceLevel.District)
                    {
                        district = first;
                        city     = provinces.FirstOrDefault(c => c.Id == district?.ParentId);
                        pro      = provinces.FirstOrDefault(c => c.Id == city?.ParentId);
                    }
                    item.StateOrProvinceId   = pro?.Id ?? 0;
                    item.StateOrProvinceName = pro?.Name;
                    item.CityId       = city?.Id ?? 0;
                    item.CityName     = city?.Name;
                    item.DistrictId   = district?.Id;
                    item.DistrictName = district?.Name;
                }
            }
            var result = list.OrderByDescending(c => c.IsDefault).ThenByDescending(c => c.UpdatedOn).ToList();

            return(result);
        }
Exemple #3
0
        public async Task <OrderAddressResult> GetOrderAddress(int orderAddressId)
        {
            var user = await _workContext.GetCurrentUserAsync();

            var countryId = (int)CountryWithId.China;
            var provinces = await _countryService.GetProvinceByCache(countryId);

            if (provinces == null || provinces.Count <= 0)
            {
                throw new Exception("省市区数据异常,请联系管理员");
            }

            var query = _orderAddressRepository
                        .Query()
                        .Where(c => c.Id == orderAddressId)
                        .Include(c => c.Country)
                        .Where(x => x.AddressType == AddressType.Shipping);

            var model = await query.Select(x => new OrderAddressResult
            {
                Id                = x.Id,
                ContactName       = x.ContactName,
                Phone             = x.Phone,
                AddressLine1      = x.AddressLine1,
                ZipCode           = x.ZipCode,
                CountryId         = x.CountryId,
                CountryName       = x.Country.Name,
                StateOrProvinceId = x.StateOrProvinceId,
            }).FirstOrDefaultAsync();

            if (model != null)
            {
                var first = provinces.FirstOrDefault(c => c.Id == model.StateOrProvinceId);
                if (first != null)
                {
                    StateOrProvinceDto pro = null, city = null, district = null;
                    if (first.Level == StateOrProvinceLevel.Default)
                    {
                        pro = first;
                    }
                    else if (first.Level == StateOrProvinceLevel.City)
                    {
                        city = first;
                        pro  = provinces.FirstOrDefault(c => c.Id == city?.ParentId);
                    }
                    else if (first.Level == StateOrProvinceLevel.District)
                    {
                        district = first;
                        city     = provinces.FirstOrDefault(c => c.Id == district?.ParentId);
                        pro      = provinces.FirstOrDefault(c => c.Id == city?.ParentId);
                    }
                    model.StateOrProvinceId   = pro?.Id ?? 0;
                    model.StateOrProvinceName = pro?.Name;
                    model.CityId       = city?.Id ?? 0;
                    model.CityName     = city?.Name;
                    model.DistrictId   = district?.Id;
                    model.DistrictName = district?.Name;
                }
            }
            return(model);
        }