/// <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); }
/// <summary> /// 添加订单信息 /// </summary> /// <param name="orderImport"></param> /// <param name="order"></param> private async Task <Shipment> AddShipment(OrderImport orderImport, Order order) { if (orderImport.LogisticsName.IsNullOrEmpty()) { return(null); } if (orderImport.TrackingNumber.IsNullOrEmpty()) { return(null); } order.ShippingStatus = ShippingStatus.Taked; var trackingNumber = orderImport.TrackingNumber.Replace("'", ""); var logistics = await _logisticsManager.FindByNameAsync(orderImport.LogisticsName); if (logistics == null) { return(null); } Shipment shipment; if (order.Shipments.Any()) { shipment = order.Shipments.FirstOrDefault(); shipment.LogisticsId = logistics.Id; //shipment.TrackingNumber = trackingNumber; shipment.CreationTime = orderImport.CreatedOnUtc; } else { shipment = new Shipment() { LogisticsId = logistics.Id, OrderId = order.Id, CreationTime = orderImport.CreatedOnUtc }; } shipment.LogisticsNumber = orderImport.TrackingNumber; if (orderImport.OrderStatus == OrderStatus.Completed) { order.OrderStatus = OrderStatus.Completed; order.ShippingStatus = ShippingStatus.Received; shipment.ReceivedOn = orderImport.DeliveriedOnUtc; } if (orderImport.OrderStatus == OrderStatus.Canceled) { order.OrderStatus = OrderStatus.Canceled; order.ShippingStatus = ShippingStatus.IssueWithReject; shipment.RejectedOn = orderImport.DeliveriedOnUtc; } if (shipment.Id == 0) { foreach (var item in order.Items) { shipment.Items.Add(new ShipmentItem() { OrderItemId = item.Id, Quantity = item.Quantity, }); } order.Shipments.Add(shipment); } return(shipment); }
public async Task <bool> ImportOrderAsync(OrderImport orderImport) { //跳过已取消订单 if (orderImport.OrderStatus == OrderStatus.Canceled && orderImport.LogisticsName.IsNullOrWhiteSpace()) { return(false); } if (!orderImport.CustomerComment.IsNullOrWhiteSpace()) { if (orderImport.CustomerComment.Trim().ToLower() == "x" || orderImport.CustomerComment.Trim() == "shua") { return(false); } } if (!orderImport.AdminComment.IsNullOrWhiteSpace()) { if (orderImport.AdminComment.Trim().ToLower() == "x" || orderImport.AdminComment.Trim() == "shua") { return(false); } } var orderItemImportList = await GetOrderItem(orderImport.ProductSku, orderImport.OrderNumber); if (!orderItemImportList.Any()) { return(false); } var order = await _orderManager.FindByOrderNumberAsync(orderImport.OrderNumber); if (order != null) { await _orderManager.OrderRepository.EnsureCollectionLoadedAsync(order, o => o.Items); await _orderManager.OrderRepository.EnsureCollectionLoadedAsync(order, o => o.Shipments); if (order.Shipments.Count == 0 || order.ShippingStatus == ShippingStatus.NotYetShipped) { await AddShipment(orderImport, order); } else { order.Shipments.FirstOrDefault().LogisticsNumber = orderImport.TrackingNumber; } order.RewardAmount = orderImport.Reward; order.CreationTime = orderImport.CreatedOnUtc; await _orderManager.UpdateAsync(order); await UnitOfWorkManager.Current.SaveChangesAsync(); return(true); } var address = await PrepareShippingAddress(orderImport); order = new Order() { OrderSource = orderImport.OrderSource, StoreId = orderImport.StoreId > 0 ? orderImport.StoreId : _storeManager.Stores.FirstOrDefault()?.Id ?? 0, OrderNumber = orderImport.OrderNumber, UserId = 0, // TODO:创建用户 SubtotalAmount = orderImport.OrderTotal, TotalAmount = orderImport.OrderTotal, DiscountAmount = orderImport.DiscountAmount, OrderStatus = orderImport.OrderStatus, PaymentStatus = PaymentStatus.Pending, ShippingName = address.FullName, ShippingProvince = address.Province, ShippingCity = address.City, ShippingDistrict = address.District, ShippingPhoneNumber = address.PhoneNumber, ShippingAddress = address.DetailAddress, ShippingStatus = ShippingStatus.NotYetShipped, CreationTime = orderImport.CreatedOnUtc, OrderType = OrderType.PayOnDelivery, AdminComment = orderImport.AdminComment, CustomerComment = orderImport.CustomerComment, RewardAmount = orderImport.Reward, Items = new Collection <OrderItem>(), Shipments = new Collection <Shipment>() }; //插入新订单 await _orderManager.CreateAsync(order); await UnitOfWorkManager.Current.SaveChangesAsync(); foreach (var orderItemImport in orderItemImportList) { var price = await _priceCalculator.GetProductPriceAsync(orderItemImport.Product, orderItemImport.Combin?.AttributesJson ?? null); var cost = await _priceCalculator.GetProductCostAsync(orderItemImport.Product, orderItemImport.Combin?.AttributesJson ?? null); //save item var orderItem = new OrderItem() { OrderItemNumber = orderImport.OrderNumber, ProductId = orderItemImport.Product.Id, Price = orderItemImport.Product.Price, UnitPrice = price, OriginalProductCost = cost, Quantity = orderItemImport.Quantity * orderImport.PackageNum, ProductName = orderItemImport.Product.Name, Weight = orderItemImport.Product.Weight, Volume = orderItemImport.Product.GetVolume(), }; if (orderItemImport.IsCombin) { var jsonAttributeList = JsonConvert.DeserializeObject <List <JsonProductAttribute> >(orderItemImport.Combin?.AttributesJson); orderItem.AttributesJson = orderItemImport.Combin?.AttributesJson ?? null; orderItem.AttributeDescription = await _productAttributeFormatter.FormatAttributesAsync(orderItemImport.Product, jsonAttributeList); } order.Items.Add(orderItem); } await _orderManager.UpdateAsync(order); await UnitOfWorkManager.Current.SaveChangesAsync(); await AddShipment(orderImport, order); await _orderManager.UpdateAsync(order); //await UnitOfWorkManager.Current.SaveChangesAsync(); return(true); }
/// <summary> /// 导入订单 /// </summary> /// <param name="orderImport"></param> /// <returns></returns> public async Task <bool> ImportOrder(OrderImport orderImport) { //跳过已取消订单 if (orderImport.OrderStatus == OrderStatus.Canceled && orderImport.LogisticsName.IsNullOrEmpty()) { return(false); } if (!orderImport.CustomerComment.IsNullOrEmpty()) { if (orderImport.CustomerComment.Trim().ToLower() == "x" || orderImport.CustomerComment.Trim() == "shua") { return(false); } } if (!orderImport.AdminComment.IsNullOrEmpty()) { if (orderImport.AdminComment.Trim().ToLower() == "x" || orderImport.AdminComment.Trim() == "shua") { return(false); } } var orderItemImportList = await GetOrderItem(orderImport.ProductSku, orderImport.OrderNumber); if (!orderItemImportList.Any()) { return(false); } var order = await FindByOrderNumberAsync(orderImport.OrderNumber); if (order != null) { if (order.Shipments.Count == 0 || order.ShippingStatus == ShippingStatus.NotYetShipped) { await AddShipment(orderImport, order); await UpdateAsync(order); } else { order.Shipments.FirstOrDefault().LogisticsNumber = orderImport.TrackingNumber; } order.RewardAmount = orderImport.Reward; order.CreationTime = orderImport.CreatedOnUtc; await UpdateAsync(order); return(true); } var address = await PrepareShippingAddress(orderImport); order = new Order() { OrderSource = orderImport.OrderSource, StoreId = orderImport.StoreId > 0 ? orderImport.StoreId : _storeManager.Stores.FirstOrDefaultAsync()?.Id ?? 0, OrderNumber = orderImport.OrderNumber, UserId = 0, // TODO:创建用户 TotalAmount = orderImport.OrderTotal, DiscountAmount = orderImport.DiscountAmount, OrderStatus = orderImport.OrderStatus, PaymentStatus = PaymentStatus.Pending, ShippingProvince = address.Province, ShippingCity = address.City, ShippingDistrict = address.District, ShippingPhoneNumber = address.PhoneNumber, ShippingAddress = address.DetailAddress, ShippingStatus = ShippingStatus.NotYetShipped, CreationTime = orderImport.CreatedOnUtc, OrderType = OrderType.PayOnDelivery, AdminComment = orderImport.AdminComment, CustomerComment = orderImport.CustomerComment, RewardAmount = orderImport.Reward, }; //插入新订单 await CreateAsync(order); foreach (var orderItemImport in orderItemImportList) { //save item var orderItem = new OrderItem() { ProductId = orderItemImport.Product.Id, Price = orderItemImport.Product.Price, UnitPrice = orderItemImport.Product.Price, OriginalProductCost = orderItemImport.Combin.OverriddenGoodCost.HasValue ? orderItemImport.Combin.OverriddenGoodCost.Value : orderItemImport.Product.GoodCost, Quantity = orderItemImport.Quantity * orderImport.PackageNum, }; if (orderItemImport.IsCombin) { var jsonAttributeList = JsonConvert.DeserializeObject <List <JsonProductAttribute> >(orderItemImport.Combin?.AttributesJson); orderItem.AttributesJson = orderItemImport.Combin?.AttributesJson ?? null; orderItem.AttributeDescription = await _productAttributeFormatter.FormatAttributesAsync(orderItemImport.Product, jsonAttributeList); } order.Items.Add(orderItem); } await AddShipment(orderImport, order); await UpdateAsync(order); return(true); }
/// <summary> /// 添加订单信息 /// </summary> /// <param name="orderImport"></param> /// <param name="order"></param> public async Task <Shipment> AddShipment(OrderImport orderImport, Order order) { if (orderImport.LogisticsName.IsNullOrWhiteSpace() && orderImport.LogisticsId == 0) { return(null); } if (orderImport.TrackingNumber.IsNullOrWhiteSpace()) { return(null); } order.ShippingStatus = ShippingStatus.Taked; // 获取物流 var logisticsId = orderImport.LogisticsId; var logisticsName = orderImport.LogisticsName; if (logisticsId == 0 || logisticsName.IsNullOrWhiteSpace()) { var logistics = await _logisticsManager.FindByNameAsync(orderImport.LogisticsName); if (logistics == null) { return(null); } logisticsId = logistics.Id; logisticsName = logistics.Name; } // 创建shipment Shipment shipment; decimal totalVolume = 0; decimal totalWeight = 0; if (order.Shipments != null && order.Shipments.Any()) { shipment = order.Shipments.FirstOrDefault(); await _shipmentManager.ShipmentRepository.EnsureCollectionLoadedAsync(shipment, s => s.Items); shipment.LogisticsId = logisticsId; //shipment.TrackingNumber = trackingNumber; shipment.CreationTime = orderImport.CreatedOnUtc; } else { shipment = new Shipment() { LogisticsName = orderImport.LogisticsName, OrderNumber = order.OrderNumber, Status = order.ShippingStatus, LogisticsId = logisticsId, OrderId = order.Id, CreationTime = orderImport.CreatedOnUtc, Items = new Collection <ShipmentItem>(), }; } shipment.LogisticsNumber = orderImport.TrackingNumber.Replace("'", ""); if (orderImport.OrderStatus == OrderStatus.Completed) { order.OrderStatus = OrderStatus.Completed; order.ShippingStatus = ShippingStatus.Received; shipment.ReceivedOn = orderImport.DeliveriedOnUtc; } if (orderImport.OrderStatus == OrderStatus.Canceled) { order.OrderStatus = OrderStatus.Canceled; order.ShippingStatus = ShippingStatus.IssueWithReject; shipment.RejectedOn = orderImport.DeliveriedOnUtc; } // 添加Item 并计算重量 if (shipment.Id == 0) { foreach (var item in order.Items.ToList()) { shipment.Items.Add(new ShipmentItem() { OrderItemId = item.Id, Quantity = item.Quantity, }); totalVolume += item.Volume; totalWeight += item.Weight; } order.Shipments.Add(shipment); } shipment.TotalVolume = totalVolume; shipment.TotalWeight = totalWeight; return(shipment); }