private void Dispatch(Packages package) { if (Order.StatusCode.Value.Equals((int)OrderStatusCode.InProcess) && Order.PaymentStatus.Equals((int)OrderPaymentStatus.Charged)) { ThreadTask threadTask = new ThreadTask(string.Format("訂單下載 - 自動提交訂單【{0}】至待出貨區", Order.OrderID), Session); MyHelp.Log("Orders", Order.OrderID, string.Format("訂單下載 - 自動提交訂單【{0}】至待出貨區", Order.OrderID), Session); package.ProcessStatus = (int)EnumData.ProcessStatus.鎖定中; Packages.Update(package, package.ID); lock (Factory) { threadTask.AddWork(Factory.StartNew(Session => { threadTask.Start(); string message = ""; using (QDLogisticsEntities db = new QDLogisticsEntities()) { IRepository <Packages> Packages = new GenericRepository <Packages>(db); IRepository <PickProduct> PickProduct = new GenericRepository <PickProduct>(db); Packages packageData = Packages.Get(package.ID); try { HttpSessionStateBase session = (HttpSessionStateBase)Session; SC_WebService SCWS = new SC_WebService("*****@*****.**", "timfromweypro"); if (!SCWS.Is_login) { throw new Exception("SC is not login"); } OrderStateInfo order = SCWS.Get_OrderStatus(packageData.OrderID.Value); if ((int)order.PaymentStatus == package.Orders.PaymentStatus) { ShipProcess shipProcess = new ShipProcess(SCWS); MyHelp.Log("Orders", packageData.OrderID, "提交至待出貨區", session); /***** 上傳Item出貨倉 *****/ var SC_order = SCWS.Get_OrderData(packageData.OrderID.Value).Order; var SC_items = SC_order.Items.Where(i => i.PackageID.Equals(packageData.ID)).ToArray(); foreach (var item in SC_items) { if (!db.Skus.AsNoTracking().Any(s => s.Sku.Equals(item.ProductID))) { throw new Exception(string.Format("系統尚未有品號 {0} 資料!", item.ProductID)); } item.ShipFromWareHouseID = packageData.Items.First(i => i.IsEnable == true && i.ID == item.ID).ShipFromWarehouseID.Value; SCWS.Update_OrderItem(SC_items.First(i => i.ID.Equals(item.ID))); } MyHelp.Log("Orders", packageData.OrderID, "更新訂單包裹的出貨倉", session); /***** 更新客戶地址 *****/ var address = SC_order.ShippingAddress; DataProcess.SetAddressData(packageData.Orders.Addresses, address, SC_order.BillingAddress); /***** 檢查運送國家 *****/ if (!string.IsNullOrEmpty(packageData.Method.CountryData)) { var countryData = JsonConvert.DeserializeObject <Dictionary <string, bool> >(packageData.Method.CountryData); if (!countryData.ContainsKey(packageData.Orders.Addresses.CountryCode.ToUpper())) { throw new Exception(string.Format("訂單【{0}】國家名稱不合,請重新確認", packageData.OrderID)); } if (!countryData[packageData.Orders.Addresses.CountryCode.ToUpper()]) { throw new Exception(string.Format("訂單【{0}】不可寄送至國家{1}", packageData.OrderID, packageData.Orders.Addresses.CountryName)); } } shipProcess.Init(packageData); var result = shipProcess.Dispatch(); if (result.Status) { MyHelp.Log("Orders", packageData.OrderID, "訂單提交完成", session); if (packageData.Items.First(i => i.IsEnable.Value).ShipWarehouses.Name.Equals("TWN")) { int[] itemIDs = packageData.Items.Where(i => i.IsEnable.Value).Select(i => i.ID).ToArray(); List <PickProduct> pickList = PickProduct.GetAll(true).Where(p => itemIDs.Contains(p.ItemID.Value)).ToList(); foreach (Items item in packageData.Items.Where(i => i.IsEnable == true)) { PickProduct pick = pickList.FirstOrDefault(pk => pk.ItemID == item.ID); if (pick != null) { pick.IsEnable = true; pick.IsPicked = false; pick.IsMail = false; pick.QtyPicked = 0; DataProcess.setPickProductData(pick, item); PickProduct.Update(pick, pick.ID); } else { pick = new PickProduct() { IsEnable = true }; DataProcess.setPickProductData(pick, item); PickProduct.Create(pick); } } PickProduct.SaveChanges(); } packageData.ProcessStatus = (int)EnumData.ProcessStatus.待出貨; } else { message = result.Message; packageData.ProcessStatus = (int)EnumData.ProcessStatus.訂單管理; } } else { message = "Payment status is different"; packageData.Orders.StatusCode = (int)OrderStatusCode.OnHold; packageData.ProcessStatus = (int)EnumData.ProcessStatus.訂單管理; } Packages.Update(packageData, packageData.ID); Packages.SaveChanges(); } catch (Exception e) { message = e.InnerException != null && !string.IsNullOrEmpty(e.InnerException.Message) ? e.InnerException.Message : e.Message; packageData.ProcessStatus = (int)EnumData.ProcessStatus.訂單管理; if (!string.IsNullOrEmpty(package.WinitNo)) { Winit_API winit = new Winit_API(); winit.CancelOutboundOrder(package.WinitNo); package.WinitNo = null; } Packages.Update(packageData, packageData.ID); Packages.SaveChanges(); } } return(message); }, Session)); } } }
public string Sync_Order(int OrderID) { string Message = ""; try { MyHelp.Log("Orders", OrderID, "訂單資料同步開始", Session); if (!SCWS.Is_login) { throw new Exception("SC is not logged in!"); } OrderStateInfo orderStateInfo = SCWS.Get_OrderStatus(OrderID); if (orderStateInfo == null) { throw new Exception("Not found order!"); } orderData = Orders.Get(OrderID); if (orderData == null) { Addresses address = new Addresses() { IsEnable = true }; Addresses.Create(address); Addresses.SaveChanges(); orderData = new Orders() { OrderID = orderStateInfo.ID, ShippingAddress = address.Id }; Orders.Create(orderData); Orders.SaveChanges(); } if (orderStateInfo.DropShipStatus == DropShipStatusType2.None) { OrderData order = SCWS.Get_OrderData(OrderID); orderData.eBayUserID = order.User.eBayUserID; Order orderDetail = order.Order; DataProcess.SetOrderData(orderData, orderDetail); DataProcess.SetAddressData(orderData.Addresses, orderDetail.ShippingAddress, orderDetail.BillingAddress); Check_Payment(orderData.Payments.Where(p => p.IsEnable.Equals(true)).ToList(), orderDetail.Payments.ToList()); int[] PackageIDs = orderDetail.Items.Select(i => i.PackageID).ToArray(); Check_Package(orderData.Packages.Where(p => p.IsEnable.Equals(true)).ToList(), orderDetail.Packages.Where(p => PackageIDs.Contains(p.ID)).ToList()); Check_Item(orderData.Items.Where(i => i.IsEnable.Equals(true)).ToList(), orderDetail.Items.ToList()); if (orderData.Packages.All(p => p.IsEnable.Value && !p.ProcessStatus.Equals((byte)EnumData.ProcessStatus.待出貨) && !p.ProcessStatus.Equals((byte)EnumData.ProcessStatus.保留中))) { if (orderData.Packages.SelectMany(p => p.Items.Select(i => i.ShipWarehouses)).All(w => !w.WarehouseType.Equals(WarehouseTypeType.DropShip))) { OrderSerialNumber[] SC_SerialNumbers = SCWS.Get_OrderItem_Serial(OrderID); List <SerialNumbers> serialNumberDatas = orderData.Packages.Where(p => p.IsEnable.Value).SelectMany(p => p.Items.Where(i => i.IsEnable.Value).SelectMany(i => i.SerialNumbers)).ToList(); Check_Serial(serialNumberDatas, SC_SerialNumbers.ToList()); } } } else { orderStateInfo.StatusCode = OrderStatusCode.Completed; Update_OrderState(orderData, orderStateInfo); } Orders.Update(orderData); Orders.SaveChanges(); MyHelp.Log("Orders", OrderID, "訂單資料同步完成", Session); } catch (DbEntityValidationException ex) { var errorMessages = ex.EntityValidationErrors.SelectMany(x => x.ValidationErrors).Select(x => x.ErrorMessage); Message = string.Join("; ", errorMessages); } catch (Exception e) { MyHelp.ErrorLog(e, string.Format("訂單【{0}】資料同步失敗", orderData.OrderID), orderData.OrderID.ToString()); Message = e.InnerException != null && !string.IsNullOrEmpty(e.InnerException.Message) ? e.InnerException.Message : e.Message; } return(Message); }