Пример #1
0
        /// <summary>
        /// 方正订单提交异常补救
        /// </summary>
        public static void FangZheng_Order_ForJobs()
        {
            try
            {
                var            SubmitDate      = DateTime.Now.AddHours(-1);
                var            FangZhengAppIds = CustomConfig.FZAppIdList;
                ContextSession contextSession  = ContextFactory.CurrentThreadContext;
                //对提交失败的订单重新提交
                FangZhengOrder.ObjectSet().Where(w => w.SubTime < SubmitDate)
                .Where(w => w.OrderStatus == 400 || w.OrderStatus == 900 || w.OrderStatus == 0)
                .Select(s => s.OrderId).Distinct().ToList()
                .ForEach(Item =>
                {
                    if (CommodityOrder.ObjectSet().Where(w => w.Id == Item && w.PaymentTime.HasValue && w.State == 1).Count() > 0)
                    {
                        FangZheng_Order_Confirm(Item);
                    }
                });
                //获得方正订单产品的 SN码
                #region MyRegion
                //var StartTime = DateTime.Parse("2018-09-18");
                //CommodityOrder.ObjectSet().Where(w => w.SubTime > StartTime && w.ExpOrderNo == null &&
                //            w.PaymentTime.HasValue && FangZhengAppIds.Contains(w.AppId))
                //.ToList().ForEach(Item =>
                //{
                //    var ExpName = string.Empty;
                //    var ExoCode = string.Empty;
                //    var SnDir = new Dictionary<string, string>();

                //    var Result = FangZheng_Logistics_Status(Item.Id);
                //    if (Result["code"].ToString() == "200")
                //    {
                //        var ExpInfo = Result["data"]["logistics"].Children();
                //        ExpInfo.ToList().ForEach(model =>
                //        {
                //            if (!string.IsNullOrWhiteSpace(model["logisticsCompany"].ToString()))
                //                ExpName = model["logisticsCompany"].ToString();
                //            if (!string.IsNullOrWhiteSpace(model["logisticsOrderId"].ToString()))
                //                ExoCode = model["logisticsOrderId"].ToString();
                //        });
                //        ExpInfo["snInfo"].ToList().ForEach(model =>
                //        {
                //            var code = model.First["productCode"].ToString();
                //            var sn = string.Join(",", model.First["sn"]);
                //            SnDir.Add(code, sn);
                //        });
                //        Item.ShipExpCo = ExpName;
                //        Item.ExpOrderNo = ExoCode;
                //        OrderItem.ObjectSet().Where(w => w.CommodityOrderId == Item.Id)
                //        .ToList().ForEach(ItemChild =>
                //        {
                //            if (string.IsNullOrWhiteSpace(ItemChild.SNCode))
                //            {
                //                var SnStr = SnDir[ItemChild.JDCode];
                //                ItemChild.SNCode = SnStr;
                //            }
                //        });
                //        contextSession.SaveChange();
                //    }
                //});
                #endregion
                var StartTime  = DateTime.Parse("2018-09-18");
                var orderItems = (from a in OrderItem.ObjectSet()
                                  join b in CommodityOrder.ObjectSet() on a.CommodityOrderId equals b.Id
                                  where a.SubTime > StartTime && b.SubTime > StartTime &&
                                  (a.SNCode == null || b.ExpOrderNo == null) &&
                                  b.PaymentTime.HasValue && FangZhengAppIds.Contains(b.AppId)
                                  select new { orderItem = a, commodityOrder = b }).ToList();
                var orderIds = orderItems.Select(p => p.orderItem.CommodityOrderId).Distinct().ToList();
                orderIds.ForEach(p =>
                {
                    try
                    {
                        var IsDelivery = false;
                        var snDir      = new Dictionary <string, string>();
                        var Result     = FangZheng_Logistics_Status(p);
                        if (Result["code"].ToString() == "200")
                        {
                            Result["data"]["logistics"].Children()["snInfo"]
                            .ToList().ForEach(model =>
                            {
                                var code = model.First["productCode"].ToString();
                                var sn   = string.Join(",", model.First["sn"]);
                                snDir.Add(code, sn);
                            });
                            orderItems.Where(x => x.orderItem.CommodityOrderId == p)
                            .ToList().ForEach(x =>
                            {
                                if (snDir.ContainsKey(x.orderItem.JDCode))
                                {
                                    if (string.IsNullOrWhiteSpace(x.orderItem.SNCode))
                                    {
                                        x.orderItem.SNCode = snDir[x.orderItem.JDCode];
                                    }
                                    if (string.IsNullOrWhiteSpace(x.commodityOrder.ExpOrderNo))
                                    {
                                        Result["data"]["logistics"].Children()
                                        .ToList().ForEach(Item =>
                                        {
                                            if (!string.IsNullOrWhiteSpace(Item["logisticsCompany"].ToString()))
                                            {
                                                x.commodityOrder.ShipExpCo = Item["logisticsCompany"].ToString();
                                            }
                                            if (!string.IsNullOrWhiteSpace(Item["logisticsOrderId"].ToString()))
                                            {
                                                x.commodityOrder.ExpOrderNo = Item["logisticsOrderId"].ToString();
                                            }
                                        });
                                        IsDelivery = true;
                                    }
                                }
                            });
                            if (contextSession.SaveChange() >= 0)
                            {
                                //更改状态为已经发货
                                if (IsDelivery)
                                {
                                    var commodityorderfacade = new CommodityOrderFacade()
                                    {
                                        ContextDTO = AuthorizeHelper.CoinInitAuthorizeInfo()
                                    };
                                    var result = commodityorderfacade.UpdateCommodityOrder(
                                        new Jinher.AMP.BTP.Deploy.CommodityOrderDTO()
                                    {
                                        Id            = p,
                                        State         = 2,
                                        ShipmentsTime = DateTime.Now
                                    });
                                    if (result.ResultCode != 0)
                                    {
                                        LogHelper.Error("方正订单状态更改失败:OrderId" + p);
                                    }
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        LogHelper.Error("方正电商接口请求异常:" + ex);
                    }
                });
            }
            catch (Exception ex)
            {
                LogHelper.Error("方正电商Job-Exception:" + ex);
            }
        }
Пример #2
0
        /// <summary>
        /// 第三方电商商品发货信息回调
        /// </summary>
        /// <param name="result"></param>
        public static ThirdResponse DeliverOrderCallback(string packageJsonStr)
        {
            LogHelper.Debug("ThirdECommerceOrderHelper.DeliverOrderCallback第三方电商商品发货信息回调,Input:" + packageJsonStr);
            if (string.IsNullOrEmpty(packageJsonStr))
            {
                return new ThirdResponse {
                           Code = 10300, Msg = "缺少参数orderPackage"
                }
            }
            ;
            try
            {
                var packageResult = JsonConvert.DeserializeObject <ThirdOrderPackage>(packageJsonStr);

                if (packageResult == null)
                {
                    return new ThirdResponse {
                               Code = 10301, Msg = "非法参数orderPackage"
                    }
                }
                ;
                Guid orderId;
                Guid.TryParse(packageResult.OrderId, out orderId);
                DateTime expCreateTime;
                DateTime.TryParse(packageResult.ExpCreateTime, out expCreateTime);
                if (orderId == Guid.Empty)
                {
                    return new ThirdResponse {
                               Code = 10302, Msg = "非法参数OrderId"
                    }
                }
                ;
                if (string.IsNullOrEmpty(packageResult.ExpressCompany))
                {
                    return new ThirdResponse {
                               Code = 10303, Msg = "非法参数ExpressCompany"
                    }
                }
                ;
                if (string.IsNullOrEmpty(packageResult.ExpressNo))
                {
                    return new ThirdResponse {
                               Code = 10304, Msg = "非法参数ExpressNo"
                    }
                }
                ;
                if (packageResult.SkuIdList == null || packageResult.SkuIdList.Count == 0)
                {
                    return new ThirdResponse {
                               Code = 10305, Msg = "非法参数SkuIdList"
                    }
                }
                ;
                if (expCreateTime == DateTime.MinValue)
                {
                    return new ThirdResponse {
                               Code = 10306, Msg = "非法参数ExpCreateTime"
                    }
                }
                ;
                #region 判断是否第三方电商订单及获取订单信息
                var order = ThirdECOrder.ObjectSet().FirstOrDefault(o => o.OrderId == orderId);
                if (order == null)
                {
                    return new ThirdResponse {
                               Code = 10307, Msg = "未找到此订单"
                    }
                }
                ;
                var orderItemList = OrderItem.ObjectSet()
                                    .Where(p => p.CommodityOrderId == orderId)
                                    .Select(p => new
                {
                    SkuId       = p.JDCode,
                    OrderItemId = p.Id,
                    p.CommodityId,
                    CommodityStockId = p.CommodityStockId ?? Guid.Empty,
                }).ToList();
                if (orderItemList.Count == 0)
                {
                    return new ThirdResponse {
                               Code = 10308, Msg = "未找到订单sku"
                    }
                }
                ;
                #endregion
                #region ThirdECOrderPackage和ThirdECOrderPackageSku
                var package = ThirdECOrderPackage.ObjectSet().FirstOrDefault(p => p.OrderId == orderId &&
                                                                             p.ExpressCompany == packageResult.ExpressCompany && p.ExpressNo == packageResult.ExpressNo);
                if (package == null)
                {
                    package = new ThirdECOrderPackage
                    {
                        Id             = Guid.NewGuid(),
                        OrderId        = order.OrderId,
                        OrderCode      = order.OrderCode,
                        ExpressCompany = packageResult.ExpressCompany,
                        ExpressNo      = packageResult.ExpressNo,
                        ExpCreateTime  = DateTime.Parse(packageResult.ExpCreateTime),
                        EntityState    = EntityState.Added
                    };

                    ContextFactory.CurrentThreadContext.SaveObject(package);
                    packageResult.SkuIdList.ForEach(skuId =>
                    {
                        var orderItem = orderItemList.FirstOrDefault(p => p.SkuId == skuId);
                        if (orderItem == null)
                        {
                            return;
                        }
                        var packageSku = new ThirdECOrderPackageSku
                        {
                            Id               = Guid.NewGuid(),
                            OrderId          = order.OrderId,
                            OrderCode        = order.OrderCode,
                            OrderItemId      = orderItem.OrderItemId,
                            CommodityId      = orderItem.CommodityId,
                            CommodityStockId = orderItem.CommodityStockId,
                            PackageId        = package.Id,
                            SkuId            = skuId,
                            ExpressCompany   = packageResult.ExpressCompany,
                            ExpressNo        = packageResult.ExpressNo,
                            EntityState      = EntityState.Added
                        };
                        ContextFactory.CurrentThreadContext.SaveObject(packageSku);
                    });
                }
                #endregion
                #region ThirdECOrderJournal
                var packageJournal = new ThirdECOrderJournal
                {
                    Id          = Guid.NewGuid(),
                    OrderId     = order.OrderId,
                    OrderCode   = order.OrderCode,
                    Name        = "订单商品发货",
                    Details     = packageResult.ExpressCompany + "|" + packageResult.ExpressNo,
                    Json        = packageJsonStr,
                    EntityState = EntityState.Added
                };
                ContextFactory.CurrentThreadContext.SaveObject(packageJournal);
                #endregion
                int count = ContextFactory.CurrentThreadContext.SaveChanges();
                if (count > 0)
                {
                    #region 订单所有商品发货后更新订单状态到已发货
                    if (ThirdECOrderPackageSku.ObjectSet().Count(p => p.OrderId == orderId) >= orderItemList.Select(x => x.SkuId).Distinct().Count())
                    {
                        var commodityOrder = new CommodityOrderDTO();
                        commodityOrder.Id            = orderId;
                        commodityOrder.State         = 2;
                        commodityOrder.ShipmentsTime = DateTime.Now;
                        var facade = new CommodityOrderFacade();
                        facade.ContextDTO = AuthorizeHelper.CoinInitAuthorizeInfo();
                        facade.UpdateCommodityOrder(commodityOrder);
                    }
                    #endregion
                }
                else
                {
                    LogHelper.Error(string.Format("ThirdECommerceOrderHelper.DeliverOrderCallback第三方电商商品发货信息回调失败,入参:{0}", packageJsonStr));
                    return(new ThirdResponse {
                        Code = 10309, Msg = "内部异常"
                    });
                }
            }
            catch (JsonReaderException ex)
            {
                LogHelper.Error("ThirdECommerceOrderHelper.DeliverOrderCallback第三方电商商品发货信息回调反序列化异常,Input:" + packageJsonStr, ex);
                return(new ThirdResponse <ThirdOrderCancelResult> {
                    Msg = "反序列化异常"
                });
            }
            catch (Exception ex)
            {
                LogHelper.Error(string.Format("ThirdECommerceOrderHelper.DeliverOrderCallback第三方电商商品发货信息回调异常,入参:{0}", packageJsonStr), ex);
                return(new ThirdResponse {
                    Code = 10310, Msg = "内部异常"
                });
            }
            return(new ThirdResponse {
                Code = 200, Msg = "ok"
            });
        }