Esempio n. 1
0
        /// <summary>
        /// 全量获取严选价格信息
        /// </summary>
        public static void AutoUpdateYXComInfo()
        {
            if (isSyncYXComInfo)
            {
                LogHelper.Info("YXJobHelper.AutoUpdateYXComInfo 正在全量获取严选价格信息,跳过。。。");
                return;
            }
            isSyncYXComInfo = true;
            LogHelper.Info("YXJobHelper.AutoUpdateYXComInfo 开始写入严选价格信息");
            try
            {
                AutoGetAllSPU();
                var YXComList     = YXComInfo.ObjectSet().ToList();
                var UpdateSpuList = YXComList.Select(s => s.SPU).ToList();
                List <YXComDetailDTO> YXComLists = new List <YXComDetailDTO>();
                for (int i = 0; i < YXComList.Count; i += 30)
                {
                    YXComLists.AddRange(YXSV.GetComDetailList(UpdateSpuList.Skip(i).Take(30).ToList()));
                    //Thread.Sleep(1000);
                }
                ContextSession contextSession = ContextFactory.CurrentThreadContext;
                List <string>  SkuIds         = new List <string>();
                foreach (var YXComlist in YXComLists)
                {
                    foreach (var item in YXComlist.skuList)
                    {
                        YXComInfo YXCom = YXComInfo.CreateYXComInfo();
                        YXCom.Id        = Guid.NewGuid();
                        YXCom.SPU       = YXComlist.id;
                        YXCom.SKU       = item.id;
                        YXCom.Price     = item.channelPrice;
                        YXCom.CostPrice = item.channelPrice * Convert.ToDecimal(0.8);
                        YXCom.SubTime   = DateTime.Now;
                        contextSession.SaveObject(YXCom);
                        SkuIds.Add(item.id);
                    }
                }
                int count1 = contextSession.SaveChanges();
                YXComInfo.ObjectSet().Context.ExecuteStoreCommand("delete from YXComInfo where SKU is null");
                #region
                //取出定时改价中严选商品
                List <Guid> YXappids     = CustomConfig.YxAppIdList;
                var         timingComIds = Jinher.AMP.BTP.TPS.YJBSV.GetYXChangeComInfo(YXappids).Data.Select(s => s.CommodityId).ToList();
                //最新价格更新到YXComInfo,对比库存表和价格审核表 判断是否需要审核(过滤掉定时改价中严选的商品)
                var         ComStockList      = CommodityStock.ObjectSet().Where(p => SkuIds.Contains(p.JDCode) && !timingComIds.Contains(p.Id)).ToList();
                var         ComIds            = ComStockList.Select(s => s.CommodityId).Distinct().ToList();
                var         ComList           = Commodity.ObjectSet().Where(p => ComIds.Contains(p.Id)).ToList();
                var         YXComInfoList     = YXComInfo.ObjectSet().Where(p => SkuIds.Contains(p.SKU)).ToList();
                var         AuditAuditModeApp = JDAuditMode.ObjectSet().Where(_ => _.PriceModeState == 0).Select(_ => _.AppId).ToList();
                List <Guid> autoAuditPriceIds = new List <Guid>();
                int         count             = 0;
                foreach (var group in ComList)
                {
                    int addCount = 0;
                    var Com      = ComList.FirstOrDefault(p => p.Id == group.Id);
                    var auditCom = AddCommodityAudit(Com);

                    var ComStocks = ComStockList.Where(p => p.CommodityId == group.Id).ToList();
                    foreach (var item in ComStocks)
                    {
                        var YXComNew = YXComInfoList.FirstOrDefault(p => p.SKU == item.JDCode);
                        if (YXComNew != null && YXComNew.Price.HasValue && item.Price != YXComNew.Price)
                        {
                            var latestAuditData = JdAuditCommodityStock.ObjectSet().Where(p => p.CommodityStockId == item.Id && p.AuditType == 2).OrderByDescending(p => p.SubTime).FirstOrDefault();
                            if (latestAuditData == null || latestAuditData.Price != item.Price)
                            {
                                count++;
                                addCount++;
                                var auditStock = AddCommodityStockAudit(contextSession, group.AppId, item, Deploy.Enum.OperateTypeEnum.京东修改现价);
                                auditStock.AuditType          = 2;
                                auditStock.JdAuditCommodityId = auditCom.Id;
                                auditStock.JdPrice            = YXComNew.Price;
                                auditStock.CostPrice          = YXComNew.Price * Convert.ToDecimal(0.8);
                                contextSession.SaveObject(auditStock);
                                if (AuditAuditModeApp.Contains(group.AppId))
                                {
                                    autoAuditPriceIds.Add(auditStock.Id);
                                }
                                LogHelper.Info("YXJobHelper.AutoUpdateYXComInfo 更新商品售价,商品Id: " + item.Id + ",SkuId: " + item.JDCode);
                            }
                        }
                    }
                    if (addCount > 0)
                    {
                        contextSession.SaveObject(auditCom);
                    }
                }
                int ccc = contextSession.SaveChanges();
                // 自动审核
                var auditComFacade = new JDAuditComFacade();
                auditComFacade.ContextDTO = AuthorizeHelper.InitAuthorizeInfo();
                if (autoAuditPriceIds.Count > 0)
                {
                    auditComFacade.AuditJDCostPrice(autoAuditPriceIds, 1, "自动审核", 0, 0);
                }
                #endregion
            }
            catch (Exception ex)
            {
                LogHelper.Error("YXJobHelper.AutoUpdateYXComInfo 异常", ex);
                isSyncYXComInfo = false;
                throw;
            }
            LogHelper.Info("YXJobHelper.AutoUpdateYXComInfo 全量获取严选商品信息");
            isSyncYXComInfo = false;
        }
