示例#1
0
 public bool ChangeOrderStatusForJobExt()
 {
     try
     {
         //更改订单状态
         var DataList = SuningSV.suning_govbus_message_get("11");
         DataList.ForEach(Item =>
         {
             var EntityList = SNOrderItem.ObjectSet()
                              .Where(w => w.CustomOrderId == Item.orderNo)
                              .ToList();
             if (EntityList != null)
             {
                 EntityList.ForEach(ItemModel =>
                 {
                     ItemModel.Status      = Convert.ToInt32(Item.status);
                     ItemModel.EntityState = EntityState.Modified;
                     SNOrderItem.ObjectSet().Attach(ItemModel);
                     //根据ID从消息池删除数据
                     if (ContextFactory.CurrentThreadContext.SaveChanges() >= 0)
                     {
                         SuningSV.suning_govbus_message_delete(Item.id);
                     }
                 });
             }
         });
         //检查确认预占失败的订单
         SNOrderItem.ObjectSet().Where(w => w.Status < 3).ToList()
         .ForEach(Item =>
         {
             SuningSV.suning_govbus_confirmorder_add(Item.OrderId, false);
         });
         return(true);
     }
     catch (Exception ex)
     {
         LogHelper.Error(string.Format("Job-SuningChangeOrderStatus:{0}", ex.Message));
         return(false);
     }
 }
示例#2
0
        public bool ChangeLogistStatusForJobExt()
        {
            ContextSession contextSession = ContextFactory.CurrentThreadContext;

            SuningSV.suning_govbus_message_get("12")
            .ForEach(Item =>
            {
                try
                {
                    var EntityQuery = SNOrderItem.ObjectSet()
                                      .Where(w => w.CustomOrderId == Item.orderNo)
                                      .Where(w => w.CustomOrderItemId == Item.orderItemNo)
                                      .FirstOrDefault();
                    if (EntityQuery != null)
                    {
                        EntityQuery.ExpressStatus = Convert.ToInt32(Item.status);
                        //根据订单获得物流信息
                        SuningSV.suning_govbus_orderlogistnew_get(new SNQueryParamsDto
                        {
                            OrderId      = EntityQuery.CustomOrderId,
                            OrderItemIds = new List <SNQueryItemParamsDto>
                            {
                                new SNQueryItemParamsDto
                                {
                                    SkuId       = EntityQuery.CustomSkuId,
                                    OrderItemId = EntityQuery.CustomOrderItemId
                                }
                            }
                        })
                        .ForEach(ItemPackage =>
                        {
                            ItemPackage.OrderItemIds.ForEach(OrderItem =>
                            {
                                var EntityModel = SNPackageTrace.ObjectSet()
                                                  .Where(w => w.OrderId == ItemPackage.OrderId)
                                                  .Where(w => w.OrderItemId == OrderItem.OrderItemId)
                                                  .Where(w => w.SkuId == OrderItem.SkuId)
                                                  .Where(w => w.PackageId == ItemPackage.PackageId)
                                                  .FirstOrDefault();
                                if (EntityModel == null)
                                {
                                    contextSession.SaveObject(new SNPackageTrace
                                    {
                                        Id = Guid.NewGuid(),
                                        CommodityOrderId = EntityQuery.OrderId,
                                        OrderId          = ItemPackage.OrderId,
                                        OrderItemId      = OrderItem.OrderItemId,
                                        SkuId            = OrderItem.SkuId,
                                        PackageId        = ItemPackage.PackageId,
                                        ReceiveTime      = string.IsNullOrWhiteSpace(ItemPackage.ReceiveTime) ? DateTime.Now : DateTime.ParseExact(ItemPackage.ReceiveTime, "yyyyMMddHHmmss", CultureInfo.CurrentCulture),
                                        ShippingTime     = string.IsNullOrWhiteSpace(ItemPackage.ShippingTime) ? DateTime.Now : DateTime.ParseExact(ItemPackage.ShippingTime, "yyyyMMddHHmmss", CultureInfo.CurrentCulture),
                                        EntityState      = EntityState.Added
                                    });
                                }
                                else
                                {
                                    EntityModel.ReceiveTime  = string.IsNullOrWhiteSpace(ItemPackage.ReceiveTime) ? DateTime.Now : DateTime.ParseExact(ItemPackage.ReceiveTime, "yyyyMMddHHmmss", CultureInfo.CurrentCulture);
                                    EntityModel.ShippingTime = string.IsNullOrWhiteSpace(ItemPackage.ShippingTime) ? DateTime.Now : DateTime.ParseExact(ItemPackage.ShippingTime, "yyyyMMddHHmmss", CultureInfo.CurrentCulture);
                                    EntityModel.EntityState  = EntityState.Modified;
                                    contextSession.SaveObject(EntityModel);
                                }
                            });
                            ItemPackage.OrderLogistics.ForEach(LogisticsItem =>
                            {
                                if (SNExpressTrace.ObjectSet().Where(w => w.OrderId == ItemPackage.OrderId && w.PackageId == ItemPackage.PackageId).FirstOrDefault() == null)
                                {
                                    contextSession.SaveObject(new SNExpressTrace
                                    {
                                        Id           = Guid.NewGuid(),
                                        OrderId      = ItemPackage.OrderId,
                                        PackageId    = ItemPackage.PackageId,
                                        OperateState = LogisticsItem.OperateState,
                                        OperateTime  = string.IsNullOrWhiteSpace(LogisticsItem.OperateTime) ? DateTime.Now : DateTime.ParseExact(LogisticsItem.OperateTime, "yyyyMMddHHmmss", CultureInfo.CurrentCulture),
                                        EntityState  = EntityState.Added
                                    });
                                }
                            });
                        });
                        //根据ID从消息池删除数据
                        if (contextSession.SaveChanges() >= 0)
                        {
                            //根据订单ID改订单状态
                            SuningSV.suning_govbus_rejection_changestatus(Item.status, EntityQuery);
                            SuningSV.suning_govbus_message_delete(Item.id);
                        }
                    }
                }
                catch (Exception ex)
                {
                    LogHelper.Error(string.Format("Job-SuningChangeLogistStatus:{0}", ex));
                }
            });
            return(true);
        }
