|
@@ -0,0 +1,67 @@
|
|
|
+package utils
|
|
|
+
|
|
|
+import (
|
|
|
+ "github.com/gin-gonic/gin"
|
|
|
+ "gorm.io/gorm"
|
|
|
+ "sfis/db"
|
|
|
+ "sfis/lock"
|
|
|
+ "sfis/model"
|
|
|
+ "sfis/model/response"
|
|
|
+)
|
|
|
+
|
|
|
+func Check(appID string, productID int, context *gin.Context, getData func() []interface{}) {
|
|
|
+ if userLock := lock.GetUserLock(appID); userLock != nil {
|
|
|
+ /**
|
|
|
+ 第二步:用户接口产品校验-加锁处理
|
|
|
+ */
|
|
|
+ //2.1 取用户接口状态校验
|
|
|
+ //userLock.Lock()
|
|
|
+ userProduct := &model.UserProduct{}
|
|
|
+ db.GetSFISDB().First(userProduct, &model.UserProduct{AppID: appID, ProductID: productID})
|
|
|
+ //userLock.Unlock()
|
|
|
+ if userProduct.InterfaceStatus != 0 {
|
|
|
+ response.FailWithDetailed(response.InterfaceDeleted, nil, "该用户接口暂不提供服务", context)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ //2.2 取用户 产品余量|钱包账户余额 校验
|
|
|
+ costModel := userProduct.CostModel
|
|
|
+ product := GetProductByID(productID)
|
|
|
+ productType := product.ProductType
|
|
|
+ userLock.Lock()
|
|
|
+ db.GetSFISDB().First(userProduct, &model.UserProduct{AppID: appID, ProductID: productID})
|
|
|
+ switch costModel {
|
|
|
+ case 0:
|
|
|
+ //按剩余量扣费
|
|
|
+ if productType == 0 {
|
|
|
+ //按次扣费-每调一次 剩余量-1
|
|
|
+ if userProduct.LeftNum == 0 {
|
|
|
+ response.FailWithDetailed(response.LeftNumEmpty, nil, "余额不足", context)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ db.GetSFISDB().Transaction(func(tx *gorm.DB) error {
|
|
|
+ //扣费
|
|
|
+ tx.Exec("update user_product set left_num = IF(`left_num`<1, 0, `left_num`-1) WHERE `app_id` = ? and product_id=?", appID, productID)
|
|
|
+ //生调用记录
|
|
|
+ //生订单
|
|
|
+ //存历史数据
|
|
|
+ return nil
|
|
|
+ })
|
|
|
+
|
|
|
+ } else if productType == 1 {
|
|
|
+ //按条扣费-每调一次剩余量-len(getDataList)
|
|
|
+ }
|
|
|
+ case 1:
|
|
|
+ //按账户钱包余额扣费
|
|
|
+ if productType == 0 {
|
|
|
+ //按次扣费-每调一次
|
|
|
+ //todo 账户余额表user_account的余额 减去 product单价*1
|
|
|
+ } else if productType == 1 {
|
|
|
+ //按条扣费-每调一次
|
|
|
+ //todo 账户余额表user_account的余额 减去 product单价*len(getDataList)
|
|
|
+ }
|
|
|
+ case 2:
|
|
|
+ //优先扣剩余量,剩余量为0,扣钱包余额
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|