作者 tangxvhui
... ... @@ -29,6 +29,10 @@ service Core {
@doc "部门-更新"
@handler systemUpdate
put /system/department/:id (DepartmentUpdateRequest) returns (DepartmentGetResponse)
@doc "部门-删除"
@handler systemDelete
delete /system/department/:id (DepartmentGetRequest) returns (DepartmentGetResponse)
}
type (
... ...
package department
import (
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/result"
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/logic/department"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types"
)
func SystemDeleteHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.DepartmentGetRequest
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}
l := department.NewSystemDeleteLogic(r.Context(), svcCtx)
resp, err := l.SystemDelete(&req)
result.HttpResult(r, w, resp, err)
}
}
... ...
... ... @@ -412,6 +412,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
Path: "/system/department/:id",
Handler: department.SystemUpdateHandler(serverCtx),
},
{
Method: http.MethodDelete,
Path: "/system/department/:id",
Handler: department.SystemDeleteHandler(serverCtx),
},
},
rest.WithJwt(serverCtx.Config.SystemAuth.AccessSecret),
rest.WithPrefix("/v1"),
... ...
... ... @@ -48,7 +48,7 @@ func (l *SystemAddLogic) SystemAdd(req *types.DepartmentAddRequest) (resp *types
CompanyId: userToken.CompanyId,
Name: req.Name,
}
err = transaction.UseTrans(l.ctx, conn.DB(), func(ctx context.Context, c transaction.Conn) error {
err = transaction.UseTrans(l.ctx, conn.DB(), func(ctx context.Context, conn transaction.Conn) error {
_, err = l.svcCtx.DepartmentRepository.Insert(l.ctx, conn, insert)
if err != nil {
return err
... ...
package department
import (
"context"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/db/transaction"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
"github.com/zeromicro/go-zero/core/logx"
)
type SystemDeleteLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewSystemDeleteLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SystemDeleteLogic {
return &SystemDeleteLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *SystemDeleteLogic) SystemDelete(req *types.DepartmentGetRequest) (resp *types.DepartmentGetResponse, err error) {
var conn = l.svcCtx.DefaultDBConn()
one, err := l.svcCtx.DepartmentRepository.FindOne(l.ctx, conn, req.Id)
if err != nil {
return nil, err
}
// 获取公司下的所有用户
_, users, err := l.svcCtx.UserRepository.Find(l.ctx, conn, domain.NewQueryOptions().
WithFindOnly().
WithKV(" companyId", one.CompanyId))
if err != nil {
return nil, err
}
err = transaction.UseTrans(l.ctx, conn.DB(), func(ctx context.Context, conn transaction.Conn) error {
_, err = l.svcCtx.DepartmentRepository.Delete(l.ctx, conn, one)
if err != nil {
return err
}
// 移除用户中关联的分组
if len(users) > 0 {
var findIndex = func(ids []int64, id int64) int {
for i, _ := range ids {
if ids[i] == id {
return i
}
}
return -1
}
for i := range users {
user := users[i]
var targetIndex = findIndex(user.Departments, req.Id)
if targetIndex != -1 { // 归属分组存在,则移除
user.Departments = append(user.Departments[:targetIndex], user.Departments[targetIndex+1:]...) // 移除分组ID
_, err = l.svcCtx.UserRepository.UpdateWithVersion(l.ctx, conn, user)
if err != nil {
return err
}
}
}
}
return nil
}, true)
return
}
... ...
... ... @@ -55,18 +55,18 @@ func (l *SystemUpdateLogic) SystemUpdate(req *types.DepartmentUpdateRequest) (re
newIdMap[req.Ids[i]] = 0
}
// 更新
err = transaction.UseTrans(l.ctx, conn.DB(), func(ctx context.Context, c transaction.Conn) error {
_, err = l.svcCtx.DepartmentRepository.UpdateWithVersion(l.ctx, conn, one)
if err != nil {
return err
}
// 获取公司下的所有用户
_, users, err := l.svcCtx.UserRepository.Find(l.ctx, conn, domain.NewQueryOptions().
WithFindOnly().
WithKV(" companyId", one.CompanyId))
if err != nil {
return nil, err
}
// 更新
err = transaction.UseTrans(l.ctx, conn.DB(), func(ctx context.Context, conn transaction.Conn) error {
_, err = l.svcCtx.DepartmentRepository.UpdateWithVersion(l.ctx, conn, one)
if err != nil {
return err
}
... ...