user_poster.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package service
  2. import (
  3. "context"
  4. "errors"
  5. "icloudapp.cn/tools/entity"
  6. "icloudapp.cn/tools/repository/mysql"
  7. sEntity "icloudapp.cn/tools/service/entity"
  8. "icloudapp.cn/tools/service/model"
  9. "icloudapp.cn/tools/util"
  10. )
  11. type UserPoster struct {
  12. Base
  13. ctx context.Context
  14. query *model.MUserPoster
  15. }
  16. func NewUserPoster(ctx context.Context) *UserPoster {
  17. userPoster := &UserPoster{ctx: ctx}
  18. userPoster.query = model.UserPoster
  19. return userPoster
  20. }
  21. func (p *UserPoster) TableName() string {
  22. return "user_poster"
  23. }
  24. func (p *UserPoster) InfoByID(posterID int64) (entity.UserPosterInfo, error) {
  25. var userPosterInfo entity.UserPosterInfo
  26. var userPoster entity.UserPoster
  27. userPoster.PosterID = posterID
  28. result := mysql.DBConn.Where("poster_id = ?", posterID).Table(p.TableName()).Find(&userPoster)
  29. if result.Error != nil {
  30. userPosterInfo.Code = entity.CodeServerBusy
  31. userPosterInfo.Msg = entity.CodeServerBusy.Msg()
  32. return userPosterInfo, errors.New(result.Error.Error())
  33. }
  34. if result.RowsAffected == 0 {
  35. userPosterInfo.Code = entity.CodeDataDoesNotExist
  36. userPosterInfo.Msg = entity.CodeDataDoesNotExist.Msg()
  37. return userPosterInfo, util.NewError("未找到相关数据")
  38. }
  39. userPosterInfo.Body = userPoster
  40. return userPosterInfo, nil
  41. }
  42. func (p *UserPoster) InfoByUUID(uuid int64) (entity.UserPosterInfo, error) {
  43. var userPosterInfo entity.UserPosterInfo
  44. var userPoster entity.UserPoster
  45. userPoster.PosterID = uuid
  46. result := mysql.DBConn.Where("uuid = ?", uuid).Table(p.TableName()).Find(&userPoster)
  47. if result.Error != nil {
  48. userPosterInfo.Code = entity.CodeServerBusy
  49. userPosterInfo.Msg = entity.CodeServerBusy.Msg()
  50. return userPosterInfo, errors.New(result.Error.Error())
  51. }
  52. if result.RowsAffected == 0 {
  53. userPosterInfo.Code = entity.CodeDataDoesNotExist
  54. userPosterInfo.Msg = entity.CodeDataDoesNotExist.Msg()
  55. return userPosterInfo, util.NewError("未找到相关数据")
  56. }
  57. userPosterInfo.Body = userPoster
  58. return userPosterInfo, nil
  59. }
  60. func (p *UserPoster) Count(uid int64) (count int64) {
  61. if uid == 0 {
  62. count, _ = p.query.WithContext(p.ctx).Count()
  63. } else {
  64. count, _ = p.query.WithContext(p.ctx).Where(p.query.UID.Eq(uid)).Count()
  65. }
  66. return count
  67. }
  68. func (p *UserPoster) Lists(uid int64, page int, pageSize int) (Page, []*sEntity.UserPoster) {
  69. count := p.Count(uid)
  70. pages := p.InitPages(count, page, pageSize)
  71. if count == 0 {
  72. return pages, nil
  73. }
  74. var lists []*sEntity.UserPoster
  75. var err error
  76. if uid > 0 {
  77. lists, err = p.query.WithContext(p.ctx).Where(p.query.UID.Eq(uid)).Find()
  78. } else {
  79. lists, err = p.query.WithContext(p.ctx).Find()
  80. }
  81. if err != nil {
  82. return pages, nil
  83. }
  84. return pages, lists
  85. }
  86. func (p *UserPoster) SimpleLists(uid int64) []*sEntity.UserPoster {
  87. lists, _ := p.query.WithContext(
  88. p.ctx).Select(
  89. p.query.PosterID,
  90. p.query.PosterName,
  91. p.query.Preview,
  92. p.query.Status,
  93. p.query.Visits,
  94. p.query.CreateAt).Where(p.query.UID.Eq(uid)).Find()
  95. return lists
  96. }