Ejemplo n.º 1
0
 public ExistingOrderInfo Get_OrderInfo(int OrderID)
 {
     return(OCS_SellerCloud.GetOrder(OCS_AuthHeader, OrderID));
 }
Ejemplo n.º 2
0
        static void OrdersSync(int day)
        {
            Console.WriteLine("--- Start to access Orderss ---");

            List <Orders>        dbOrders        = GetOrders(day);
            List <Payments>      dbPayments      = new List <Payments>();
            List <Packages>      dbPackages      = new List <Packages>();
            List <Items>         dbItems         = new List <Items>();
            List <SerialNumbers> dbSerialNumbers = new List <SerialNumbers>();
            List <Skus>          dbSkus          = new List <Skus>();

            List <Orders> OrderList = new List <Orders>();

            List <Orders>        Orders        = new List <Orders>();
            List <Addresses>     Addresses     = new List <Addresses>();
            List <Payments>      Payments      = new List <Payments>();
            List <Packages>      Packages      = new List <Packages>();
            List <Items>         Items         = new List <Items>();
            List <BundleItems>   BundleItems   = new List <BundleItems>();
            List <SerialNumbers> SerialNumbers = new List <SerialNumbers>();
            List <Skus>          Skus          = new List <Skus>();

            Console.WriteLine("--- Get Orders from " + Today.AddDays(-day).ToString() + " to " + Today.ToString() + " ---");
            var result = OCS_sellerCloud.SearchOrders(OCS_authHeader, Today.AddDays(-day), Today, "", 0);

            if (result.Rows.Count > 0)
            {
                int[] OrderIDs = result.Rows.Cast <DataRow>().Select(o => (int)o.ItemArray.First()).ToArray();
                OrderList = OS_sellerCloud.Orders_GetOrderStates(OS_authHeader, OS_options, OrderIDs).Where(o => o.DropShipStatus == OrderService.DropShipStatusType1.None)
                            .Select(o => new Orders()
                {
                    OrderID        = o.ID,
                    StatusCode     = (int)o.StatusCode,
                    PaymentStatus  = (int)o.PaymentStatus,
                    ShippingStatus = (int)o.ShippingStatus,
                    IsConfirmed    = o.IsConfirmed,
                    ConfirmBy      = o.ConfirmedBy,
                    ConfirmOn      = o.ConfirmedOn
                }).ToList();
            }

            Console.WriteLine("--- Get <" + OrderList.Count() + "> Orders from Sellercloud ---");
            Console.WriteLine("--- End to access Orders ---");

            IEnumerable <Orders> newOrders = OrderList.Except(dbOrders).Where(n => n.StatusCode != (int)OrderService.OrderStatusCode.Canceled).ToList();

            Console.WriteLine("--- There are <" + newOrders.Count() + "> new Orders ---");
            if (newOrders.Any())
            {
                OrderData[] newOrderList             = OS_sellerCloud.Orders_GetDatas(OS_authHeader, OS_options, newOrders.Select(o => o.OrderID).ToArray());
                Dictionary <int, string> eBayUserIDs = newOrderList.ToDictionary(o => o.OrderPackages.First().OrderID, o => o.User.eBayUserID);
                //Dictionary<int, OrderSerialNumber[]> serialNumbers = newOrderList.ToDictionary(o => o.Order.ID, o=> o.Serials);

                foreach (Orders order in newOrders)
                {
                    OrderCreationService.Order orderDetail = OCS_sellerCloud.GetOrderFull(OCS_authHeader, order.OrderID);

                    Addresses address = new Addresses()
                    {
                        Id = DataProcess.InsertAddress(conn)
                    };
                    Addresses.Add(DataProcess.SetAddressData(address, orderDetail.ShippingAddress));

                    order.ShippingAddress = address.Id;
                    order.eBayUserID      = eBayUserIDs[order.OrderID];
                    Orders.Add(DataProcess.SetOrderData(order, orderDetail));

                    foreach (OrderCreationService.OrderPayment paymentDetail in orderDetail.Payments)
                    {
                        Payments payment = new Payments()
                        {
                            IsEnable = true, ID = paymentDetail.ID
                        };
                        Payments.Add(DataProcess.SetPaymentData(payment, paymentDetail));
                    }

                    foreach (OrderCreationService.Package packageDetail in orderDetail.Packages)
                    {
                        Packages package = new Packages()
                        {
                            IsEnable = true, ID = packageDetail.ID
                        };
                        Packages.Add(DataProcess.SetPackageData(package, packageDetail));
                    }

                    ExistingOrderInfo orderInfo = OCS_sellerCloud.GetOrder(OCS_authHeader, order.OrderID);
                    foreach (OrderCreationService.OrderItem itemDetail in orderDetail.Items)
                    {
                        Items item = orderInfo.Items
                                     .Select(i => new Items()
                        {
                            IsEnable = true, ID = i.OrderItemUniqueIDInDB, SKU = i.SKU.Trim(), DisplayName = i.ItemName, UnitPrice = i.UnitPrice
                        })
                                     .First(i => i.ID == itemDetail.ID);
                        Items.Add(DataProcess.SetItemData(item, itemDetail));
                        Skus.Add(DataProcess.SetSkuData(
                                     new Skus()
                        {
                            IsEnable = true, Sku = item.ProductID, Battery = false, ParentShadow = GetProductParent(item.ProductID)
                        },
                                     OS_sellerCloud.GetProductFullInfo(OS_authHeader, OS_options, item.ProductID)));

                        if (itemDetail.KitItemsCount > 0)
                        {
                            foreach (OrderCreationService.OrderBundleItem bundleDetail in itemDetail.BundleItems)
                            {
                                BundleItems bundle = new BundleItems()
                                {
                                    IsEnable = true, ID = bundleDetail.ID
                                };
                                BundleItems.Add(DataProcess.SetBundleData(bundle, bundleDetail));
                                Skus.Add(new Skus()
                                {
                                    IsEnable = true, Sku = bundleDetail.ProductID, ProductName = bundleDetail.ProductName, Brand = 0, Battery = false
                                });
                            }
                        }
                    }
                }
                DataProcess.BulkInsert(conn, "Orders", Orders);
            }

            IEnumerable <Orders> updateOrders = OrderList.Except(OrderList.Except(dbOrders)).Except(dbOrders, new OrdersComparer()).ToList();

            Console.WriteLine("--- There are <" + updateOrders.Count() + "> Orders need to update ---");
            if (updateOrders.Any())
            {
                foreach (Orders order in updateOrders)
                {
                    OrderCreationService.Order orderDetail = OCS_sellerCloud.GetOrderFull(OCS_authHeader, order.OrderID);

                    Addresses address = new Addresses()
                    {
                        Id = dbOrders.Where(o => o.OrderID == order.OrderID).FirstOrDefault().ShippingAddress.Value
                    };
                    Addresses.Add(DataProcess.SetAddressData(address, orderDetail.ShippingAddress));

                    dbPayments = GetPayments(dbPayments, order.OrderID);
                    foreach (OrderCreationService.OrderPayment PaymentDetail in orderDetail.Payments)
                    {
                        Payments Payment = new Payments()
                        {
                            ID = PaymentDetail.ID
                        };
                        Payments.Add(DataProcess.SetPaymentData(Payment, PaymentDetail));
                    }

                    dbPackages = GetPackages(dbPackages, order.OrderID);
                    foreach (OrderCreationService.Package packageDetail in orderDetail.Packages)
                    {
                        Packages package = new Packages()
                        {
                            ID = packageDetail.ID
                        };
                        Packages.Add(DataProcess.SetPackageData(package, packageDetail));
                    }

                    dbItems = GetItems(dbItems, order.OrderID);
                    foreach (OrderCreationService.OrderItem itemDetail in orderDetail.Items)
                    {
                        Items item = new Items()
                        {
                            ID = itemDetail.ID
                        };
                        Items.Add(DataProcess.SetItemData(item, itemDetail));
                    }

                    dbSerialNumbers = GetSerialNumbers(dbSerialNumbers, order.OrderID);
                    foreach (OrderSerialNumber serialNumberDetail in OS_sellerCloud.Serials_ListFor(OS_authHeader, OS_options, order.OrderID))
                    {
                        SerialNumbers serialNumber = new SerialNumbers();
                        SerialNumbers.Add(DataProcess.SetSerialNumberData(serialNumber, serialNumberDetail));
                    }
                }

                DataTable orderTable = ToDataTable(updateOrders.Select(o => new
                {
                    OrderID        = o.OrderID,
                    StatusCode     = o.StatusCode,
                    PaymentStatus  = o.PaymentStatus,
                    ShippingStatus = o.ShippingStatus,
                    IsConfirmed    = o.IsConfirmed,
                    ConfirmBy      = o.ConfirmBy,
                    ConfirmOn      = o.ConfirmOn
                }).ToList());

                DataProcess.BatchUpdate(conn, orderTable, "OrderData");
            }

            if (Addresses.Any())
            {
                DataProcess.BatchUpdate(conn, ToDataTable(Addresses), "AddressData");
            }

            IEnumerable <Payments> newPayments = Payments.Except(dbPayments);

            if (newPayments.Any())
            {
                Console.WriteLine("--- There are <" + newPayments.Count() + "> new Payments ---");
                DataProcess.BulkInsert(conn, "Payments", newPayments);
            }
            IEnumerable <Payments> updatePayments = Payments.Except(newPayments).Except(dbPayments, new PaymentsComparer());

            if (updatePayments.Any())
            {
                Console.WriteLine("--- There are <" + updatePayments.Count() + "> Payments need to update ---");
                DataProcess.BatchUpdate(conn, ToDataTable(updatePayments), "PaymentData");
            }

            IEnumerable <Packages> newPackages = Packages.Except(dbPackages);

            if (newPackages.Any())
            {
                Console.WriteLine("--- There are <" + newPackages.Count() + "> new Packages ---");
                DataProcess.BulkInsert(conn, "Packages", newPackages);
            }

            IEnumerable <Packages> updatePackages = Packages.Except(newPackages).Except(dbPackages, new PackagesComparer());

            if (updatePackages.Any())
            {
                Console.WriteLine("--- There are <" + updatePackages.Count() + "> Packages need to update ---");
                DataProcess.BatchUpdate(conn, ToDataTable(updatePackages), "PackageData");
            }

            IEnumerable <Items> newItems = Items.Except(dbItems);

            if (newItems.Any())
            {
                Console.WriteLine("--- There are <" + newItems.Count() + "> new Items ---");
                DataProcess.BulkInsert(conn, "Items", newItems);
            }
            IEnumerable <Items> updateItems = Items.Except(newItems).Except(dbItems, new ItemsComparer());

            if (updateItems.Any())
            {
                Console.WriteLine("--- There are <" + updateItems.Count() + "> Items need to update ---");
                DataProcess.BatchUpdate(conn, ToDataTable(updateItems), "ItemData");
            }

            if (BundleItems.Any())
            {
                DataProcess.BulkInsert(conn, "BundleItems", BundleItems);
            }

            if (Skus.Any())
            {
                IEnumerable <Skus> newSkus = Skus.Except(GetSkus());
                if (newSkus.Any())
                {
                    DataProcess.BulkInsert(conn, "Skus", newSkus);
                }
            }

            IEnumerable <SerialNumbers> newSerialNumbers = SerialNumbers.Except(dbSerialNumbers);

            if (newSerialNumbers.Any())
            {
                Console.WriteLine("--- There are <" + newSerialNumbers.Count() + "> new SerialNumbers ---");
                DataProcess.BulkInsert(conn, "SerialNumbers", newSerialNumbers);
            }
        }