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.
 
 
 

103 lines
2.7 KiB

package collect
import (
"context"
"github.com/gogf/gf/v2/database/gdb"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/util/gconv"
v1 "xgit.pub/module/cms/app/api/collect/v1"
"xgit.pub/module/cms/app/dao"
"xgit.pub/module/cms/app/model/entity"
"xgit.pub/module/cms/app/service"
)
type sCollect struct {
}
func init() {
Collect := New()
service.RegisterCollect(Collect)
}
func New() *sCollect {
return &sCollect{}
}
// GetList 获取列表
func (s *sCollect) GetList(ctx context.Context, req *v1.GetListReq) (res *v1.GetListRes, err error) {
res = &v1.GetListRes{}
tx := dao.Collect.Ctx(ctx)
if req.Key != "" {
tx = tx.WhereLike(dao.Collect.Columns().Name, "%"+req.Key+"%")
}
res.Total, err = tx.Count()
if err != nil {
return
}
if req.Order == "" {
tx = tx.OrderDesc(dao.Collect.Columns().Id)
}
var list []*entity.Collect
err = tx.Page(req.Page, req.PageSize).Scan(&list)
res.Rows = list
res.Page = req.Page
res.PageSize = req.PageSize
return
}
// Get 获取详情
func (s *sCollect) Get(ctx context.Context, req *v1.GetReq) (res *v1.GetRes, err error) {
if req.Id < 1 {
err = gerror.New("id is empty")
return
}
err = dao.Collect.Ctx(ctx).Where(dao.Collect.Columns().Id, req.Id).Scan(&res)
return
}
// Create 创建
func (s *sCollect) Create(ctx context.Context, req *v1.CreateReq) (res *v1.CreateRes, err error) {
err = dao.Collect.Ctx(ctx).Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
_, err = dao.Collect.Ctx(ctx).Insert(&req)
return err
})
return
}
// Update 更新
func (s *sCollect) Update(ctx context.Context, req *v1.UpdateReq) (res *v1.UpdateRes, err error) {
err = dao.Collect.Ctx(ctx).Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
if req.Id < 1 {
return gerror.New("id is empty")
}
maps := gconv.Map(req)
_, err = dao.Collect.Ctx(ctx).Data(
maps).Where(dao.Collect.Columns().Id, req.Id).Update()
return err
})
return
}
// Delete 删除
func (s *sCollect) Delete(ctx context.Context, req *v1.DeleteReq) (res *v1.DeleteRes, err error) {
err = dao.Collect.Ctx(ctx).Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
if req.Id < 1 {
return gerror.New("id is empty")
}
_, err = dao.Collect.Ctx(ctx).Where(dao.Collect.Columns().Id, req.Id).Delete()
return err
})
return
}
// BatchDelete 批量删除
func (s *sCollect) BatchDelete(ctx context.Context, req *v1.BatchDeleteReq) (res *v1.BatchDeleteRes, err error) {
err = dao.Collect.Ctx(ctx).Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
if len(req.Ids) < 1 {
return gerror.New("ids is empty")
}
_, err = dao.Collect.Ctx(ctx).WhereIn(dao.Collect.Columns().Id, req.Ids).Delete()
return err
})
return
}