public FSNotificationServicePolicyBase(NotificationService apnsService) { if (apnsService != null) { ApnsService = apnsService; } _dbContext = new YintaiHangzhouContext(); }
public void BulkDelete(int jobId) { using (var unWrapContext = new YintaiHangzhouContext()) { unWrapContext.Database.ExecuteSqlCommand(@"exec dbo.ProductBulkDelete @jobId", new[] { new SqlParameter("@jobId", jobId) }); } }
public IEnumerable <ProductPublishResult> Publish(int customerId, int jobId) { using (var unWrapContext = new YintaiHangzhouContext()) { return(unWrapContext.Database.SqlQuery <ProductPublishResult>(@"exec dbo.ProductStagePublish2 @inUser,@jobId" , new[] { new SqlParameter("inUser", customerId) , new SqlParameter("jobId", jobId) }).ToList()); } }
public IEnumerable <ProductValidateResult> Validate(int customerId, int jobId) { using (var unWrapContext = new YintaiHangzhouContext()) { var result = unWrapContext.Database.SqlQuery <ProductValidateResult>(@"exec dbo.ProductStageValidate @inUser,@jobId", new[] { new SqlParameter("inUser", customerId) , new SqlParameter("jobId", jobId) }); return(result.ToArray()); } }
private void IndexHotwork(ElasticClient client, DateTime benchDate) { ILog log = LogManager.GetLogger(this.GetType()); int cursor = 0; int size = 100; int successCount = 0; Stopwatch sw = new Stopwatch(); sw.Start(); using (var db = new YintaiHangzhouContext("YintaiHangzhouContext")) { var words = from p in db.HotWords where (p.CreatedDate >= benchDate || p.UpdatedDate >= benchDate) select p; var prods = from p in words.ToList() select new ESHotword() { Id = p.Id, SortOrder = p.SortOrder, Status = p.Status, Type = p.Type, Word = p.Type == 1?p.Word:JsonConvert.DeserializeObject <dynamic>(p.Word).name, BrandId = p.Type == 1?0:JsonConvert.DeserializeObject <dynamic>(p.Word).id }; int totalCount = prods.Count(); client.MapFromAttributes <ESHotword>(); while (cursor < totalCount) { var result = client.IndexMany(prods.OrderByDescending(p => p.Id).Skip(cursor).Take(size)); if (!result.IsValid) { foreach (var item in result.Items) { if (item.OK) { successCount++; } else { log.Info(string.Format("id index failed:{0}", item.Id)); } } } else { successCount += result.Items.Count(); } cursor += size; } } sw.Stop(); log.Info(string.Format("{0} hotwords in {1} => {2} docs/s", successCount, sw.Elapsed, successCount / sw.Elapsed.TotalSeconds)); }
private void IndexStorePromotion(ElasticClient client, DateTime benchDate) { ILog log = LogManager.GetLogger(this.GetType()); int cursor = 0; int size = 100; int successCount = 0; Stopwatch sw = new Stopwatch(); sw.Start(); using (var db = new YintaiHangzhouContext("YintaiHangzhouContext")) { var prods = db.StorePromotions.Where(r => r.CreateDate >= benchDate || r.UpdateDate >= benchDate) .GroupJoin(db.PointOrderRules.Where(r => r.Status != (int)DataStatus.Deleted), o => o.Id, i => i.StorePromotionId, (o, i) => new { S = o, R = i }); int totalCount = prods.Count(); client.MapFromAttributes <ESStorePromotion>(); while (cursor < totalCount) { var linq = from l in prods.OrderByDescending(p => p.S.Id).Skip(cursor).Take(size).ToList() select new ESStorePromotion().FromEntity <ESStorePromotion>(l.S, s => { s.ExchangeRule = JsonConvert.SerializeObject(l.R.Select(r => new { rangefrom = r.RangeFrom, rangeto = r.RangeTo, ratio = r.Ratio })); }); var result = client.IndexMany(linq); if (!result.IsValid) { foreach (var item in result.Items) { if (item.OK) { successCount++; } else { log.Info(string.Format("id index failed:{0}", item.Id)); } } } else { successCount += result.Items.Count(); } cursor += size; } } sw.Stop(); log.Info(string.Format("{0} store promotions in {1} => {2} docs/s", successCount, sw.Elapsed, successCount / sw.Elapsed.TotalSeconds)); }
private void IndexTag(ElasticClient client, DateTime benchDate) { ILog log = LogManager.GetLogger(this.GetType()); int cursor = 0; int size = 100; int successCount = 0; Stopwatch sw = new Stopwatch(); sw.Start(); using (var db = new YintaiHangzhouContext("YintaiHangzhouContext")) { var prods = from p in db.Tags where (p.CreatedDate >= benchDate || p.UpdatedDate >= benchDate) select new ESTag() { Id = p.Id, Name = p.Name, Description = p.Description, Status = p.Status, SortOrder = p.SortOrder }; int totalCount = prods.Count(); client.MapFromAttributes <ESTag>(); while (cursor < totalCount) { var result = client.IndexMany(prods.OrderByDescending(p => p.Id).Skip(cursor).Take(size)); if (!result.IsValid) { foreach (var item in result.Items) { if (item.OK) { successCount++; } else { log.Info(string.Format("id index failed:{0}", item.Id)); } } } else { successCount += result.Items.Count(); } cursor += size; } } sw.Stop(); log.Info(string.Format("{0} tags in {1} => {2} docs/s", successCount, sw.Elapsed, successCount / sw.Elapsed.TotalSeconds)); }
public void Execute(IJobExecutionContext context) { ILog log = LogManager.GetLogger(this.GetType()); JobDataMap data = context.JobDetail.JobDataMap; var benchDate = data.ContainsKey("benchdate")?data.GetDateTime("benchdate"):DateTime.Today.AddDays(-1); int successCount = 0; int cursor = 0; int size = 100; Stopwatch sw = new Stopwatch(); sw.Start(); using (var db = new YintaiHangzhouContext("YintaiHangzhouContext")) { var prods = from r in db.UserAccounts where r.AccountType == (int)AccountType.Point && (from p in db.PointHistories where (p.CreatedDate >= benchDate || p.UpdatedDate >= benchDate) && p.User_Id == r.User_Id select p).Any() select r; int totalCount = prods.Count(); while (cursor < totalCount) { var linq = prods.OrderByDescending(p => p.Id).Skip(cursor).Take(size).ToList(); foreach (var l in linq) { var sumPoints = db.PointHistories.Where(p => p.User_Id == l.User_Id && p.Status != (int)DataStatus.Deleted).Sum(p => p.Amount); if (sumPoints == l.Amount) { continue; } l.Amount = sumPoints > 0 ? sumPoints : 0; l.UpdatedDate = DateTime.Now; db.Entry(l).State = System.Data.EntityState.Modified; db.SaveChanges(); successCount++; } cursor += size; } } sw.Stop(); log.Info(string.Format("{0} accounts in {1} => {2} docs/s", successCount, sw.Elapsed, successCount / sw.Elapsed.TotalSeconds)); }
protected virtual void QueueNotification(PushBroker push, IJobExecutionContext context) { ILog log = LogManager.GetLogger(typeof(ApnsNotificationJob)); int cursor = 0; int size = 100; int successCount = 0; DateTime startDate = DateTime.Today; DateTime endDate = startDate.AddDays(1); Stopwatch sw = new Stopwatch(); sw.Start(); using (var db = new YintaiHangzhouContext("YintaiHangzhouContext")) { var prods = (from p in db.Promotions where (p.StartDate >= startDate && p.StartDate < endDate) && p.Status == 1 select p).OrderByDescending(p => p.IsTop).ThenByDescending(p => p.CreatedDate).FirstOrDefault(); if (prods != null) { var devices = (from d in db.DeviceLogs where d.Status == 1 select new { DeviceToken = d.DeviceToken }).Distinct(); int totalCount = devices.Count(); while (cursor < totalCount) { var pageDevices = devices.OrderBy(o => o.DeviceToken).Skip(cursor).Take(size); foreach (var device in pageDevices) { push.QueueNotification(new AppleNotification() .ForDeviceToken(device.DeviceToken) .WithAlert(prods.Name) .WithBadge(1) .WithCustomItem("from", JsonConvert.SerializeObject(new { targettype = (int)PushSourceType.Promotion, targetvalue = prods.Id.ToString() })) .WithSound("sound.caf")); successCount++; } cursor += size; } } } sw.Stop(); log.Info(string.Format("{0} notifications in {1} => {2} notis/s", successCount, sw.Elapsed, successCount / sw.Elapsed.TotalSeconds)); }
private void Query(YintaiHangzhouContext db, int minPoints, Action <IQueryable <LinqInner> > callback) { var accounts = from p in db.UserAccounts join c in db.Cards on p.User_Id equals c.User_Id where p.AccountType == (int)AccountType.Point && p.Status == (int)DataStatus.Normal && p.Amount >= minPoints && c.CardNo.Length > 0 select new LinqInner() { U = p, C = c }; if (callback != null) { callback(accounts); } }
private void IndexPromotionCode(ElasticClient client, DateTime benchDate) { ILog log = LogManager.GetLogger(this.GetType()); int cursor = 0; int size = 100; int successCount = 0; Stopwatch sw = new Stopwatch(); sw.Start(); using (var db = new YintaiHangzhouContext("YintaiHangzhouContext")) { var prods = from r in db.CouponHistories where (r.CreatedDate >= benchDate) select r; int totalCount = prods.Count(); while (cursor < totalCount) { var linq = prods.OrderByDescending(p => p.Id).Skip(cursor).Take(size).ToList(); foreach (var l in linq) { try { AwsHelper.SendMessage(l.TypeName , () => l.Composing()); successCount++; } catch (Exception ex) { log.Info(ex); } } cursor += size; } } sw.Stop(); log.Info(string.Format("{0} promotion codes in {1} => {2} docs/s", successCount, sw.Elapsed, successCount / sw.Elapsed.TotalSeconds)); }
private void IndexSpecialTopic(ElasticClient client, DateTime?benchDate, Func <IQueryable <SpecialTopicEntity>, YintaiHangzhouContext, IQueryable <SpecialTopicEntity> > whereCondition) { ILog log = LogManager.GetLogger(this.GetType()); int cursor = 0; int size = 100; int successCount = 0; Stopwatch sw = new Stopwatch(); sw.Start(); using (var db = new YintaiHangzhouContext("YintaiHangzhouContext")) { var linq = db.SpecialTopics.AsQueryable(); if (benchDate.HasValue) { linq = linq.Where(p => p.CreatedDate >= benchDate.Value || p.UpdatedDate >= benchDate.Value); } else if (whereCondition != null) { linq = whereCondition(linq, db); } var prods = from p in linq let resource = (from r in db.Resources where r.SourceId == p.Id && r.SourceType == 9 select new ESResource() { Domain = r.Domain, Name = r.Name, SortOrder = r.SortOrder, IsDefault = r.IsDefault, Type = r.Type, Width = r.Width, Height = r.Height }) select new ESSpecialTopic() { Id = p.Id, Name = p.Name, Description = p.Description, Status = p.Status, CreatedDate = p.CreatedDate, CreateUser = p.CreatedUser, Resource = resource, Type = p.Type, TargetValue = p.TargetValue }; int totalCount = prods.Count(); client.MapFromAttributes <ESSpecialTopic>(); while (cursor < totalCount) { var result = client.IndexMany(prods.OrderByDescending(p => p.Id).Skip(cursor).Take(size)); if (!result.IsValid) { foreach (var item in result.Items) { if (item.OK) { successCount++; } else { log.Info(string.Format("id index failed:{0}", item.Id)); } } } else { successCount += result.Items.Count(); } cursor += size; } } sw.Stop(); log.Info(string.Format("{0} special topics in {1} => {2} docs/s", successCount, sw.Elapsed, successCount / sw.Elapsed.TotalSeconds)); if (successCount > 0 && CascadPush) { //index related products log.Info("index products affected by related specialtopic "); IndexProds(client, null, (p, db) => { return(p.Where(prod => (from pro in db.SpecialTopics from ppr in db.SpecialTopicProductRelations where ppr.SpecialTopic_Id == pro.Id && (pro.CreatedDate >= benchDate || pro.UpdatedDate >= benchDate) && ppr.Product_Id == prod.Id select ppr.Product_Id).Any())); }); } }
public void Execute(IJobExecutionContext context) { ILog log = LogManager.GetLogger(this.GetType()); JobDataMap data = context.JobDetail.JobDataMap; var interval = data.ContainsKey("interval") ? data.GetIntValue("interval") : 24 * 60; var benchDate = DateTime.Now.AddMinutes(-interval); var host = data.GetString("awshost"); var public_key = data.GetString("publickey"); var private_key = data.GetString("privatekey"); dynamic jsonResponse = null; AwsHelper.SendHttpMessage(host, new { benchdate = benchDate.ToUniversalTime() }, public_key, private_key, r => jsonResponse = r, null); if (jsonResponse == null) { log.Info("request error!"); return; } int successCount = 0; Stopwatch sw = new Stopwatch(); sw.Start(); using (var db = new YintaiHangzhouContext("YintaiHangzhouContext")) { foreach (var dynamicObject in jsonResponse.data) { try { string code = dynamicObject.code; int? status = dynamicObject.status; DateTime opeDate = dynamicObject.created_at; CouponStatus targetStatus = CouponStatus.Used; CouponActionType targetActionType = CouponActionType.Consume; if (status.HasValue && status.Value == -1) { targetStatus = CouponStatus.Normal; targetActionType = CouponActionType.Rebate; } switch ((int)dynamicObject.coupontype) { case 1: var coupon = db.StoreCoupons.Where(s => s.Code == code && s.Status != (int)CouponStatus.Deleted).FirstOrDefault(); if (coupon != null) { coupon.Status = (int)targetStatus; coupon.UpdateDate = opeDate.ToLocalTime(); coupon.UpdateUser = 0; db.Entry(coupon).State = EntityState.Modified; db.CouponLogs.Add(new CouponLogEntity() { ActionType = (int)targetActionType, BrandNo = dynamicObject.brandno, Code = code, ConsumeStoreNo = dynamicObject.storeno, CreateDate = opeDate.ToLocalTime(), CreateUser = 0, ReceiptNo = dynamicObject.receiptno, Type = 1 }); db.SaveChanges(); successCount++; } break; case 2: var coupon2 = db.CouponHistories.Where(s => s.CouponId == code && s.Status != (int)CouponStatus.Deleted).FirstOrDefault(); if (coupon2 != null) { coupon2.Status = (int)targetStatus; db.Entry(coupon2).State = EntityState.Modified; db.CouponLogs.Add(new CouponLogEntity() { ActionType = (int)targetActionType, BrandNo = dynamicObject.brandno, Code = code, ConsumeStoreNo = dynamicObject.storeno, CreateDate = opeDate.ToLocalTime(), CreateUser = 0, ReceiptNo = dynamicObject.receiptno, Type = 2 }); db.SaveChanges(); successCount++; } break; } } catch (Exception ex) { log.Error(ex); } } } sw.Stop(); log.Info(string.Format("{0} status logs in {1} => {2} docs/s", successCount, sw.Elapsed, successCount / sw.Elapsed.TotalSeconds)); }
protected override void QueueNotification(PushBroker push, IJobExecutionContext context) { ILog log = LogManager.GetLogger(typeof(ApnsNotificationCommonJob)); JobDataMap data = context.JobDetail.JobDataMap; var interval = data.GetIntValue("intervalofsec"); int cursor = 0; int size = 100; int successCount = 0; var benchDate = DateTime.Now.AddSeconds(-interval); Stopwatch sw = new Stopwatch(); sw.Start(); using (var db = new YintaiHangzhouContext("YintaiHangzhouContext")) { var prods = (from p in db.NotificationLogs where p.CreateDate >= benchDate && p.Status == (int)NotificationStatus.Default select p); if (prods != null) { int totalCount = prods.Count(); while (cursor < totalCount) { var linq = prods.OrderByDescending(p => p.Id).Skip(cursor).Take(size).ToList(); foreach (var l in linq) { try { if (l.SourceType.Value == (int)SourceType.Comment) { // if it's comment, always notify all comments owners of this item var comment = db.Comments.Find(l.SourceId); if (comment == null) { continue; } var relatedComments = db.Comments.Where(c => c.SourceType == comment.SourceType && c.SourceId == comment.SourceId && c.User_Id != comment.User_Id) .Join(db.DeviceLogs.Where(d => d.User_Id > 0), o => o.User_Id, i => i.User_Id, (o, i) => new { Token = i.DeviceToken }).ToList(); if (comment.SourceType == (int)SourceType.Product) { var product = db.Products.Where(p => p.Id == comment.SourceId && p.RecommendUser != comment.User_Id) .Join(db.DeviceLogs.Where(d => d.User_Id > 0), o => o.CreatedUser, i => i.User_Id, (o, i) => new { Token = i.DeviceToken }).FirstOrDefault(); if (product != null) { relatedComments.Add(new { Token = product.Token }); } } else if (comment.SourceType == (int)SourceType.Promotion) { var promotion = db.Promotions.Where(p => p.Id == comment.SourceId && p.RecommendUser != comment.User_Id) .Join(db.DeviceLogs.Where(d => d.User_Id > 0), o => o.CreatedUser, i => i.User_Id, (o, i) => new { Token = i.DeviceToken }).FirstOrDefault(); if (promotion != null) { relatedComments.Add(new { Token = promotion.Token }); } } foreach (var device in relatedComments.Distinct()) { push.QueueNotification(new AppleNotification() .ForDeviceToken(device.Token) .WithAlert("新评论...") .WithBadge(1) .WithCustomItem("from", JsonConvert.SerializeObject(new { targettype = (int)PushSourceType.SelfComment, targetvalue = comment.SourceId })) .WithSound("sound.caf")); successCount++; } l.NotifyDate = DateTime.Now; l.Status = (int)NotificationStatus.Notified; db.Entry(l).State = System.Data.EntityState.Modified; db.SaveChanges(); } } catch (Exception ex) { log.Info(ex); } } cursor += size; } } } sw.Stop(); log.Info(string.Format("{0} notifications in {1} => {2} notis/s", successCount, sw.Elapsed, successCount / sw.Elapsed.TotalSeconds)); }
public void Execute(IJobExecutionContext context) { ILog log = LogManager.GetLogger(this.GetType()); JobDataMap data = context.JobDetail.JobDataMap; var privatekey = data.GetString("privatekey"); var publickey = data.GetString("publickey"); var minPoints = data.GetInt("minpoints"); var groupPointConvertUrl = data.GetString("pointconverturl"); int point2GroupRatio = int.Parse(ConfigurationManager.AppSettings["point2groupratio"]); string appStoreNo = ConfigurationManager.AppSettings["appStoreNoInGroup"]; if (string.IsNullOrEmpty(appStoreNo)) { log.Info("app store no in group is empty!"); return; } int cursor = 0; int successCount = 0; int size = 100; int totalCount = 0; Stopwatch sw = new Stopwatch(); sw.Start(); Type linqType = new { U = (UserAccountEntity)null, C = (CardEntity)null }.GetType(); using (var db = new YintaiHangzhouContext("YintaiHangzhouContext")) { Query(db, minPoints, a => totalCount = a.Count()); } while (cursor < totalCount) { List <LinqInner> accounts = null; using (var db = new YintaiHangzhouContext("YintaiHangzhouContext")) { Query(db, minPoints, acs => accounts = acs.OrderBy(a => a.U.Id).Skip(cursor).Take(size).ToList()); } foreach (var account in accounts) { using (var ts = new TransactionScope()) { // step1: check account balanced using (var db = new YintaiHangzhouContext("YintaiHangzhouContext")) { var correctPoints = db.PointHistories.Where(p => p.User_Id == account.U.User_Id && p.Status != (int)DataStatus.Deleted).Sum(p => p.Amount); var convertPoints = correctPoints - correctPoints % point2GroupRatio; if (convertPoints < minPoints) { continue; } //step 2: insert deduct point history db.PointHistories.Add(new PointHistoryEntity() { Amount = -convertPoints, CreatedDate = DateTime.Now, CreatedUser = 0, Description = string.Format("转换为集团积点{0}", -convertPoints), Name = string.Format("转换为集团积点{0}", -convertPoints), PointSourceType = (int)PointSourceType.System, PointSourceId = 0, Status = (int)DataStatus.Normal, Type = (int)PointType.Convert2Group, UpdatedDate = DateTime.Now, UpdatedUser = 0, User_Id = account.U.User_Id }); db.SaveChanges(); // step 3: call group service to convert points string errorMsg; bool canConvert = GroupServiceHelper.SendHttpMessage(groupPointConvertUrl, publickey, privatekey, new { cardno = account.C.CardNo, amount = convertPoints / point2GroupRatio, storeno = appStoreNo }, out errorMsg); if (canConvert) { ts.Complete(); successCount++; } else { log.Info(string.Format("convert points failed for cardno:{0},msg:{1}", account.C.CardNo, errorMsg)); } } } } cursor += size; } sw.Stop(); log.Info(string.Format("{0} points from app2group in {1} => {2} docs/s", successCount, sw.Elapsed, successCount / sw.Elapsed.TotalSeconds)); }
private void IndexProds(ElasticClient client, DateTime?benchDate, Func <IQueryable <ProductEntity>, YintaiHangzhouContext, IQueryable <ProductEntity> > whereCondition) { ILog log = LogManager.GetLogger(this.GetType()); int cursor = 0; int successCount = 0; int size = 100; Stopwatch sw = new Stopwatch(); sw.Start(); using (var db = new YintaiHangzhouContext("YintaiHangzhouContext")) { var linq = db.Products.AsQueryable(); if (benchDate.HasValue) { linq = linq.Where(p => p.CreatedDate >= benchDate.Value || p.UpdatedDate >= benchDate.Value); } else if (whereCondition != null) { linq = whereCondition(linq, db); } var prods = from p in linq join s in db.Stores on p.Store_Id equals s.Id join b in db.Brands on p.Brand_Id equals b.Id join t in db.Tags on p.Tag_Id equals t.Id let resource = (from r in db.Resources where r.SourceId == p.Id && r.SourceType == 1 select new ESResource() { Domain = r.Domain, Name = r.Name, SortOrder = r.SortOrder, IsDefault = r.IsDefault, Type = r.Type, Width = r.Width, Height = r.Height }) let specials = from psp in db.SpecialTopicProductRelations where psp.Product_Id == p.Id join sp in db.SpecialTopics on psp.SpecialTopic_Id equals sp.Id select new ESSpecialTopic { Id = sp.Id, Name = sp.Name, Description = sp.Description } let promotions = from ppr in db.Promotion2Product where ppr.ProdId == p.Id join pro in db.Promotions on ppr.ProId equals pro.Id select new ESPromotion { Id = pro.Id, Name = pro.Name, Description = pro.Description, CreatedDate = p.CreatedDate, StartDate = pro.StartDate, EndDate = pro.EndDate, Status = pro.Status } select new ESProduct() { Id = p.Id, Name = p.Name, Description = p.Description, CreatedDate = p.CreatedDate, Price = p.Price, RecommendedReason = p.RecommendedReason, Status = p.Status, CreateUserId = p.CreatedUser, SortOrder = p.SortOrder, Tag = new ESTag() { Id = t.Id, Name = t.Name, Description = t.Description }, Store = new ESStore() { Id = s.Id, Name = s.Name, Description = s.Description, Address = s.Location, Location = new Location { Lon = s.Longitude, Lat = s.Latitude }, GpsAlt = s.GpsAlt, GpsLat = s.GpsLat, GpsLng = s.GpsLng, Tel = s.Tel }, Brand = new ESBrand() { Id = b.Id, Name = b.Name, Description = b.Description, EngName = b.EnglishName }, Resource = resource, SpecialTopic = specials, Promotion = promotions }; int totalCount = prods.Count(); client.MapFromAttributes <ESProduct>(); while (cursor < totalCount) { var result = client.IndexMany(prods.OrderByDescending(p => p.Id).Skip(cursor).Take(size)); if (!result.IsValid) { foreach (var item in result.Items) { if (item.OK) { successCount++; } else { log.Info(string.Format("id index failed:{0}", item.Id)); } } } else { successCount += result.Items.Count(); } cursor += size; } } sw.Stop(); log.Info(string.Format("{0} products in {1} => {2} docs/s", successCount, sw.Elapsed, successCount / sw.Elapsed.TotalSeconds)); }
private void IndexResource(ElasticClient client, DateTime benchDate) { ILog log = LogManager.GetLogger(this.GetType()); int cursor = 0; int size = 100; int successCount = 0; Stopwatch sw = new Stopwatch(); sw.Start(); using (var db = new YintaiHangzhouContext("YintaiHangzhouContext")) { var prods = from r in db.Resources where (r.CreatedDate >= benchDate || r.UpdatedDate >= benchDate) select new ESResource() { Id = r.Id, Status = r.Status, Domain = r.Domain, Name = r.Name, SortOrder = r.SortOrder, IsDefault = r.IsDefault, Type = r.Type, Width = r.Width, Height = r.Height, SourceId = r.SourceId, SourceType = r.SourceType }; int totalCount = prods.Count(); client.MapFromAttributes <ESResource>(); while (cursor < totalCount) { var result = client.IndexMany(prods.OrderByDescending(p => p.Id).Skip(cursor).Take(size)); if (!result.IsValid) { foreach (var item in result.Items) { if (item.OK) { successCount++; } else { log.Info(string.Format("id index failed:{0}", item.Id)); } } } else { successCount += result.Items.Count(); } cursor += size; } } sw.Stop(); log.Info(string.Format("{0} resources in {1} => {2} docs/s", successCount, sw.Elapsed, successCount / sw.Elapsed.TotalSeconds)); //update related source if any affected if (successCount > 0 && CascadPush) { //index related products log.Info("index products affected by related resources "); IndexProds(client, null, (p, db) => { return(p.Where(prod => (from r in db.Resources where r.SourceId == prod.Id && (r.CreatedDate >= benchDate || r.UpdatedDate >= benchDate) && r.SourceType == (int)SourceType.Product select r.Id).Any())); }); //index related promotions log.Info("index promotions affected by related resources "); IndexPros(client, null, (p, db) => { return(p.Where(prod => (from r in db.Resources where r.SourceId == prod.Id && (r.CreatedDate >= benchDate || r.UpdatedDate >= benchDate) && r.SourceType == (int)SourceType.Promotion select r.Id).Any())); }); //index related specialtopics log.Info("index specialtopics affected by related resources "); IndexSpecialTopic(client, null, (p, db) => { return(p.Where(prod => (from r in db.Resources where r.SourceId == prod.Id && (r.CreatedDate >= benchDate || r.UpdatedDate >= benchDate) && r.SourceType == (int)SourceType.SpecialTopic select r.Id).Any())); }); } }
private void IndexBrand(ElasticClient client, DateTime benchDate) { ILog log = LogManager.GetLogger(this.GetType()); int cursor = 0; int size = 100; int successCount = 0; Stopwatch sw = new Stopwatch(); sw.Start(); using (var db = new YintaiHangzhouContext("YintaiHangzhouContext")) { var prods = from p in db.Brands where (p.CreatedDate >= benchDate || p.UpdatedDate >= benchDate) select new ESBrand() { Id = p.Id, Name = p.Name, Description = p.Description, Status = p.Status, Group = p.Group }; int totalCount = prods.Count(); client.MapFromAttributes <ESBrand>(); while (cursor < totalCount) { var result = client.IndexMany(prods.OrderByDescending(p => p.Id).Skip(cursor).Take(size)); if (!result.IsValid) { foreach (var item in result.Items) { if (item.OK) { successCount++; } else { log.Info(string.Format("id index failed:{0}", item.Id)); } } } else { successCount += result.Items.Count(); } cursor += size; } } sw.Stop(); log.Info(string.Format("{0} brands in {1} => {2} docs/s", successCount, sw.Elapsed, successCount / sw.Elapsed.TotalSeconds)); //update related source if any affected if (successCount > 0 && CascadPush) { //index related products log.Info("index products affected by related brands "); IndexProds(client, null, (p, db) => { return(p.Where(prod => (from r in db.Brands where (r.CreatedDate >= benchDate || r.UpdatedDate >= benchDate) && r.Id == prod.Brand_Id select r.Id).Any())); }); } }
private void IndexStore(ElasticClient client, DateTime benchDate) { ILog log = LogManager.GetLogger(this.GetType()); int cursor = 0; int size = 100; int successCount = 0; Stopwatch sw = new Stopwatch(); sw.Start(); using (var db = new YintaiHangzhouContext("YintaiHangzhouContext")) { var prods = from s in db.Stores let resource = (from r in db.Resources where r.SourceId == s.Id && r.SourceType == (int)SourceType.StoreLogo select new ESResource() { Domain = r.Domain, Name = r.Name, SortOrder = r.SortOrder, IsDefault = r.IsDefault, Type = r.Type, Width = r.Width, Height = r.Height }) where (s.CreatedDate >= benchDate || s.UpdatedDate >= benchDate) select new ESStore() { Id = s.Id, Name = s.Name, Description = s.Description, Address = s.Location, Location = new Location { Lon = s.Longitude, Lat = s.Latitude }, GpsAlt = s.GpsAlt, GpsLat = s.GpsLat, GpsLng = s.GpsLng, Tel = s.Tel, Status = s.Status, Resource = resource }; int totalCount = prods.Count(); client.MapFromAttributes <ESStore>(); while (cursor < totalCount) { var result = client.IndexMany(prods.OrderByDescending(p => p.Id).Skip(cursor).Take(size)); if (!result.IsValid) { foreach (var item in result.Items) { if (item.OK) { successCount++; } else { log.Info(string.Format("id index failed:{0}", item.Id)); } } } else { successCount += result.Items.Count(); } cursor += size; } } sw.Stop(); log.Info(string.Format("{0} stores in {1} => {2} docs/s", successCount, sw.Elapsed, successCount / sw.Elapsed.TotalSeconds)); }
private void IndexBanner(ElasticClient client, DateTime benchDate) { ILog log = LogManager.GetLogger(this.GetType()); int cursor = 0; int size = 100; int successCount = 0; Stopwatch sw = new Stopwatch(); sw.Start(); using (var db = new YintaiHangzhouContext("YintaiHangzhouContext")) { var prods = from p in db.Banners join pro in db.Promotions on p.SourceId equals pro.Id where (p.CreatedDate >= benchDate || p.UpdatedDate >= benchDate) && p.SourceType == 2 let resource = (from r in db.Resources where r.SourceId == p.Id && r.SourceType == 11 select new ESResource() { Domain = r.Domain, Name = r.Name, SortOrder = r.SortOrder, IsDefault = r.IsDefault, Type = r.Type, Width = r.Width, Height = r.Height }) select new ESBanner() { Id = p.Id, SortOrder = p.SortOrder, CreatedDate = p.CreatedDate, Status = p.Status, SourceType = p.SourceType, Promotion = new ESPromotion() { Id = pro.Id }, Resource = resource }; int totalCount = prods.Count(); client.MapFromAttributes <ESBanner>(); while (cursor < totalCount) { var result = client.IndexMany(prods.OrderByDescending(p => p.Id).Skip(cursor).Take(size)); if (!result.IsValid) { foreach (var item in result.Items) { if (item.OK) { successCount++; } else { log.Info(string.Format("id index failed:{0}", item.Id)); } } } else { successCount += result.Items.Count(); } cursor += size; } } sw.Stop(); log.Info(string.Format("{0} banners in {1} => {2} docs/s", successCount, sw.Elapsed, successCount / sw.Elapsed.TotalSeconds)); }
private void IndexPros(ElasticClient client, DateTime?benchDate, Func <IQueryable <PromotionEntity>, YintaiHangzhouContext, IQueryable <PromotionEntity> > whereCondition) { ILog log = LogManager.GetLogger(this.GetType()); int cursor = 0; int size = 100; int successCount = 0; Stopwatch sw = new Stopwatch(); sw.Start(); using (var db = new YintaiHangzhouContext("YintaiHangzhouContext")) { var linq = db.Promotions.AsQueryable(); if (benchDate.HasValue) { linq = linq.Where(p => p.CreatedDate >= benchDate.Value || p.UpdatedDate >= benchDate.Value); } else if (whereCondition != null) { linq = whereCondition(linq, db); } var prods = from p in linq join s in db.Stores on p.Store_Id equals s.Id let resource = (from r in db.Resources where r.SourceId == p.Id && r.SourceType == 2 select new ESResource() { Domain = r.Domain, Name = r.Name, SortOrder = r.SortOrder, IsDefault = r.IsDefault, Type = r.Type, Width = r.Width, Height = r.Height }) select new ESPromotion() { Id = p.Id, Name = p.Name, Description = p.Description, CreatedDate = p.CreatedDate, StartDate = p.StartDate, EndDate = p.EndDate, FavoriteCount = p.FavoriteCount, IsTop = p.IsTop, Status = p.Status, CreateUserId = p.CreatedUser, Store = new ESStore() { Id = s.Id, Name = s.Name, Description = s.Description, Address = s.Location, Location = new Location { Lon = s.Longitude, Lat = s.Latitude }, GpsAlt = s.GpsAlt, GpsLat = s.GpsLat, GpsLng = s.GpsLng, Tel = s.Tel }, Resource = resource, ShowInList = p.IsMain.HasValue?p.IsMain.Value:true, PublicCode = p.PublicProCode }; int totalCount = prods.Count(); client.MapFromAttributes <ESPromotion>(); while (cursor < totalCount) { var result = client.IndexMany(prods.OrderByDescending(p => p.Id).Skip(cursor).Take(size)); if (!result.IsValid) { foreach (var item in result.Items) { if (item.OK) { successCount++; } else { log.Info(string.Format("id index failed:{0}", item.Id)); } } } else { successCount += result.Items.Count(); } cursor += size; } } sw.Stop(); log.Info(string.Format("{0} promotions in {1} => {2} docs/s", successCount, sw.Elapsed, successCount / sw.Elapsed.TotalSeconds)); if (successCount > 0 && CascadPush) { //index related products log.Info("index products affected by related promotions "); IndexProds(client, null, (p, db) => { return(p.Where(prod => (from pro in db.Promotions from ppr in db.Promotion2Product where ppr.ProId == pro.Id && (pro.CreatedDate >= benchDate || pro.UpdatedDate >= benchDate) && ppr.ProdId == prod.Id select ppr.ProdId).Any())); }); } }
public void Execute(IJobExecutionContext context) { JobDataMap data = context.JobDetail.JobDataMap; var privatekey = data.GetString("privatekey"); var publickey = data.GetString("publickey"); var awsExpireurl = data.GetString("awsexpireurl"); int point2GroupRatio = int.Parse(ConfigurationManager.AppSettings["point2groupratio"]); ILog log = LogManager.GetLogger(this.GetType()); int cursor = 0; int successCount = 0; int size = 100; DateTime fromDate = DateTime.Today.AddDays(-1); Stopwatch sw = new Stopwatch(); sw.Start(); using (var db = new YintaiHangzhouContext("YintaiHangzhouContext")) { var coupons = from p in db.StoreCoupons where p.ValidEndDate >= fromDate && p.ValidEndDate < DateTime.Today && p.Status == (int)CouponStatus.Normal select p; int totalCount = coupons.Count(); while (cursor < totalCount) { foreach (var coupon in coupons.OrderBy(c => c.Id).Skip(cursor).Take(size)) { bool canRebate = AwsHelper.SendHttpMessage(awsExpireurl, new { code = coupon.Code } , publickey , privatekey , null , null); if (canRebate) { var point = db.PointHistories.Where(u => u.User_Id == coupon.UserId.Value && u.Type == (int)PointType.VoidCoupon && u.Description == coupon.Code).FirstOrDefault(); if (point == null) { db.PointHistories.Add(new PointHistoryEntity() { Amount = (decimal)coupon.Points * point2GroupRatio, CreatedDate = DateTime.Now, CreatedUser = 0, Description = coupon.Code, Name = string.Format("代金券过期返回积点{0}", coupon.Points * point2GroupRatio), PointSourceType = (int)PointSourceType.System, PointSourceId = 0, Status = (int)DataStatus.Normal, Type = (int)PointType.VoidCoupon, UpdatedDate = DateTime.Now, UpdatedUser = 0, User_Id = coupon.UserId.Value }); db.SaveChanges(); successCount++; } } } cursor += size; } } sw.Stop(); log.Info(string.Format("{0} rebate points in {1} => {2} docs/s", successCount, sw.Elapsed, successCount / sw.Elapsed.TotalSeconds)); }