作者 郑周

1. 编辑通讯录的上级

@@ -113,3 +113,31 @@ func (rs *RoleUserService) ListRole(in *command.UserRoleQueryCommand) (interface @@ -113,3 +113,31 @@ func (rs *RoleUserService) ListRole(in *command.UserRoleQueryCommand) (interface
113 } 113 }
114 return tool_funs.SimpleWrapGridMap(total, tempList), nil 114 return tool_funs.SimpleWrapGridMap(total, tempList), nil
115 } 115 }
  116 +
  117 +// GetHRBP 当前操作人是否拥有HRBP权限
  118 +func GetHRBP(transactionContext application.TransactionContext, companyId int, operatorId int) (int, error) {
  119 + roleRepo := factory.CreateRoleRepository(map[string]interface{}{"transactionContext": transactionContext})
  120 + roleUserRepo := factory.CreateRoleUserRepository(map[string]interface{}{"transactionContext": transactionContext})
  121 + _, roleList, err := roleRepo.Find(map[string]interface{}{"type": domain.RoleTypeSystem, "companyId": companyId})
  122 + if err != nil {
  123 + return -1, application.ThrowError(application.INTERNAL_SERVER_ERROR, "获取角色信息列表"+err.Error())
  124 + }
  125 + _, userRoleList, err := roleUserRepo.Find(map[string]interface{}{"companyId": companyId, "userId": operatorId})
  126 + if err != nil {
  127 + return -1, application.ThrowError(application.INTERNAL_SERVER_ERROR, "获取用户的角色信息列表"+err.Error())
  128 + }
  129 + // 拥有HRBP权限
  130 + hrbp := -1
  131 + for _, v := range userRoleList {
  132 + for _, v2 := range roleList {
  133 + if v.RoleId == v2.Id {
  134 + hrbp = 1
  135 + break
  136 + }
  137 + }
  138 + if hrbp == 1 {
  139 + break
  140 + }
  141 + }
  142 + return hrbp, nil
  143 +}
  1 +package adapter
  2 +
  3 +import "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
  4 +
  5 +type UserResp struct {
  6 + *domain.User
  7 + ParentName string `json:"parentName"` // 上级名称
  8 + PositionNames []string `json:"positionNames"` // 职位名称
  9 +}
  1 +package command
  2 +
  3 +import (
  4 + "github.com/beego/beego/v2/core/validation"
  5 +)
  6 +
  7 +type EditParentCommand struct {
  8 + Id int64 `cname:"用户ID" json:"id,string" valid:"Required"`
  9 + ParentId int64 `cname:"上级ID" json:"parentId,string" valid:"Required"`
  10 + CompanyId int `cname:"公司ID" json:"companyId"`
  11 + OperatorId int `cname:"操作人ID" json:"operatorId"`
  12 +}
  13 +
  14 +func (in *EditParentCommand) Valid(*validation.Validation) {
  15 +
  16 +}
@@ -4,6 +4,9 @@ import ( @@ -4,6 +4,9 @@ import (
4 "github.com/linmadan/egglib-go/core/application" 4 "github.com/linmadan/egglib-go/core/application"
5 "github.com/linmadan/egglib-go/utils/tool_funs" 5 "github.com/linmadan/egglib-go/utils/tool_funs"
6 "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/factory" 6 "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/factory"
  7 + service "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/role"
  8 + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/user/adapter"
  9 + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/user/command"
7 "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/user/query" 10 "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/user/query"
8 "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain" 11 "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
9 ) 12 )
@@ -46,6 +49,8 @@ func (us *UserService) ListByDepartment(in *query.ListByDepartmentQuery) (interf @@ -46,6 +49,8 @@ func (us *UserService) ListByDepartment(in *query.ListByDepartmentQuery) (interf
46 }() 49 }()
47 50
48 userRepo := factory.CreateUserRepository(map[string]interface{}{"transactionContext": transactionContext}) 51 userRepo := factory.CreateUserRepository(map[string]interface{}{"transactionContext": transactionContext})
  52 + positionRepo := factory.CreatePositionRepository(map[string]interface{}{"transactionContext": transactionContext})
  53 +
