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.

100 lines
2.6 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
  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. return
  38. }
  39. // Get 获取详情
  40. func (s *sCollect) Get(ctx context.Context, req *v1.GetReq) (res *v1.GetRes, err error) {
  41. if req.Id < 1 {
  42. err = gerror.New("id is empty")
  43. return
  44. }
  45. err = dao.Collect.Ctx(ctx).Where(dao.Collect.Columns().Id, req.Id).Scan(&res)
  46. return
  47. }
  48. // Create 创建
  49. func (s *sCollect) Create(ctx context.Context, req *v1.CreateReq) (res *v1.CreateRes, err error) {
  50. err = dao.Collect.Ctx(ctx).Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
  51. _, err = dao.Collect.Ctx(ctx).Insert(&req)
  52. return err
  53. })
  54. return
  55. }
  56. // Update 更新
  57. func (s *sCollect) Update(ctx context.Context, req *v1.UpdateReq) (res *v1.UpdateRes, err error) {
  58. err = dao.Collect.Ctx(ctx).Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
  59. if req.Id < 1 {
  60. return gerror.New("id is empty")
  61. }
  62. maps := gconv.Map(req)
  63. _, err = dao.Collect.Ctx(ctx).Data(
  64. maps).Where(dao.Collect.Columns().Id, req.Id).Update()
  65. return err
  66. })
  67. return
  68. }
  69. // Delete 删除
  70. func (s *sCollect) Delete(ctx context.Context, req *v1.DeleteReq) (res *v1.DeleteRes, err error) {
  71. err = dao.Collect.Ctx(ctx).Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
  72. if req.Id < 1 {
  73. return gerror.New("id is empty")
  74. }
  75. _, err = dao.Collect.Ctx(ctx).Where(dao.Collect.Columns().Id, req.Id).Delete()
  76. return err
  77. })
  78. return
  79. }
  80. // BatchDelete 批量删除
  81. func (s *sCollect) BatchDelete(ctx context.Context, req *v1.BatchDeleteReq) (res *v1.BatchDeleteRes, err error) {
  82. err = dao.Collect.Ctx(ctx).Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
  83. if len(req.Ids) < 1 {
  84. return gerror.New("ids is empty")
  85. }
  86. _, err = dao.Collect.Ctx(ctx).WhereIn(dao.Collect.Columns().Id, req.Ids).Delete()
  87. return err
  88. })
  89. return
  90. }