dao.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. package dao
  2. import (
  3. "context"
  4. "go-common/app/service/live/wallet/conf"
  5. "go-common/app/service/live/wallet/model"
  6. "go-common/library/cache/memcache"
  7. "go-common/library/cache/redis"
  8. xsql "go-common/library/database/sql"
  9. "go-common/library/ecode"
  10. "go-common/library/log"
  11. httpx "go-common/library/net/http/blademaster"
  12. "go-common/library/queue/databus"
  13. "math/rand"
  14. "time"
  15. )
  16. type Dao struct {
  17. c *conf.Config
  18. mc *memcache.Pool
  19. db *xsql.DB
  20. redis *redis.Pool
  21. cacheExpire int32
  22. httpClient *httpx.Client
  23. changeDataBus *databus.Databus
  24. }
  25. // New init mysql db
  26. func New(c *conf.Config) (dao *Dao) {
  27. dao = &Dao{
  28. c: c,
  29. mc: memcache.NewPool(c.Memcache.Wallet),
  30. db: xsql.NewMySQL(c.DB.Wallet),
  31. redis: redis.NewPool(c.Redis.Wallet),
  32. cacheExpire: c.WalletExpire,
  33. httpClient: httpx.NewClient(c.HTTPClient),
  34. changeDataBus: databus.New(c.DataBus.Change),
  35. }
  36. return
  37. }
  38. // Close close the resource.
  39. func (d *Dao) Close() {
  40. d.mc.Close()
  41. d.db.Close()
  42. d.redis.Close()
  43. d.changeDataBus.Close()
  44. }
  45. // Ping dao ping
  46. func (d *Dao) Ping(c context.Context) (err error) {
  47. if err = d.db.Ping(c); err != nil {
  48. log.Error("PingDb error(%v)", err)
  49. return
  50. }
  51. if err = d.pingMC(c); err != nil {
  52. return err
  53. }
  54. return d.PingRedis(c)
  55. }
  56. // pingMc ping
  57. func (d *Dao) pingMC(c context.Context) (err error) {
  58. item := &memcache.Item{
  59. Key: "ping",
  60. Value: []byte{1},
  61. Expiration: d.cacheExpire,
  62. }
  63. conn := d.mc.Get(c)
  64. err = conn.Set(item)
  65. conn.Close()
  66. if err != nil {
  67. log.Error("PingMemcache conn.Set(%v) error(%v)", item, err)
  68. }
  69. return
  70. }
  71. func (d *Dao) PingRedis(c context.Context) (err error) {
  72. var conn = d.redis.Get(c)
  73. _, err = conn.Do("SET", "PING", "PONG")
  74. conn.Close()
  75. return
  76. }
  77. // 通过提供的sql和bind来更新,没有实际业务意义,只是为了少写重复代码
  78. func execSqlWithBindParams(d *Dao, c context.Context, sql *string, bindParams ...interface{}) (affect int64, err error) {
  79. res, err := d.db.Exec(c, *sql, bindParams...)
  80. if err != nil {
  81. log.Error("db.Exec(%s) error(%v)", *sql, err)
  82. return
  83. }
  84. return res.RowsAffected()
  85. }
  86. func randomString(l int) string {
  87. str := "0123456789abcdefghijklmnopqrstuvwxyz"
  88. bytes := []byte(str)
  89. result := []byte{}
  90. r := rand.New(rand.NewSource(time.Now().UnixNano()))
  91. for i := 0; i < l; i++ {
  92. result = append(result, bytes[r.Intn(len(bytes))])
  93. }
  94. return string(result)
  95. }
  96. func (d *Dao) GetDetailByCache(c context.Context, uid int64) (wallet *model.Detail, err error) {
  97. mcDetail, err := d.WalletCache(c, uid)
  98. if err == ecode.ServerErr {
  99. return
  100. }
  101. if err == nil {
  102. if d.IsNewVersion(c, mcDetail) {
  103. wallet = mcDetail.Detail
  104. return
  105. } else {
  106. log.Info("user wallet hit but version old uid: %d", uid)
  107. }
  108. }
  109. if wallet, err = d.Detail(c, uid); err == nil {
  110. d.SetWalletCache(c, &model.McDetail{Detail: wallet, Exist: true, Version: d.CacheVersion(c)}, 86400)
  111. }
  112. return
  113. }
  114. // Melonseed 获取瓜子数
  115. func (d *Dao) GetMelonseedByCache(c context.Context, uid int64) (wallet *model.Melonseed, err error) {
  116. detail, err := d.GetDetailByCache(c, uid)
  117. wallet = new(model.Melonseed)
  118. if err == nil {
  119. wallet = &model.Melonseed{
  120. Uid: detail.Uid,
  121. Gold: detail.Gold,
  122. IapGold: detail.IapGold,
  123. Silver: detail.Silver,
  124. }
  125. }
  126. return
  127. }
  128. func (d *Dao) ModifyCoin(c context.Context, coinNum int, uid int64, coinTypeNo int32, delCache bool) (bool, error) {
  129. var (
  130. affect int64
  131. err error
  132. res bool
  133. )
  134. switch coinTypeNo {
  135. case model.SysCoinTypeIosGold:
  136. affect, err = d.AddIapGold(c, uid, coinNum)
  137. case model.SysCoinTypeSilver:
  138. affect, err = d.AddSilver(c, uid, coinNum)
  139. case model.SysCoinTypeGold:
  140. affect, err = d.AddGold(c, uid, coinNum)
  141. default:
  142. // do nothing
  143. }
  144. if affect > 0 {
  145. res = true
  146. if delCache {
  147. d.DelWalletCache(c, uid)
  148. }
  149. }
  150. return res, err
  151. }
  152. func (d *Dao) GetCoin(c context.Context, coinTypeNo int32, uid int64) (interface{}, error) {
  153. userCoin, err := d.Melonseed(c, uid)
  154. switch coinTypeNo {
  155. case model.SysCoinTypeIosGold:
  156. return userCoin.IapGold, err
  157. case model.SysCoinTypeGold:
  158. return userCoin.Gold, err
  159. case model.SysCoinTypeSilver:
  160. return userCoin.Silver, err
  161. case model.SysCoinTypeMetal:
  162. metal, err := d.GetMetal(c, uid)
  163. return metal, err
  164. default:
  165. return nil, nil
  166. }
  167. }
  168. func (d *Dao) RechargeCoin(c context.Context, coinNum int, uid int64, coinTypeNo int32, delCache bool) (bool, error) {
  169. var (
  170. affect int64
  171. err error
  172. res bool
  173. )
  174. switch coinTypeNo {
  175. case model.SysCoinTypeIosGold:
  176. affect, err = d.RechargeIapGold(c, uid, coinNum)
  177. case model.SysCoinTypeSilver:
  178. affect, err = d.AddSilver(c, uid, coinNum)
  179. case model.SysCoinTypeGold:
  180. affect, err = d.RechargeGold(c, uid, coinNum)
  181. default:
  182. // do nothing
  183. }
  184. if affect > 0 {
  185. res = true
  186. if delCache {
  187. d.DelWalletCache(c, uid)
  188. }
  189. }
  190. return res, err
  191. }
  192. func (d *Dao) ConsumeCoin(c context.Context, coinNum int, uid int64, coinTypeNo int32, seeds int64, delCache bool, reason interface{}) (success bool, err error) {
  193. var affect int64
  194. switch coinTypeNo {
  195. case model.SysCoinTypeGold:
  196. affect, err = d.ConsumeGold(c, uid, coinNum)
  197. case model.SysCoinTypeIosGold:
  198. affect, err = d.ConsumeIapGold(c, uid, coinNum)
  199. case model.SysCoinTypeSilver:
  200. affect, err = d.ConsumeSilver(c, uid, coinNum)
  201. case model.SysCoinTypeMetal:
  202. success, _, err = d.ModifyMetal(c, uid, int64(-1*coinNum), seeds, reason)
  203. default:
  204. }
  205. if model.IsLocalCoin(coinTypeNo) {
  206. if affect > 0 {
  207. success = true
  208. if delCache {
  209. d.DelWalletCache(c, uid)
  210. }
  211. }
  212. }
  213. return
  214. }