Bladeren bron

wip:数据库锁优化

fuwencai 1 jaar geleden
bovenliggende
commit
6d6fc5cade
1 gewijzigde bestanden met toevoegingen van 66 en 35 verwijderingen
  1. 66 35
      src/jfw/modules/subscribepay/src/entity/equityActive.go

+ 66 - 35
src/jfw/modules/subscribepay/src/entity/equityActive.go

@@ -71,30 +71,7 @@ func (e *EquityActive) GiftVip() {
 		equityStockLock.Unlock()
 	}()
 	//3. 判断库存
-	count, eInfo := e.findVipStock(activeId, mold)
-	threshold, b := config.Config.EquityActive.MailAlarm.Threshold[fmt.Sprintf("%v", mold)]
-	if count < 1 || eInfo == nil || len(*eInfo) == 0 {
-		// 没有库存
-		if b {
-			go e.sendAlarmMail(activeName, activeId, threshold.Name, mold, 0)
-		}
-		return
-	}
-	if b && int(count)-1 <= threshold.Value {
-		// 发库存告警时这减去1是因为这一次马上会消耗一个
-		go e.sendAlarmMail(activeName, activeId, threshold.Name, mold, int(count)-1)
-	}
-	eId := common.IntAll((*eInfo)[0]["id"])
-	//4. 更新库存
-	if !e.updateVipStock(eId) {
-		log.Println("equityActive  更新库存信息失败:", eId, e.OrderCode)
-		return
-	}
-	//6. 发送短信、站内信
-	eName := common.ObjToString((*eInfo)[0]["name"])
-	code := common.ObjToString((*eInfo)[0]["code"])
-	exEndTime := common.ObjToString((*eInfo)[0]["ex_end_time"])
-	go e.sendVipMsg(eName, code, exEndTime)
+	e.processEquityInfo(activeName, activeId, mold)
 }
 
 // txActivityStart 判断活动是否开启 开启则返回活动id
@@ -140,15 +117,75 @@ func (e *EquityActive) matchOrder() (ok bool, mold int) {
 	return
 }
 
-// 查看兑换权益码信息
-func (e *EquityActive) findVipStock(activeId, mold int) (count int64, data *[]map[string]interface{}) {
+// 处理库存
+func (e *EquityActive) processEquityInfo(activeName string, activeId, mold int) {
+	count := 0
+	tx, err := util.Mysql.DB.Begin()
+	if err != nil {
+		log.Println("equityActive db Begin:", err)
+		return
+	}
 	countQuery := fmt.Sprintf("SELECT count(*)  FROM %s where state= 0 and mold=? and active_id=? ;", TableEquityInfo)
-	count = util.Mysql.CountBySql(countQuery, mold, activeId)
+	rows, err := tx.Query(countQuery, mold, activeId)
+	if err != nil || rows == nil {
+		log.Println("获取剩余库存数量失败:", err, rows)
+		return
+	}
+	defer rows.Close()
+	for rows.Next() {
+		err = rows.Scan(&count)
+		if err != nil {
+			log.Println("遍历剩余库存数量失败:", err, rows)
+			return
+		}
+	}
 	if count <= 0 {
+		log.Println("库存剩余为0:", countQuery, mold, activeId)
+		return
+	}
+	query := fmt.Sprintf("SELECT id,name,code,date_format(ex_end_time,'%%Y-%%m-%%d') as ex_end_time  FROM %s where state= 0 and mold=? and active_id=? limit 1  for update;", TableEquityInfo)
+	data, err := tx.Query(query, mold, activeId)
+	if err != nil || data == nil {
+		tx.Rollback()
+		log.Println("获取视频权益码信息失败:", err, data)
 		return
 	}
-	query := fmt.Sprintf("SELECT id,name,code,date_format(ex_end_time,'%%Y-%%m-%%d') as ex_end_time  FROM %s where state= 0 and mold=? and active_id=? limit 1 ;", TableEquityInfo)
-	data = util.Mysql.SelectBySql(query, mold, activeId)
+	eId, name, code, exEndTime := 0, "", "", ""
+	defer data.Close()
+	for data.Next() {
+		err = data.Scan(&eId, &name, &code, &exEndTime)
+	}
+	if err != nil {
+		log.Println("获取视频权益码失败:", err, data)
+		tx.Rollback()
+		return
+	}
+	threshold, b := config.Config.EquityActive.MailAlarm.Threshold[fmt.Sprintf("%v", mold)]
+	if count < 1 || eId <= 0 {
+		// 没有库存
+		if b {
+			go e.sendAlarmMail(activeName, activeId, threshold.Name, mold, 0)
+		}
+		return
+	}
+	if b && int(count)-1 <= threshold.Value {
+		// 发库存告警时这减去1是因为这一次消耗一个
+		go e.sendAlarmMail(activeName, activeId, threshold.Name, mold, int(count)-1)
+	}
+	update := fmt.Sprintf("update %s set order_code=?,state=?,update_time=? where id=? ", TableEquityInfo)
+	_, err = tx.Exec(update, e.OrderCode, valueStateGifted, date.NowFormat(date.Date_Full_Layout), eId)
+	if err != nil {
+		tx.Rollback()
+		log.Println("更新视频权益码失败", err)
+		return
+	}
+	err = tx.Commit()
+	if err != nil {
+		log.Println("视频权益码commit失败", err)
+		return
+	}
+	//6. 发送短信、站内信
+	go e.sendVipMsg(name, code, exEndTime)
 	return
 }
 
@@ -178,12 +215,6 @@ func (e *EquityActive) setUserID() {
 	return
 }
 
-// 更新库存
-func (e *EquityActive) updateVipStock(eInfoId int) bool {
-	update := fmt.Sprintf("update %s set order_code=?,state=?,update_time=? where id=? ", TableEquityInfo)
-	return util.Mysql.UpdateOrDeleteBySql(update, e.OrderCode, valueStateGifted, date.NowFormat(date.Date_Full_Layout), eInfoId) > 0
-}
-
 // 发送消息
 func (e *EquityActive) sendVipMsg(eName, code, ex_end_time string) {
 	// 发送短信