Exemplo n.º 1
0
        /// <summary>
        /// 按价格随机筛选一个元素
        /// </summary>
        /// <param name="count">数量</param>
        /// <param name="type">广告类型</param>
        /// <param name="cid">分类id</param>
        /// <returns></returns>
        public List <Advertisement> GetsByWeightedPrice(int count, AdvertiseType type, IPLocation ipinfo, int?cid = null)
        {
            var(location, _, _) = ipinfo;
            return(CacheManager.GetOrAdd($"Advertisement:{location.Crc32()}:{type}:{count}-{cid}", _ =>
            {
                var atype = type.ToString("D");
                Expression <Func <Advertisement, bool> > where = a => a.Types.Contains(atype) && a.Status == Status.Available;
                var catCount = CategoryRepository.Count(_ => true);
                where = where.And(a => a.RegionMode == RegionLimitMode.All || (a.RegionMode == RegionLimitMode.AllowRegion ? Regex.IsMatch(location, a.Regions) : !Regex.IsMatch(location, a.Regions)));
                if (cid.HasValue)
                {
                    var scid = cid.ToString();
                    if (Any(a => a.CategoryIds.Contains(scid)))
                    {
                        where = where.And(a => a.CategoryIds.Contains(scid) || string.IsNullOrEmpty(a.CategoryIds));
                    }
                }

                var list = GetQuery(where).OrderBy(a => - Math.Log(DataContext.Random()) / ((double)a.Price / a.Types.Length * catCount / (string.IsNullOrEmpty(a.CategoryIds) ? catCount : (a.CategoryIds.Length + 1)))).Take(count).ToList();
                var ids = list.Select(a => a.Id).ToArray();
                GetQuery(a => ids.Contains(a.Id)).UpdateFromQuery(a => new Advertisement()
                {
                    DisplayCount = a.DisplayCount + 1
                });

                return list;
            }));
        }
Exemplo n.º 2
0
        /// <summary>
        /// 按价格随机筛选一个元素
        /// </summary>
        /// <param name="count">数量</param>
        /// <param name="type">广告类型</param>
        /// <param name="cid">分类id</param>
        /// <returns></returns>
        public List <Advertisement> GetsByWeightedPrice(int count, AdvertiseType type, int?cid = null)
        {
            Expression <Func <Advertisement, bool> > where = a => a.Types.Contains(type.ToString("D")) && a.Status == Status.Available;
            if (cid.HasValue)
            {
                var scid = cid.ToString();
                if (ValueCacheManager.GetOrAdd(scid, s => Any(a => a.CategoryIds.Contains(scid))))
                {
                    where = where.And(a => a.CategoryIds.Contains(scid) || string.IsNullOrEmpty(a.CategoryIds));
                }
                else
                {
                    where = where.And(a => string.IsNullOrEmpty(a.CategoryIds));
                }
            }
            return(CacheManager.GetOrAdd($"{count}{type}{cid}", _ =>
            {
                var list = GetQuery(@where).AsEnumerable().Select(a => new WeightedItem <Advertisement>(a, (int)a.Price)).WeightedItems(count);
                foreach (var item in list)
                {
                    item.DisplayCount += 1;
                }

                SaveChanges();
                return list;
            }));
        }
