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.

147 lines
3.9 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
  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. }
  82. res.Total, _ = tx.Count()
  83. err = tx.Page(req.Page, req.PageSize).Scan(&ms)
  84. res.Page = req.Page
  85. res.PageSize = req.PageSize
  86. res.Rows = ms
  87. return
  88. }
  89. // Create 创建
  90. func (s *sVideo) Create(ctx context.Context, req *v1.CreateReq) (res *v1.CreateRes, err error) {
  91. err = dao.Video.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
  92. _, err = dao.Video.Ctx(ctx).InsertAndGetId(req)
  93. return err
  94. })
  95. return
  96. }
  97. // Update 更新
  98. func (s *sVideo) Update(ctx context.Context, req *v1.UpdateReq) (err error) {
  99. return dao.Video.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
  100. if req.Id == 0 {
  101. return gerror.New("编号不能为空")
  102. }
  103. _, err = dao.Video.Ctx(ctx).OmitEmpty().Data(req).Where(dao.Video.Columns().Id, req.Id).Update()
  104. return err
  105. })
  106. }
  107. // Delete 删除
  108. func (s *sVideo) Delete(ctx context.Context, req *v1.DeleteReq) (err error) {
  109. return dao.Video.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
  110. if req.Id < 1 {
  111. return gerror.New("编号不能为空")
  112. }
  113. _, err = dao.Video.Ctx(ctx).Where(dao.Video.Columns().Id, req.Id).Delete()
  114. return nil
  115. })
  116. }
  117. // BatchDelete 批量删除
  118. func (s *sVideo) BatchDelete(ctx context.Context, req *v1.BatchDeleteReq) (err error) {
  119. return dao.Video.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
  120. if len(req.Ids) < 1 {
  121. return gerror.New("编号不能为空")
  122. }
  123. _, err = dao.Video.Ctx(ctx).WhereIn(dao.Video.Columns().Id, req.Ids).Delete()
  124. return nil
  125. })
  126. }
  127. // Get 获取
  128. func (s *sVideo) Get(ctx context.Context, req *v1.GetReq) (res *v1.GetRes, err error) {
  129. err = dao.Video.Ctx(ctx).Where(dao.Video.Columns().Id, req.Id).Scan(&res)
  130. if err != nil {
  131. err = gerror.New("Video not found")
  132. }
  133. return
  134. }