fav.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "time"
  6. "go-common/library/database/sql"
  7. "go-common/library/log"
  8. xtime "go-common/library/time"
  9. )
  10. // sql cachekey
  11. const (
  12. _addUserFavSQL = "INSERT INTO user_favorite (mid, item_id, type, status) VALUES(?, ?, ?, ?)"
  13. _selectUserFavSQL = "SELECT status, mtime FROM user_favorite WHERE mid=? AND type=? AND item_id=? LIMIT 1"
  14. _updateUserFavSQL = "UPDATE user_favorite SET status=? where mid=? AND type=? AND item_id=? AND mtime=? LIMIT 1"
  15. _userFavList = "%d:USERFAVLIST"
  16. _userFavState = "%d:USERFAVSTATE:%d:%d"
  17. )
  18. // FavUpdate 收藏状态更新
  19. func (d *Dao) FavUpdate(c context.Context, itemID int64, mid int64, typ int32, status int32) (err error) {
  20. res := d.db.Raw(_selectUserFavSQL, mid, typ, itemID)
  21. if err = res.Error; err != nil {
  22. log.Error("d.FavUpdate() error(%v)", err)
  23. return
  24. }
  25. var (
  26. oldStatus int32
  27. lastMtime xtime.Time
  28. notFound bool
  29. )
  30. if err = res.Row().Scan(&oldStatus, &lastMtime); err != nil {
  31. if err == sql.ErrNoRows {
  32. notFound = true
  33. } else {
  34. log.Error("d.FavUpdate() res.Row().Scan() error(%v)", err)
  35. return
  36. }
  37. }
  38. if notFound {
  39. // 插入
  40. if err = d.db.Exec(_addUserFavSQL, mid, itemID, typ, status).Error; err != nil {
  41. log.Error("d.FavUpdate(%d, %d, %d, %d) Exec(%s) error(%v)", mid, itemID, typ, status, _addUserFavSQL, err)
  42. return
  43. }
  44. } else {
  45. // 更新
  46. if oldStatus == status {
  47. log.Info("d.FavUpdate(%d, %d, %d, %d) 前后状态相同 oldStatus %d ", mid, itemID, typ, status, oldStatus)
  48. return
  49. }
  50. if err = d.db.Exec(_updateUserFavSQL, status, mid, typ, itemID, lastMtime.Time().Format("2006-01-02 15:04:05")).Error; err != nil {
  51. log.Error("d.FavUpdate(%d, %d, %d, %d) Exec(%s) error(%v)", mid, itemID, typ, status, _updateUserFavSQL, err)
  52. return
  53. }
  54. }
  55. return
  56. }
  57. // UserFavStateCache 设置用户收藏缓存 支持多种类型
  58. func (d *Dao) UserFavStateCache(c context.Context, itemID int64, mid int64, typ int32, status int32) (err error) {
  59. conn := d.redis.Get(c)
  60. defer conn.Close()
  61. _, err = conn.Do("SETEX", fmt.Sprintf(_userFavState, mid, itemID, typ), _expireHalfhour, status)
  62. if err != nil {
  63. log.Error("d.UserFavState(%d, %d, %d, %d) SETEX error(%v)", itemID, mid, typ, status, err)
  64. return
  65. }
  66. return
  67. }
  68. // UpdateFavListCache 更新收藏列表缓存
  69. func (d *Dao) UpdateFavListCache(c context.Context, itemID int64, mid int64, status int32) (err error) {
  70. conn := d.redis.Get(c)
  71. defer conn.Close()
  72. if status == 1 {
  73. _, err = conn.Do("ZADD", fmt.Sprintf(_userFavList, mid), time.Now().Unix(), itemID)
  74. } else {
  75. _, err = conn.Do("ZREM", fmt.Sprintf(_userFavList, mid), itemID)
  76. }
  77. if err != nil {
  78. log.Error("d.UpdateFavListCache(%d, %d, %d) error(%v)", itemID, mid, status, err)
  79. return
  80. }
  81. return
  82. }