public ActionResult Void(StoreCouponDetailRequest request, int?authuid, UserModel authUser) { request.AuthUser = authUser; var coupon = _storecouponRepo.Get(sp => sp.Id == request.StoreCouponId && sp.UserId.Value == authUser.Id && sp.Status != (int)CouponStatus.Used && sp.ValidEndDate >= DateTime.Now).FirstOrDefault(); if (coupon == null) { return new RestfulResult { Data = new ExecuteResult { StatusCode = StatusCode.InternalServerError, Message = "代金券已失效或使用!" } } } ; using (var ts = new TransactionScope()) { // step1: void coupon coupon.Status = (int)CouponStatus.Deleted; _storecouponRepo.Update(coupon); //step2: rebate points of app var newPoint = _pointRepo.Insert(new PointHistoryEntity() { CreatedDate = DateTime.Now, CreatedUser = authUser.Id, Description = "取消代金券退回积分", Name = string.Format("取消代金券,返回{0}", ConfigManager.Point2GroupRatio * coupon.Points.Value), PointSourceId = request.StoreCouponId, PointSourceType = (int)PointSourceType.Group, Status = (int)DataStatus.Normal, UpdatedDate = DateTime.Now, UpdatedUser = authUser.Id, User_Id = authUser.Id, Type = (int)PointType.VoidCoupon, Amount = ConfigManager.Point2GroupRatio * coupon.Points.Value }); //step3: insert coupon log _couponlogRepo.Insert(new CouponLogEntity() { ActionType = (int)CouponActionType.Void, Code = coupon.Code, CreateDate = DateTime.Now, CreateUser = authUser.Id, Type = (int)CouponType.StorePromotion }); // step4: void action should call aws service directly to check the real time coupon status string message = string.Empty; bool isVoidSuccess = AwsHelper.SendHttpMessage(ConfigManager.AwsHttpUrlVoidCoupon, new { code = coupon.Code }, ConfigManager.AwsHttpPublicKey, ConfigManager.AwsHttpPrivateKey, r => message = r.message, null); // step3: commit if (newPoint != null && isVoidSuccess) { ts.Complete(); } else { return(new RestfulResult { Data = new ExecuteResult { StatusCode = StatusCode.InternalServerError, Message = "取消代金券异常!" } }); } } return(new RestfulResult { Data = new ExecuteResult { StatusCode = StatusCode.Success, Message = "取消代金券成功,积点将于1个工作日后返回会员卡账户!" } }); } }
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)); }
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)); }