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.

113 lines
2.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
  1. package collect
  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/util/gconv"
  7. v1 "xgit.pub/module/cms/app/api/collect/v1"
  8. "xgit.pub/module/cms/app/dao"
  9. "xgit.pub/module/cms/app/model/entity"
  10. "xgit.pub/module/cms/app/service"
  11. )
  12. type sCollect struct {
  13. }
  14. func init() {
  15. Collect := New()
  16. service.RegisterCollect(Collect)
  17. }
  18. func New() *sCollect {
  19. return &sCollect{}
  20. }
  21. // GetList 获取列表
  22. func (s *sCollect) GetList(ctx context.Context, req *v1.GetListReq) (res *v1.GetListRes, err error) {
  23. res = &v1.GetListRes{}
  24. tx := dao.Collect.Ctx(ctx)
  25. if req.Key != "" {
  26. tx = tx.WhereLike(dao.Collect.Columns().Name, "%"+req.Key+"%")
  27. }
  28. res.Total, err = tx.Count()
  29. if err != nil {
  30. return
  31. }
  32. if req.Order == "" {
  33. tx = tx.OrderDesc(dao.Collect.Columns().Id)
  34. }
  35. var list []*entity.Collect
  36. err = tx.Page(req.Page, req.PageSize).Scan(&list)
  37. res.Rows = list
  38. res.Page = req.Page
  39. res.PageSize = req.PageSize
  40. return
  41. }
  42. // Get 获取详情
  43. func (s *sCollect) Get(ctx context.Context, req *v1.GetReq) (res *v1.GetRes, err error) {
  44. if req.Id < 1 {
  45. err = gerror.New("id is empty")
  46. return
  47. }
  48. err = dao.Collect.Ctx(ctx).Where(dao.Collect.Columns().Id, req.Id).Scan(&res)
  49. return
  50. }
  51. // Create 创建
  52. func (s *sCollect) Create(ctx context.Context, req *v1.CreateReq) (res *v1.CreateRes, err error) {
  53. err = dao.Collect.Ctx(ctx).Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
  54. _, err = dao.Collect.Ctx(ctx).Insert(&req)
  55. return err
  56. })
  57. return
  58. }
  59. // Update 更新
  60. func (s *sCollect) Update(ctx context.Context, req *v1.UpdateReq) (res *v1.UpdateRes, err error) {
  61. err = dao.Collect.Ctx(ctx).Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
  62. if req.Id < 1 {
  63. return gerror.New("id is empty")
  64. }
  65. maps := gconv.Map(req)
  66. _, err = dao.Collect.Ctx(ctx).Data(
  67. maps).Where(dao.Collect.Columns().Id, req.Id).Update()
  68. return err
  69. })
  70. return
  71. }
  72. // Delete 删除
  73. func (s *sCollect) Delete(ctx context.Context, req *v1.DeleteReq) (res *v1.DeleteRes, err error) {
  74. err = dao.Collect.Ctx(ctx).Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
  75. if req.Id < 1 {
  76. return gerror.New("id is empty")
  77. }
  78. _, err = dao.Collect.Ctx(ctx).Where(dao.Collect.Columns().Id, req.Id).Delete()
  79. return err
  80. })
  81. return
  82. }
  83. // BatchDelete 批量删除
  84. func (s *sCollect) BatchDelete(ctx context.Context, req *v1.BatchDeleteReq) (res *v1.BatchDeleteRes, err error) {
  85. err = dao.Collect.Ctx(ctx).Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
  86. if len(req.Ids) < 1 {
  87. return gerror.New("ids is empty")
  88. }
  89. _, err = dao.Collect.Ctx(ctx).WhereIn(dao.Collect.Columns().Id, req.Ids).Delete()
  90. return err
  91. })
  92. return
  93. }
  94. // GetCollectNameById 获取收藏名称
  95. func (s *sCollect) GetCollectNameById(ctx context.Context, id int) (name string) {
  96. var collect entity.Collect
  97. err := dao.Collect.Ctx(ctx).Where(dao.Collect.Columns().Id, id).Scan(&collect)
  98. if err != nil {
  99. return
  100. }
  101. return collect.Name
  102. }