/// <summary> /// 增减用户的文券数量 /// </summary> /// <param name="user">用户对象</param> /// <param name="event">文券事件</param> /// <param name="change">文券数量变化,正数为增加,负数为减少</param> /// <param name="description">文券记录描述</param> /// <param name="logTime">文券日志记录时间,如果为 null 则使用当前时间</param> /// <exception cref="ArgumentNullException">user 参数为 null</exception> public async Task UpdateAsync(KeylolUser user, CouponEvent @event, int change, object description = null, DateTime?logTime = null) { if (user == null) { throw new ArgumentNullException(nameof(user)); } var log = new CouponLog { Change = change, Event = @event, UserId = user.Id, Description = JsonConvert.SerializeObject(description) }; _dbContext.CouponLogs.Add(log); bool saveFailed; do { try { saveFailed = false; user.Coupon += log.Change; log.Balance = user.Coupon; log.CreateTime = logTime ?? DateTime.Now; await _dbContext.SaveChangesAsync(); NotificationProvider.Hub <CouponHub, ICouponHubClient>().User(user.Id)? .OnCouponChanged(log.Event.ToString(), log.Change, log.Balance); } catch (DbUpdateConcurrencyException e) { saveFailed = true; await e.Entries.Single().ReloadAsync(); } } while (saveFailed); }
public ActionResult GetReductionCard() { Coupons model = null; int couponsId = Context.GetRequestInt("couponsId", 0); if (couponsId <= 0) { return(Json(new { isok = false, msg = "我钥匙被弄丢了", coupon = model })); } int orderId = Context.GetRequestInt("orderId", 0); if (orderId <= 0) { return(Json(new { isok = false, msg = "我是不存在的", coupon = model })); } int aId = 0; Food store_Food = null; int orderType = Context.GetRequestInt("orderType", 0);//默认专业版,多门店版 switch (orderType) { case 0: //专业版,多门店版 EntGoodsOrder orderInfo = EntGoodsOrderBLL.SingleModel.GetModel(orderId); if (orderInfo == null) { return(Json(new { isok = false, msg = "活动已过期", coupon = model })); } aId = orderInfo.aId; break; case 1: //餐饮 FoodGoodsOrder orderInfo_Food = FoodGoodsOrderBLL.SingleModel.GetModel(orderId); if (orderInfo_Food == null) { return(Json(new { isok = false, msg = "活动已过期", coupon = model })); } store_Food = FoodBLL.SingleModel.GetModel(orderInfo_Food.StoreId); if (store_Food == null) { return(Json(new { isok = false, msg = "活动已过期_", coupon = model })); } aId = store_Food.appId; break; } lock (lockgetReductionCard) { model = CouponsBLL.SingleModel.GetModelByIdAndAppId(couponsId, aId, (int)CouponState.已开启); if (model == null) { return(Json(new { isok = false, msg = "立减金已消失", coupon = model })); } List <CouponLog> loglist = CouponLogBLL.SingleModel.GetList($"couponid in ({model.Id}) and state!=4"); int userId = Context.GetRequestInt("userId", 0); if (userId <= 0) { return(Json(new { isok = false, msg = "id错误", coupon = model })); } C_UserInfo user = C_UserInfoBLL.SingleModel.GetModel(userId); if (user == null) { return(Json(new { isok = false, msg = "用户不存在", coupon = model })); } DateTime date = DateTime.Now; if ((DateTime.Compare(date, model.StartUseTime) > 0 && DateTime.Compare(date, model.EndUseTime) < 0 && model.ValType == 0) || model.ValType != 0) { List <CouponLog> logList = CouponLogBLL.SingleModel.GetList($" CouponId={model.Id} and fromorderid={orderId} ") ?? new List <CouponLog>(); List <C_UserInfo> userList = new List <C_UserInfo>(); string userIds = string.Join(",", logList.Select(log => log.UserId).ToList()); if (logList.Count > 0) { userList = C_UserInfoBLL.SingleModel.GetList($"id in ( {userIds} )") ?? new List <C_UserInfo>(); model.StartUseTime = logList[0].StartUseTime; model.EndUseTime = logList[0].EndUseTime; } List <CouponLog> userGetList = CouponLogBLL.SingleModel.GetList($"couponId={couponsId} and userid={user.Id}"); //已领取完 if (userList.Count >= model.SatisfyNum) { C_UserInfo userInfo = userList.Where(u => u.Id == user.Id).FirstOrDefault(); return(Json(new { isok = true, coupon = model, userList = userList, userInfo = userInfo })); } //未领取过这份立减金 if (logList.Where(log => log.UserId == user.Id).ToList().Count <= 0) { List <CouponLog> temploglist = loglist?.Where(w => w.CouponId == model.Id).ToList(); if (temploglist != null && temploglist.Count > 0) { //已领取份数 int orderCount = temploglist.GroupBy(g => g.FromOrderId).Count(); //库存是否足够 model.RemNum = model.CreateNum - orderCount; if (model.RemNum <= 0) { return(Json(new { isok = false, msg = "立减金已放送完毕,请关注下次的立减金优惠哦", coupon = model })); } } //超过领取限制 if (userGetList != null && userGetList.Count >= model.LimitReceive && model.LimitReceive > 0) { return(Json(new { isok = false, msg = "你已超过领取限制", coupon = model })); } bool isok = false; CouponLog getCoupon = new CouponLog { CouponId = model.Id, CouponName = model.CouponName, FromOrderId = orderId, UserId = user.Id, State = 4, StartUseTime = model.StartUseTime, EndUseTime = model.EndUseTime, AddTime = DateTime.Now, StoreId = model.StoreId }; getCoupon.Id = Convert.ToInt32(CouponLogBLL.SingleModel.Add(getCoupon)); isok = getCoupon.Id > 0; if (isok) { logList.Add(getCoupon); userList.Add(user); } else { return(Json(new { isok = false, msg = "出了点小问题~", coupon = model })); } //满足条件 if (userList.Count == model.SatisfyNum) { if (model.ValType == 1) { model.StartUseTime = Convert.ToDateTime(date.AddDays(1).ToString("yyyy-MM-dd 00:00:00")); model.EndUseTime = Convert.ToDateTime(date.AddDays(model.ValDay).ToString("yyyy-MM-dd 23:59:59")); } //领券当天有效 else if (model.ValType == 2) { model.StartUseTime = Convert.ToDateTime(date.ToString("yyyy-MM-dd 00:00:00")); model.EndUseTime = Convert.ToDateTime(date.AddDays(model.ValDay - 1).ToString("yyyy-MM-dd 23:59:59")); } foreach (CouponLog log in logList) { log.StartUseTime = model.StartUseTime; log.EndUseTime = model.EndUseTime; log.State = 0; CouponLogBLL.SingleModel.Update(log, "State,StartUseTime,EndUseTime"); } } } return(Json(new { isok = true, userList = userList, coupon = model, userInfo = user })); } } return(Json(new { isok = false, msg = "立减金Miss", coupon = model })); }
public ActionResult GetReductionCardV2() { returnObj = new Return_Msg_APP(); int couponsId = Context.GetRequestInt("couponsId", 0); int orderId = Context.GetRequestInt("orderId", 0); int userId = Context.GetRequestInt("userId", 0); int aid = Context.GetRequestInt("aid", 0); returnObj.code = "0"; if (couponsId <= 0) { returnObj.Msg = "无效优惠券参数"; return(Json(returnObj)); } if (orderId <= 0) { returnObj.Msg = "无效订单参数"; return(Json(returnObj)); } if (userId <= 0) { returnObj.Msg = "无效用户参数"; return(Json(returnObj)); } C_UserInfo user = C_UserInfoBLL.SingleModel.GetModel(userId); if (user == null) { returnObj.Msg = "用户不存在"; return(Json(returnObj)); } Coupons model = null; lock (lockgetReductionCard) { model = CouponsBLL.SingleModel.GetModelByIdAndAppId(couponsId, aid, (int)CouponState.已开启); if (model == null) { returnObj.Msg = "立减金活动已过期"; return(Json(returnObj)); } List <CouponLog> allLogList = CouponLogBLL.SingleModel.GetListByIds(model.Id.ToString()); DateTime date = DateTime.Now; if ((DateTime.Compare(date, model.StartUseTime) > 0 && DateTime.Compare(date, model.EndUseTime) < 0 && model.ValType == 0) || model.ValType != 0) { List <CouponLog> logList = CouponLogBLL.SingleModel.GetListByOrderId(model.Id, orderId) ?? new List <CouponLog>(); if (logList.Count > 0) { model.StartUseTime = logList[0].StartUseTime; model.EndUseTime = logList[0].EndUseTime; } else { if (allLogList != null) { //已领取份数 int orderCount = allLogList.GroupBy(g => g.FromOrderId).Count(); //库存是否足够 model.RemNum = model.CreateNum - orderCount; if (model.RemNum <= 0) { returnObj.Msg = "立减金已放送完毕,请关注下次的立减金优惠哦"; return(Json(returnObj)); } } } string userIds = string.Join(",", logList.Select(log => log.UserId).ToList()); List <C_UserInfo> userList = C_UserInfoBLL.SingleModel.GetListByIds(userIds) ?? new List <C_UserInfo>(); List <CouponLog> userGetList = CouponLogBLL.SingleModel.GetListByUserId(couponsId, user.Id); //已领取完 if (userList.Count >= model.SatisfyNum) { C_UserInfo userInfo = userList.Where(u => u.Id == user.Id).FirstOrDefault(); returnObj.isok = true; returnObj.dataObj = new { userList = userList, coupon = model, userInfo = user }; return(Json(returnObj)); } //未领取过这份立减金 if (logList.Where(log => log.UserId == user.Id).ToList().Count <= 0) { //超过领取限制 if (userGetList != null && userGetList.Count >= model.LimitReceive && model.LimitReceive > 0) { returnObj.Msg = "你已超过领取限制"; return(Json(returnObj)); } bool isok = false; CouponLog getCoupon = new CouponLog { CouponId = model.Id, CouponName = model.CouponName, FromOrderId = orderId, UserId = user.Id, State = 4, StartUseTime = model.StartUseTime, EndUseTime = model.EndUseTime, AddTime = DateTime.Now, StoreId = model.StoreId, }; getCoupon.Id = Convert.ToInt32(CouponLogBLL.SingleModel.Add(getCoupon)); isok = getCoupon.Id > 0; if (isok) { logList.Add(getCoupon); userList.Add(user); } else { returnObj.Msg = "请重新领取"; return(Json(returnObj)); } //满足条件 if (userList.Count == model.SatisfyNum) { if (model.ValType == 1) { model.StartUseTime = Convert.ToDateTime(date.AddDays(1).ToString("yyyy-MM-dd 00:00:00")); model.EndUseTime = Convert.ToDateTime(date.AddDays(model.ValDay).ToString("yyyy-MM-dd 23:59:59")); } //领券当天有效 else if (model.ValType == 2) { model.StartUseTime = Convert.ToDateTime(date.ToString("yyyy-MM-dd 00:00:00")); model.EndUseTime = Convert.ToDateTime(date.AddDays(model.ValDay - 1).ToString("yyyy-MM-dd 23:59:59")); } string couponLogIds = string.Join(",", logList.Select(s => s.Id)); CouponLogBLL.SingleModel.UpdateCouponLogState(couponLogIds, 0, model.StartUseTime, model.EndUseTime); } } returnObj.code = "1"; returnObj.isok = true; returnObj.dataObj = new { userList = userList, coupon = model, userInfo = user }; return(Json(returnObj)); } } returnObj.Msg = "立减金丢失"; return(Json(returnObj)); }
public ActionResult GetCoupon() { string appId = Context.GetRequest("appId", string.Empty); int couponId = Context.GetRequestInt("couponId", 0); int userId = Context.GetRequestInt("userId", 0); if (couponId <= 0) { return(Json(new { isok = false, msg = "优惠券ID不能小于0" }, JsonRequestBehavior.AllowGet)); } if (userId <= 0) { return(Json(new { isok = false, msg = "用户ID不能小于0" }, JsonRequestBehavior.AllowGet)); } C_UserInfo usermodel = C_UserInfoBLL.SingleModel.GetModel(userId); if (usermodel == null) { return(Json(new { isok = false, msg = "找不到用户" }, JsonRequestBehavior.AllowGet)); } XcxAppAccountRelation xcxrelation = _xcxAppAccountRelationBLL.GetModelByAppid(appId); if (xcxrelation == null) { return(Json(new { isok = false, msg = "未找到小程序授权资料" }, JsonRequestBehavior.AllowGet)); } string userids = ""; string aids = ""; string errMsg = string.Empty; //店铺ID int storeId = _xcxAppAccountRelationBLL.ReturnStoreIdByAId(xcxrelation.Id, ref errMsg, ref aids, ref userids); if (!string.IsNullOrWhiteSpace(errMsg)) { return(Json(new { isok = false, errMsg }, JsonRequestBehavior.AllowGet)); } lock (lockgetcoupon) { string nowtime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); Coupons couponmodel = CouponsBLL.SingleModel.GetValidModel(couponId, nowtime); if (couponmodel == null) { return(Json(new { isok = false, msg = "找不到优惠券!" }, JsonRequestBehavior.AllowGet)); } int couponlogcount = CouponLogBLL.SingleModel.GetCountByCouponId(couponId); if (couponlogcount >= couponmodel.CreateNum) { return(Json(new { isok = false, msg = "您下手太慢了,优惠券被抢光了!" }, JsonRequestBehavior.AllowGet)); } //判断用户领取是否超过限制 if (couponmodel.LimitReceive > 0) { int usercouponlogcount = CouponLogBLL.SingleModel.GetCountByCouponIdAndUserId(couponId, userId); if (usercouponlogcount >= couponmodel.LimitReceive) { return(Json(new { isok = false, msg = "您已领取过了,快去使用!" }, JsonRequestBehavior.AllowGet)); } } //判断有会员等级限制 if (couponmodel.VipLevel > 0) { VipRelation viprelationmodel = VipRelationBLL.SingleModel.GetModelByRidAndUid(xcxrelation.AppId, userId); if (viprelationmodel != null) { VipLevel viplevel = VipLevelBLL.SingleModel.GetModel(viprelationmodel.levelid); if (viplevel == null || viplevel.level < couponmodel.VipLevel) { return(Json(new { isok = false, msg = "您的会员等级不够,无法领取优惠券!" }, JsonRequestBehavior.AllowGet)); } } } CouponLog couponlog = new CouponLog(); couponlog.AddTime = DateTime.Now; couponlog.CouponId = couponmodel.Id; if (couponmodel.ValType == 0) { //指定优惠券固定时间 couponlog.StartUseTime = couponmodel.StartUseTime; couponlog.EndUseTime = couponmodel.EndUseTime; } else { //领券时间 string getcouptime = DateTime.Now.ToShortDateString(); //1:领到券次日开始N天内有效,2:领到券当日开始N天内有效 couponlog.StartUseTime = DateTime.Parse(getcouptime + " 00:00:00").AddDays(couponmodel.ValType == 1 ? 1 : 0); couponlog.EndUseTime = DateTime.Parse(getcouptime + " 23:59:59").AddDays(couponmodel.ValDay + (couponmodel.ValType == 1 ? 0 : -1)); } couponlog.CouponName = couponmodel.CouponName; couponlog.UserId = userId; couponlog.Id = Convert.ToInt32(CouponLogBLL.SingleModel.Add(couponlog)); return(Json(new { isok = couponlog.Id > 0, msg = couponlog.Id > 0 ? "领取成功" : "领取失败" }, JsonRequestBehavior.AllowGet)); } }