sys_post.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * @desc:岗位处理
  3. * @company:云南奇讯科技有限公司
  4. * @Author: yixiaohu
  5. * @Date: 2022/4/7 23:18
  6. */
  7. package service
  8. import (
  9. "context"
  10. "github.com/gogf/gf/v2/frame/g"
  11. "github.com/gogf/gf/v2/util/gconv"
  12. "github.com/tiger1103/gfast/v3/api/v1/system"
  13. "github.com/tiger1103/gfast/v3/internal/app/system/consts"
  14. "github.com/tiger1103/gfast/v3/internal/app/system/model/entity"
  15. "github.com/tiger1103/gfast/v3/internal/app/system/service/internal/dao"
  16. "github.com/tiger1103/gfast/v3/internal/app/system/service/internal/do"
  17. "github.com/tiger1103/gfast/v3/library/liberr"
  18. )
  19. type IPost interface {
  20. List(ctx context.Context, req *system.PostSearchReq) (res *system.PostSearchRes, err error)
  21. Add(ctx context.Context, req *system.PostAddReq) (err error)
  22. Edit(ctx context.Context, req *system.PostEditReq) (err error)
  23. Delete(ctx context.Context, ids []int) (err error)
  24. GetUsedPost(ctx context.Context) (list []*entity.SysPost, err error)
  25. }
  26. type postImpl struct {
  27. }
  28. var postService = postImpl{}
  29. func Post() IPost {
  30. return IPost(&postService)
  31. }
  32. // List 岗位列表
  33. func (s *postImpl) List(ctx context.Context, req *system.PostSearchReq) (res *system.PostSearchRes, err error) {
  34. res = new(system.PostSearchRes)
  35. err = g.Try(func() {
  36. m := dao.SysPost.Ctx(ctx)
  37. if req != nil {
  38. if req.PostCode != "" {
  39. m = m.Where("post_code like ?", "%"+req.PostCode+"%")
  40. }
  41. if req.PostName != "" {
  42. m = m.Where("post_name like ?", "%"+req.PostName+"%")
  43. }
  44. if req.Status != "" {
  45. m = m.Where("status", gconv.Uint(req.Status))
  46. }
  47. }
  48. res.Total, err = m.Count()
  49. liberr.ErrIsNil(ctx, err, "获取岗位失败")
  50. if req.PageNum == 0 {
  51. req.PageNum = 1
  52. }
  53. if req.PageSize == 0 {
  54. req.PageSize = consts.PageSize
  55. }
  56. res.CurrentPage = req.PageNum
  57. err = m.Page(req.PageNum, req.PageSize).Order("post_sort asc,post_id asc").Scan(&res.PostList)
  58. liberr.ErrIsNil(ctx, err, "获取岗位失败")
  59. })
  60. return
  61. }
  62. func (s *postImpl) Add(ctx context.Context, req *system.PostAddReq) (err error) {
  63. err = g.Try(func() {
  64. _, err = dao.SysPost.Ctx(ctx).Insert(do.SysPost{
  65. PostCode: req.PostCode,
  66. PostName: req.PostName,
  67. PostSort: req.PostSort,
  68. Status: req.Status,
  69. Remark: req.Remark,
  70. CreatedBy: Context().GetUserId(ctx),
  71. })
  72. liberr.ErrIsNil(ctx, err, "添加岗位失败")
  73. })
  74. return
  75. }
  76. func (s *postImpl) Edit(ctx context.Context, req *system.PostEditReq) (err error) {
  77. err = g.Try(func() {
  78. _, err = dao.SysPost.Ctx(ctx).WherePri(req.PostId).Update(do.SysPost{
  79. PostCode: req.PostCode,
  80. PostName: req.PostName,
  81. PostSort: req.PostSort,
  82. Status: req.Status,
  83. Remark: req.Remark,
  84. UpdatedBy: Context().GetUserId(ctx),
  85. })
  86. liberr.ErrIsNil(ctx, err, "修改岗位失败")
  87. })
  88. return
  89. }
  90. func (s *postImpl) Delete(ctx context.Context, ids []int) (err error) {
  91. err = g.Try(func() {
  92. _, err = dao.SysPost.Ctx(ctx).Where(dao.SysPost.Columns().PostId+" in(?)", ids).Delete()
  93. liberr.ErrIsNil(ctx, err, "删除失败")
  94. })
  95. return
  96. }
  97. // GetUsedPost 获取正常状态的岗位
  98. func (s *postImpl) GetUsedPost(ctx context.Context) (list []*entity.SysPost, err error) {
  99. err = g.Try(func() {
  100. err = dao.SysPost.Ctx(ctx).Where(dao.SysPost.Columns().Status, 1).
  101. Order(dao.SysPost.Columns().PostSort + " ASC, " + dao.SysPost.Columns().PostId + " ASC ").Scan(&list)
  102. liberr.ErrIsNil(ctx, err, "获取岗位数据失败")
  103. })
  104. return
  105. }