123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- package service
- import (
- "context"
- "errors"
- "icloudapp.cn/tools/entity"
- "icloudapp.cn/tools/repository/mysql"
- sEntity "icloudapp.cn/tools/service/entity"
- "icloudapp.cn/tools/service/model"
- "icloudapp.cn/tools/util"
- )
- type UserPoster struct {
- Base
- ctx context.Context
- query *model.MUserPoster
- }
- func NewUserPoster(ctx context.Context) *UserPoster {
- userPoster := &UserPoster{ctx: ctx}
- userPoster.query = model.UserPoster
- return userPoster
- }
- func (p *UserPoster) TableName() string {
- return "user_poster"
- }
- func (p *UserPoster) InfoByID(posterID int64) (entity.UserPosterInfo, error) {
- var userPosterInfo entity.UserPosterInfo
- var userPoster entity.UserPoster
- userPoster.PosterID = posterID
- result := mysql.DBConn.Where("poster_id = ?", posterID).Table(p.TableName()).Find(&userPoster)
- if result.Error != nil {
- userPosterInfo.Code = entity.CodeServerBusy
- userPosterInfo.Msg = entity.CodeServerBusy.Msg()
- return userPosterInfo, errors.New(result.Error.Error())
- }
- if result.RowsAffected == 0 {
- userPosterInfo.Code = entity.CodeDataDoesNotExist
- userPosterInfo.Msg = entity.CodeDataDoesNotExist.Msg()
- return userPosterInfo, util.NewError("未找到相关数据")
- }
- userPosterInfo.Body = userPoster
- return userPosterInfo, nil
- }
- func (p *UserPoster) InfoByUUID(uuid int64) (entity.UserPosterInfo, error) {
- var userPosterInfo entity.UserPosterInfo
- var userPoster entity.UserPoster
- userPoster.PosterID = uuid
- result := mysql.DBConn.Where("uuid = ?", uuid).Table(p.TableName()).Find(&userPoster)
- if result.Error != nil {
- userPosterInfo.Code = entity.CodeServerBusy
- userPosterInfo.Msg = entity.CodeServerBusy.Msg()
- return userPosterInfo, errors.New(result.Error.Error())
- }
- if result.RowsAffected == 0 {
- userPosterInfo.Code = entity.CodeDataDoesNotExist
- userPosterInfo.Msg = entity.CodeDataDoesNotExist.Msg()
- return userPosterInfo, util.NewError("未找到相关数据")
- }
- userPosterInfo.Body = userPoster
- return userPosterInfo, nil
- }
- func (p *UserPoster) Count(uid int64) (count int64) {
- if uid == 0 {
- count, _ = p.query.WithContext(p.ctx).Count()
- } else {
- count, _ = p.query.WithContext(p.ctx).Where(p.query.UID.Eq(uid)).Count()
- }
- return count
- }
- func (p *UserPoster) Lists(uid int64, page int, pageSize int) (Page, []*sEntity.UserPoster) {
- count := p.Count(uid)
- pages := p.InitPages(count, page, pageSize)
- if count == 0 {
- return pages, nil
- }
- var lists []*sEntity.UserPoster
- var err error
- if uid > 0 {
- lists, err = p.query.WithContext(p.ctx).Where(p.query.UID.Eq(uid)).Find()
- } else {
- lists, err = p.query.WithContext(p.ctx).Find()
- }
- if err != nil {
- return pages, nil
- }
- return pages, lists
- }
- func (p *UserPoster) SimpleLists(uid int64) []*sEntity.UserPoster {
- lists, _ := p.query.WithContext(
- p.ctx).Select(
- p.query.PosterID,
- p.query.PosterName,
- p.query.Preview,
- p.query.Status,
- p.query.Visits,
- p.query.CreateAt).Where(p.query.UID.Eq(uid)).Find()
- return lists
- }
|