wallet.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/app/job/live/wallet/model"
  6. "go-common/library/log"
  7. )
  8. const (
  9. _shard = 10
  10. _insWallet = "INSERT IGNORE INTO user_wallet_%d(uid,gold,iap_gold,silver) VALUES(?,?,?,?)"
  11. _mergeWallet = "INSERT INTO user_wallet_%d(uid,gold,iap_gold,silver) VALUES(?,?,?,?) ON DUPLICATE KEY UPDATE gold=gold+%d,iap_gold=iap_gold+%d,silver=silver+%d"
  12. )
  13. func tableIndex(uid int64) int64 {
  14. return uid % _shard
  15. }
  16. func (d *Dao) InitWallet(c context.Context, user *model.User) (row int64, err error) {
  17. res, err := d.db.Exec(c, fmt.Sprintf(_insWallet, tableIndex(user.Uid)), user.Uid, user.Gold, user.IapGold, user.Silver)
  18. if err != nil {
  19. log.Error("[dao.wallet|InitWallet] d.db.Exec err: %v {uid:%d gold:%d iap_gold:%d silver:%d}", err, user.Uid, user.Gold, user.IapGold, user.Silver)
  20. return
  21. }
  22. return res.RowsAffected()
  23. }
  24. func (d *Dao) MergeWallet(c context.Context, uid int64, gold int64, iap_gold int64, silver int64) (row int64, err error) {
  25. res, err := d.db.Exec(c, fmt.Sprintf(_mergeWallet, tableIndex(uid), gold, iap_gold, silver), uid, gold, iap_gold, silver)
  26. if err != nil {
  27. log.Error("[dao.wallet|MergeWallet] d.db.Exec err: %v {uid:%d gold:%d iap_gold:%d silver:%d}", err, uid, gold, iap_gold, silver)
  28. return
  29. }
  30. return res.RowsAffected()
  31. }