api_util.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package utils
  2. import (
  3. // "context"
  4. "fmt"
  5. "log"
  6. "sfbase/global"
  7. "sfbase/redis"
  8. "sfis/db"
  9. "sfis/lock"
  10. "sfis/model"
  11. "sfis/model/response"
  12. "strings"
  13. "time"
  14. "github.com/gin-gonic/gin"
  15. "go.uber.org/zap"
  16. )
  17. func Check(appID string, productID int, c *gin.Context, getData func() ([]map[string]interface{}, int, error), param, ip string) {
  18. lock.MainLock.Lock()
  19. userLock := lock.UserLockMap[appID]
  20. lock.MainLock.Unlock()
  21. var err error
  22. datas := []map[string]interface{}{}
  23. orderCode := ""
  24. errStr := ""
  25. /**
  26. 第二步:用户接口产品校验-加锁处理
  27. */
  28. //2.1 取用户接口状态校验-可加锁也可不加锁 这个没有太严谨
  29. //userLock.Lock()
  30. userProduct := &model.UserProduct{}
  31. db.GetSFISDB().First(userProduct, &model.UserProduct{AppID: appID, ProductID: productID})
  32. //userLock.Unlock()
  33. if userProduct.ID == 0 {
  34. response.FailWithDetailed(response.InterfaceDeleted, nil, "该用户接口未购买", c)
  35. return
  36. } else if userProduct.InterfaceStatus != 0 {
  37. response.FailWithDetailed(response.InterfaceDeleted, nil, "该用户接口暂不提供服务", c)
  38. return
  39. }
  40. //2.2 取用户(产品余量|钱包账户余额)校验-必须加锁
  41. costModel := userProduct.CostModel //扣费模式 0扣余量,1-扣余额
  42. product := GetProductByID(productID)
  43. userLock.Lock()
  44. log.Println(param + "锁住......")
  45. //
  46. // ctx, _ := context.WithTimeout(c, 20*time.Second)
  47. // go func() {
  48. // var i = 0
  49. // for {
  50. // select {
  51. // case <-ctx.Done():
  52. // userLock.Unlock()
  53. // return
  54. // case <-time.After(time.Second * time.Duration(2)):
  55. // i++
  56. // if i == 10 {
  57. // userLock.Unlock()
  58. // return
  59. // }
  60. // continue
  61. // }
  62. // }
  63. // }()
  64. // time.Sleep(time.Second * time.Duration(25))
  65. //
  66. db.GetSFISDB().First(userProduct, &model.UserProduct{AppID: appID, ProductID: productID})
  67. // costModel = 0
  68. switch costModel {
  69. case 0:
  70. //按剩余量扣费
  71. datas, orderCode, err, errStr = costByLeftNum(getData, appID, productID, userProduct, product, param, ip)
  72. case 1:
  73. //按账户钱包余额扣费
  74. datas, orderCode, err, errStr = costByAccountBalance(getData, appID, productID, userProduct, product, param, ip)
  75. case 2:
  76. //优先扣剩余量,剩余量为0,扣钱包余额
  77. }
  78. userLock.Unlock()
  79. log.Println(param + "解锁......")
  80. if err == nil {
  81. db.GetQyfw().Save("user_data", map[string]interface{}{
  82. "app_id": appID,
  83. "result_num": len(datas),
  84. "result_content": datas,
  85. "order_code": orderCode,
  86. "status": 200,
  87. "user_product_id": userProduct.ID,
  88. "create_at": time.Now().Unix(),
  89. })
  90. limittodaykey := fmt.Sprintf("limittoday_%d_%d_%s", time.Now().Day(), userProduct.ProductID, userProduct.AppID)
  91. redis.Incr("limit", limittodaykey)
  92. response.FailWithDetailed(response.SUCCESS, datas, "OK", c)
  93. } else {
  94. if strings.Contains(errStr, "不足") {
  95. response.FailWithDetailed(response.LeftNumEmpty, nil, errStr, c)
  96. } else {
  97. global.Logger.Error("数据库操作失败", zap.Any("error:", err))
  98. response.FailWithDetailed(response.QueryError, nil, "内部错误", c)
  99. }
  100. }
  101. }