private void UpdatePurchaseOrder(Packages package, int reTry = 0) { try { TaskFactory factory = System.Web.HttpContext.Current.Application.Get("TaskFactory") as TaskFactory; ThreadTask threadTask = new ThreadTask(string.Format("直發商待出貨區 - 更新訂單【{0}】以及PO【{1}】資料至SC", package.OrderID, package.POId)); lock (factory) { threadTask.AddWork(factory.StartNew(session => { threadTask.Start(); string error = ""; try { HttpSessionStateBase Session = (HttpSessionStateBase)session; SyncProcess Sync = new SyncProcess(Session); error = Sync.Update_PurchaseOrder(package.ID); } catch (Exception e) { error = e.InnerException != null && !string.IsNullOrEmpty(e.InnerException.Message) ? e.InnerException.Message : e.Message; } return(error); }, HttpContext.Session)); } } catch (Exception e) { throw e; } }
public ActionResult CheckWarehouse() { try { TaskFactory factory = System.Web.HttpContext.Current.Application.Get("TaskFactory") as TaskFactory; ThreadTask threadTask = new ThreadTask("出貨倉資料同步工作"); MyHelp.Log("Warehouses", null, "出貨倉資料同步"); lock (factory) { threadTask.AddWork(factory.StartNew(Session => { threadTask.Start(); string message = ""; HttpSessionStateBase session = (HttpSessionStateBase)Session; try { SyncProcess Sync = new SyncProcess(session); message = Sync.Sync_Warehouse(); } catch (Exception e) { message = e.InnerException != null && !string.IsNullOrEmpty(e.InnerException.Message) ? e.InnerException.Message : e.Message; } return(message); }, HttpContext.Session)); } } finally { } return(Content(JsonConvert.SerializeObject(new { status = true, message = "Sync starting!" }), "appllication/json")); }
public ActionResult CheckPurchaseItem() { SyncResult result = new SyncResult(); try { IRepository <Packages> Packages = new GenericRepository <Packages>(db); IRepository <Items> Items = new GenericRepository <Items>(db); string[] productIDs = Packages.GetAll(true).Where(p => p.ProcessStatus == (int)EnumData.ProcessStatus.待出貨).Join(Items.GetAll(true), p => p.ID, i => i.PackageID, (p, i) => i.ProductID).Distinct().ToArray(); if (productIDs.Length == 0) { return(Content(JsonConvert.SerializeObject(result.set_error("沒有需要同步的產品!")), "appllication/json")); } TaskFactory factory = System.Web.HttpContext.Current.Application.Get("TaskFactory") as TaskFactory; ThreadTask threadTask = new ThreadTask("產品序號同步工作"); lock (factory) { threadTask.AddWork(factory.StartNew(Session => { threadTask.Start(); string message = ""; HttpSessionStateBase session = (HttpSessionStateBase)Session; try { SyncProcess Sync = new SyncProcess(session); message = Sync.Sync_PurchaseItem(productIDs); } catch (DbEntityValidationException ex) { var errorMessages = ex.EntityValidationErrors.SelectMany(x => x.ValidationErrors).Select(x => x.ErrorMessage); message = string.Join("; ", errorMessages); } catch (Exception e) { message = e.Message; } return(message); }, HttpContext.Session)); } result.taskID = threadTask.ID; } catch (Exception e) { return(Content(JsonConvert.SerializeObject(result.set_error(e.Message)), "appllication/json")); } return(Content(JsonConvert.SerializeObject(result), "appllication/json")); }
public ActionResult GetOrder(List <string> orderIDs) { SyncResult result = new SyncResult(); if (!orderIDs.Any()) { return(Content(JsonConvert.SerializeObject(result.set_error("沒有取得訂單號碼!")), "appllication/json")); } try { TaskFactory factory = System.Web.HttpContext.Current.Application.Get("TaskFactory") as TaskFactory; foreach (string orderID in orderIDs) { ThreadTask threadTask = new ThreadTask(string.Format("訂單管理區 - 訂單【{0}】資料同步", orderID)); lock (factory) { threadTask.AddWork(factory.StartNew(Session => { threadTask.Start(); string message = ""; HttpSessionStateBase session = (HttpSessionStateBase)Session; try { SyncProcess Sync = new SyncProcess(session); message = Sync.Sync_Order(int.Parse(orderID)); } catch (DbEntityValidationException ex) { var errorMessages = ex.EntityValidationErrors.SelectMany(x => x.ValidationErrors).Select(x => x.ErrorMessage); message = string.Join("; ", errorMessages); } catch (Exception e) { message = e.InnerException != null && !string.IsNullOrEmpty(e.InnerException.Message) ? e.InnerException.Message : e.Message; } return(message); }, HttpContext.Session)); } } } catch (Exception e) { return(Content(JsonConvert.SerializeObject(result.set_error(e.Message)), "appllication/json")); } return(Content(JsonConvert.SerializeObject(result), "appllication/json")); }
public ActionResult CheckSku() { try { TaskFactory factory = System.Web.HttpContext.Current.Application.Get("TaskFactory") as TaskFactory; ThreadTask threadTask = new ThreadTask("品號資料同步工作"); MyHelp.Log("Skus", null, "品號資料同步"); lock (factory) { threadTask.AddWork(factory.StartNew(() => SyncData(threadTask, "Skus"))); } } finally { } return(Content(JsonConvert.SerializeObject(new { status = true, message = "Sync starting!" }), "appllication/json")); }
public ActionResult CheckOrder(int day) { SyncResult result = new SyncResult(); try { TaskFactory factory = System.Web.HttpContext.Current.Application.Get("TaskFactory") as TaskFactory; ThreadTask threadTask = new ThreadTask(string.Format("同步{0}天訂單資料", day)); lock (factory) { threadTask.AddWork(factory.StartNew(Session => { threadTask.Start(); string message = ""; HttpSessionStateBase session = (HttpSessionStateBase)Session; try { SyncProcess Sync = new SyncProcess(session, factory); message = Sync.Sync_Orders(day); } catch (DbEntityValidationException ex) { var errorMessages = ex.EntityValidationErrors.SelectMany(x => x.ValidationErrors).Select(x => x.ErrorMessage); message = string.Join("; ", errorMessages); } catch (Exception e) { message = e.InnerException != null && !string.IsNullOrEmpty(e.InnerException.Message) ? e.InnerException.Message : e.Message; } return(message); }, Session)); } } catch (Exception e) { return(Content(JsonConvert.SerializeObject(result.set_error(e.Message)), "appllication/json")); } return(Content(JsonConvert.SerializeObject(result), "appllication/json")); }
public ActionResult Dispatch(int[] packageIDs, int?method, string tracking) { AjaxResult result = new AjaxResult(); try { if (!packageIDs.Any()) { throw new Exception("沒有給訂單!"); } int warehouseID = 0; if (!int.TryParse(Session["warehouseId"].ToString(), out warehouseID)) { throw new Exception("找不到出貨倉!"); } SC_WebService SCWS = new SC_WebService(Session["ApiUserName"].ToString(), Session["ApiPassword"].ToString()); if (!SCWS.Is_login) { throw new Exception("SC is not login"); } Packages = new GenericRepository <Packages>(db); IRepository <Box> Box = new GenericRepository <Box>(db); IRepository <DirectLineLabel> Label = new GenericRepository <DirectLineLabel>(db); TaskFactory factory = System.Web.HttpContext.Current.Application.Get("TaskFactory") as TaskFactory; ThreadTask threadTask = new ThreadTask("Dropship DL 訂單 Dispatch"); lock (factory) { threadTask.AddWork(factory.StartNew(Session => { threadTask.Start(); string message = ""; HttpSessionStateBase session = (HttpSessionStateBase)Session; try { TimeZoneConvert timeZoneConvert = new TimeZoneConvert(); List <Packages> dispatchList = new List <Packages>(); List <string> errorList = new List <string>(); List <Packages> packageList = db.Packages.AsNoTracking().Where(p => p.IsEnable.Value && packageIDs.Contains(p.ID)).ToList(); DirectLine directLine = db.DirectLine.Find(packageList[0].Method.DirectLine); string boxID = string.Format("{0}-{1}", directLine.Abbreviation, timeZoneConvert.Utc.ToString("yyyyMMdd")); int count = db.Box.Where(b => b.IsEnable && b.DirectLine.Equals(directLine.ID) && b.BoxID.Contains(boxID)).Select(b => b.MainBox).Distinct().Count() + 1; byte[] Byte = BitConverter.GetBytes(count); Byte[0] += 64; boxID = string.Format("{0}-{1}", boxID, System.Text.Encoding.ASCII.GetString(Byte.Take(1).ToArray())); foreach (Packages package in packageList) { DirectLineLabel label = package.Label; OrderData order = SCWS.Get_OrderData(package.OrderID.Value); if (CheckOrderStatus(package, order.Order)) { ThreadTask uploadPOTask = new ThreadTask(string.Format("直發商待出貨區 - 更新訂單【{0}】以及PO【{1}】資料至SC", package.OrderID, package.POId), session); lock (factory) { uploadPOTask.AddWork(factory.StartNew(() => { uploadPOTask.Start(); string error = ""; try { SyncProcess Sync = new SyncProcess(session, factory); error = Sync.Update_PurchaseOrder(package.ID, false); if (string.IsNullOrEmpty(error)) { //if (directLine.Abbreviation.Equals("ECOF")) //{ // ThreadTask SyncTask = new ThreadTask(string.Format("Direct Line 訂單【{0}】SC更新", package.OrderID)); // SyncTask.AddWork(factory.StartNew(() => // { // SyncTask.Start(); // SyncProcess sync = new SyncProcess(session); // return sync.Update_Tracking(package); // })); //} //else //{ // foreach (Items item in package.Items.Where(i => i.IsEnable.Value).ToList()) // { // if (item.SerialNumbers.Any()) SCWS.Update_ItemSerialNumber(item.ID, item.SerialNumbers.Select(s => s.SerialNumber).ToArray()); // } //} foreach (Items item in package.Items.Where(i => i.IsEnable.Value).ToList()) { if (item.SerialNumbers.Any()) { SCWS.Update_ItemSerialNumber(item.ID, item.SerialNumbers.Select(s => s.SerialNumber).ToArray()); } } } } catch (Exception e) { error = e.InnerException != null && !string.IsNullOrEmpty(e.InnerException.Message) ? e.InnerException.Message : e.Message; } return(error); })); } package.ProcessStatus = (int)EnumData.ProcessStatus.已出貨; package.BoxID = label.BoxID = boxID; label.Status = (byte)EnumData.LabelStatus.正常; dispatchList.Add(package); } else { MyHelp.Log("DirectLineLabel", label.LabelID, string.Format("標籤【{0}】狀態異常", label.LabelID), session); package.Orders.StatusCode = (int)order.Order.StatusCode; package.Orders.PaymentStatus = (int)order.Order.PaymentStatus; label.Status = (byte)EnumData.LabelStatus.鎖定中; if (order.Order.StatusCode.Equals((int)OrderStatusCode.Canceled)) { label.Status = (byte)EnumData.LabelStatus.作廢; SerialNumbers = new GenericRepository <SerialNumbers>(db); foreach (var ss in SerialNumbers.GetAll().Where(s => s.OrderID.Equals(package.OrderID))) { SerialNumbers.Delete(ss); } ; } errorList.Add(string.Format("標籤【{0}】狀態異常,請重新取出!", package.OrderID.Value)); } Packages.Update(package, package.ID); Label.Update(label, label.LabelID); } if (dispatchList.Any()) { Box box = new Box() { IsEnable = true, BoxID = boxID, MainBox = boxID, DirectLine = directLine.ID, FirstMileMethod = method ?? 0, WarehouseFrom = warehouseID, ShippingStatus = method.HasValue ? (byte)EnumData.DirectLineStatus.運輸中 : (byte)EnumData.DirectLineStatus.已到貨, BoxType = (byte)EnumData.DirectLineBoxType.DirectLine, TrackingNumber = tracking, Create_at = timeZoneConvert.Utc }; db.Entry(box).State = System.Data.Entity.EntityState.Added; db.SaveChanges(); MyHelp.Log("Box", boxID, string.Format("Box【{0}】建立成功", boxID), session); MyHelp.Log("Box", box.BoxID, string.Format("寄送 Box【{0}】DL資料", box.BoxID), session); SendMailToCarrier(box, db.DirectLine.AsNoTracking().First(d => d.ID.Equals(box.DirectLine))); MyHelp.Log("Box", box.BoxID, string.Format("Box【{0}】完成出貨", box.BoxID), session); } Packages.SaveChanges(); if (errorList.Any()) { message = string.Join("\n", errorList.ToArray()); } } catch (Exception e) { message = e.InnerException != null && !string.IsNullOrEmpty(e.InnerException.Message) ? e.InnerException.Message : e.Message; } return(message); }, HttpContext.Session)); } } catch (Exception e) { result.status = false; result.message = e.InnerException != null && !string.IsNullOrEmpty(e.InnerException.Message) ? e.InnerException.Message : e.Message; } return(Json(result, JsonRequestBehavior.AllowGet)); }
public void Receive() { try { using (LinkReceive receive = new LinkReceive(HttpContext.Request.Url.Query)) { CaseEvent eventData = db.CaseEvent.AsNoTracking().FirstOrDefault(c => c.ID.Equals(receive.CaseID)); if (eventData == null) { throw new Exception("找不到Case Event!"); } if (!eventData.Status.Equals((byte)EnumData.CaseEventStatus.Open)) { throw new Exception("執行動作無效!"); } Packages package = db.Packages.AsNoTracking().FirstOrDefault(p => p.IsEnable.Value && p.ID.Equals(eventData.PackageID)); if (package == null) { throw new Exception("找不到訂單!"); } using (CaseLog CaseLog = new CaseLog(package, Session)) { Label = new GenericRepository <DirectLineLabel>(db); TaskFactory factory = System.Web.HttpContext.Current.Application.Get("TaskFactory") as TaskFactory; switch (receive.Type) { case (byte)EnumData.CaseEventType.CancelShipment: if (!receive.Request.Equals((byte)EnumData.CaseEventRequest.Failed) && !receive.ReturnWarehouseID.HasValue) { throw new Exception("沒有選擇退貨倉!"); } CaseLog.CancelShipmentResponse(receive.Request, receive.ReturnWarehouseID); eventData = CaseLog.GetCaseEvent(EnumData.CaseEventType.CancelShipment); if (eventData.Request.Equals((byte)EnumData.CaseEventRequest.Successful) && eventData.Status.Equals((byte)EnumData.CaseEventStatus.Locked)) { ThreadTask threadTask = new ThreadTask(string.Format("Direct Line 訂單【{0}】同步資料", package.OrderID)); threadTask.AddWork(factory.StartNew(() => { threadTask.Start(); SyncProcess sync = new SyncProcess(Session); return(sync.Sync_Order(package.OrderID.Value)); })); DirectLineLabel label = db.DirectLineLabel.AsNoTracking().First(l => l.IsEnable && l.LabelID.Equals(eventData.LabelID)); label.Status = (byte)EnumData.LabelStatus.作廢; Label.Update(label, label.LabelID); Label.SaveChanges(); } break; case (byte)EnumData.CaseEventType.UpdateShipment: CaseLog.UpdateShipmentResponse(receive.Request); break; case (byte)EnumData.CaseEventType.ChangeShippingMethod: eventData = CaseLog.ChangeShippingMethodResponse(receive.Request); if (eventData.Request.Equals((byte)EnumData.CaseEventRequest.Successful) && eventData.Status.Equals((byte)EnumData.CaseEventStatus.Close)) { package = db.Packages.Find(eventData.PackageID); if (!string.IsNullOrEmpty(package.TrackingNumber)) { ThreadTask threadTask = new ThreadTask(string.Format("Direct Line 訂單【{0}】SC更新", package.OrderID)); threadTask.AddWork(factory.StartNew(() => { threadTask.Start(); SyncProcess sync = new SyncProcess(Session); return(sync.Update_Tracking(package)); })); } } break; case (byte)EnumData.CaseEventType.ResendShipment: eventData = CaseLog.ResendShipmentResponse(receive.Request); if (eventData.Request.Equals((byte)EnumData.CaseEventRequest.Successful) && eventData.Status.Equals((byte)EnumData.CaseEventStatus.Close)) { DirectLineLabel label = db.DirectLineLabel.AsNoTracking().First(l => l.IsEnable && l.LabelID.Equals(eventData.NewLabelID)); package = db.Packages.AsNoTracking().First(p => p.ID.Equals(label.PackageID)); if (!string.IsNullOrEmpty(package.TrackingNumber)) { label.Status = (byte)EnumData.LabelStatus.完成; ThreadTask threadTask = new ThreadTask(string.Format("Direct Line 訂單【{0}】SC更新", package.OrderID)); threadTask.AddWork(factory.StartNew(() => { threadTask.Start(); SyncProcess sync = new SyncProcess(Session); return(sync.Update_Tracking(package)); })); } Label.Update(label, label.LabelID); Label.SaveChanges(); } break; case (byte)EnumData.CaseEventType.ReturnPackage: CaseLog.ReturnPackageResponse(receive.Request); break; } } } Response.Write("Success!"); } catch (Exception e) { Response.Write(e.InnerException != null && !string.IsNullOrEmpty(e.InnerException.Message) ? e.InnerException.Message : e.Message); } }
public ActionResult CheckCompany() { TaskFactory factory = System.Web.HttpContext.Current.Application.Get("TaskFactory") as TaskFactory; ThreadTask threadTask = new ThreadTask("公司資料同步工作"); lock (factory) { threadTask.AddWork(factory.StartNew(session => { threadTask.Start(); string error = ""; try { IRepository <Companies> Companies = new GenericRepository <Companies>(db); List <Companies> dbCompany = Companies.GetAll(true).ToList(); HttpSessionStateBase Session = (HttpSessionStateBase)session; SC_WebService SCWS = new SC_WebService(Session["ApiUserName"].ToString(), Session["ApiPassword"].ToString()); List <Companies> companyList = SCWS.Get_AllCompany().Select(c => new Companies() { ID = c.ID, CompanyName = c.CompanyName }).ToList(); IEnumerable <Companies> newCompany = companyList.Except(dbCompany).ToList(); if (newCompany.Any()) { foreach (Companies company in newCompany) { var ncompany = new Companies { CompanyName = company.CompanyName, ID = company.ID }; Companies.Create(ncompany); } } IEnumerable <Companies> updateCompany = companyList.Except(companyList.Except(dbCompany)).Except(dbCompany, new CompanyComparer()).ToList(); if (updateCompany.Any()) { foreach (Companies company in updateCompany) { Companies update = dbCompany.Find(c => c.ID == company.ID); update.CompanyName = company.CompanyName; Companies.Update(update, update.ID); } } Companies.SaveChanges(); } catch (Exception e) { error = e.Message; } return(error); }, HttpContext.Session)); } SyncResult result = new SyncResult(threadTask.ID); MyHelp.Log("Companies", null, "公司資料同步"); return(Content(JsonConvert.SerializeObject(result), "appllication/json")); }