作者 郑周

1 删除部门,以及相关联的用户关联部门

@@ -29,6 +29,10 @@ service Core { @@ -29,6 +29,10 @@ service Core {
29 @doc "部门-更新" 29 @doc "部门-更新"
30 @handler systemUpdate 30 @handler systemUpdate
31 put /system/department/:id (DepartmentUpdateRequest) returns (DepartmentGetResponse) 31 put /system/department/:id (DepartmentUpdateRequest) returns (DepartmentGetResponse)
  32 +
  33 + @doc "部门-删除"
  34 + @handler systemDelete
  35 + delete /system/department/:id (DepartmentGetRequest) returns (DepartmentGetResponse)
32 } 36 }
33 37
34 type ( 38 type (
  1 +package department
  2 +
  3 +import (
  4 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/result"
  5 + "net/http"
  6 +
  7 + "github.com/zeromicro/go-zero/rest/httpx"
  8 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/logic/department"
  9 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc"
  10 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types"
  11 +)
  12 +
  13 +func SystemDeleteHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
  14 + return func(w http.ResponseWriter, r *http.Request) {
  15 + var req types.DepartmentGetRequest
  16 + if err := httpx.Parse(r, &req); err != nil {
  17 + httpx.ErrorCtx(r.Context(), w, err)
  18 + return
  19 + }
  20 +
  21 + l := department.NewSystemDeleteLogic(r.Context(), svcCtx)
  22 + resp, err := l.SystemDelete(&req)
  23 + result.HttpResult(r, w, resp, err)
  24 + }
  25 +}
@@ -407,6 +407,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { @@ -407,6 +407,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
407 Path: "/system/department/:id", 407 Path: "/system/department/:id",
408 Handler: department.SystemUpdateHandler(serverCtx), 408 Handler: department.SystemUpdateHandler(serverCtx),
409 }, 409 },
  410 + {
  411 + Method: http.MethodDelete,
  412 + Path: "/system/department/:id",
  413 + Handler: department.SystemDeleteHandler(serverCtx),
  414 + },
