cost_by_left_num.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. log.Println("1111111", err)
  37. return datas, 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) 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. orderCode := utils.GenerateSimpleToken()
  100. 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
  101. if err != nil {
  102. log.Printf("appID:[%s],productID:[%d] execute insert into interface_order error:[%v]", appID, productID, err)
  103. tx.Rollback()
  104. return err
  105. }
  106. //存历史数据
  107. return nil
  108. })
  109. case 1:
  110. //按条扣费-(每调一次剩余量-len(getDataList))
  111. errs = db.GetSFISDB().Transaction(func(tx *gorm.DB) error {
  112. orderBefore := userProduct.LeftNum
  113. orderAfter := userProduct.LeftNum - dataLen
  114. //扣费
  115. 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
  116. if err != nil {
  117. log.Printf("appID:[%s],productID:[%d] execute cost money error:[%v]", appID, productID, err)
  118. tx.Rollback()
  119. return err
  120. }
  121. //生调用记录
  122. 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
  123. if err != nil {
  124. log.Printf("appID:[%s],productID:[%d] execute insert into user_call_record error:[%v]", appID, productID, err)
  125. tx.Rollback()
  126. return err
  127. }
  128. //生订单
  129. 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
  130. if err != nil {
  131. log.Printf("appID:[%s],productID:[%d] execute insert into interface_order error:[%v]", appID, productID, err)
  132. tx.Rollback()
  133. return err
  134. }
  135. //存历史数据
  136. return nil
  137. })
  138. }
  139. return errs
  140. }