package video import ( "context" "github.com/gogf/gf/v2/database/gdb" "github.com/gogf/gf/v2/errors/gerror" "github.com/gogf/gf/v2/text/gstr" v1 "xgit.pub/module/cms/app/api/video/v1" "xgit.pub/module/cms/app/dao" "xgit.pub/module/cms/app/model" "xgit.pub/module/cms/app/service" ) type sVideo struct { } func init() { Video := New() service.RegisterVideo(Video) } func New() *sVideo { return &sVideo{} } func (s *sVideo) GetList(ctx context.Context, req *v1.GetListReq) (res *v1.GetListRes, err error) { res = &v1.GetListRes{} tx := dao.Video.Ctx(ctx) //var ms []*entity.Video var ms []*model.Video if req.Title != "" { //标题 tx = tx.WhereLike(dao.Video.Columns().Title, "%"+req.Title+"%") } //if req.TitleSub != "" { // tx = tx.WhereLike(dao.Video.Columns().TitleSub, "%"+req.TitleSub+"%") //} if len(req.CategoryIdList) > 0 { //分类 tx = tx.WhereIn(dao.Video.Columns().CategoryId, req.CategoryIdList) } if req.Lock != "" { //锁定 tx = tx.Where(dao.Video.Columns().Lock, req.Lock) } if req.IsEnd != "" { //完结 tx = tx.Where(dao.Video.Columns().IsEnd, req.IsEnd) } if req.Copyright != "" { //版权 tx = tx.Where(dao.Video.Columns().Copyright, req.Copyright) } if req.Year > 0 { //年份 tx = tx.Where(dao.Video.Columns().Year, req.Year) } if req.Actor != "" { //演员 tx = tx.WhereLike(dao.Video.Columns().Actor, "%"+req.Actor+"%") } if req.Director != "" { //导演 tx = tx.WhereLike(dao.Video.Columns().Director, "%"+req.Director+"%") } if req.Writer != "" { //编剧 tx = tx.WhereLike(dao.Video.Columns().Writer, "%"+req.Writer+"%") } if req.Order == "" { tx = tx.OrderDesc(dao.Video.Columns().Id) } if err = tx.Page(req.Page, req.PageSize).Scan(&ms); err != nil { return } if res.Total, err = tx.Count(); err != nil { return } for idx, item := range ms { if len(item.Actor) > 0 { ms[idx].ActorList = gstr.Split(item.Actor, ",") } else { ms[idx].ActorList = []string{} } if len(item.Director) > 0 { ms[idx].DirectorList = gstr.Split(item.Director, ",") } else { ms[idx].DirectorList = []string{} } if len(item.Writer) > 0 { ms[idx].WriterList = gstr.Split(item.Writer, ",") } else { ms[idx].WriterList = []string{} } ms[idx].CategoryName = service.Category().GetCategoryNameById(ctx, item.CategoryId) ms[idx].CollectName = service.Collect().GetCollectNameById(ctx, item.CollectId) } res.Total, _ = tx.Count() err = tx.Page(req.Page, req.PageSize).Scan(&ms) res.Page = req.Page res.PageSize = req.PageSize res.Rows = ms return } // Create 创建 func (s *sVideo) Create(ctx context.Context, req *v1.CreateReq) (res *v1.CreateRes, err error) { err = dao.Video.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error { _, err = dao.Video.Ctx(ctx).InsertAndGetId(req) return err }) return } // Update 更新 func (s *sVideo) Update(ctx context.Context, req *v1.UpdateReq) (err error) { return dao.Video.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error { if req.Id == 0 { return gerror.New("编号不能为空") } _, err = dao.Video.Ctx(ctx).OmitEmpty().Data(req).Where(dao.Video.Columns().Id, req.Id).Update() return err }) } // Delete 删除 func (s *sVideo) Delete(ctx context.Context, req *v1.DeleteReq) (err error) { return dao.Video.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error { if req.Id < 1 { return gerror.New("编号不能为空") } _, err = dao.Video.Ctx(ctx).Where(dao.Video.Columns().Id, req.Id).Delete() return nil }) } // BatchDelete 批量删除 func (s *sVideo) BatchDelete(ctx context.Context, req *v1.BatchDeleteReq) (err error) { return dao.Video.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error { if len(req.Ids) < 1 { return gerror.New("编号不能为空") } _, err = dao.Video.Ctx(ctx).WhereIn(dao.Video.Columns().Id, req.Ids).Delete() return nil }) } // Get 获取 func (s *sVideo) Get(ctx context.Context, req *v1.GetReq) (res *v1.GetRes, err error) { err = dao.Video.Ctx(ctx).Where(dao.Video.Columns().Id, req.Id).Scan(&res) if err != nil { err = gerror.New("Video not found") } return }