user_gift.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package dao
  2. import (
  3. "context"
  4. "crypto/md5"
  5. "encoding/hex"
  6. "fmt"
  7. "go-common/app/service/live/gift/model"
  8. "go-common/library/database/sql"
  9. "go-common/library/log"
  10. "strconv"
  11. "time"
  12. )
  13. var (
  14. _getBag = "SELECT id,gift_num FROM user_gift_%s WHERE uid = ? AND gift_id = ? AND expireat = ? LIMIT 1"
  15. _getBagByID = "SELECT id,gift_num FROM user_gift_%s WHERE id = ?"
  16. _updateBagNum = "UPDATE user_gift_%s SET gift_num = gift_num + ? WHERE id = ?"
  17. _insertBag = "INSERT INTO user_gift_%s (uid,gift_id,gift_num,expireat) VALUES (?,?,?,?)"
  18. _getBagList = "SELECT id,uid,gift_id,gift_num,expireat FROM user_gift_%s WHERE uid = ? AND gift_num > 0 AND (expireat = 0 OR expireat > ?)"
  19. )
  20. // GetBag GetBag
  21. func (d *Dao) GetBag(ctx context.Context, uid, giftID, expireAt int64) (res *model.BagInfo, err error) {
  22. log.Info("GetBag,uid:%d,giftID:%d,expireAt:%d", uid, giftID, expireAt)
  23. row := d.db.QueryRow(ctx, fmt.Sprintf(_getBag, getPostFix(uid)), uid, giftID, expireAt)
  24. res = &model.BagInfo{}
  25. if err = row.Scan(&res.ID, &res.GiftNum); err != nil {
  26. if err == sql.ErrNoRows {
  27. err = nil
  28. return
  29. }
  30. log.Error("GetBag row.Scan error(%v)", err)
  31. }
  32. return
  33. }
  34. // UpdateBagNum UpdateBagNum
  35. func (d *Dao) UpdateBagNum(ctx context.Context, uid, id, num int64) (affected int64, err error) {
  36. log.Info("UpdateBagNum,uid:%d,id:%d,num:%d", uid, id, num)
  37. res, err := d.db.Exec(ctx, fmt.Sprintf(_updateBagNum, getPostFix(uid)), num, id)
  38. if err != nil {
  39. log.Error("UpdateBagNum error(%v)", err)
  40. return
  41. }
  42. return res.RowsAffected()
  43. }
  44. // AddBag AddBag
  45. func (d *Dao) AddBag(ctx context.Context, uid, giftID, giftNum, expireAt int64) (affected int64, err error) {
  46. log.Info("AddBag,uid:%d,giftID:%d,giftNum:%d,expireAt:%d", uid, giftID, giftNum, expireAt)
  47. res, err := d.db.Exec(ctx, fmt.Sprintf(_insertBag, getPostFix(uid)), uid, giftID, giftNum, expireAt)
  48. if err != nil {
  49. log.Error("AddBag error(%v)", err)
  50. return
  51. }
  52. return res.LastInsertId()
  53. }
  54. // GetBagByID GetBagByID
  55. func (d *Dao) GetBagByID(ctx context.Context, uid, id int64) (res *model.BagInfo, err error) {
  56. log.Info("GetBagByID,uid:%d,id:%d", uid, id)
  57. row := d.db.QueryRow(ctx, fmt.Sprintf(_getBagByID, getPostFix(uid)), id)
  58. res = &model.BagInfo{}
  59. if err = row.Scan(&res.ID, &res.GiftNum); err != nil {
  60. if err == sql.ErrNoRows {
  61. err = nil
  62. return
  63. }
  64. log.Error("GetBagByID row.Scan error(%v)", err)
  65. }
  66. return
  67. }
  68. func getPostFix(uid int64) string {
  69. uidStr := strconv.Itoa(int(uid))
  70. h := md5.New()
  71. h.Write([]byte(uidStr))
  72. md5Str := hex.EncodeToString(h.Sum(nil))
  73. return md5Str[0:1]
  74. }
  75. // GetBagList GetBagList
  76. func (d *Dao) GetBagList(ctx context.Context, uid int64) (list []*model.BagGiftList, err error) {
  77. log.Info("GetBagList,uid:%d", uid)
  78. curTime := time.Now().Unix()
  79. rows, err := d.db.Query(ctx, fmt.Sprintf(_getBagList, getPostFix(uid)), uid, curTime)
  80. if err != nil {
  81. log.Error("GetBagGiftList error,err %v", err)
  82. return
  83. }
  84. defer rows.Close()
  85. for rows.Next() {
  86. b := &model.BagGiftList{}
  87. if err = rows.Scan(&b.ID, &b.UID, &b.GiftID, &b.GiftNum, &b.ExpireAt); err != nil {
  88. log.Error("GetBagGiftList scan error,err %v", err)
  89. return
  90. }
  91. list = append(list, b)
  92. }
  93. return
  94. }