作者 唐旭辉

用户管理: 增加高管身份切换按钮

1 package command 1 package command
2 2
  3 +import (
  4 + "errors"
  5 +
  6 + "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain"
  7 +)
  8 +
3 type EditUserPermissionCommand struct { 9 type EditUserPermissionCommand struct {
4 Id int64 `json:"id"` 10 Id int64 `json:"id"`
5 CompanyId int64 `json:"-"` 11 CompanyId int64 `json:"-"`
6 PermissionType []int64 `json:"permissionType"` //权限数据 12 PermissionType []int64 `json:"permissionType"` //权限数据
7 CheckedPartner []int64 `json:"checkedPartner"` //可查看合伙人列表合伙人 13 CheckedPartner []int64 `json:"checkedPartner"` //可查看合伙人列表合伙人
  14 + IsSenior int8 `json:"isSenior"`
  15 +}
  16 +
  17 +func (cmd EditUserPermissionCommand) Validate() error {
  18 + if cmd.IsSenior <= 0 {
  19 + return errors.New("是否是高管必填")
  20 + }
  21 + if !(cmd.IsSenior == domain.UserIsSeniorNo || cmd.IsSenior == domain.UserIsSeniorYes) {
  22 + return errors.New("是否是高管必填")
  23 + }
  24 + return nil
8 } 25 }
@@ -383,6 +383,7 @@ func (service UsersService) buildGetUserData(userData *domain.Users, partnerList @@ -383,6 +383,7 @@ func (service UsersService) buildGetUserData(userData *domain.Users, partnerList
383 "isAdmin": 0, 383 "isAdmin": 0,
384 "status": 0, 384 "status": 0,
385 "checkedPartner": []map[string]interface{}{}, 385 "checkedPartner": []map[string]interface{}{},
  386 + "isSenior": userData.IsSenior,
386 } 387 }
387 if userData.IsSuperAdmin() { 388 if userData.IsSuperAdmin() {
388 result["isAdmin"] = 1 389 result["isAdmin"] = 1
@@ -433,6 +434,9 @@ func (service UsersService) EditUserPermission(cmd command.EditUserPermissionCom @@ -433,6 +434,9 @@ func (service UsersService) EditUserPermission(cmd command.EditUserPermissionCom
433 transactionContext, _ = factory.CreateTransactionContext(nil) 434 transactionContext, _ = factory.CreateTransactionContext(nil)
434 err error 435 err error
435 ) 436 )
  437 + if err = cmd.Validate(); err != nil {
  438 + return lib.ThrowError(lib.BUSINESS_ERROR, err.Error())
  439 + }
436 if err = transactionContext.StartTransaction(); err != nil { 440 if err = transactionContext.StartTransaction(); err != nil {
437 return lib.ThrowError(lib.TRANSACTION_ERROR, err.Error()) 441 return lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
438 } 442 }
@@ -501,9 +505,6 @@ func (service UsersService) EditUserPermission(cmd command.EditUserPermissionCom @@ -501,9 +505,6 @@ func (service UsersService) EditUserPermission(cmd command.EditUserPermissionCom
501 partners = append(partners, p) 505 partners = append(partners, p)
502 } 506 }
503 for i := range permissionList { 507 for i := range permissionList {
504 - // if permissionList[i].Code == domain.PERMINSSION_ADMIN_USER && !usersData.IsSuperAdmin() {  
505 - // return lib.ThrowError(lib.BUSINESS_ERROR, "操作异常")  
506 - // }  
507 p := domain.AdminPermissionBase{ 508 p := domain.AdminPermissionBase{
508 Id: permissionList[i].Id, 509 Id: permissionList[i].Id,
509 Code: permissionList[i].Code, 510 Code: permissionList[i].Code,
@@ -512,6 +513,7 @@ func (service UsersService) EditUserPermission(cmd command.EditUserPermissionCom @@ -512,6 +513,7 @@ func (service UsersService) EditUserPermission(cmd command.EditUserPermissionCom
512 } 513 }
513 updateMap := map[string]interface{}{ 514 updateMap := map[string]interface{}{
514 "AccessPartners": partners, 515 "AccessPartners": partners,
  516 + "IsSenior": cmd.IsSenior,
515 } 517 }
516 if !usersData.IsSuperAdmin() { 518 if !usersData.IsSuperAdmin() {
517 updateMap["Permission"] = permissionsBase 519 updateMap["Permission"] = permissionsBase
@@ -20,6 +20,12 @@ const ( @@ -20,6 +20,12 @@ const (
20 UserIsAdmin int8 = 2 20 UserIsAdmin int8 = 2
21 ) 21 )
22 22
  23 +//Users.IsSenior 用户是否是公司高管【1:是】【2:否】
  24 +const (
  25 + UserIsSeniorYes int8 = 1
  26 + UserIsSeniorNo int8 = 2
  27 +)
  28 +
23 //Users 企业平台的用户 29 //Users 企业平台的用户
24 type Users struct { 30 type Users struct {
25 Id int64 //用户id 31 Id int64 //用户id
@@ -38,11 +44,12 @@ type Users struct { @@ -38,11 +44,12 @@ type Users struct {
38 Avatar string ///头像 44 Avatar string ///头像
39 Remarks string //备注 45 Remarks string //备注
40 ChargeStatus int8 //是否为当前公司主管 【1:是】【2:否】 46 ChargeStatus int8 //是否为当前公司主管 【1:是】【2:否】
41 - CreateAt time.Time  
42 - UpdateAt time.Time 47 + CreateAt time.Time //
  48 + UpdateAt time.Time //
43 Permission []AdminPermissionBase //权限 49 Permission []AdminPermissionBase //权限
44 - AccessPartners []Partner 50 + AccessPartners []Partner //
45 AdminType int8 //是否是公司负责人,即超级管理员 1普通用户 2主管理员 51 AdminType int8 //是否是公司负责人,即超级管理员 1普通用户 2主管理员
  52 + IsSenior int8 //是否是公司高管【1:是】【2:否】;用于确定是否可以拥有“可查看的合伙人”
46 } 53 }
47 54
48 //IsUsable 用户是否可用 55 //IsUsable 用户是否可用
@@ -71,6 +78,17 @@ func (u Users) HasPermissionByCode(code string) bool { @@ -71,6 +78,17 @@ func (u Users) HasPermissionByCode(code string) bool {
71 return false 78 return false
72 } 79 }
73 80
  81 +func (u *Users) SetIsSenior(senior int8) {
  82 + switch senior {
  83 + case UserIsSeniorYes:
  84 + u.IsSenior = senior
  85 + case UserIsSeniorNo:
  86 + u.IsSenior = senior
  87 + u.AccessPartners = make([]Partner, 0)
  88 + default:
  89 + }
  90 +}
  91 +
74 func (u *Users) Update(m map[string]interface{}) error { 92 func (u *Users) Update(m map[string]interface{}) error {
75 if v, ok := m["CompanyId"]; ok { 93 if v, ok := m["CompanyId"]; ok {
76 u.CompanyId = v.(int64) 94 u.CompanyId = v.(int64)
@@ -126,6 +144,10 @@ func (u *Users) Update(m map[string]interface{}) error { @@ -126,6 +144,10 @@ func (u *Users) Update(m map[string]interface{}) error {
126 if v, ok := m["AdminType"]; ok { 144 if v, ok := m["AdminType"]; ok {
127 u.AdminType = v.(int8) 145 u.AdminType = v.(int8)
128 } 146 }
  147 + if v, ok := m["IsSenior"]; ok {
  148 + senior := v.(int8)
  149 + u.SetIsSenior(senior)
  150 + }
129 return nil 151 return nil
130 } 152 }
131 153
@@ -29,6 +29,7 @@ type Users struct { @@ -29,6 +29,7 @@ type Users struct {
29 ChargeStatus int8 //是否为当前公司主管 【1:是】【2:否】 29 ChargeStatus int8 //是否为当前公司主管 【1:是】【2:否】
30 Permission []domain.AdminPermissionBase //权限 30 Permission []domain.AdminPermissionBase //权限
31 AccessPartners []domain.Partner //可查看的合伙人 31 AccessPartners []domain.Partner //可查看的合伙人
  32 + IsSenior int8 //是否是公司高管【1:是】【2:否】;用于确定是否可以拥有“可查看的合伙人”
32 CreateAt time.Time 33 CreateAt time.Time
33 UpdateAt time.Time 34 UpdateAt time.Time
34 DeleteAt time.Time 35 DeleteAt time.Time
@@ -48,6 +48,7 @@ func (repository UsersRepository) transformPgModelToDomainModel(m *models.Users) @@ -48,6 +48,7 @@ func (repository UsersRepository) transformPgModelToDomainModel(m *models.Users)
48 Permission: m.Permission, 48 Permission: m.Permission,
49 AccessPartners: m.AccessPartners, 49 AccessPartners: m.AccessPartners,
50 AdminType: m.AdminType, 50 AdminType: m.AdminType,
  51 + IsSenior: m.IsSenior,
51 }, nil 52 }, nil
52 } 53 }
53 54
@@ -78,6 +79,7 @@ func (reponsitory UsersRepository) Add(u *domain.Users) error { @@ -78,6 +79,7 @@ func (reponsitory UsersRepository) Add(u *domain.Users) error {
78 Permission: u.Permission, 79 Permission: u.Permission,
79 AccessPartners: u.AccessPartners, 80 AccessPartners: u.AccessPartners,
80 AdminType: u.AdminType, 81 AdminType: u.AdminType,
  82 + IsSenior: u.IsSenior,
81 } 83 }
82 _, err = tx.Model(m).Insert() 84 _, err = tx.Model(m).Insert()
83 return err 85 return err
@@ -110,6 +112,7 @@ func (reponsitory UsersRepository) Edit(u *domain.Users) error { @@ -110,6 +112,7 @@ func (reponsitory UsersRepository) Edit(u *domain.Users) error {
110 Permission: u.Permission, 112 Permission: u.Permission,
111 AccessPartners: u.AccessPartners, 113 AccessPartners: u.AccessPartners,
112 AdminType: u.AdminType, 114 AdminType: u.AdminType,
  115 + IsSenior: u.IsSenior,
113 } 116 }
114 _, err = tx.Model(m).WherePK().Update() 117 _, err = tx.Model(m).WherePK().Update()
115 return err 118 return err
@@ -122,6 +122,7 @@ func (c *UserController) EditUserPermission() { @@ -122,6 +122,7 @@ func (c *UserController) EditUserPermission() {
122 Id int64 `json:"id"` 122 Id int64 `json:"id"`
123 PermissionType []int64 `json:"permissionType"` 123 PermissionType []int64 `json:"permissionType"`
124 CheckedPartner []int64 `json:"checkedPartner"` //合伙人 124 CheckedPartner []int64 `json:"checkedPartner"` //合伙人
  125 + IsSenior int8 `json:"isSenior"` //是否是高管【1:是】【2:否】
125 } 126 }
126 var ( 127 var (
127 param UserDetailParam 128 param UserDetailParam
@@ -139,6 +140,7 @@ func (c *UserController) EditUserPermission() { @@ -139,6 +140,7 @@ func (c *UserController) EditUserPermission() {
139 CompanyId: companyId, 140 CompanyId: companyId,
140 PermissionType: param.PermissionType, 141 PermissionType: param.PermissionType,
141 CheckedPartner: param.CheckedPartner, 142 CheckedPartner: param.CheckedPartner,
  143 + IsSenior: param.IsSenior,
142 }) 144 })
143 if err != nil { 145 if err != nil {
144 c.ResponseError(err) 146 c.ResponseError(err)