410 }, 415 },
411 rest.WithJwt(serverCtx.Config.SystemAuth.AccessSecret), 416 rest.WithJwt(serverCtx.Config.SystemAuth.AccessSecret),
412 rest.WithPrefix("/v1"), 417 rest.WithPrefix("/v1"),
@@ -48,7 +48,7 @@ func (l *SystemAddLogic) SystemAdd(req *types.DepartmentAddRequest) (resp *types @@ -48,7 +48,7 @@ func (l *SystemAddLogic) SystemAdd(req *types.DepartmentAddRequest) (resp *types
48 CompanyId: userToken.CompanyId, 48 CompanyId: userToken.CompanyId,
49 Name: req.Name, 49 Name: req.Name,
50 } 50 }
51 - err = transaction.UseTrans(l.ctx, conn.DB(), func(ctx context.Context, c transaction.Conn) error { 51 + err = transaction.UseTrans(l.ctx, conn.DB(), func(ctx context.Context, conn transaction.Conn) error {
52 _, err = l.svcCtx.DepartmentRepository.Insert(l.ctx, conn, insert) 52 _, err = l.svcCtx.DepartmentRepository.Insert(l.ctx, conn, insert)
53 if err != nil { 53 if err != nil {
54 return err 54 return err
  1 +package department
  2 +
  3 +import (
  4 + "context"
  5 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc"
  6 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types"
  7 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/db/transaction"
  8 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
  9 +
  10 + "github.com/zeromicro/go-zero/core/logx"
  11 +)
  12 +
  13 +type SystemDeleteLogic struct {
  14 + logx.Logger
  15 + ctx context.Context
  16 + svcCtx *svc.ServiceContext
  17 +}
  18 +
  19 +func NewSystemDeleteLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SystemDeleteLogic {
  20 + return &SystemDeleteLogic{
  21 + Logger: logx.WithContext(ctx),
  22 + ctx: ctx,
  23 + svcCtx: svcCtx,
  24 + }
  25 +}
  26 +
  27 +func (l *SystemDeleteLogic) SystemDelete(req *types.DepartmentGetRequest) (resp *types.DepartmentGetResponse, err error) {
  28 + var conn = l.svcCtx.DefaultDBConn()
  29 +
  30 + one, err := l.svcCtx.DepartmentRepository.FindOne(l.ctx, conn, req.Id)
  31 + if err != nil {
  32 + return nil, err
  33 + }
  34 +
  35 + // 获取公司下的所有用户
  36 + _, users, err := l.svcCtx.UserRepository.Find(l.ctx, conn, domain.NewQueryOptions().
  37 + WithFindOnly().
  38 + WithKV(" companyId", one.CompanyId))
  39 + if err != nil {
  40 + return nil, err
  41 + }
  42 +
  43 + err = transaction.UseTrans(l.ctx, conn.DB(), func(ctx context.Context, conn transaction.Conn) error {
  44 + _, err = l.svcCtx.DepartmentRepository.Delete(l.ctx, conn, one)
  45 + if err != nil {
  46 + return err
  47 + }
  48 +
  49 + // 移除用户中关联的分组
  50 + if len(users) > 0 {
  51 + var findIndex = func(ids []int64, id int64) int {
  52 + for i, _ := range ids {
  53 + if ids[i] == id {
  54 + return i
  55 + }
  56 + }
  57 + return -1
  58 + }
  59 + for i := range users {
  60 + user := users[i]
  61 + var targetIndex = findIndex(user.Departments, req.Id)
  62 + if targetIndex != -1 { // 归属分组存在,则移除
  63 + user.Departments = append(user.Departments[:targetIndex], user.Departments[targetIndex+1:]...) // 移除分组ID
  64 + _, err = l.svcCtx.UserRepository.UpdateWithVersion(l.ctx, conn, user)
  65 + if err != nil {
  66 + return err
  67 + }
  68 + }
  69 + }
  70 + }
  71 + return nil
  72 + }, true)
  73 +
  74 + return
  75 +}
@@ -55,21 +55,21 @@ func (l *SystemUpdateLogic) SystemUpdate(req *types.DepartmentUpdateRequest) (re @@ -55,21 +55,21 @@ func (l *SystemUpdateLogic) SystemUpdate(req *types.DepartmentUpdateRequest) (re
55 newIdMap[req.Ids[i]] = 0 55 newIdMap[req.Ids[i]] = 0
56 } 56 }
57 57
  58 + // 获取公司下的所有用户
  59 + _, users, err := l.svcCtx.UserRepository.Find(l.ctx, conn, domain.NewQueryOptions().
  60 + WithFindOnly().
  61 + WithKV(" companyId", one.CompanyId))
  62 + if err != nil {
  63 + return nil, err
  64 + }
  65 +
58 // 更新 66 // 更新
59 - err = transaction.UseTrans(l.ctx, conn.DB(), func(ctx context.Context, c transaction.Conn) error { 67 + err = transaction.UseTrans(l.ctx, conn.DB(), func(ctx context.Context, conn transaction.Conn) error {
60 _, err = l.svcCtx.DepartmentRepository.UpdateWithVersion(l.ctx, conn, one) 68 _, err = l.svcCtx.DepartmentRepository.UpdateWithVersion(l.ctx, conn, one)
61 if err != nil { 69 if err != nil {
62 return err 70 return err
63 } 71 }
64 72
65 - // 获取公司下的所有用户  
66 - _, users, err := l.svcCtx.UserRepository.Find(l.ctx, conn, domain.NewQueryOptions().  
67 - WithFindOnly().  
68 - WithKV(" companyId", one.CompanyId))  
69 - if err != nil {  
70 - return err  
71 - }  
72 -  
73 var findIndex = func(ids []int64, id int64) int { 73 var findIndex = func(ids []int64, id int64) int {
74 for i, _ := range ids { 74 for i, _ := range ids {
75 if ids[i] == id { 75 if ids[i] == id {