wallet.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package dao
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. "go-common/app/service/live/wallet/model"
  7. "go-common/library/log"
  8. )
  9. const (
  10. _shard = 10
  11. _insWallet = "INSERT IGNORE INTO user_wallet_%d (uid,gold,iap_gold,silver) VALUES(?,?,?,?)"
  12. _selMelonseed = "SELECT uid,gold,iap_gold,silver FROM user_wallet_%d WHERE uid=?"
  13. _selDetail = "SELECT uid,gold,iap_gold,silver,gold_recharge_cnt,gold_pay_cnt,silver_pay_cnt, cost_base FROM user_wallet_%d WHERE uid=?"
  14. _addGold = "INSERT INTO user_wallet_%d(uid,gold) VALUES(?,?) ON DUPLICATE KEY UPDATE gold=gold+%d"
  15. _rechargeGold = "INSERT INTO user_wallet_%d(uid,gold,gold_recharge_cnt) VALUES(?,?,?) ON DUPLICATE KEY UPDATE gold=gold+%d,gold_recharge_cnt=gold_recharge_cnt+%d"
  16. _addIapGold = "INSERT INTO user_wallet_%d(uid,iap_gold) VALUES(?,?) ON DUPLICATE KEY UPDATE iap_gold=iap_gold+%d"
  17. _rechargeIapGold = "INSERT INTO user_wallet_%d(uid,iap_gold,gold_recharge_cnt) VALUES(?,?,?) ON DUPLICATE KEY UPDATE iap_gold=iap_gold+%d,gold_recharge_cnt=gold_recharge_cnt+%d"
  18. _addSilver = "INSERT INTO user_wallet_%d(uid,silver) VALUES(?,?) ON DUPLICATE KEY UPDATE silver=silver+%d"
  19. _consumeGold = "UPDATE user_wallet_%d SET gold=gold-%d,gold_pay_cnt=gold_pay_cnt+%d WHERE uid=?"
  20. _consumeIapGold = "UPDATE user_wallet_%d SET iap_gold=iap_gold-%d,gold_pay_cnt=gold_pay_cnt+%d WHERE uid=?"
  21. _consumeSilver = "UPDATE user_wallet_%d SET silver=silver-%d,silver_pay_cnt=silver_pay_cnt+%d WHERE uid=?"
  22. _changeCosetBase = "UPDATE user_wallet_%d SET cost_base=? WHERE uid=?"
  23. )
  24. func tableIndex(uid int64) int64 {
  25. return uid % _shard
  26. }
  27. // InitExp 初始化用户钱包,用于首次查询
  28. func (d *Dao) InitWallet(c context.Context, uid int64, gold int64, iap_gold int64, silver int64) (row int64, err error) {
  29. res, err := d.db.Exec(c, fmt.Sprintf(_insWallet, tableIndex(uid)), uid, gold, iap_gold, silver)
  30. if err != nil {
  31. log.Error("[dao.wallet|InitWallet] d.db.Exec err: %v", err)
  32. return
  33. }
  34. return res.RowsAffected()
  35. }
  36. // Melonseed 获取瓜子数
  37. func (d *Dao) Melonseed(c context.Context, uid int64) (wallet *model.Melonseed, err error) {
  38. row := d.db.QueryRow(c, fmt.Sprintf(_selMelonseed, tableIndex(uid)), uid)
  39. wallet = &model.Melonseed{}
  40. if err = row.Scan(&wallet.Uid, &wallet.Gold, &wallet.IapGold, &wallet.Silver); err == sql.ErrNoRows {
  41. // 查询结果为空时,初始化数据
  42. _, err = d.InitWallet(c, uid, 0, 0, 0)
  43. }
  44. if err != nil {
  45. log.Error("[dao.wallet|Melonseed] row.Scan err: %v", err)
  46. return
  47. }
  48. return
  49. }
  50. // Detail 详细数据
  51. func (d *Dao) Detail(c context.Context, uid int64) (detail *model.Detail, err error) {
  52. row := d.db.QueryRow(c, fmt.Sprintf(_selDetail, tableIndex(uid)), uid)
  53. detail = &model.Detail{}
  54. err = row.Scan(&detail.Uid, &detail.Gold, &detail.IapGold, &detail.Silver, &detail.GoldRechargeCnt,
  55. &detail.GoldPayCnt, &detail.SilverPayCnt, &detail.CostBase)
  56. if err == sql.ErrNoRows {
  57. detail.Gold = 0
  58. detail.IapGold = 0
  59. detail.Silver = 0
  60. detail.GoldPayCnt = 0
  61. detail.GoldRechargeCnt = 0
  62. detail.SilverPayCnt = 0
  63. detail.CostBase = 0
  64. detail.Uid = uid
  65. err = nil
  66. return
  67. }
  68. if err != nil {
  69. log.Error("[dao.wallet|Detail] row.Scan err: %v", err)
  70. return
  71. }
  72. return
  73. }
  74. func (d *Dao) DetailWithoutDefault(c context.Context, uid int64) (detail *model.Detail, err error) {
  75. row := d.db.QueryRow(c, fmt.Sprintf(_selDetail, tableIndex(uid)), uid)
  76. detail = &model.Detail{}
  77. err = row.Scan(&detail.Uid, &detail.Gold, &detail.IapGold, &detail.Silver, &detail.GoldRechargeCnt,
  78. &detail.GoldPayCnt, &detail.SilverPayCnt, &detail.CostBase)
  79. return
  80. }
  81. // AddGold 添加金瓜子
  82. func (d *Dao) AddGold(c context.Context, uid int64, num int) (affect int64, err error) {
  83. s := fmt.Sprintf(_addGold, tableIndex(uid), num)
  84. return execSqlWithBindParams(d, c, &s, uid, num)
  85. }
  86. // RechargeGold 充值金瓜子 记入充值总值
  87. func (d *Dao) RechargeGold(c context.Context, uid int64, num int) (affect int64, err error) {
  88. s := fmt.Sprintf(_rechargeGold, tableIndex(uid), num, num)
  89. return execSqlWithBindParams(d, c, &s, uid, num, num)
  90. }
  91. // RechargeGold 添加IOS金瓜子
  92. func (d *Dao) AddIapGold(c context.Context, uid int64, num int) (affect int64, err error) {
  93. s := fmt.Sprintf(_addIapGold, tableIndex(uid), num)
  94. return execSqlWithBindParams(d, c, &s, uid, num)
  95. }
  96. // RechargeGold 充值IOS金瓜子 记入充值总值
  97. func (d *Dao) RechargeIapGold(c context.Context, uid int64, num int) (affect int64, err error) {
  98. s := fmt.Sprintf(_rechargeIapGold, tableIndex(uid), num, num)
  99. return execSqlWithBindParams(d, c, &s, uid, num, num)
  100. }
  101. // AddSilver 添加银瓜子
  102. func (d *Dao) AddSilver(c context.Context, uid int64, num int) (affect int64, err error) {
  103. s := fmt.Sprintf(_addSilver, tableIndex(uid), num)
  104. return execSqlWithBindParams(d, c, &s, uid, num)
  105. }
  106. // AddSilver 消费金瓜子 记入消费总值
  107. func (d *Dao) ConsumeGold(c context.Context, uid int64, num int) (affect int64, err error) {
  108. s := fmt.Sprintf(_consumeGold, tableIndex(uid), num, num)
  109. return execSqlWithBindParams(d, c, &s, uid)
  110. }
  111. // AddSilver 消费IOS金瓜子 记入消费总值
  112. func (d *Dao) ConsumeIapGold(c context.Context, uid int64, num int) (affect int64, err error) {
  113. s := fmt.Sprintf(_consumeIapGold, tableIndex(uid), num, num)
  114. return execSqlWithBindParams(d, c, &s, uid)
  115. }
  116. // ConsumeSilver 消费银瓜子 记入消费总值
  117. func (d *Dao) ConsumeSilver(c context.Context, uid int64, num int) (affect int64, err error) {
  118. s := fmt.Sprintf(_consumeSilver, tableIndex(uid), num, num)
  119. return execSqlWithBindParams(d, c, &s, uid)
  120. }
  121. func (d *Dao) ChangeCostBase(c context.Context, uid int64, num int64) (affect int64, err error) {
  122. s := fmt.Sprintf(_changeCosetBase, tableIndex(uid))
  123. return execSqlWithBindParams(d, c, &s, num, uid)
  124. }
  125. // 更新镜像时间 方便测试
  126. func (d *Dao) UpdateSnapShotTime(context context.Context, uid int64, time string) (int64, error) {
  127. s := fmt.Sprintf("update user_wallet_%d set snapshot_time = ?", tableIndex(uid))
  128. return execSqlWithBindParams(d, context, &s, time)
  129. }