作者 tangxvhui
... ... @@ -48,6 +48,7 @@ type (
DepartmentUpdateRequest {
Id int64 `path:"id"`
Name string `json:"name"`
Ids []int64 `json:"ids"` // 用户ID
}
DepartmentListRequest {
... ...
... ... @@ -14,14 +14,19 @@ info(
jwt: SystemAuth
)
service Core {
@doc "角色详情"
@handler systemGetRole
get /system/role/:id (RoleGetRequest) returns (RoleGetResponse)
@doc "角色新增"
@handler systemSaveRole
post /system/role (RoleSaveRequest) returns (RoleSaveResponse)
@doc "角色删除"
@handler systemDeleteRole
delete /system/role/:id (RoleDeleteRequest) returns (RoleDeleteResponse)
@doc "角色更新"
@handler systemUpdateRole
put /system/role/:id (RoleUpdateRequest) returns (RoleUpdateResponse)
@doc "角色列表搜索"
@handler systemSearchRole
post /system/role/search (RoleSearchRequest) returns (RoleSearchResponse)
}
... ...
... ... @@ -31,14 +31,14 @@ func (l *SystemAddLogic) SystemAdd(req *types.DepartmentAddRequest) (resp *types
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).WithFindOnly().
total, _, err := l.svcCtx.DepartmentRepository.Find(l.ctx, conn, domain.NewQueryOptions().
WithCountOnly().
WithKV("companyId", userToken.CompanyId).
WithKV("name", req.Name))
if err != nil {
return nil, err
}
if len(list) > 0 {
if total > 0 {
return nil, xerr.NewErrMsg("该分组名称已存在(不能重复)")
}
... ...
... ... @@ -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)
return err
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)
... ...
... ... @@ -33,9 +33,10 @@ func (l *MiniUserDepartmentUsersLogic) MiniUserDepartmentUsers(req *types.MiniUs
userToken = contextdata.GetUserTokenFromCtx(l.ctx)
users []*domain.User
departments []*domain.Department
groups = make([]DepartmentUser, 0)
)
resp = map[string]interface{}{
"list": make([]DepartmentUser, 0),
"list": groups,
}
_, departments, err = l.svcCtx.DepartmentRepository.Find(l.ctx, conn, domain.IndexCompanyId(userToken.CompanyId)().WithFindOnly())
if err != nil {
... ... @@ -58,13 +59,17 @@ func (l *MiniUserDepartmentUsersLogic) MiniUserDepartmentUsers(req *types.MiniUs
}
if lo.Contains(user.Departments, item.Id) && !groupUserSet.Contains(user.Id) {
group.Users = append(group.Users, &domain.User{
Id: user.Id,
Name: user.Name,
Id: user.Id,
Name: user.Name,
PinYinName: user.PinYinName,
})
}
}
groups = append(groups, group)
})
resp = map[string]interface{}{
"list": groups,
}
return
}
... ...
... ... @@ -2,6 +2,7 @@ package user
import (
"context"
"github.com/samber/lo"
"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"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/contextdata"
... ... @@ -33,6 +34,9 @@ func (l *SystemUserAccountEnableLogic) SystemUserAccountEnable(req *types.System
users []*domain.User
userToken = contextdata.GetUserTokenFromCtx(l.ctx)
)
if !lo.Contains([]int{domain.UserEnable, domain.UserDisable}, req.Status) {
return nil, xerr.NewErrMsg("启用状态有误")
}
if _, users, err = l.svcCtx.UserRepository.Find(l.ctx, conn, domain.IndexCompanyId(userToken.CompanyId)().MustWithKV("ids", req.UserIds).WithFindOnly()); err != nil {
return nil, xerr.NewErrMsgErr("公司不存在", err)
}
... ... @@ -50,5 +54,6 @@ func (l *SystemUserAccountEnableLogic) SystemUserAccountEnable(req *types.System
}, true); err != nil {
return nil, xerr.NewErrMsgErr("更新启用状态失败", err)
}
resp = &types.SystemUserAccountEnableResponse{}
return
}
... ...
... ... @@ -914,8 +914,9 @@ type DepartmentGetResponse struct {
}
type DepartmentUpdateRequest struct {
Id int64 `path:"id"`
Name string `json:"name"`
Id int64 `path:"id"`
Name string `json:"name"`
Ids []int64 `json:"ids"` // 用户ID
}
type DepartmentListRequest struct {
... ...
... ... @@ -211,8 +211,8 @@ func (repository *UserRepository) FindDepartmentUsers(ctx context.Context, conn
total int64
)
queryFunc := func() (interface{}, error) {
tx = tx.Model(&ms).Order("id desc")
tx.Select("id", "name", "departments")
tx = tx.Model(&ms).Order("pin_yin_name asc")
tx.Select("id", "name", "departments", "pin_yin_name")
tx.Where("company_id = ?", companyId)
tx.Where("audit_status in (?)", domain.UserAuditStatusPassed)
tx.Where("enable = ?", domain.UserEnable)
... ...