49 inMap := tool_funs.SimpleStructToMap(in) 54 inMap := tool_funs.SimpleStructToMap(in)
50 if in.DepartmentId == 0 { 55 if in.DepartmentId == 0 {
51 delete(inMap, "departmentId") // 删除部门ID字段 56 delete(inMap, "departmentId") // 删除部门ID字段
@@ -54,8 +59,108 @@ func (us *UserService) ListByDepartment(in *query.ListByDepartmentQuery) (interf @@ -54,8 +59,108 @@ func (us *UserService) ListByDepartment(in *query.ListByDepartmentQuery) (interf
54 if err != nil { 59 if err != nil {
55 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 60 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
56 } 61 }
  62 +
  63 + parentIds := make([]int64, 0) // 上级ID
  64 + positionIds := make([]int, 0) // 职位ID
  65 + for i := range list {
  66 + if list[i].ParentId > 0 {
  67 + parentIds = append(parentIds, list[i].ParentId)
  68 + }
  69 +
  70 + pList := list[i].PositionId
  71 + for i2 := range pList {
  72 + if pList[i2] > 0 {
  73 + positionIds = append(positionIds, pList[i2])
  74 + }
  75 + }
  76 + }
  77 +
  78 + // 获取上级
  79 + parentMap := map[int64]*domain.User{}
  80 + if len(parentIds) > 0 {
  81 + _, parentList, err := userRepo.Find(map[string]interface{}{"ids": parentIds})
  82 + if err != nil {
  83 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  84 + }
  85 + for i := range parentList {
  86 + parentMap[parentList[i].Id] = parentList[i]
  87 + }
  88 + }
  89 +
  90 + // 获取职位
  91 + positionMap := map[int64]*domain.Position{}
  92 + if len(positionIds) > 0 {
  93 + _, positionList, err := positionRepo.Find(map[string]interface{}{"ids": positionIds})
  94 + if err != nil {
  95 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  96 + }
  97 + for i := range positionList {
  98 + positionMap[positionList[i].Id] = positionList[i]
  99 + }
  100 + }
  101 +
  102 + adapters := make([]*adapter.UserResp, 0)
  103 + for i := range list {
  104 + adp := &adapter.UserResp{
  105 + User: list[i],
  106 + }
  107 + // 上级名称
  108 + if v, ok := parentMap[list[i].ParentId]; ok {
  109 + adp.ParentName = v.Name
  110 + }
  111 + // 职位名称数组
  112 + adp.PositionNames = make([]string, 0)
  113 + pList := list[i].PositionId
  114 + for i2 := range pList {
  115 + if v2, ok := positionMap[int64(pList[i2])]; ok {
  116 + adp.PositionNames = append(adp.PositionNames, v2.Name)
  117 + }
  118 + }
  119 + adapters = append(adapters, adp)
  120 + }
  121 +
57 if err := transactionContext.CommitTransaction(); err != nil { 122 if err := transactionContext.CommitTransaction(); err != nil {
58 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) 123 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
59 } 124 }
60 - return tool_funs.SimpleWrapGridMap(int64(count), list), nil 125 + return tool_funs.SimpleWrapGridMap(int64(count), adapters), nil
  126 +}
  127 +
  128 +// EditParentUser 保存上级
  129 +func (us *UserService) EditParentUser(in *command.EditParentCommand) error {
  130 + transactionContext, err := factory.ValidateStartTransaction(in)
  131 + if err != nil {
  132 + return err
  133 + }
  134 + defer func() {
  135 + transactionContext.RollbackTransaction()
  136 + }()
  137 +
  138 + hrbp, err := service.GetHRBP(transactionContext, in.CompanyId, in.OperatorId)
  139 + if err != nil {
  140 + return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  141 + }
  142 + if hrbp != 1 {
  143 + return application.ThrowError(application.BUSINESS_ERROR, "拥有HRBP权限才能编辑")
  144 + }
  145 +
  146 + userRepo := factory.CreateUserRepository(map[string]interface{}{"transactionContext": transactionContext})
  147 + user, err := userRepo.FindOne(map[string]interface{}{"id": in.Id})
  148 + if err != nil {
  149 + return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  150 + }
  151 + // 上级ID是否存在
  152 + _, err = userRepo.FindOne(map[string]interface{}{"id": in.ParentId})
  153 + if err != nil {
  154 + return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  155 + }
  156 + user.ParentId = in.ParentId
  157 +
  158 + if _, err = userRepo.Update(user); err != nil {
  159 + return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  160 + }
  161 +
  162 + if err := transactionContext.CommitTransaction(); err != nil {
  163 + return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  164 + }
  165 + return nil
61 } 166 }
@@ -4,6 +4,7 @@ import ( @@ -4,6 +4,7 @@ import (
4 "github.com/linmadan/egglib-go/core/application" 4 "github.com/linmadan/egglib-go/core/application"
5 "github.com/linmadan/egglib-go/web/beego" 5 "github.com/linmadan/egglib-go/web/beego"
6 "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/user" 6 "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/user"
  7 + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/user/command"
7 "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/user/query" 8 "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/user/query"
8 "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain" 9 "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
9 "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/port/beego/middlewares" 10 "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/port/beego/middlewares"
@@ -33,3 +34,15 @@ func (controller *UserController) ListByDepartment() { @@ -33,3 +34,15 @@ func (controller *UserController) ListByDepartment() {
33 controller.Response((&user.UserService{}).ListByDepartment(in)) 34 controller.Response((&user.UserService{}).ListByDepartment(in))
34 } 35 }
35 } 36 }
  37 +
  38 +func (controller *UserController) EditParentUser() {
  39 + in := &command.EditParentCommand{}
  40 + if err := controller.Unmarshal(in); err != nil {
  41 + controller.Response(nil, application.ThrowError(application.ARG_ERROR, err.Error()))
  42 + } else {
  43 + ua := middlewares.GetUser(controller.Ctx)
  44 + in.CompanyId = int(ua.CompanyId)
  45 + in.OperatorId = int(ua.UserId)
  46 + controller.Response(nil, (&user.UserService{}).EditParentUser(in))
  47 + }
  48 +}
@@ -12,6 +12,7 @@ func init() { @@ -12,6 +12,7 @@ func init() {
12 web.NSBefore(filters.AllowCors(), middlewares.CheckAdminToken()), 12 web.NSBefore(filters.AllowCors(), middlewares.CheckAdminToken()),
13 web.NSRouter("/search", &controllers.UserController{}, "Post:ListUsers"), 13 web.NSRouter("/search", &controllers.UserController{}, "Post:ListUsers"),
14 web.NSRouter("/list-dep", &controllers.UserController{}, "Post:ListByDepartment"), 14 web.NSRouter("/list-dep", &controllers.UserController{}, "Post:ListByDepartment"),
  15 + web.NSRouter("/edit-parent", &controllers.UserController{}, "Put:EditParentUser"),
15 ) 16 )
16 web.AddNamespace(ns) 17 web.AddNamespace(ns)
17 } 18 }