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.

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