Esempio n. 1
0
        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));
        }
Esempio n. 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));
        }
        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));
        }
Esempio n. 5
0
        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));
        }