public ActionResult FreightTrial(FreightTrialFilterModel filter, bool isImport = true)
        {
            var viewModels = new FreightTrialViewModels {
                Filter = filter
            };

            try
            {
                viewModels = GetFreightList(filter);
                var listTile = new Dictionary <string, string>()
                {
                    { "ShippingMethodName", "运输方式" },
                    { "Weight", "计算重量" },
                    { "ShippingFee", "运费" },
                    { "RegistrationFee", "挂号费" },
                    { "FuelFee", "燃油费" },
                    { "SundryFee", "杂费" },
                    { "TotalFee", "总费用" },
                    { "DeliveryTime", "时效" },
                    { "Remarks", "备注" },
                };
                ExcelHelper.WriteToDownLoad("ShippingFreight.xls", "sheet1", viewModels.FreightList, listTile);
            }
            catch (Exception e)
            {
                ViewBag.CountryList   = GetCountryList("");
                ViewBag.GoodsTypeList = GetGoodsTypeList();
                ErrorNotification(e.Message);
            }

            return(View(viewModels));
        }
        public ActionResult FreightTrial(FreightTrialFilterModel filter)
        {
            ViewBag.CountryList   = GetCountryList("");
            ViewBag.GoodsTypeList = GetGoodsTypeList();
            var viewModels = new FreightTrialViewModels {
                Filter = filter
            };

            try
            {
                viewModels = GetFreightList(filter);
            }
            catch (Exception e)
            {
                ErrorNotification(e.Message);
            }

            return(View(viewModels));
        }
        public FreightTrialViewModels BindData(FreightTrialFilterModel filter)
        {
            ViewBag.CountryList   = GetCountryList("");
            ViewBag.GoodsTypeList = GetGoodsTypeList();
            var viewModels = new FreightTrialViewModels {
                Filter = filter
            };

            try
            {
                viewModels = GetFreightList(filter);
            }
            catch (Exception e)
            {
                ErrorNotification(e.Message);
            }

            viewModels.ShowCategoryListModel = ShowCategoryListModel();

            return(viewModels);
        }
        private FreightTrialViewModels GetFreightList(FreightTrialFilterModel filter)
        {
            var viewModels = new FreightTrialViewModels {
                Filter = filter
            };

            if (!filter.Weight.HasValue &&
                (!filter.Length.HasValue && !filter.Width.HasValue && !filter.Height.HasValue))
            {
                throw new Exception("重量或长宽高必填其中一项");
            }

            if (string.IsNullOrWhiteSpace(filter.CountryCode))
            {
                throw new Exception("请选择发货国家");
            }
            var customerCode = _workContext.User.UserUame;
            var customer     = _customerService.GetCustomer(customerCode);

            if (customer == null || !customer.CustomerTypeID.HasValue)
            {
                throw new Exception("客户类型不存在");
            }
            var list = _freightService.GetCustomerShippingPrices(new FreightPackageModel()
            {
                Weight         = filter.Weight ?? 0,
                Length         = filter.Length ?? 0,
                Width          = filter.Width ?? 0,
                Height         = filter.Height ?? 0,
                CountryCode    = filter.CountryCode,
                ShippingTypeId = filter.PackageType,
                CustomerTypeId = customer.CustomerTypeID.Value,
                CustomerId     = customer.CustomerID
            });

            var shippingList = _freightService.GetShippingMethodListByCustomerCode(customerCode, true);

            foreach (var item in list)
            {
                if (!item.CanShipping)
                {
                    continue;
                }
                if (item.ShippingMethodId == null)
                {
                    throw new Exception(string.Format("没有运输方式"));
                }
                var shippingMethod =
                    shippingList.First(
                        s => s.ShippingMethodId == item.ShippingMethodId.Value);
                if (shippingMethod == null)
                {
                    throw new Exception(string.Format("运输方式【{0}】不存在", item.ShippingMethodId.Value));
                }

                viewModels.FreightList.Add(new FreightModel
                {
                    ShippingMethodName = shippingList.First(s => item.ShippingMethodId != null && s.ShippingMethodId == item.ShippingMethodId.Value).ShippingMethodName,
                    Weight             = item.Weight,
                    ShippingFee        = item.ShippingFee,
                    RegistrationFee    = item.RegistrationFee,
                    RemoteAreaFee      = item.RemoteAreaFee,
                    FuelFee            = item.FuelFee,
                    OtherFee           = item.OtherFee,
                    DeliveryTime       = item.DeliveryTime,
                    Remarks            = item.Remark
                });
            }
            viewModels.FreightList = viewModels.FreightList.OrderBy(o => o.TotalFee).ToList();
            return(viewModels);
        }
        private FreightTrialViewModels GetFreightList(FreightTrialFilterModel filter)
        {
            var viewModels = new FreightTrialViewModels {
                Filter = filter
            };

            if (!filter.Weight.HasValue &&
                (!string.IsNullOrWhiteSpace(filter.Length) && !string.IsNullOrWhiteSpace(filter.Width) && !string.IsNullOrWhiteSpace(filter.Height)))
            {
                throw new Exception("重量或长宽高必填其中一项");
            }

            if (string.IsNullOrWhiteSpace(filter.CountryCode))
            {
                throw new Exception("请选择发货国家");
            }
            string customerCode   = "";
            Guid   customerId     = Guid.Empty;
            int    customerTypeId = 12;//默认12

            if (null != _workContext.User)
            {
                customerCode = _workContext.User.UserUame;
                var customer = _customerService.GetCustomer(customerCode);
                if (customer != null && customer.CustomerTypeID.HasValue)
                {
                    customerTypeId = customer.CustomerTypeID.Value;
                    customerId     = customer.CustomerID;
                }
            }
            else
            {
                customerCode = _customerService.GetCustomerCode(customerTypeId);
            }
            var list = _freightService.GetCustomerShippingPrices(new FreightPackageModel()
            {
                Weight             = filter.Weight ?? 0,
                Length             = Convert.ToInt32(string.IsNullOrEmpty(filter.Length) ? "0" : filter.Length),
                Width              = Convert.ToInt32(string.IsNullOrEmpty(filter.Width) ? "0" : filter.Width),
                Height             = Convert.ToInt32(string.IsNullOrEmpty(filter.Height) ? "0" : filter.Height),
                CountryCode        = filter.CountryCode,
                ShippingTypeId     = filter.PackageType,
                CustomerTypeId     = customerTypeId,
                CustomerId         = customerId,
                EnableTariffPrepay = false,
            });

            var shippingList = _freightService.GetShippingMethodListByCustomerCode(customerCode, true);

            foreach (var item in list)
            {
                if (!item.CanShipping)
                {
                    continue;
                }
                if (item.ShippingMethodId == null)
                {
                    throw new Exception(string.Format("没有运输方式"));
                }
                var shippingMethod =
                    shippingList.Find(
                        s => s.ShippingMethodId == item.ShippingMethodId.Value);
                if (shippingMethod == null)
                {
                    throw new Exception(string.Format("运输方式【{0}】不存在", item.ShippingMethodId.Value));
                }

                viewModels.FreightList.Add(new FreightModel
                {
                    ShippingMethodName = shippingList.First(s => item.ShippingMethodId != null && s.ShippingMethodId == item.ShippingMethodId.Value).ShippingMethodName,
                    Weight             = item.Weight,
                    ShippingFee        = item.ShippingFee,
                    RegistrationFee    = item.RegistrationFee,
                    RemoteAreaFee      = item.RemoteAreaFee,
                    FuelFee            = item.FuelFee,
                    OtherFee           = item.Value - (item.ShippingFee + item.RegistrationFee + item.RemoteAreaFee + item.FuelFee),
                    DeliveryTime       = item.DeliveryTime,
                    Remarks            = item.Remark,
                    TariffPrepayFee    = item.TariffPrepayFee,
                });
            }
            viewModels.FreightList = viewModels.FreightList.OrderBy(o => o.TotalFee).ToList();
            return(viewModels);
        }