示例#3
0
        /// <summary>
        /// 同步商品上下架
        /// </summary>
        public static void AutoUpdateSNSkuStateByMessage()
        {
            try
            {
                LogHelper.Info("SNJobHelper.AutoUpdateJdSkuStateByMessage 开始同步易派客商品上下架");
                var messages = SuningSV.suning_govbus_message_get("10");
                if (messages == null || messages.Count == 0)
                {
                    return;
                }
                var delMsg = messages = messages.Where(_ => _.status == "1" || _.status == "2" || _.status == "0" || _.status == "4").ToList();
                if (messages == null || messages.Count == 0)
                {
                    return;
                }
                LogHelper.Info("SNJobHelper.AutoUpdateJdSkuStateByMessage 开始同步易派客商品上下架,获取结果如下:" + JsonHelper.JsonSerializer(messages));
                //status 1上架 2下架 0 添加 4 删除
                // 0 1代表上架 2 4 代表下架
                var skuIds = messages.Where(_ => _.status == "1" || _.status == "2" || _.status == "0" || _.status == "4").Select(_ => _.cmmdtyCode).Where(_ => !string.IsNullOrEmpty(_)).Distinct().ToList();
                // 易派客商品Ids
                var allCommodityIds = Commodity.ObjectSet().Where(_ => !_.IsDel && YPKAppIdList.Contains(_.AppId)).Select(_ => _.Id).ToList();
                List <CommodityStock> commodityStocks = new List <CommodityStock>();
                for (int i = 0; i < allCommodityIds.Count; i += 100)
                {
                    var currentCommodityIds = allCommodityIds.Skip(i).Take(100).ToList();
                    commodityStocks.AddRange(CommodityStock.ObjectSet().Where(_ => currentCommodityIds.Contains(_.CommodityId) &&
                                                                              skuIds.Contains(_.JDCode)).ToList());
                }

                var stockCommodityIds = commodityStocks.Select(_ => _.CommodityId).Distinct();
                var stockCommodities  = Commodity.ObjectSet().Where(_ => stockCommodityIds.Contains(_.Id)).ToList();

                List <SNSkuStateDto> snSkuStates = new List <SNSkuStateDto>();
                for (int i = 0; i < skuIds.Count; i += 30)
                {
                    snSkuStates.AddRange(SuningSV.GetSkuState(skuIds.Skip(i).Take(30).ToList()));
                }

                int count = 0;

                //List<Guid> autoAuditOnShelfIds = new List<Guid>();
                //List<Guid> autoAuditOffShelfIds = new List<Guid>();
                //var autoAuditAppIds = JDAuditMode.ObjectSet().Where(_ => _.StockModeState == 0).Select(_ => _.AppId).ToList();

                ContextSession contextSession = ContextFactory.CurrentThreadContext;
                foreach (var group in commodityStocks.GroupBy(_ => _.CommodityId))
                {
                    var addCount = 0;
                    var com      = stockCommodities.Where(_ => _.Id == group.Key).FirstOrDefault();
                    var auditCom = AddCommodityAudit(com);
                    foreach (var item in group)
                    {
                        var snState = snSkuStates.Where(_ => _.skuId == item.JDCode).FirstOrDefault();
                        if (snState == null)
                        {
                            LogHelper.Info("SNJobHelper.AutoUpdateSkuState-SKU 未获取到易派客上下架状态,商品Id: " + item.Id + ",SkuId: " + item.JDCode);
                            continue;
                        }
                        // 转换JD状态
                        var state      = 0;       // 上架
                        int auditState = 2;       // 已上架
                        if (snState.state == "0") // 下架
                        {
                            state      = 1;
                            auditState = 1;
                        }
                        if (item.State != state)
                        {
                            // 对比审核表
                            var latestAuditState = JdAuditCommodityStock.ObjectSet().Where(_ => _.CommodityStockId == item.Id &&
                                                                                           _.AuditType == 3).OrderByDescending(_ => _.SubTime).Select(_ => _.JdStatus).FirstOrDefault();

                            if (latestAuditState == 0 || latestAuditState != auditState)
                            {
                                count++;
                                addCount++;
                                var auditStock = AddCommodityStockAudit(contextSession, com.AppId, item, Deploy.Enum.OperateTypeEnum.架无货商品审核);
                                auditStock.AuditType          = 3;
                                auditStock.JdAuditCommodityId = auditCom.Id;
                                auditStock.JdStatus           = auditState;
                                contextSession.SaveObject(auditStock);
                                //if (autoAuditAppIds.Contains(com.AppId))
                                //{
                                //    if (auditState == 2)
                                //    {
                                //        autoAuditOnShelfIds.Add(auditStock.Id);
                                //    }
                                //    else
                                //    {
                                //        autoAuditOffShelfIds.Add(auditStock.Id);
                                //    }
                                //}
                                LogHelper.Info("SNJobHelper.AutoUpdateSkuState-SKU 更新易派客商品上下架状态,商品Id: " + item.Id + ",SkuId: " + item.JDCode);
                            }
                        }
                    }

                    if (addCount > 0)
                    {
                        contextSession.SaveObject(auditCom);
                    }

                    if (count >= 200)
                    {
                        contextSession.SaveChanges();
                        count = 0;
                    }
                }

                // 处理无属性商品,并Stock表中无数据的情况
                count = 0;
                var oldcommodityIds = allCommodityIds.Except(stockCommodityIds).ToList();

                for (int i = 0; i < oldcommodityIds.Count; i += 100)
                {
                    var commodityIds = oldcommodityIds.Skip(i).Take(100);

                    var commodities = Commodity.ObjectSet().Where(_ => commodityIds.Contains(_.Id) &&
                                                                  (string.IsNullOrEmpty(_.ComAttribute) || _.ComAttribute == "[]") &&
                                                                  skuIds.Contains(_.JDCode)).ToList();
                    foreach (var com in commodities)
                    {
                        var snState = snSkuStates.Where(_ => _.skuId == com.JDCode).FirstOrDefault();
                        if (snState == null)
                        {
                            LogHelper.Info("SNJobHelper.AutoUpdateSkuState 未获取到易派客上下架状态,商品Id: " + com.Id + ",SkuId: " + com.JDCode);
                            continue;
                        }
                        // 转换JD状态
                        var state      = 0;       // 上架
                        int auditState = 2;       // 已上架
                        if (snState.state == "0") // 下架
                        {
                            state      = 1;
                            auditState = 1;
                        }
                        // 售价
                        if (com.State != state)
                        {
                            // 对比审核表
                            var latestAuditState = JdAuditCommodityStock.ObjectSet().Where(_ => _.CommodityStockId == com.Id &&
                                                                                           _.AuditType == 3).OrderByDescending(_ => _.SubTime).Select(_ => _.JdStatus).FirstOrDefault();
                            if (latestAuditState == 0 || latestAuditState != auditState)
                            {
                                var auditCom   = AddCommodityAudit(com);
                                var auditStock = AddCommodityStockAudit(contextSession, com, Deploy.Enum.OperateTypeEnum.架无货商品审核);
                                auditStock.AuditType          = 3;
                                auditStock.JdAuditCommodityId = auditCom.Id;
                                auditStock.JdStatus           = auditState;
                                contextSession.SaveObject(auditStock);
                                //if (auditState == 2)
                                //{
                                //    autoAuditOnShelfIds.Add(auditStock.Id);
                                //}
                                //else
                                //{
                                //    autoAuditOffShelfIds.Add(auditStock.Id);
                                //}
                                contextSession.SaveObject(auditCom);
                                count++;
                                LogHelper.Info("SNJobHelper.AutoUpdateSkuState 更新易派客商品上下架状态,商品Id: " + com.Id + ",SkuId: " + com.JDCode);
                            }
                        }

                        if (count >= 200)
                        {
                            contextSession.SaveChanges();
                            count = 0;
                        }
                    }

                    contextSession.SaveChanges();
                }

                //删除消息
                foreach (var item in delMsg)
                {
                    SuningSV.suning_govbus_message_delete(item.id);
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error("SNJobHelper.AutoUpdateSkuState 异常", ex);
                throw;
            }
            LogHelper.Info("SNJobHelper.AutoUpdateJdSkuStateByMessage 结束同步Jd商品上下架");
        }