|
@@ -126,25 +126,45 @@ var proxyHandler = func(r *ghttp.Request) {
|
|
}
|
|
}
|
|
if isCharging {
|
|
if isCharging {
|
|
if db.GateWatMySql.ExecTx("计费处理", func(tx *sql.Tx) bool {
|
|
if db.GateWatMySql.ExecTx("计费处理", func(tx *sql.Tx) bool {
|
|
|
|
+ deductMode := 2 //赠送扣除
|
|
appid := r.GetQuery("appid").String()
|
|
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
|
|
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 {
|
|
if datas == nil || len(*datas) == 0 {
|
|
log.WithContext(r.Context()).Error(appid, "没有找到该账户信息")
|
|
log.WithContext(r.Context()).Error(appid, "没有找到该账户信息")
|
|
return false
|
|
return false
|
|
}
|
|
}
|
|
//余额不足
|
|
//余额不足
|
|
amount := gconv.Int64((*datas)[0]["amount"])
|
|
amount := gconv.Int64((*datas)[0]["amount"])
|
|
- if amount < 0 {
|
|
|
|
|
|
+ giftCount := gconv.Int64((*datas)[0]["count"])
|
|
|
|
+ if amount < 0 && giftCount < 0 {
|
|
status = -1
|
|
status = -1
|
|
log.WithContext(r.Context()).Error(appid, "余额不足")
|
|
log.WithContext(r.Context()).Error(appid, "余额不足")
|
|
return false
|
|
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 {
|
|
if logRes1 <= 0 || logRes2 <= 0 {
|
|
return false
|
|
return false
|
|
}
|
|
}
|