...
|
...
|
@@ -31,15 +31,15 @@ func (l *SystemUpdateLogic) SystemUpdate(req *types.DepartmentUpdateRequest) (re |
|
|
var userToken = contextdata.GetUserTokenFromCtx(l.ctx)
|
|
|
var conn = l.svcCtx.DefaultDBConn()
|
|
|
|
|
|
_, list, err := l.svcCtx.DepartmentRepository.Find(l.ctx, conn, domain.NewQueryOptions().
|
|
|
WithOffsetLimit(1, 1).WithCountOnly().
|
|
|
total, _, err := l.svcCtx.DepartmentRepository.Find(l.ctx, conn, domain.NewQueryOptions().
|
|
|
WithCountOnly().
|
|
|
WithKV("companyId", userToken.CompanyId).
|
|
|
WithKV("notId", req.Id). // 排除自己
|
|
|
WithKV("name", req.Name))
|
|
|
if err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
if len(list) > 0 {
|
|
|
if total > 0 {
|
|
|
return nil, xerr.NewErrMsg("该分组名称已存在(不能重复)")
|
|
|
}
|
|
|
|
...
|
...
|
@@ -49,10 +49,59 @@ func (l *SystemUpdateLogic) SystemUpdate(req *types.DepartmentUpdateRequest) (re |
|
|
}
|
|
|
one.Name = req.Name
|
|
|
|
|
|
// 更新分组中的用户Id
|
|
|
var newIdMap = map[int64]int{}
|
|
|
for i := range req.Ids {
|
|
|
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 err
|
|
|
}
|
|
|
|
|
|
var findIndex = func(ids []int64, id int64) int {
|
|
|
for i, _ := range ids {
|
|
|
if ids[i] == id {
|
|
|
return i
|
|
|
}
|
|
|
}
|
|
|
return -1
|
|
|
}
|
|
|
for i := range users {
|
|
|
var user = users[i]
|
|
|
if _, ok := newIdMap[user.Id]; ok {
|
|
|
var targetIndex = findIndex(user.Departments, req.Id)
|
|
|
if targetIndex == -1 { // 归属分组不存在,则新增
|
|
|
user.Departments = append(user.Departments)
|
|
|
_, err = l.svcCtx.UserRepository.UpdateWithVersion(l.ctx, conn, user)
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
}
|
|
|
} else {
|
|
|
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)
|
|
|
if err != nil {
|
|
|
return nil, xerr.NewErrMsgErr("分组修改失败", err)
|
...
|
...
|
|