Exemplo n.º 1
0
        /// <summary>
        /// اگر مسیر داده نشود از مسیر پیش فرض استفاده خواهد شد
        /// </summary>
        /// <param name="rootPath"></param>
        /// <returns></returns>
        public static async Task <List <Advertise> > GetAllAsync(string rootPath = "", Guid visitorGuid = new Guid())
        {
            var ret = new List <Advertise>();

            try
            {
                if (string.IsNullOrEmpty(rootPath))
                {
                    rootPath = ConfigurationManager.AppSettings.Get("RootPath");
                }
                if (string.IsNullOrEmpty(rootPath) || !Directory.Exists(rootPath))
                {
                    return(ret);
                }

                if (visitorGuid != new Guid())
                {
                    var lst = await VisitorAdvBusiness.GetAllAsync(visitorGuid);

                    var visitorAdvList = lst.Select(p => p.AdvName).ToList();
                    foreach (var dir in Directory.GetDirectories(rootPath))
                    {
                        var lastFolderName = Path.GetFileName(dir);
                        if (visitorAdvList.IndexOf(lastFolderName) >= 0)
                        {
                            var newAdv = new Advertise(lastFolderName, rootPath);
                            ret.Add(newAdv);
                        }
                    }
                }
                else
                {
                    foreach (var dir in Directory.GetDirectories(rootPath))
                    {
                        string lastFolderName = Path.GetFileName(dir);
                        var    newAdv         = new Advertise(lastFolderName, rootPath);
                        ret.Add(newAdv);
                    }
                }

                return(ret.Where(p => p != null && p.Titles.Any() && !string.IsNullOrEmpty(p.Content) && p.Content.Length > 50)?.ToList() ?? new List <Advertise>());
            }
            catch (Exception ex)
            {
                WebErrorLog.ErrorLogInstance.StartLog(ex);
                return(ret);
            }
        }
Exemplo n.º 2
0
        private async Task <AdvertiseLogBusiness> GetNextAdv(long simCardNumber)
        {
            var newAdvertiseLogBusiness = new AdvertiseLogBusiness();
            var monitor = new PerfMonitor();

            try
            {
                newAdvertiseLogBusiness.SimCardNumber = simCardNumber;

                #region find visitor and text replacements
                //پیدا کردن ویزیتور ها و متن جایگزین محتوا برای سیم کارت مورد نظر
                var replacements = await VisitorBusiness.GetMasterSlaveAdvReplacementsAsync(newAdvertiseLogBusiness.SimCardNumber);

                if (replacements?.MasterGuid == null)
                {
                    return(null);
                }

                newAdvertiseLogBusiness.MasterVisitorGuid = (Guid)replacements.MasterGuid;
                if (replacements.SlaveGuid != null)
                {
                    newAdvertiseLogBusiness.SlaveVisitorGuid = (Guid)replacements.SlaveGuid;
                }
                #endregion

                #region find visitorAdvs
                //لیست آگهی های مرتبط با ویزیتور دریافت می شود
                AdvertiseList = await Advertise.GetAllAsync(AdvRootPath, newAdvertiseLogBusiness.MasterVisitorGuid);

                AdvertiseList = AdvertiseList.Where(q => q.PishNevis == false).ToList();

                if (!(AdvertiseList?.Count > 0))
                {
                    return(null);
                }

                #endregion

                #region findNextAdvIndex


                var nextAdvIndex = new Random().Next(AdvertiseList.Count);
                #endregion

                #region GetPath

                string path = null;
                path = Path.Combine(Path.Combine(_cls?.NiazSetting?.PicPath, AdvertiseList[nextAdvIndex].AdvName) ==
                                    AdvertiseList[nextAdvIndex].AdvName
                    ? AdvertiseList[nextAdvIndex].RootPath
                    : _cls?.NiazSetting?.PicPath, AdvertiseList[nextAdvIndex].AdvName);
                newAdvertiseLogBusiness.Adv = path;
                #endregion


                #region FindNextTitle
                //تایتل آگهی دریافت می شود
                if (!(AdvertiseList[nextAdvIndex].Titles?.Count > 0))
                {
                    return(null);
                }

                var nextTitleIndex = new Random(DateTime.Now.Millisecond).Next(AdvertiseList[nextAdvIndex].Titles.Count);
                newAdvertiseLogBusiness.Title = AdvertiseList[nextAdvIndex].Titles[nextTitleIndex];


                if (string.IsNullOrEmpty(newAdvertiseLogBusiness.Content))
                {
                    return(null);
                }
                #endregion

                #region GetContent
                //کانتنت آگهی دریافت می شود

                newAdvertiseLogBusiness.Content = AdvertiseList[nextAdvIndex].Content
                                                  .Replace("<master>", replacements.AdvReplacement1)
                                                  .Replace("<slave>", replacements.AdvReplacement2)
                                                  .Replace("<tell>", replacements.Phone)
                                                  .Replace("(", "<")
                                                  .Replace(")", ">");

                if (string.IsNullOrEmpty(newAdvertiseLogBusiness.Content))
                {
                    return(null);
                }

                #endregion

                #region FindImages
                //عکسهای آگهی دریافت می شود
                newAdvertiseLogBusiness.ImagesPathList = GetNextImages(newAdvertiseLogBusiness.Adv,
                                                                       _cls?.NiazSetting?.PicCountInPerAdv ?? 3);
                #endregion

                //قیمت آگهی دریافت می شود
                newAdvertiseLogBusiness.Price = AdvertiseList[nextAdvIndex].Price;

                #region GetCity

                var city = await CityBusiness.GetNextRandomCityAsync(newAdvertiseLogBusiness.MasterVisitorGuid,
                                                                     AdvertiseType.NiazKade);

                newAdvertiseLogBusiness.City  = city?.CityName ?? "مشهد";
                newAdvertiseLogBusiness.State = city?.State.StateName ?? "خراسان رضوی";
                #endregion

                #region GetCategory
                if (AdvertiseList[nextAdvIndex].NiazKadeCategories != null &&
                    AdvertiseList[nextAdvIndex].NiazKadeCategories.Count > 0)
                {
                    newAdvertiseLogBusiness.SubCategory1 = AdvertiseList[nextAdvIndex]?.NiazKadeCategories[0] ?? "";
                    newAdvertiseLogBusiness.SubCategory2 = AdvertiseList[nextAdvIndex]?.NiazKadeCategories[1] ?? "";
                }
                else
                {
                    newAdvertiseLogBusiness.SubCategory1 = null;
                    newAdvertiseLogBusiness.SubCategory2 = null;
                }
                #endregion

                return(newAdvertiseLogBusiness);
            }
            catch (Exception ex)
            {
                WebErrorLog.ErrorLogInstance.StartLog(ex);
                return(null);
            }

            finally { monitor.Dispose(); }
        }