瀏覽代碼

缓存处理

WH01243 2 年之前
父節點
當前提交
4572b7bd80
共有 4 個文件被更改,包括 60 次插入16 次删除
  1. 5 0
      http-client.env.json
  2. 23 9
      service/activityService.go
  3. 14 7
      service/activityServiceNew.go
  4. 18 0
      test.http

+ 5 - 0
http-client.env.json

@@ -0,0 +1,5 @@
+{
+  "dev": {
+    "name": "value"
+  }
+}

+ 23 - 9
service/activityService.go

@@ -32,9 +32,16 @@ func (service *ActivityService) LotteryReceive(data *activity.LotteryOperation)
 		if int64(count) > 0 {
 			continue
 		}
-		if fool, _ := util.Exists(util.CODE, "lottery_"+fmt.Sprint(lotteryId)); !fool {
-
-			continue
+		key := "lottery_" + fmt.Sprint(lotteryId)
+		if fool, _ := util.Exists(util.CODE, key); !fool {
+			//缓存处理stockNumber
+			lotteryList := []entity.Lottery{}
+			orm.Table("lottery").Where("id=?", lotteryId).Get(&lotteryList)
+			if len(lotteryList) > 0 {
+				util.PutCKV("other", key, lotteryList[0].StockNumber)
+			} else {
+				continue
+			}
 		}
 		logx.Info(fmt.Sprint(lotteryId) + "Id奖券领取")
 		//每种劵处理
@@ -94,12 +101,12 @@ func (service *ActivityService) LotteryReceive(data *activity.LotteryOperation)
 			}
 		}
 		//6、修改redis余额
