You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

148 lines
4.0 KiB

9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
  1. package video
  2. import (
  3. "context"
  4. "github.com/gogf/gf/v2/database/gdb"
  5. "github.com/gogf/gf/v2/errors/gerror"
  6. "github.com/gogf/gf/v2/text/gstr"
  7. v1 "xgit.pub/module/cms/app/api/video/v1"
  8. "xgit.pub/module/cms/app/dao"
  9. "xgit.pub/module/cms/app/model"
  10. "xgit.pub/module/cms/app/service"
  11. )
  12. type sVideo struct {
  13. }
  14. func init() {
  15. Video := New()
  16. service.RegisterVideo(Video)
  17. }
  18. func New() *sVideo {
  19. return &sVideo{}
  20. }
  21. func (s *sVideo) GetList(ctx context.Context, req *v1.GetListReq) (res *v1.GetListRes, err error) {
  22. res = &v1.GetListRes{}
  23. tx := dao.Video.Ctx(ctx)
  24. //var ms []*entity.Video
  25. var ms []*model.Video
  26. if req.Title != "" { //标题
  27. tx = tx.WhereLike(dao.Video.Columns().Title, "%"+req.Title+"%")
  28. }
  29. //if req.TitleSub != "" {
  30. // tx = tx.WhereLike(dao.Video.Columns().TitleSub, "%"+req.TitleSub+"%")
  31. //}
  32. if len(req.CategoryIdList) > 0 { //分类
  33. tx = tx.WhereIn(dao.Video.Columns().CategoryId, req.CategoryIdList)
  34. }
  35. if req.Lock != "" { //锁定
  36. tx = tx.Where(dao.Video.Columns().Lock, req.Lock)
  37. }
  38. if req.IsEnd != "" { //完结
  39. tx = tx.Where(dao.Video.Columns().IsEnd, req.IsEnd)
  40. }
  41. if req.Copyright != "" { //版权
  42. tx = tx.Where(dao.Video.Columns().Copyright, req.Copyright)
  43. }
  44. if req.Year > 0 { //年份
  45. tx = tx.Where(dao.Video.Columns().Year, req.Year)
  46. }
  47. if req.Actor != "" { //演员
  48. tx = tx.WhereLike(dao.Video.Columns().Actor, "%"+req.Actor+"%")
  49. }
  50. if req.Director != "" { //导演
  51. tx = tx.WhereLike(dao.Video.Columns().Director, "%"+req.Director+"%")
  52. }
  53. if req.Writer != "" { //编剧
  54. tx = tx.WhereLike(dao.Video.Columns().Writer, "%"+req.Writer+"%")
  55. }
  56. if req.Order == "" {
  57. tx = tx.OrderDesc(dao.Video.Columns().Id)
  58. }
  59. if err = tx.Page(req.Page, req.PageSize).Scan(&ms); err != nil {
  60. return
  61. }
  62. if res.Total, err = tx.Count(); err != nil {
  63. return
  64. }
  65. for idx, item := range ms {
  66. if len(item.Actor) > 0 {
  67. ms[idx].ActorList = gstr.Split(item.Actor, ",")
  68. } else {
  69. ms[idx].ActorList = []string{}
  70. }
  71. if len(item.Director) > 0 {
  72. ms[idx].DirectorList = gstr.Split(item.Director, ",")
  73. } else {
  74. ms[idx].DirectorList = []string{}
  75. }
  76. if len(item.Writer) > 0 {
  77. ms[idx].WriterList = gstr.Split(item.Writer, ",")
  78. } else {
  79. ms[idx].WriterList = []string{}
  80. }
  81. ms[idx].CategoryName = service.Category().GetCategoryNameById(ctx, item.CategoryId)
  82. ms[idx].CollectName = service.Collect().GetCollectNameById(ctx, item.CollectId)
  83. }
  84. res.Total, _ = tx.Count()
  85. err = tx.Page(req.Page, req.PageSize).Scan(&ms)
  86. res.Page = req.Page
  87. res.PageSize = req.PageSize
  88. res.Rows = ms
  89. return
  90. }
  91. // Create 创建
  92. func (s *sVideo) Create(ctx context.Context, req *v1.CreateReq) (res *v1.CreateRes, err error) {
  93. err = dao.Video.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
  94. _, err = dao.Video.Ctx(ctx).InsertAndGetId(req)
  95. return err
  96. })
  97. return
  98. }
  99. // Update 更新
  100. func (s *sVideo) Update(ctx context.Context, req *v1.UpdateReq) (err error) {
  101. return dao.Video.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
  102. if req.Id == 0 {
  103. return gerror.New("编号不能为空")
  104. }
  105. _, err = dao.Video.Ctx(ctx).OmitEmpty().Data(req).Where(dao.Video.Columns().Id, req.Id).Update()
  106. return err
  107. })
  108. }
  109. // Delete 删除
  110. func (s *sVideo) Delete(ctx context.Context, req *v1.DeleteReq) (err error) {
  111. return dao.Video.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
  112. if req.Id < 1 {
  113. return gerror.New("编号不能为空")
  114. }
  115. _, err = dao.Video.Ctx(ctx).Where(dao.Video.Columns().Id, req.Id).Delete()
  116. return nil
  117. })
  118. }
  119. // BatchDelete 批量删除
  120. func (s *sVideo) BatchDelete(ctx context.Context, req *v1.BatchDeleteReq) (err error) {
  121. return dao.Video.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
  122. if len(req.Ids) < 1 {
  123. return gerror.New("编号不能为空")
  124. }
  125. _, err = dao.Video.Ctx(ctx).WhereIn(dao.Video.Columns().Id, req.Ids).Delete()
  126. return nil
  127. })
  128. }
  129. // Get 获取
  130. func (s *sVideo) Get(ctx context.Context, req *v1.GetReq) (res *v1.GetRes, err error) {
  131. err = dao.Video.Ctx(ctx).Where(dao.Video.Columns().Id, req.Id).Scan(&res)
  132. if err != nil {
  133. err = gerror.New("Video not found")
  134. }
  135. return
  136. }