userRecharge.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package service
  2. import (
  3. "sfbase/global"
  4. "sfis/db"
  5. "sfis/lock"
  6. "sfis/model"
  7. "sfis/model/response"
  8. "github.com/gin-gonic/gin"
  9. "log"
  10. "sfis/utils"
  11. "time"
  12. "go.uber.org/zap"
  13. "gorm.io/gorm"
  14. )
  15. func MoneyRecharge(appid string, money int, context *gin.Context) {
  16. //取出用户锁
  17. lock.MainLock.Lock()
  18. userLock := lock.UserLockMap[appid]
  19. lock.MainLock.Unlock()
  20. userLock.Lock()
  21. defer userLock.Unlock()
  22. //查用户当前余额
  23. userAccount := &model.UserAccount{}
  24. db.GetSFISDB().First(userAccount, &model.UserAccount{AppID: appid})
  25. errs := db.GetSFISDB().Transaction(func(tx *gorm.DB) error {
  26. moneyBefore := userAccount.Money
  27. moneyAfter := userAccount.Money + money
  28. //充值
  29. err := tx.Exec("update user_account set money = ? WHERE `app_id` = ?", moneyAfter, appid).Error
  30. if err != nil {
  31. log.Printf("appID:[%s],money:[%d] execute cost user_account error:[%v]", appid, moneyAfter, err)
  32. tx.Rollback()
  33. return err
  34. }
  35. //生充值记录
  36. err = tx.Exec("insert into user_money_record (app_id,`before`,`after`,trade_money) values (?,?,?,?)", appid, moneyBefore, moneyAfter, money).Error
  37. if err != nil {
  38. log.Printf("appID:[%s],trade_money:[%d] execute insert into user_money_record error:[%v]", appid, money, err)
  39. tx.Rollback()
  40. return err
  41. }
  42. return nil
  43. })
  44. if errs == nil {
  45. response.Ok(context)
  46. } else {
  47. global.Logger.Error("数据库操作失败", zap.Any("error:", errs))
  48. response.FailWithMessage("充值失败", context)
  49. }
  50. }
  51. func ProductRecharge(appid string, productId, rechargeNum int, endTime string, context *gin.Context) {
  52. //取出用户锁
  53. lock.MainLock.Lock()
  54. userLock := lock.UserLockMap[appid]
  55. lock.MainLock.Unlock()
  56. userLock.Lock()
  57. defer userLock.Unlock()
  58. endTimes := ""
  59. if endTime != "" {
  60. end := endTime + " 23:59:59"
  61. loc, _ := time.LoadLocation("Local")
  62. if ends, err := time.ParseInLocation("2006-01-02 15:04:05", end, loc); err == nil {
  63. endTimes = ends.Local().Format("2006-01-02 15:04:05")
  64. }
  65. }
  66. //查用户当前剩余量
  67. userProduct := &model.UserProduct{}
  68. db.GetSFISDB().First(userProduct, &model.UserProduct{AppID: appid, ProductID: productId})
  69. product := &model.Product{}
  70. if products, ok := utils.ProductCaches.Map.Load(productId); ok {
  71. product = products.(*model.Product)
  72. }
  73. errs := db.GetSFISDB().Transaction(func(tx *gorm.DB) error {
  74. before := userProduct.LeftNum
  75. after := userProduct.LeftNum + rechargeNum
  76. //充值
  77. var err error
  78. if endTimes != "" {
  79. err = tx.Exec("update user_product set left_num = ?,end_at = ? WHERE `app_id` = ? and product_id = ?", after, endTimes, appid, productId).Error
  80. if err != nil {
  81. log.Printf("appID:[%s],left_num:[%d],endtime:[%s] execute cost user_product error:[%v]", appid, after, endTimes, err)
  82. tx.Rollback()
  83. return err
  84. }
  85. } else {
  86. err = tx.Exec("update user_product set left_num = ? WHERE `app_id` = ? and product_id = ?", after, appid, productId).Error
  87. if err != nil {
  88. log.Printf("appID:[%s],left_num:[%d] execute cost user_product error:[%v]", appid, after, err)
  89. tx.Rollback()
  90. return err
  91. }
  92. }
  93. //生购买记录
  94. err = tx.Exec("insert into user_buy_record (app_id,product_id,user_product_id,`before`,`after`,trade_money,buy_type,history_unit_price) values (?,?,?,?,?,?,?,?)", appid, productId, userProduct.ID, before, after, product.UnitPrice*rechargeNum, 1, product.UnitPrice).Error
  95. if err != nil {
  96. log.Printf("appID:[%s],product_id:[%d],user_product_id:[%d],after:[%d],trade_money:[%d] execute insert into user_buy_record error:[%v]", appid, productId, userProduct.ID, after, product.UnitPrice*rechargeNum, err)
  97. tx.Rollback()
  98. return err
  99. }
  100. return nil
  101. })
  102. if errs == nil {
  103. response.Ok(context)
  104. } else {
  105. global.Logger.Error("数据库操作失败", zap.Any("error:", errs))
  106. response.FailWithMessage("充值失败", context)
  107. }
  108. }