-		if util.GetInt(util.CODE, "lottery_"+fmt.Sprint(lotteryId)) <= 0 {
+		if util.GetInt(util.CODE, key) <= 0 {
 			logx.Info("奖券余额不足:", err)
 			orm.Close()
 			continue
 		}
-		if !util.DecrbyLimit(util.CODE, "lottery_"+fmt.Sprint(lotteryId), 1, 0) {
+		if !util.DecrbyLimit(util.CODE, key, 1, 0) {
 			logx.Info("修改redis奖券库存失败:", err)
 			orm.Close()
 			continue
@@ -423,9 +430,16 @@ func (service *ActivityService) LotteryStateChange(data *activity.UpdateStateReq
 	if data.DiscountId != 0 {
 		fool := true
 		discountId := data.DiscountId
+		key := "discount_" + fmt.Sprint(discountId)
 		if fool, _ = util.Exists(code, "discount_"+fmt.Sprint(discountId)); !fool {
-			orm.Rollback()
-			return entity.ErrorCode, "奖卷领取失败"
+			discountList := []entity.Discount{}
+			orm.Table("discount").Where("id=?", discountId).Get(&discountList)
+			if len(discountList) > 0 {
+				util.PutCKV("other", key, discountList[0].StockNumber)
+			} else {
+				orm.Rollback()
+				return entity.ErrorCode, "奖卷领取失败"
+			}
 		}
 		logx.Info(fmt.Sprint(discountId) + "Id奖券领取")
 		//1、查找活动赠品对应关系信息
@@ -456,12 +470,12 @@ func (service *ActivityService) LotteryStateChange(data *activity.UpdateStateReq
 			return entity.ErrorCode, "活动结束不可领劵"
 		}
 		//2、修改redis余额
-		if util.GetInt(code, "discount_"+fmt.Sprint(discountId)) <= 0 {
+		if util.GetInt(code, key) <= 0 {
 			logx.Info("赠品余额不足:", err)
 			orm.Rollback()
 			return entity.ErrorCode, "奖券余额不足"
 		}
-		if !util.DecrbyLimit(code, "discount_"+fmt.Sprint(discountId), 1, 0) {
+		if !util.DecrbyLimit(code, key, 1, 0) {
 			logx.Info("修改redis赠品库存失败:", err)
 			orm.Rollback()
 			return entity.ErrorCode, "修改redis奖券库存失败"

+ 14 - 7
service/activityServiceNew.go

@@ -166,7 +166,7 @@ func (service *ActivityServiceNew) UserAllLottery(in *activity.AllLotteryReq) ma
 	return result
 }
 
-//商品下的活动查询
+// 商品下的活动查询
 func getAllActivity(productCode, userId, appId, userName, userPhone string, receivingLocation int64, orm *xorm.Engine, userGroupMap map[string]bool) []entity.Activity {
 	//1、查找活动信息
 	activityArr := []entity.Activity{}
@@ -277,7 +277,7 @@ func getAllActivity(productCode, userId, appId, userName, userPhone string, rece
 	return activityArr
 }
 
-//判断奖券是否领取过
+// 判断奖券是否领取过
 func isDraw(userId, appId string, lotteryId int64, orm *xorm.Engine) (bool, entity.UserPrize) {
 	userLottery := entity.UserPrize{}
 	//判断手里的奖券是否使用了
@@ -286,7 +286,14 @@ func isDraw(userId, appId string, lotteryId int64, orm *xorm.Engine) (bool, enti
 		return false, userLottery
 	}
 	if fool, _ := util.Exists(util.CODE, "lottery_"+fmt.Sprint(lotteryId)); !fool {
-		return false, userLottery
+		lotteryList := []entity.Lottery{}
+		key := "lottery_" + fmt.Sprint(lotteryId)
+		orm.Table("lottery").Where("id=?", lotteryId).Get(&lotteryList)
+		if len(lotteryList) > 0 {
+			util.PutCKV("other", key, lotteryList[0].StockNumber)
+		} else {
+			return false, userLottery
+		}
 	}
 	logx.Info(fmt.Sprint(lotteryId) + "Id奖券领取")
 	//每种劵处理
@@ -369,7 +376,7 @@ func isDraw(userId, appId string, lotteryId int64, orm *xorm.Engine) (bool, enti
 	return true, userLottery
 }
 
-//领取操作
+// 领取操作
 func drawHandle(userId, appId, userName, userPhone string, lotteryId int64, userLottery entity.UserPrize) {
 	if lotteryId == 0 {
 		return
@@ -409,7 +416,7 @@ func drawHandle(userId, appId, userName, userPhone string, lotteryId int64, user
 	orm.Commit()
 }
 
-//用户下可以使用的奖券
+// 用户下可以使用的奖券
 func availableLottery(orm *xorm.Engine, activityId int64, lotteryIdArr, userId, appId string) []entity.LotteryJson {
 	//查询奖券信息
 	var userLotteryList []entity.LotteryJson
@@ -427,7 +434,7 @@ func availableLottery(orm *xorm.Engine, activityId int64, lotteryIdArr, userId,
 	return userLotteryList
 }
 
-//满折满减预热活动奖券查询
+// 满折满减预热活动奖券查询
 func lotteryWaitHandle(orm *xorm.Engine, lotteryIdArr, userId, appId string, activityId int64, userPrice *[]entity.LotteryJson) []entity.LotteryJson {
 	//var err error
 	//没有商品标识时处理
@@ -447,7 +454,7 @@ func lotteryWaitHandle(orm *xorm.Engine, lotteryIdArr, userId, appId string, act
 	return nil
 }
 
-//商品下所有奖券查询
+// 商品下所有奖券查询
 func (service *ActivityServiceNew) GetAllLottery(in *activity.Request) (int64, string, []entity.Activity) {
 	//1、查找活动信息
 	orm := entity.Engine

+ 18 - 0
test.http

@@ -0,0 +1,18 @@
+POST http://192.168.3.240:8071/jyMarketing/activity/detailActivity
+Content-Type: application/x-www-form-urlencoded
+
+id=1
+
+###
+POST http://localhost:80/api/item
+Content-Type: application/json
+
+{}
+
+###
+POST http://192.168.3.240:8071/jyMarketing/activity/detailActivity?id=1
+Content-Type: application/json
+
+{}
+
+###