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));
        }
Exemple #2
0
        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));
        }