cost_by_left_num.go 5.1 KB

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