createDeptLogic.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package dept
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "time"
  7. "perms-system-server/internal/consts"
  8. authHelper "perms-system-server/internal/logic/auth"
  9. deptModel "perms-system-server/internal/model/dept"
  10. "perms-system-server/internal/response"
  11. "perms-system-server/internal/svc"
  12. "perms-system-server/internal/types"
  13. "github.com/zeromicro/go-zero/core/logx"
  14. "github.com/zeromicro/go-zero/core/stores/sqlx"
  15. )
  16. type CreateDeptLogic struct {
  17. logx.Logger
  18. ctx context.Context
  19. svcCtx *svc.ServiceContext
  20. }
  21. func NewCreateDeptLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateDeptLogic {
  22. return &CreateDeptLogic{
  23. Logger: logx.WithContext(ctx),
  24. ctx: ctx,
  25. svcCtx: svcCtx,
  26. }
  27. }
  28. // CreateDept 创建部门。在指定父部门下新建子部门,自动继承路径层级。仅超管可调用。
  29. func (l *CreateDeptLogic) CreateDept(req *types.CreateDeptReq) (resp *types.IdResp, err error) {
  30. if err := authHelper.RequireSuperAdmin(l.ctx); err != nil {
  31. return nil, err
  32. }
  33. if len(req.Name) > 64 {
  34. return nil, response.ErrBadRequest("部门名称长度不能超过64个字符")
  35. }
  36. if len(req.Remark) > 255 {
  37. return nil, response.ErrBadRequest("备注长度不能超过255个字符")
  38. }
  39. // 审计 L-R18-5:Sort 只在同级部门间相对有效,用不到 int64 的极端值;把合法区间固定为
  40. // [-100000, 100000],与 permsLevel 1-999 的"业务侧人类可读范围"思路一致,避免前端偶发
  41. // 把 math.MaxInt64 之类的值透传到 DB 触发"排序溢出"的 edge case。
  42. if req.Sort < -100000 || req.Sort > 100000 {
  43. return nil, response.ErrBadRequest("排序值必须在 -100000 到 100000 之间")
  44. }
  45. parentPath := "/"
  46. if req.ParentId > 0 {
  47. parent, err := l.svcCtx.SysDeptModel.FindOne(l.ctx, req.ParentId)
  48. if err != nil {
  49. return nil, response.ErrNotFound("父部门不存在")
  50. }
  51. parentPath = parent.Path
  52. }
  53. now := time.Now().Unix()
  54. var deptId int64
  55. deptType := req.DeptType
  56. if deptType == "" {
  57. deptType = consts.DeptTypeNormal
  58. } else if deptType != consts.DeptTypeNormal && deptType != consts.DeptTypeDev {
  59. return nil, response.ErrBadRequest("无效的部门类型")
  60. }
  61. err = l.svcCtx.SysDeptModel.TransactCtx(l.ctx, func(ctx context.Context, session sqlx.Session) error {
  62. if req.ParentId > 0 {
  63. // 审计 L-R12-2:事务内用 FindOneForShareTx 重取父部门快照——同时拿到 status 与
  64. // path。此前只 SELECT `id` 无法感知"父部门在事务外 FindOne 之后被 UpdateDept 禁用"
  65. // 这条交错:读到 id 仍存在就放行,结果子部门会以 Enabled 挂在已 Disabled 的父部门下,
  66. // 让运营侧的"禁用整个子树"意图被静默绕过。改用事务内带 S 锁的完整行读取,同时把
  67. // parentPath 覆盖为事务内视图——与事务外 FindOne 理论上一致(UpdateDept 不改 path),
  68. // 但如果未来 UpdateDept 扩展支持 path 重写,这里已经内建 snapshot。
  69. parent, err := l.svcCtx.SysDeptModel.FindOneForShareTx(ctx, session, req.ParentId)
  70. if err != nil {
  71. if errors.Is(err, sqlx.ErrNotFound) {
  72. return response.ErrNotFound("父部门已被删除")
  73. }
  74. return err
  75. }
  76. if parent.Status != consts.StatusEnabled {
  77. return response.ErrBadRequest("父部门已被禁用,无法创建子部门")
  78. }
  79. parentPath = parent.Path
  80. }
  81. result, err := l.svcCtx.SysDeptModel.InsertWithTx(ctx, session, &deptModel.SysDept{
  82. ParentId: req.ParentId,
  83. Name: req.Name,
  84. Path: parentPath,
  85. Sort: req.Sort,
  86. DeptType: deptType,
  87. Remark: req.Remark,
  88. Status: consts.StatusEnabled,
  89. CreateTime: now,
  90. UpdateTime: now,
  91. })
  92. if err != nil {
  93. return err
  94. }
  95. deptId, _ = result.LastInsertId()
  96. // 审计 L-R17-5:以前用 FindOneWithTx 把刚 INSERT 的整行读回来,仅为了把 path 覆写成
  97. // `parentPath + deptId + "/"` 后再 UpdateWithTx 走整行回写——3 次 DB 往返。现在改调
  98. // UpdatePathWithTx 只改 path/updateTime 两列,减一次 SELECT,sys_dept X 锁持有时长
  99. // 同步缩短,降低并发 DeleteDept / UpdateDept 的尾部延迟。
  100. finalPath := fmt.Sprintf("%s%d/", parentPath, deptId)
  101. return l.svcCtx.SysDeptModel.UpdatePathWithTx(ctx, session, deptId, finalPath, now)
  102. })
  103. if err != nil {
  104. return nil, err
  105. }
  106. return &types.IdResp{Id: deptId}, nil
  107. }