cost_by_left_num.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package utils
  2. import (
  3. "errors"
  4. "fmt"
  5. "log"
  6. "sfbase/global"
  7. "sfbase/utils"
  8. "sfis/db"
  9. "sfis/model"
  10. "time"
  11. "gorm.io/gorm"
  12. )
  13. /**
  14. 扣产品剩余量
  15. */
  16. func costByLeftNum(getData func() ([]map[string]interface{}, int, error), appID string, productID int, userProduct *model.UserProduct, product *model.Product, param, ip string) ([]map[string]interface{}, error) {
  17. productType := product.ProductType
  18. datas := []map[string]interface{}{}
  19. var err error
  20. beforeJudge := before(productType, userProduct)
  21. if beforeJudge {
  22. data, statusCode, _ := execute(getData, appID, productID)
  23. err = after(productType, len(data), userProduct, statusCode, param, ip)
  24. if err == nil && data != nil && len(data) > 0 {
  25. datas = data
  26. } else {
  27. if err != nil {
  28. global.Logger.Error("数据库操作失败", getUserProductError(appID, productID, err)...)
  29. } else {
  30. global.Logger.Info("无数据", getUserProductError(appID, productID, err)...)
  31. }
  32. }
  33. } else {
  34. err = errors.New("剩余量不足")
  35. }
  36. return datas, err
  37. }
  38. /**
  39. 用户产品剩余量|账户余额判断
  40. */
  41. func before(productType int, userProduct *model.UserProduct) bool {
  42. switch productType {
  43. case 0:
  44. //按次扣费-判断left_num
  45. if userProduct.LeftNum == 0 {
  46. //response.FailWithDetailed(response.LeftNumEmpty, nil, "余额不足", context)
  47. return false
  48. }
  49. case 1:
  50. //按条扣费-判断单次调用的limit
  51. if userProduct.LeftNum < userProduct.DataNumLimitOneTimes {
  52. //response.FailWithDetailed(response.LeftNumEmpty, nil, "剩余数据量小于该产品每次返回数据量,可能造成数据量不准确", context)
  53. return false
  54. }
  55. }
  56. return true
  57. }
  58. /**
  59. 执行接口查询
  60. */
  61. func execute(getData func() ([]map[string]interface{}, int, error), appID string, productID int) ([]map[string]interface{}, int, error) {
  62. data, statusCode, err := getData()
  63. if err != nil {
  64. global.Logger.Error("调用数据出错:", getUserProductError(appID, productID, err)...)
  65. return nil, -1, err
  66. }
  67. dataLen := len(data)
  68. global.Logger.Info("调用成功:", getUserProductInfo(appID, productID, "数据长度", dataLen)...)
  69. return data, statusCode, nil
  70. }
  71. func after(productType int, dataLen int, userProduct *model.UserProduct, statusCode int, param, ip string) error {
  72. appID := userProduct.AppID
  73. productID := userProduct.ProductID
  74. userProductID := userProduct.ID
  75. var errs error
  76. orderCode := fmt.Sprint(time.Now().Year()) + fmt.Sprint(utils.GetRandom(8))
  77. switch productType {
  78. case 0:
  79. //按次扣费-(每调一次剩余量-1)
  80. errs = db.GetSFISDB().Transaction(func(tx *gorm.DB) error {
  81. orderBefore := userProduct.LeftNum
  82. orderAfter := userProduct.LeftNum - 1
  83. //扣费
  84. err := tx.Exec("update user_product set left_num = IF(`left_num`<1, 0, `left_num`-1) WHERE `app_id` = ? and product_id=?", appID, productID).Error
  85. if err != nil {
  86. log.Printf("appID:[%s],productID:[%d] execute cost left_num-1 error:[%v]", appID, productID, err)
  87. tx.Rollback()
  88. return err
  89. }
  90. //生调用记录
  91. err = tx.Exec("insert into user_call_record (app_id,user_product_id,status,ip,param,order_code) values (?,?,?,?,?,?)", appID, userProductID, statusCode, ip, param, orderCode).Error
  92. if err != nil {
  93. log.Printf("appID:[%s],productID:[%d] execute insert into user_call_record error:[%v]", appID, productID, err)
  94. tx.Rollback()
  95. return err
  96. }
  97. //生订单
  98. err = tx.Exec("insert into interface_order (order_code,app_id,user_product_id,`before`,`after`,cost_model,trade_num) values (?,?,?,?,?,?,?)", orderCode, appID, userProductID, orderBefore, orderAfter, 0, 1).Error
  99. if err != nil {
  100. log.Printf("appID:[%s],productID:[%d] execute insert into interface_order error:[%v]", appID, productID, err)
  101. tx.Rollback()
  102. return err
  103. }
  104. //存历史数据
  105. return nil
  106. })
  107. case 1:
  108. //按条扣费-(每调一次剩余量-len(getDataList))
  109. errs = db.GetSFISDB().Transaction(func(tx *gorm.DB) error {
  110. orderBefore := userProduct.LeftNum
  111. orderAfter := userProduct.LeftNum - dataLen
  112. //扣费
  113. err := tx.Exec("update user_product set left_num = IF(`left_num`<1, 0, `left_num`-?) WHERE `app_id` = ? and product_id=?", dataLen, appID, productID).Error
  114. if err != nil {
  115. log.Printf("appID:[%s],productID:[%d] execute cost money error:[%v]", appID, productID, err)
  116. tx.Rollback()
  117. return err
  118. }
  119. //生调用记录
  120. err = tx.Exec("insert into user_call_record (app_id,user_product_id,status,ip,param,order_code) values (?,?,?,?,?,?)", appID, userProductID, statusCode, ip, param, orderCode).Error
  121. if err != nil {
  122. log.Printf("appID:[%s],productID:[%d] execute insert into user_call_record error:[%v]", appID, productID, err)
  123. tx.Rollback()
  124. return err
  125. }
  126. //生订单
  127. err = tx.Exec("insert into interface_order (order_code,app_id,user_product_id,`before`,`after`,cost_model,trade_num) values (?,?,?,?,?,?,?)", orderCode, appID, userProductID, orderBefore, orderAfter, 0, dataLen).Error
  128. if err != nil {
  129. log.Printf("appID:[%s],productID:[%d] execute insert into interface_order error:[%v]", appID, productID, err)
  130. tx.Rollback()
  131. return err
  132. }
  133. //存历史数据
  134. return nil
  135. })
  136. }
  137. return errs
  138. }