Example #1
0
        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);
        }