Exemplo n.º 3
0
        /// <summary>
        /// 按权重随机筛选一个元素
        /// </summary>
        /// <param name="count">数量</param>
        /// <param name="type">广告类型</param>
        /// <param name="cid">分类id</param>
        /// <returns></returns>
        public List <Advertisement> GetsByWeightedRandom(int count, AdvertiseType type, int?cid = null)
        {
            Expression <Func <Advertisement, bool> > where = a => a.Types.Contains(type.ToString("D")) && a.Status == Status.Available;
            if (cid.HasValue)
            {
                where = where.And(a => a.CategoryId == cid || a.CategoryId == null);
            }

            return(GetRandomWeightList(GetQueryFromCache(where).ToList(), count));
        }
        /// <summary>
        /// 按价格随机筛选一个元素
        /// </summary>
        /// <param name="count">数量</param>
        /// <param name="type">广告类型</param>
        /// <param name="cid">分类id</param>
        /// <returns></returns>
        public List <Advertisement> GetsByWeightedPrice(int count, AdvertiseType type, int?cid = null)
        {
            Expression <Func <Advertisement, bool> > where = a => a.Types.Contains(type.ToString("D")) && a.Status == Status.Available;
            if (cid.HasValue)
            {
                where = where.And(a => a.CategoryId == cid || a.CategoryId == null);
            }

            return(CacheManager.GetOrAdd($"{count}{type}{cid}", _ => GetQuery(where).AsEnumerable().Select(a => new WeightedItem <Advertisement>(a, (int)a.Price)).WeightedItems(count)));
        }
        /// <summary>
        /// 按权重随机筛选一个元素
        /// </summary>
        /// <param name="count">数量</param>
        /// <param name="type">广告类型</param>
        /// <param name="cid">分类id</param>
        /// <returns></returns>
        public List <Advertisement> GetsByWeightedRandom(int count, AdvertiseType type, int?cid = null)
        {
            Expression <Func <Advertisement, bool> > where = a => a.Types.Contains(type.ToString("D")) && a.Status == Status.Available;
            if (cid.HasValue)
            {
                var scid = cid.ToString();
                if (ValueCacheManager.GetOrAdd(scid, s => Any(a => a.CategoryIds.Contains(scid))))
                {
                    where = where.And(a => a.CategoryIds.Contains(scid) || string.IsNullOrEmpty(a.CategoryIds));
                }
                else
                {
                    where = where.And(a => string.IsNullOrEmpty(a.CategoryIds));
                }
            }

            return(CacheManager.GetOrAdd($"{count}{type}{cid}", _ => GetQuery(where).AsEnumerable().Select(a => new WeightedItem <Advertisement>(a, a.Weight)).WeightedItems(count)));
        }
        /// <summary>
        /// 按价格随机筛选一个元素
        /// </summary>
        /// <param name="count">数量</param>
        /// <param name="type">广告类型</param>
        /// <param name="ipinfo"></param>
        /// <param name="cid">分类id</param>
        /// <param name="keywords"></param>
        /// <returns></returns>
        public List <Advertisement> GetsByWeightedPrice(int count, AdvertiseType type, IPLocation ipinfo, int?cid = null, string keywords = "")
        {
            var(location, _, _) = ipinfo;
            return(CacheManager.GetOrAdd($"Advertisement:{location.Crc32()}:{type}:{count}-{cid}-{keywords}", _ =>
            {
                var atype = type.ToString("D");
                Expression <Func <Advertisement, bool> > where = a => a.Types.Contains(atype) && a.Status == Status.Available;
                var catCount = CategoryRepository.Count(_ => true);
                where = where.And(a => a.RegionMode == RegionLimitMode.All || (a.RegionMode == RegionLimitMode.AllowRegion ? Regex.IsMatch(location, a.Regions) : !Regex.IsMatch(location, a.Regions)));
                if (cid.HasValue)
                {
                    var pids = CategoryRepository.GetQuery(c => c.Id == cid).Select(c => c.ParentId + "|" + c.Parent.ParentId).Cacheable().ToArray();
                    var scid = pids.Select(s => s.Trim('|')).Where(s => !string.IsNullOrEmpty(s)).Append(cid + "").Join("|");
                    if (Any(a => Regex.IsMatch(a.CategoryIds, scid)))
                    {
                        where = where.And(a => Regex.IsMatch(a.CategoryIds, scid) || string.IsNullOrEmpty(a.CategoryIds));
                    }
                }

                if (!keywords.IsNullOrEmpty())
                {
                    var regex = _luceneIndexSearcher.CutKeywords(keywords).Select(Regex.Escape).Join("|");
                    where = where.And(a => Regex.IsMatch(a.Title + a.Description, regex));
                }

                var list = GetQuery(where).OrderBy(a => - Math.Log(DataContext.Random()) / ((double)a.Price / a.Types.Length * catCount / (string.IsNullOrEmpty(a.CategoryIds) ? catCount : (a.CategoryIds.Length + 1)))).Take(count).ToList();
                if (list.Count == 0 && keywords is { Length: > 0 })
                {
                    return GetsByWeightedPrice(count, type, ipinfo, cid);
                }

                var ids = list.Select(a => a.Id).ToArray();
                GetQuery(a => ids.Contains(a.Id)).UpdateFromQuery(a => new Advertisement
                {
                    DisplayCount = a.DisplayCount + 1
                });

                return list;
            }));
Exemplo n.º 7
0
        /// <summary>
        /// 按价格随机筛选一个元素
        /// </summary>
        /// <param name="count">数量</param>
        /// <param name="type">广告类型</param>
        /// <param name="cid">分类id</param>
        /// <returns></returns>
        public List <Advertisement> GetsByWeightedPrice(int count, AdvertiseType type, int?cid = null)
        {
            return(CacheManager.GetOrAdd($"{count}{type}{cid}", _ =>
            {
                Expression <Func <Advertisement, bool> > where = a => a.Types.Contains(type.ToString("D")) && a.Status == Status.Available;
                if (cid.HasValue)
                {
                    var scid = cid.ToString();
                    if (Any(a => a.CategoryIds.Contains(scid)))
                    {
                        where = where.And(a => a.CategoryIds.Contains(scid) || string.IsNullOrEmpty(a.CategoryIds));
                    }
                }
                var list = GetQuery(where).AsEnumerable().Select(a => new WeightedItem <Advertisement>(a, a.CategoryIds is { Length: > 0 } ? (int)a.Price * 2 : (int)a.Price)).WeightedItems(count);
                var ids = list.Select(a => a.Id).ToArray();
                GetQuery(a => ids.Contains(a.Id)).UpdateFromQuery(a => new Advertisement()
                {
                    DisplayCount = a.DisplayCount + 1
                });

                return list;
            }));
Exemplo n.º 8
0
        public static async Task <bool> GoToNextSite(AdvertiseType type, short mode)
        {
            try
            {
                //mode 0 => حرکت رو به جلو
                //mode 1 => از سر گیری سایت ها بعد از ریستارت مورم
                //SettingBusiness _cls;
                //var res = await SettingBusiness.GetAllAsync();
                //_cls = res.Count == 0 ? new SettingBusiness() : res[0];
                var path = Path.Combine(Application.StartupPath, "SiteRate.txt");
                var lst  = File.ReadAllLines(path).ToList();
                if (!lst.Any())
                {
                    return(false);
                }
                var index = "";
                if (mode == 0)
                {
                    for (var i = 0; i < lst.Count; i++)
                    {
                        if (lst[i] != type.ToString())
                        {
                            continue;
                        }
                        if (i + 1 == lst.Count)
                        {
                            index = null;
                            break;
                        }

                        index = lst[i + 1];
                        break;
                    }
                }
                else
                {
                    CloseAllChromeWindows();
                    index = lst[0];
                }

                switch (index)
                {
                case "Divar":
                    //if (_cls.DivarSetting.CountAdvInIp != 0)
                    //{
                    //    var divar = await DivarAdv.GetInstance();
                    //    await divar.StartRegisterAdv();
                    //    return true;
                    //}
                    break;

                case "Sheypoor":
                    //if (_cls.SheypoorSetting.CountAdvInIp != 0)
                    //{
                    //    var sheypoor = await SheypoorAdv.GetInstance();
                    //    await sheypoor.StartRegisterAdv();
                    //    return true;
                    //}
                    break;
                }

                return(false);
            }
            catch (Exception ex)
            {
                WebErrorLog.ErrorInstence.StartErrorLog(ex);
                return(false);
            }
        }