void StatisticPayProduct(DateTime statisticStartDate, DateTime statisticEndDate)
        {
            Himall.Entity.Entities entity = new Himall.Entity.Entities();

            //时间段内已支付预约单
            var payOrders = from o in entity.OrderInfo
                            join i in entity.OrderItemInfo on o.Id equals i.OrderId
                            where o.PayDate.HasValue && o.PayDate.Value >= statisticStartDate && o.PayDate.Value < statisticEndDate
                            select new
            {
                OrderId              = o.Id,
                OrderDate            = o.OrderDate,
                ShopId               = o.ShopId,
                ProductId            = i.ProductId,
                ProductName          = i.ProductName,
                TotalAmount          = i.RealTotalPrice,   //实际上是金额
                OrderProductQuantity = i.Quantity,
                UserId               = o.UserId
            };

            var payOrderGroups = payOrders.GroupBy(e => e.ProductId);

            List <ProductVistiInfo> productVisitRange = new List <ProductVistiInfo>();
            var pids             = payOrderGroups.Select(e => e.Key).Distinct();
            var oldProductVisits = entity.ProductVistiInfo.Where(e => e.Date == statisticStartDate && pids.Contains(e.ProductId)).ToList();

            foreach (var g in payOrderGroups)
            {
                ProductVistiInfo productVisit = oldProductVisits.FirstOrDefault(e => e.ProductId == g.Key);
                if (productVisit == null)
                {
                    productVisit = new ProductVistiInfo();
                    productVisitRange.Add(productVisit);
                    //使用量、使用金额在预约单逻辑里有实时处理,如果没有记录则进行统计
                    productVisit.SaleCounts  = g.Sum(e => e.OrderProductQuantity);
                    productVisit.SaleAmounts = g.Sum(e => e.TotalAmount);
                }
                productVisit.Date          = statisticStartDate;
                productVisit.ProductId     = g.Key;
                productVisit.PayUserCounts = g.Select(e => e.UserId).Distinct().Count();
                productVisit.StatisticFlag = true;
                var item = g.FirstOrDefault();
                if (item != null)
                {
                    productVisit.ShopId = item.ShopId;
                }
            }
            //将没有付款记录的统计信息,统一修改为已统计
            var noPayOrdersStatistic = oldProductVisits.Where(e => !pids.Any(p => p == e.ProductId));

            foreach (var v in noPayOrdersStatistic)
            {
                v.StatisticFlag = true;
            }
            entity.ProductVistiInfo.AddRange(productVisitRange);
            entity.SaveChanges();
        }
        private void Save(Himall.Entity.Entities entity, StatisticOrderCommentsInfo comment)
        {
            var exists = entity.StatisticOrderCommentsInfo.Where(c => c.ShopId == comment.ShopId && c.CommentKey == comment.CommentKey).FirstOrDefault();

            if (exists == null)
            {
                var shop = entity.ShopInfo.Where(c => c.Id == comment.ShopId).FirstOrDefault();
                if (shop != null)
                {
                    comment.Himall_Shops = shop;
                    entity.StatisticOrderCommentsInfo.Add(comment);
                }
            }
            else
            {
                exists.CommentValue = comment.CommentValue;
            }
        }
        /// <summary>
        /// 取上一次统计时间
        /// </summary>
        /// <returns></returns>
        DateTime GetStatisticDate()
        {
            DateTime startDate = DateTime.Now.Date;

            try
            {
                Himall.Entity.Entities entity = new Himall.Entity.Entities();

                var firstRecord = entity.ProductVistiInfo.Where(e => e.StatisticFlag == false).FirstOrDefault();
                if (firstRecord != null)
                {
                    startDate = firstRecord.Date;
                }
                else
                {
                    //是否第一次统计
                    var visitInfo = entity.ProductVistiInfo.FirstOrDefault();
                    if (visitInfo == null)
                    {
                        var firstOrder = entity.OrderInfo.FirstOrDefault();
                        if (firstOrder != null)
                        {
                            startDate = firstOrder.OrderDate.Date;
                        }
                        else
                        {
                            startDate = DateTime.Now.Date;
                        }
                    }
                    else
                    {
                        startDate = visitInfo.Date;
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error("有问题:" + ex.Message + ":::" + ex.StackTrace);
            }
            return(startDate);
        }
        public void AutoOrderComment()
        {
            try
            {
                using (TransactionScope transaction = new TransactionScope())
                {
                    Himall.Entity.Entities entity = new Himall.Entity.Entities();

                    //获取所有的评分数据
                    var lstOrderComments = (from p in entity.OrderCommentInfo
                                            group p by p.ShopId into g
                                            select new OrderCommentsModel
                    {
                        ShopId = g.Key,
                        AvgDeliveryMark = g.Average(p => p.DeliveryMark),
                        AvgPackMark = g.Average(p => p.PackMark),
                        AvgServiceMark = g.Average(p => p.ServiceMark),
                        CategoryIds = entity.BusinessCategoryInfo.Where(c => c.ShopId == g.Key).Select(c => c.CategoryId).ToList()
                    }
                                            ).ToList();



                    foreach (var item in lstOrderComments)
                    {
                        //获取同行业的店铺
                        List <OrderCommentsModel> peerShops = new List <OrderCommentsModel>();

                        foreach (var cId in item.CategoryIds)
                        {
                            var shops = lstOrderComments.Where(c => c.CategoryIds.Contains(cId)).Select(c => c);
                            if (shops != null && shops.Count() > 0)
                            {
                                peerShops.AddRange(shops);
                            }
                        }
                        var avgPackMarkPeerShops     = peerShops.Count != 0 ? peerShops.Average(c => c.AvgPackMark) : 0d;
                        var avgDeliveryMarkPeerShops = peerShops.Count != 0 ? peerShops.Average(c => c.AvgDeliveryMark) : 0d;
                        var avgServiceMarkPeerShops  = peerShops.Count != 0 ? peerShops.Average(c => c.AvgServiceMark) : 0d;


                        var productAndDescriptionMax = peerShops.Count != 0 ? lstOrderComments.Where(c => peerShops.Where(o => o.ShopId == c.ShopId).Count() > 0).Max(c => c.AvgPackMark) : 0;
                        var productAndDescriptionMin = peerShops.Count != 0 ? lstOrderComments.Where(c => peerShops.Where(o => o.ShopId == c.ShopId).Count() > 0).Min(c => c.AvgPackMark) : 0;
                        var sellerServiceAttitudeMax = peerShops.Count != 0 ? lstOrderComments.Where(c => peerShops.Where(o => o.ShopId == c.ShopId).Count() > 0).Max(c => c.AvgServiceMark) : 0;
                        var sellerServiceAttitudeMin = peerShops.Count != 0 ? lstOrderComments.Where(c => peerShops.Where(o => o.ShopId == c.ShopId).Count() > 0).Min(c => c.AvgServiceMark) : 0;
                        var sellerDeliverySpeedMax   = peerShops.Count != 0 ? lstOrderComments.Where(c => peerShops.Where(o => o.ShopId == c.ShopId).Count() > 0).Max(c => c.AvgDeliveryMark) : 0;
                        var sellerDeliverySpeedMin   = peerShops.Count != 0 ? lstOrderComments.Where(c => peerShops.Where(o => o.ShopId == c.ShopId).Count() > 0).Min(c => c.AvgDeliveryMark) : 0;
                        //宝贝与描述相符
                        Save(entity, new StatisticOrderCommentsInfo
                        {
                            ShopId       = item.ShopId,
                            CommentKey   = StatisticOrderCommentsInfo.EnumCommentKey.ProductAndDescription,
                            CommentValue = (decimal)item.AvgPackMark
                        });

                        //宝贝与描述相符 同行比较
                        Save(entity, new StatisticOrderCommentsInfo
                        {
                            ShopId       = item.ShopId,
                            CommentKey   = StatisticOrderCommentsInfo.EnumCommentKey.ProductAndDescriptionPeer,
                            CommentValue = (decimal)avgPackMarkPeerShops
                        });

                        //宝贝与描述相符 同行业商家最高得分
                        Save(entity, new StatisticOrderCommentsInfo
                        {
                            ShopId       = item.ShopId,
                            CommentKey   = StatisticOrderCommentsInfo.EnumCommentKey.ProductAndDescriptionMax,
                            CommentValue = (decimal)productAndDescriptionMax
                        });

                        //宝贝与描述相符 同行业商家最低得分
                        Save(entity, new StatisticOrderCommentsInfo
                        {
                            ShopId       = item.ShopId,
                            CommentKey   = StatisticOrderCommentsInfo.EnumCommentKey.ProductAndDescriptionMin,
                            CommentValue = (decimal)productAndDescriptionMin
                        });

                        //卖家的服务态度
                        Save(entity, new StatisticOrderCommentsInfo
                        {
                            ShopId       = item.ShopId,
                            CommentKey   = StatisticOrderCommentsInfo.EnumCommentKey.SellerServiceAttitude,
                            CommentValue = (decimal)item.AvgServiceMark
                        });
                        //卖家的服务态度  同行业比对
                        Save(entity, new StatisticOrderCommentsInfo
                        {
                            ShopId       = item.ShopId,
                            CommentKey   = StatisticOrderCommentsInfo.EnumCommentKey.SellerServiceAttitudePeer,
                            CommentValue = (decimal)avgServiceMarkPeerShops
                        });

                        //卖家服务态度 同行业商家最高得分
                        Save(entity, new StatisticOrderCommentsInfo
                        {
                            ShopId       = item.ShopId,
                            CommentKey   = StatisticOrderCommentsInfo.EnumCommentKey.SellerServiceAttitudeMax,
                            CommentValue = (decimal)sellerServiceAttitudeMax
                        });
                        //卖家服务态度 同行业商家最低得分
                        Save(entity, new StatisticOrderCommentsInfo
                        {
                            ShopId       = item.ShopId,
                            CommentKey   = StatisticOrderCommentsInfo.EnumCommentKey.SellerServiceAttitudeMin,
                            CommentValue = (decimal)sellerServiceAttitudeMin
                        });

                        //卖家的发货速度
                        Save(entity, new StatisticOrderCommentsInfo
                        {
                            ShopId       = item.ShopId,
                            CommentKey   = StatisticOrderCommentsInfo.EnumCommentKey.SellerDeliverySpeed,
                            CommentValue = (decimal)item.AvgDeliveryMark
                        });
                        //卖家的发货速度  同行业比对
                        Save(entity, new StatisticOrderCommentsInfo
                        {
                            ShopId       = item.ShopId,
                            CommentKey   = StatisticOrderCommentsInfo.EnumCommentKey.SellerDeliverySpeedPeer,
                            CommentValue = (decimal)avgDeliveryMarkPeerShops
                        });
                        //卖家发货速度 同行业商家最高得分
                        Save(entity, new StatisticOrderCommentsInfo
                        {
                            ShopId       = item.ShopId,
                            CommentKey   = StatisticOrderCommentsInfo.EnumCommentKey.SellerDeliverySpeedMax,
                            CommentValue = (decimal)sellerDeliverySpeedMax
                        });
                        //卖家发货速度 同行业商家最低得分
                        Save(entity, new StatisticOrderCommentsInfo
                        {
                            ShopId       = item.ShopId,
                            CommentKey   = StatisticOrderCommentsInfo.EnumCommentKey.SellerDeliverySpeedMin,
                            CommentValue = (decimal)sellerDeliverySpeedMin
                        });
                    }



                    int rows = entity.SaveChanges();
                    transaction.Complete();
                    //  Log.Error("没bug:"+rows);
                }
            }
            catch (Exception ex)
            {
                string error = ex.Message;
                //    Log.Error("有bug:" + ex.Message + ":" + ex.StackTrace);
            }
        }