소스 검색

feat:xiugai

wangchuanjin 6 달 전
부모
커밋
ce2fcfaf88
2개의 변경된 파일31개의 추가작업 그리고 8개의 파일을 삭제
  1. 5 2
      core/proxy/middleware/filterFuncs.go
  2. 26 6
      core/proxy/proxyServer.go

+ 5 - 2
core/proxy/middleware/filterFuncs.go

@@ -47,7 +47,10 @@ func filterBefore(r *ghttp.Request) error {
 	} else if sign == "" {
 		return NewErrorWithCode(GLOBAL_ERR_MISSPARAM, "sign")
 	}
-	users := db.GateWatMySql.SelectBySql(`select * from user where appid=?`, appid)
+	users := db.GateWatMySql.SelectBySql(`SELECT a.secretkey,a.amount,b.count FROM USER a LEFT JOIN (
+		SELECT a.id,b.count FROM USER a INNER JOIN gift b ON (a.appid= AND a.id=b.user_id)
+		INNER JOIN front_proxy c ON (c.url=? AND b.front_proxy_id=c.id)
+		) b ON (a.id=b.id) WHERE a.appid=?`, appid, url_, appid)
 	if users == nil || len(*users) == 0 {
 		return NewErrorWithCode(GLOBAL_ERR_INVALIDPARAM, "appid")
 	}
@@ -62,7 +65,7 @@ func filterBefore(r *ghttp.Request) error {
 		return NewErrorWithCode(GLOBAL_ERR_ERR)
 	} else if gCtx.RouterRule.Price <= 0 {
 		return nil
-	} else if amount := gconv.Int64((*users)[0]["amount"]); amount-gCtx.RouterRule.Price < 0 {
+	} else if amount, giftCount := gconv.Int64((*users)[0]["amount"]), gconv.Int64((*users)[0]["count"]); amount-gCtx.RouterRule.Price < 0 && giftCount <= 0 {
 		return NewErrorWithCode(GLOBAL_ERR_NOBALANCE, fmt.Sprint(amount))
 	}
 	return nil

+ 26 - 6
core/proxy/proxyServer.go

@@ -126,25 +126,45 @@ var proxyHandler = func(r *ghttp.Request) {
 		}
 		if isCharging {
 			if db.GateWatMySql.ExecTx("计费处理", func(tx *sql.Tx) bool {
+				deductMode := 2 //赠送扣除
 				appid := r.GetQuery("appid").String()
-				updateRes := db.GateWatMySql.UpdateOrDeleteBySqlByTx(tx, `update user set amount=amount-? where appid=?`, gCtx.RouterRule.Price, appid)
-				if updateRes <= 0 {
-					log.WithContext(r.Context()).Error(appid, "更新计费失败")
+				giftUpdate := db.GateWatMySql.UpdateOrDeleteBySqlByTx(tx, `UPDATE USER a INNER JOIN gift b ON (a.appid=? AND a.id=b.user_id and b.count>0)
+					INNER JOIN front_proxy c ON (c.url=? AND b.front_proxy_id=c.id)
+					SET b.count=b.count-1`, appid, gCtx.RouterRule.ReqUrl)
+				if giftUpdate < 0 {
+					log.WithContext(r.Context()).Error(appid, "更新赠送扣除计费失败")
 					return false
+				} else if giftUpdate == 0 {
+					updateRes := db.GateWatMySql.UpdateOrDeleteBySqlByTx(tx, `update user set amount=amount-? where appid=?`, gCtx.RouterRule.Price, appid)
+					if updateRes <= 0 {
+						log.WithContext(r.Context()).Error(appid, "更新金额扣除计费失败")
+						return false
+					}
+					deductMode = 1
 				}
-				datas := db.GateWatMySql.SelectBySqlByTx(tx, `select amount from user where appid=?`, appid)
+				datas := db.GateWatMySql.SelectBySqlByTx(tx, `SELECT a.amount,b.count FROM USER a LEFT JOIN (
+					SELECT a.id,b.count FROM USER a INNER JOIN gift b ON (a.appid=? AND a.id=b.user_id)
+					INNER JOIN front_proxy c ON (c.url=? AND b.front_proxy_id=c.id)
+					) b ON (a.id=b.id) WHERE a.appid=?`, appid, gCtx.RouterRule.ReqUrl, appid)
 				if datas == nil || len(*datas) == 0 {
 					log.WithContext(r.Context()).Error(appid, "没有找到该账户信息")
 					return false
 				}
 				//余额不足
 				amount := gconv.Int64((*datas)[0]["amount"])
-				if amount < 0 {
+				giftCount := gconv.Int64((*datas)[0]["count"])
+				if amount < 0 && giftCount < 0 {
 					status = -1
 					log.WithContext(r.Context()).Error(appid, "余额不足")
 					return false
 				}
-				logRes1, logRes2 := db.GateWatMySql.InsertBatchByTx(tx, "log", []string{"appid", "url", "param", "result", "price", "surplus", "ip", "create_time"}, []interface{}{appid, gCtx.RouterRule.ReqUrl, string(reqBody), result, gCtx.RouterRule.Price, amount, r.GetClientIp(), gtime.Now().String()})
+				var surplus int64
+				if deductMode == 1 {
+					surplus = amount
+				} else {
+					surplus = giftCount
+				}
+				logRes1, logRes2 := db.GateWatMySql.InsertBatchByTx(tx, "log", []string{"appid", "url", "param", "result", "price", "surplus", "mode", "ip", "create_time"}, []interface{}{appid, gCtx.RouterRule.ReqUrl, string(reqBody), result, gCtx.RouterRule.Price, surplus, deductMode, r.GetClientIp(), gtime.Now().String()})
 				if logRes1 <= 0 || logRes2 <= 0 {
 					return false
 				}