Esempio n. 2
0
        /// <summary>
        /// 同步商品价格
        /// </summary>
        public static void AutoUpdatePriceByMessage()
        {
            LogHelper.Info("SNJobHelper.AutoUpdatePriceByMessage 开始同步易派客商品价格");
            try
            {
                var messages = SuningSV.GetPriceMessage();
                if (messages == null || messages.Count == 0)
                {
                    return;
                }
                LogHelper.Info("SNJobHelper.AutoUpdatePriceByMessage 开始同步SN商品价格,获取结果如下:" + JsonHelper.JsonSerializer(messages));
                var skuIds = messages.Select(_ => _.cmmdtyCode).Where(_ => !string.IsNullOrEmpty(_)).Distinct().ToList();
                //
                // 易派客商品Ids
                var allCommodityIds = Commodity.ObjectSet().Where(_ => !_.IsDel && YPKAppIdList.Contains(_.AppId)).Select(_ => _.Id);
                var commodityStocks = CommodityStock.ObjectSet().Where(_ => allCommodityIds.Contains(_.CommodityId) &&
                                                                       skuIds.Contains(_.JDCode)).ToList();
                var stockCommodityIds = commodityStocks.Select(_ => _.CommodityId).Distinct();
                var stockCommodities  = Commodity.ObjectSet().Where(_ => stockCommodityIds.Contains(_.Id)).ToList();

                List <Guid>       autoAuditPriceIds        = new List <Guid>();
                List <Guid>       autoAuditCostPriceIds    = new List <Guid>();
                var               autoAuditPriceAppIds     = JDAuditMode.ObjectSet().Where(_ => _.PriceModeState == 0).Select(_ => _.AppId).ToList();
                var               autoAuditCostPriceAppIds = JDAuditMode.ObjectSet().Where(_ => _.CostModeState == 0).Select(_ => _.AppId).ToList();
                List <SNPriceDto> SNPrices = new List <SNPriceDto>();
                for (int i = 0; i < skuIds.Count; i += 30)
                {
                    SNPrices.AddRange(SuningSV.GetPrice(skuIds.Skip(i).Take(30).ToList()));
                }
                ContextSession contextSession = ContextFactory.CurrentThreadContext;
                int            count          = 0;
                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 snprice = SNPrices.Where(_ => _.skuId == item.JDCode).FirstOrDefault();
                        if (snprice == null)
                        {
                            LogHelper.Info("SNJobHelper.AutoUpdatePriceByMessage-SKU 未获取到易派客价,商品Id: " + item.Id + ",SkuId: " + item.JDCode);
                            continue;
                        }
                        // 进货价
                        if (!string.IsNullOrEmpty(snprice.price))
                        {
                            // 对比审核表
                            var latestAuditData = JdAuditCommodityStock.ObjectSet().Where(_ => _.CommodityStockId == item.Id &&
                                                                                          _.AuditType == 1 && _.JdCostPrice.HasValue).OrderByDescending(_ => _.SubTime).Select(_ => _.JdCostPrice).FirstOrDefault();
                            if (latestAuditData == null || latestAuditData.Value != Convert.ToDecimal(snprice.price))
                            {
                                count++;
                                addCount++;
                                var auditStock = AddCommodityStockAudit(contextSession, com.AppId, item, Deploy.Enum.OperateTypeEnum.京东修改进货价);
                                auditStock.AuditType          = 1;
                                auditStock.JdAuditCommodityId = auditCom.Id;
                                auditStock.JdPrice            = string.IsNullOrEmpty(snprice.snPrice) ? Decimal.Zero : Convert.ToDecimal(snprice.snPrice);
                                auditStock.JdCostPrice        = string.IsNullOrEmpty(snprice.price) ? Decimal.Zero : Convert.ToDecimal(snprice.price);
                                contextSession.SaveObject(auditStock);
                                if (autoAuditCostPriceAppIds.Contains(com.AppId))
                                {
                                    autoAuditCostPriceIds.Add(auditStock.Id);
                                }
                                LogHelper.Info("SNJobHelper.AutoUpdatePriceByMessage 更新易派客商品进货价,商品Id: " + item.Id + ",SkuId: " + item.JDCode);
                            }
                        }

                        // 售价
                        if (item.Price != Convert.ToDecimal(snprice.snPrice))
                        {
                            // 对比审核表
                            var latestAuditData = JdAuditCommodityStock.ObjectSet().Where(_ => _.CommodityStockId == item.Id &&
                                                                                          _.AuditType == 2 && _.JdPrice.HasValue).OrderByDescending(_ => _.SubTime).Select(_ => _.JdPrice).FirstOrDefault();
                            if (latestAuditData == null || latestAuditData.Value != Convert.ToDecimal(snprice.snPrice))
                            {
                                count++;
                                addCount++;
                                var auditStock = AddCommodityStockAudit(contextSession, com.AppId, item, Deploy.Enum.OperateTypeEnum.京东修改现价);
                                auditStock.AuditType          = 2;
                                auditStock.JdAuditCommodityId = auditCom.Id;
                                auditStock.JdPrice            = Convert.ToDecimal(snprice.snPrice);
                                contextSession.SaveObject(auditStock);
                                if (autoAuditPriceAppIds.Contains(com.AppId))
                                {
                                    autoAuditPriceIds.Add(auditStock.Id);
                                }
                                LogHelper.Info("SNJobHelper.AutoUpdatePriceByMessage 更新易派客商品售价,商品Id: " + item.Id + ",SkuId: " + item.JDCode);
                            }
                        }
                    }

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

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


                // 处理无属性商品,并Stock表中无数据的情况
                count = 0;
                var commodityIds = allCommodityIds.Except(stockCommodityIds);
                var commodities  = Commodity.ObjectSet().Where(_ => commodityIds.Contains(_.Id) &&
                                                               (string.IsNullOrEmpty(_.ComAttribute) || _.ComAttribute == "[]") &&
                                                               skuIds.Contains(_.JDCode)).ToList();
                foreach (var com in commodities)
                {
                    var snprice = SNPrices.Where(_ => _.skuId == com.JDCode).FirstOrDefault();
                    if (snprice == null)
                    {
                        LogHelper.Info("SNJobHelper.AutoUpdatePriceByMessage-SKU 未获取到 易派客价,商品Id: " + com.Id + ",SkuId: " + com.JDCode);
                        continue;
                    }
                    var addCount = 0;
                    var auditCom = AddCommodityAudit(com);

                    // 进货价
                    if (!string.IsNullOrEmpty(snprice.price) && com.CostPrice != Convert.ToDecimal(snprice.price))
                    {
                        // 对比审核表
                        var latestAuditData = JdAuditCommodityStock.ObjectSet().Where(_ => _.CommodityId == com.Id &&
                                                                                      _.AuditType == 1 && _.JdCostPrice.HasValue).OrderByDescending(_ => _.SubTime).Select(_ => _.JdCostPrice).FirstOrDefault();
                        if (latestAuditData == null || latestAuditData.Value != Convert.ToDecimal(snprice.price))
                        {
                            count++;
                            addCount++;
                            var auditStock = AddCommodityStockAudit(contextSession, com, Deploy.Enum.OperateTypeEnum.京东修改进货价);
                            auditStock.AuditType          = 1;
                            auditStock.JdAuditCommodityId = auditCom.Id;
                            auditStock.JdCostPrice        = Convert.ToDecimal(snprice.price);
                            contextSession.SaveObject(auditStock);
                            if (autoAuditCostPriceAppIds.Contains(com.AppId))
                            {
                                autoAuditCostPriceIds.Add(auditStock.Id);
                            }
                            LogHelper.Info("SNJobHelper.AutoUpdatePriceByMessage 更新易派客商品进货价,商品Id: " + com.Id + ",SkuId: " + com.JDCode);
                        }
                    }

                    // 售价
                    if (com.Price != Convert.ToDecimal(snprice.snPrice))
                    {
                        // 对比审核表
                        var latestAuditData = JdAuditCommodityStock.ObjectSet().Where(_ => _.CommodityId == com.Id &&
                                                                                      _.AuditType == 2 && _.JdPrice.HasValue).OrderByDescending(_ => _.SubTime).Select(_ => _.JdPrice).FirstOrDefault();
                        if (latestAuditData == null || latestAuditData.Value != Convert.ToDecimal(snprice.snPrice))
                        {
                            count++;
                            addCount++;
                            var auditStock = AddCommodityStockAudit(contextSession, com, Deploy.Enum.OperateTypeEnum.京东修改现价);
                            auditStock.AuditType          = 2;
                            auditStock.JdAuditCommodityId = auditCom.Id;
                            auditStock.JdPrice            = Convert.ToDecimal(snprice.snPrice);
                            contextSession.SaveObject(auditStock);
                            if (autoAuditPriceAppIds.Contains(com.AppId))
                            {
                                autoAuditPriceIds.Add(auditStock.Id);
                            }
                            LogHelper.Info("SNJobHelper.AutoUpdatePriceByMessage 更新易派客商品售价,商品Id: " + com.Id + ",SkuId: " + com.JDCode);
                        }
                    }

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

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

                contextSession.SaveChanges();

                // 自动审核
                var auditComFacade = new JDAuditComFacade();
                auditComFacade.ContextDTO = AuthorizeHelper.InitAuthorizeInfo();
                if (autoAuditCostPriceIds.Count > 0)
                {
                    auditComFacade.AuditJDCostPrice(autoAuditCostPriceIds, 1, "自动审核", 0, 0);
                }
                if (autoAuditPriceIds.Count > 0)
                {
                    auditComFacade.AuditJDPrice(autoAuditPriceIds, 1, 0, "自动审核", 0);
                }

                // 删除消息
                //JDSV.DelMessage(messages.Select(_ => _.Id).ToList());
            }
            catch (Exception ex)
            {
                LogHelper.Error("SNJobHelper.AutoUpdatePrice 异常", ex);
                throw;
            }
            LogHelper.Info("SNJobHelper.AutoUpdatePriceByMessage 结束同步易派客商品价格");
        }