作者 郑周

1.增加角色模型及功能

2.增加角色和用户关系模型及功能
@@ -5,6 +5,7 @@ go 1.16 @@ -5,6 +5,7 @@ go 1.16
5 require ( 5 require (
6 github.com/Shopify/sarama v1.25.0 6 github.com/Shopify/sarama v1.25.0
7 github.com/beego/beego/v2 v2.0.5 7 github.com/beego/beego/v2 v2.0.5
  8 + github.com/bwmarrin/snowflake v0.3.0
8 github.com/go-pg/pg/v10 v10.10.7 9 github.com/go-pg/pg/v10 v10.10.7
9 github.com/linmadan/egglib-go v0.0.0-20210827085852-177fa745932d 10 github.com/linmadan/egglib-go v0.0.0-20210827085852-177fa745932d
10 ) 11 )
@@ -6,12 +6,33 @@ import ( @@ -6,12 +6,33 @@ import (
6 "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain" 6 "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
7 pgDB "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/infrastructure/pg" 7 pgDB "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/infrastructure/pg"
8 "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/infrastructure/repository" 8 "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/infrastructure/repository"
  9 + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/utils"
9 ) 10 )
10 11
11 func CreateTransactionContext(options map[string]interface{}) (application.TransactionContext, error) { 12 func CreateTransactionContext(options map[string]interface{}) (application.TransactionContext, error) {
12 return pg.NewPGTransactionContext(pgDB.DB), nil 13 return pg.NewPGTransactionContext(pgDB.DB), nil
13 } 14 }
14 15
  16 +// StartTransaction 事务创建
  17 +func StartTransaction() (application.TransactionContext, error) {
  18 + transactionContext, err := CreateTransactionContext(nil)
  19 + if err != nil {
  20 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  21 + }
  22 + if err := transactionContext.StartTransaction(); err != nil {
  23 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  24 + }
  25 + return transactionContext, nil
  26 +}
  27 +
  28 +// ValidateStartTransaction 事务创建并校验入参
  29 +func ValidateStartTransaction(in interface{}) (application.TransactionContext, error) {
  30 + if err := utils.ValidateCommand(in); err != nil {
  31 + return nil, application.ThrowError(application.ARG_ERROR, err.Error())
  32 + }
  33 + return StartTransaction()
  34 +}
  35 +
15 func CreateCompanyRepository(options map[string]interface{}) domain.CompanyRepository { 36 func CreateCompanyRepository(options map[string]interface{}) domain.CompanyRepository {
16 var transactionContext *pg.TransactionContext 37 var transactionContext *pg.TransactionContext
17 if value, ok := options["transactionContext"]; ok { 38 if value, ok := options["transactionContext"]; ok {
@@ -35,3 +56,19 @@ func CreateDepartmentRepository(options map[string]interface{}) domain.Departmen @@ -35,3 +56,19 @@ func CreateDepartmentRepository(options map[string]interface{}) domain.Departmen
35 } 56 }
36 return repository.NewDepartmentRepository(transactionContext) 57 return repository.NewDepartmentRepository(transactionContext)
37 } 58 }
  59 +
  60 +func CreateRoleRepository(options map[string]interface{}) domain.RoleRepository {
  61 + var transactionContext *pg.TransactionContext
  62 + if value, ok := options["transactionContext"]; ok {
  63 + transactionContext = value.(*pg.TransactionContext)
  64 + }
  65 + return repository.NewRoleRepository(transactionContext)
  66 +}
  67 +
  68 +func CreateRoleUserRepository(options map[string]interface{}) domain.RoleUserRepository {
  69 + var transactionContext *pg.TransactionContext
  70 + if value, ok := options["transactionContext"]; ok {
  71 + transactionContext = value.(*pg.TransactionContext)
  72 + }
  73 + return repository.NewRoleUserRepository(transactionContext)
  74 +}
  1 +package adapter
  2 +
  3 +import (
  4 + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
  5 +)
  6 +
  7 +type RoleContainUser struct {
  8 + RoleId int64 `json:"roleId,string"`
  9 + UserId int64 `json:"userId,string"`
  10 + UserName string `json:"userName"`
  11 + UserEmail string `json:"userEmail"`
  12 +}
  13 +
  14 +type RoleUserAdapter struct {
  15 + domain.Role
  16 + Users []*RoleContainUser `json:"users"`
  17 +}
  1 +package command
  2 +
  3 +import "github.com/beego/beego/v2/core/validation"
  4 +
  5 +type CreateRoleCommand struct {
  6 + CompanyId int64 `cname:"公司ID" json:"companyId"`
  7 + Name string `cname:"角色名称" json:"name" valid:"Required"`
  8 + Description string `cname:"角色描述" json:"description"`
  9 +}
  10 +
  11 +func (in *CreateRoleCommand) Valid(validation *validation.Validation) {
  12 + if in.CompanyId == 0 {
  13 + validation.SetError("companyId", "公司ID无效")
  14 + return
  15 + }
  16 +
  17 + if len(in.Name) > 30 {
  18 + validation.SetError("name", "角色名称最大长度30个字符")
  19 + return
  20 + }
  21 +}
  1 +package command
  2 +
  3 +import "github.com/beego/beego/v2/core/validation"
  4 +
  5 +type DeleteRoleCommand struct {
  6 + CompanyId int64 `cname:"公司ID" json:"companyId"`
  7 + Id int64 `cname:"角色ID" json:"id,string" valid:"Required"`
  8 +}
  9 +
  10 +func (in *DeleteRoleCommand) Valid(validation *validation.Validation) {
  11 + if in.CompanyId == 0 {
  12 + validation.SetError("companyId", "公司ID无效")
  13 + return
  14 + }
  15 +}
  1 +package command
  2 +
  3 +import "github.com/beego/beego/v2/core/validation"
  4 +
  5 +// QueryRoleUserCommand 查询角色列表(关联用户)
  6 +type QueryRoleUserCommand struct {
  7 + CompanyId int64 `cname:"公司ID" json:"companyId"`
  8 + PageNumber int `cname:"分页页码" json:"pageNumber" valid:"Required"`
  9 + PageSize int `cname:"分页数量" json:"pageSize" valid:"Required"`
  10 +}
  11 +
  12 +func (in *QueryRoleUserCommand) Valid(validation *validation.Validation) {
  13 + if in.CompanyId == 0 {
  14 + validation.SetError("companyId", "公司ID无效")
  15 + return
  16 + }
  17 +}
  1 +package command
  2 +
  3 +import "github.com/beego/beego/v2/core/validation"
  4 +
  5 +type UpdateRoleCommand struct {
  6 + CompanyId int64 `cname:"公司ID" json:"companyId"`
  7 + Id int64 `cname:"角色ID" json:"id,string" valid:"Required"`
  8 + Name string `cname:"角色名称" json:"name" valid:"Required"`
  9 + Description string `cname:"角色描述" json:"description"`
  10 +}
  11 +
  12 +func (in *UpdateRoleCommand) Valid(validation *validation.Validation) {
  13 + if in.CompanyId == 0 {
  14 + validation.SetError("companyId", "公司ID无效")
  15 + }
  16 +
  17 + if len(in.Name) > 30 {
  18 + validation.SetError("name", "角色名称最大长度30个字符")
  19 + return
  20 + }
  21 +}
  1 +package command
  2 +
  3 +import "github.com/beego/beego/v2/core/validation"
  4 +
  5 +// UserRoleCreateCommand 用户加入到该角色中
  6 +type UserRoleCreateCommand struct {
  7 + CompanyId int64 `cname:"公司ID" json:"companyId"`
  8 + RoleId int64 `cname:"角色ID" json:"roleId,string" valid:"Required"`
  9 + UserIds []string `cname:"用户ID" json:"userIds"`
  10 +}
  11 +
  12 +func (in *UserRoleCreateCommand) Valid(validation *validation.Validation) {
  13 + if in.CompanyId == 0 {
  14 + validation.SetError("companyId", "公司ID无效")
  15 + return
  16 + }
  17 +
  18 + if len(in.UserIds) == 0 {
  19 + validation.SetError("userIds", "添加的用户不存在")
  20 + return
  21 + }
  22 +}
  1 +package command
  2 +
  3 +import "github.com/beego/beego/v2/core/validation"
  4 +
  5 +// UserRoleDeleteCommand 角色中移除用户
  6 +type UserRoleDeleteCommand struct {
  7 + CompanyId int64 `cname:"公司ID" json:"companyId"`
  8 + RoleId int64 `cname:"角色ID" json:"roleId,string" valid:"Required"`
  9 + UserId int64 `cname:"用户ID" json:"userId,string" valid:"Required"`
  10 +}
  11 +
  12 +func (in *UserRoleDeleteCommand) Valid(validation *validation.Validation) {
  13 + if in.CompanyId == 0 {
  14 + validation.SetError("companyId", "公司ID无效")
  15 + return
  16 + }
  17 +}
  1 +package command
  2 +
  3 +import "github.com/beego/beego/v2/core/validation"
  4 +
  5 +// UserRoleQueryCommand 角色中的用户
  6 +type UserRoleQueryCommand struct {
  7 + CompanyId int64 `cname:"公司ID" json:"companyId"`
  8 + RoleId int64 `cname:"角色ID" json:"roleId,string" valid:"Required"`
  9 + PageNumber int64 `cname:"分页页码" json:"pageNumber"`
  10 + PageSize int64 `cname:"分页数量" json:"pageSize"`
  11 +}
  12 +
  13 +func (in *UserRoleQueryCommand) Valid(validation *validation.Validation) {
  14 + if in.CompanyId == 0 {
  15 + validation.SetError("companyId", "公司ID无效")
  16 + return
  17 + }
  18 +}
  1 +package service
  2 +
  3 +import (
  4 + "github.com/linmadan/egglib-go/core/application"
  5 + "github.com/linmadan/egglib-go/utils/tool_funs"
  6 + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/factory"
  7 + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/role/adapter"
  8 + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/role/command"
  9 + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
  10 +)
  11 +
  12 +type RoleService struct {
  13 +}
  14 +
  15 +func NewRoleService() *RoleService {
  16 + newRoleService := &RoleService{}
  17 + return newRoleService
  18 +}
  19 +
  20 +// Create 创建
  21 +func (rs *RoleService) Create(in *command.CreateRoleCommand) (interface{}, error) {
  22 + transactionContext, err := factory.ValidateStartTransaction(in)
  23 + if err != nil {
  24 + return nil, err
  25 + }
  26 + defer func() {
  27 + transactionContext.RollbackTransaction()
  28 + }()
  29 + roleRepository := factory.CreateRoleRepository(map[string]interface{}{"transactionContext": transactionContext})
  30 +
  31 + // 检测名称重复
  32 + count, err := roleRepository.Count(map[string]interface{}{"name": in.Name, "companyId": in.CompanyId})
  33 + if err != nil {
  34 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  35 + }
  36 + if count > 0 {
  37 + return nil, application.ThrowError(application.BUSINESS_ERROR, "角色名称已存在")
  38 + }
  39 + newRole := &domain.Role{
  40 + Id: 0,
  41 + Name: in.Name,
  42 + Type: domain.RoleTypeCommon,
  43 + Description: in.Description,
  44 + CompanyId: in.CompanyId,
  45 + }
  46 + role, err := roleRepository.Insert(newRole)
  47 + if err != nil {
  48 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  49 + }
  50 + if err := transactionContext.CommitTransaction(); err != nil {
  51 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  52 + }
  53 + return role, nil
  54 +
  55 +}
  56 +
  57 +func (rs *RoleService) Update(in *command.UpdateRoleCommand) (interface{}, error) {
  58 + transactionContext, err := factory.ValidateStartTransaction(in)
  59 + if err != nil {
  60 + return nil, err
  61 + }
  62 + defer func() {
  63 + transactionContext.RollbackTransaction()
  64 + }()
  65 +
  66 + roleRepository := factory.CreateRoleRepository(map[string]interface{}{"transactionContext": transactionContext})
  67 +
  68 + // 检测名称重复(排除自己)
  69 + count, err := roleRepository.Count(map[string]interface{}{"name": in.Name, "companyId": in.CompanyId, "notId": in.Id})
  70 + if err != nil {
  71 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  72 + }
  73 + if count > 0 {
  74 + return nil, application.ThrowError(application.BUSINESS_ERROR, "角色名称已存在")
  75 + }
  76 +
  77 + role, err := roleRepository.FindOne(map[string]interface{}{"id": in.Id})
  78 + if err != nil {
  79 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  80 + }
  81 +
  82 + role.Name = in.Name
  83 + role.Name = in.Description
  84 +
  85 + role, err = roleRepository.Insert(role)
  86 + if err != nil {
  87 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  88 + }
  89 + if err := transactionContext.CommitTransaction(); err != nil {
  90 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  91 + }
  92 + return role, nil
  93 +}
  94 +
  95 +func (rs *RoleService) Remove(in *command.DeleteRoleCommand) (interface{}, error) {
  96 + transactionContext, err := factory.ValidateStartTransaction(in)
  97 + if err != nil {
  98 + return nil, err
  99 + }
  100 + defer func() {
  101 + transactionContext.RollbackTransaction()
  102 + }()
  103 +
  104 + roleRepository := factory.CreateRoleRepository(map[string]interface{}{"transactionContext": transactionContext})
  105 + roleUserRepository := factory.CreateRoleUserRepository(map[string]interface{}{"transactionContext": transactionContext})
  106 +
  107 + role, err := roleRepository.FindOne(map[string]interface{}{"id": in.Id})
  108 + if err != nil {
  109 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  110 + }
  111 + if _, err := roleRepository.Remove(role); err != nil {
  112 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  113 + }
  114 +
  115 + // 获取角色所有关联的用户,并删除
  116 + _, roleUsers, err := roleUserRepository.Find(map[string]interface{}{"roleId": in.Id, "companyId": in.CompanyId})
  117 + if err != nil {
  118 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  119 + }
  120 + ids := make([]int64, 0)
  121 + for i := range roleUsers {
  122 + ids = append(ids, roleUsers[i].Id)
  123 + }
  124 + if len(ids) > 0 {
  125 + err := roleUserRepository.BatchDeleteById(ids)
  126 + if err != nil {
  127 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  128 + }
  129 + }
  130 +
  131 + if err := transactionContext.CommitTransaction(); err != nil {
  132 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  133 + }
  134 + return role, nil
  135 +}
  136 +
  137 +func (rs *RoleService) ListForUser(in *command.QueryRoleUserCommand) (interface{}, error) {
  138 + transactionContext, err := factory.StartTransaction()
  139 + if err != nil {
  140 + return nil, err
  141 + }
  142 + defer func() {
  143 + transactionContext.RollbackTransaction()
  144 + }()
  145 + roleRepository := factory.CreateRoleRepository(map[string]interface{}{"transactionContext": transactionContext})
  146 + ruRepository := factory.CreateRoleUserRepository(map[string]interface{}{"transactionContext": transactionContext})
  147 +
  148 + in.PageNumber = 1
  149 + in.PageSize = 9999999
  150 + _, roles, err := roleRepository.Find(tool_funs.SimpleStructToMap(in))
  151 + if err != nil {
  152 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  153 + }
  154 + if len(roles) == 0 {
  155 + return nil, application.ThrowError(application.BUSINESS_ERROR, "未找到角色数据")
  156 + }
  157 +
  158 + adapterList := make([]*adapter.RoleUserAdapter, 0)
  159 + for i := range roles {
  160 + v := roles[i]
  161 + tempList, err := ruRepository.FindAllContainUser(1, 10, in.CompanyId, v.Id)
  162 + if err != nil {
  163 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  164 + }
  165 + roleUser := &adapter.RoleUserAdapter{}
  166 + roleUser.Id = v.Id
  167 + roleUser.Name = v.Name
  168 + roleUser.Type = v.Type
  169 + roleUser.Description = v.Description
  170 + roleUser.CompanyId = v.CompanyId
  171 + roleUser.Users = tempList
  172 + adapterList = append(adapterList, roleUser)
  173 + }
  174 +
  175 + return map[string]interface{}{"list": adapterList}, nil
  176 +}
  1 +package service
  2 +
  3 +import (
  4 + "github.com/linmadan/egglib-go/core/application"
  5 + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/factory"
  6 + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/role/command"
  7 + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
  8 + "strconv"
  9 +)
  10 +
  11 +type RoleUserService struct {
  12 +}
  13 +
  14 +func NewRoleUserService() *RoleUserService {
  15 + newRoleUserService := &RoleUserService{}
  16 + return newRoleUserService
  17 +}
  18 +
  19 +// Create 创建
  20 +func (rs *RoleUserService) Create(in *command.UserRoleCreateCommand) (interface{}, error) {
  21 + transactionContext, err := factory.ValidateStartTransaction(in)
  22 + if err != nil {
  23 + return nil, err
  24 + }
  25 + defer func() {
  26 + transactionContext.RollbackTransaction()
  27 + }()
  28 + roleUserRepository := factory.CreateRoleUserRepository(map[string]interface{}{"transactionContext": transactionContext})
  29 +
  30 + // 检测已存在的关联用户
  31 + _, rus, err := roleUserRepository.Find(map[string]interface{}{"roleId": in.RoleId, "companyId": in.CompanyId, "userIds": in.UserIds, "limit": 9999999})
  32 + if err != nil {
  33 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  34 + }
  35 + existUserIds := make(map[int64]int64)
  36 + for i := range rus {
  37 + existUserIds[rus[i].UserId] = rus[i].UserId
  38 + }
  39 + for i := range in.UserIds {
  40 + int64Num, _ := strconv.ParseInt(in.UserIds[i], 10, 64)
  41 + // 不存在关联关系时,新增数据
  42 + if _, ok := existUserIds[int64Num]; !ok {
  43 + newRoleUser := &domain.RoleUser{
  44 + Id: 0,
  45 + RoleId: in.RoleId,
  46 + UserId: int64Num,
  47 + CompanyId: in.CompanyId,
  48 + }
  49 + _, err := roleUserRepository.Insert(newRoleUser)
  50 + if err != nil {
  51 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  52 + }
  53 + }
  54 + }
  55 +
  56 + if err := transactionContext.CommitTransaction(); err != nil {
  57 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  58 + }
  59 + return nil, nil
  60 +}
  61 +
  62 +func (rs *RoleUserService) Remove(in *command.UserRoleDeleteCommand) (interface{}, error) {
  63 + transactionContext, err := factory.ValidateStartTransaction(in)
  64 + if err != nil {
  65 + return nil, err
  66 + }
  67 + defer func() {
  68 + transactionContext.RollbackTransaction()
  69 + }()
  70 +
  71 + roleUserRepository := factory.CreateRoleUserRepository(map[string]interface{}{"transactionContext": transactionContext})
  72 +
  73 + // 检测已存在的关联用户
  74 + _, rus, err := roleUserRepository.Find(map[string]interface{}{"roleId": in.RoleId, "companyId": in.CompanyId, "userId": in.UserId})
  75 + if err != nil {
  76 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  77 + }
  78 + if len(rus) == 0 {
  79 + return nil, application.ThrowError(application.BUSINESS_ERROR, "数据不存在")
  80 + }
  81 +
  82 + if _, err := roleUserRepository.Remove(rus[0]); err != nil {
  83 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  84 + }
  85 + if err := transactionContext.CommitTransaction(); err != nil {
  86 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  87 + }
  88 + return rus, nil
  89 +}
  90 +
  91 +//func (rs *RoleUserService) ListForUser(in *command.UserRoleQueryCommand) (interface{}, error) {
  92 +// transactionContext, err := factory.StartTransaction()
  93 +// if err != nil {
  94 +// return nil, err
  95 +// }
  96 +// defer func() {
  97 +// transactionContext.RollbackTransaction()
  98 +// }()
  99 +// roleRepository := factory.CreateRoleUserRepository(map[string]interface{}{"transactionContext": transactionContext})
  100 +// userRepository := factory.CreateUserRepository(map[string]interface{}{"transactionContext": transactionContext})
  101 +//
  102 +// in.PageNumber = 1
  103 +// in.PageSize = 9999999
  104 +//
  105 +// conditionMap := tool_funs.SimpleStructToMap(in)
  106 +// _, roles, err := roleRepository.Find(conditionMap)
  107 +// if err != nil {
  108 +// return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  109 +// }
  110 +// if len(roles) == 0 {
  111 +// return nil, application.ThrowError(application.BUSINESS_ERROR, "未找到角色数据")
  112 +// }
  113 +//
  114 +// ids := make([]int64, 0)
  115 +// for i := range roles {
  116 +// ids = append(ids, roles[i].Id)
  117 +// }
  118 +//
  119 +// _, users, err := userRepository.Find(conditionMap)
  120 +// if err != nil {
  121 +// return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  122 +// }
  123 +//
  124 +// //for i := range users {
  125 +// // users[i].RoleUserIds
  126 +// //}
  127 +//
  128 +// //if err := transactionContext.CommitTransaction(); err != nil {
  129 +// // return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  130 +// //}
  131 +// //groupAdapter := &adapter.RoleUserAdapter{}
  132 +// //newList := groupAdapter.TransformTree(groups)
  133 +// return map[string]interface{}{"list": users}, nil
  134 +//}
@@ -4,7 +4,7 @@ package constant @@ -4,7 +4,7 @@ package constant
4 4
5 import "os" 5 import "os"
6 6
7 -var KAFKA_HOSTS = "127.7.8.1:9094" // 1.116.151.79:9092 7 +var KAFKA_HOSTS = "127.0.0.1:9092" // 1.116.151.79:9092
8 8
9 var KAFKA_GROUP_ID = "performance_dev" 9 var KAFKA_GROUP_ID = "performance_dev"
10 10
1 -//go:build local  
2 -  
3 -package constant  
4 -  
5 -var KAFKA_HOSTS = "127.0.0.1:9092" // 1.116.151.79:9092  
6 -  
7 -var KAFKA_GROUP_ID = "performance_dev"  
8 -  
9 -var KAFKA_BUSINESS_ADMIN_TOPIC = "mmm-business-admin-dev"  
10 -  
11 -func init() {  
12 -  
13 -}  
  1 +package domain
  2 +
  3 +import "time"
  4 +
  5 +const (
  6 + RoleTypeCommon int = 0 // 角色类型-后台添加角色
  7 + RoleTypeSystem int = 1 // 角色类型-系统预制角色(不可删除、编辑)
  8 +)
  9 +
  10 +type Role struct {
  11 + Id int64 `json:"id,string"`
  12 + Name string `json:"name"`
  13 + Type int `json:"type"`
  14 + Description string `json:"description"`
  15 + CompanyId int64 `json:"companyId,string"`
  16 + CreatedAt time.Time `json:"createdAt"`
  17 + UpdatedAt time.Time `json:"updatedAt"`
  18 + DeletedAt *time.Time `json:"deletedAt"`
  19 +}
  20 +
  21 +type RoleRepository interface {
  22 + Insert(role *Role) (*Role, error)
  23 + Remove(role *Role) (*Role, error)
  24 + FindOne(queryOptions map[string]interface{}) (*Role, error)
  25 + Find(queryOptions map[string]interface{}) (int64, []*Role, error)
  26 + Count(queryOptions map[string]interface{}) (int64, error)
  27 +}
  1 +package domain
  2 +
  3 +import (
  4 + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/role/adapter"
  5 + "time"
  6 +)
  7 +
  8 +type RoleUser struct {
  9 + Id int64 `json:"id,string"`
  10 + RoleId int64 `json:"roleId,string"`
  11 + UserId int64 `json:"userId,string"`
  12 + CompanyId int64 `json:"companyId,string"`
  13 + CreatedAt time.Time `json:"createdAt"`
  14 + UpdatedAt time.Time `json:"updatedAt"`
  15 + DeletedAt *time.Time `json:"deletedAt"`
  16 +}
  17 +
  18 +type RoleUserRepository interface {
  19 + Insert(role *RoleUser) (*RoleUser, error)
  20 + Remove(role *RoleUser) (*RoleUser, error)
  21 + FindOne(queryOptions map[string]interface{}) (*RoleUser, error)
  22 + Find(queryOptions map[string]interface{}) (int64, []*RoleUser, error)
  23 + Count(queryOptions map[string]interface{}) (int64, error)
  24 + BatchDeleteById(ids []int64) error
  25 + FindAllContainUser(pageSize int, pageNumber int, companyId int64, roleId int64) ([]*adapter.RoleContainUser, error)
  26 +}
@@ -9,6 +9,7 @@ type User struct { @@ -9,6 +9,7 @@ type User struct {
9 CompanyId int64 // 公司编号 9 CompanyId int64 // 公司编号
10 AdminType int // 1普通员工 2 主管理员 10 AdminType int // 1普通员工 2 主管理员
11 Name string // 用户姓名 11 Name string // 用户姓名
  12 + Email string // 邮箱
12 Status int // 用户状态(1正常 2禁用) 13 Status int // 用户状态(1正常 2禁用)
13 DepartmentId []int // 用户归属的部门 14 DepartmentId []int // 用户归属的部门
14 UpdateAt time.Time // 更新时间 15 UpdateAt time.Time // 更新时间
@@ -2,6 +2,9 @@ package pg @@ -2,6 +2,9 @@ package pg
2 2
3 import ( 3 import (
4 "fmt" 4 "fmt"
  5 + "github.com/go-pg/pg/v10/orm"
  6 + "github.com/linmadan/egglib-go/persistent/pg/comment"
  7 + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/infrastructure/pg/models"
5 8
6 "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/log" 9 "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/log"
7 10
@@ -25,4 +28,26 @@ func init() { @@ -25,4 +28,26 @@ func init() {
25 }) 28 })
26 } 29 }
27 30
  31 + models := []interface{}{
  32 + &models.User{},
  33 + &models.Company{},
  34 + &models.Department{},
  35 + &models.Role{},
  36 + }
  37 +
  38 + if !constant.DISABLE_CREATE_TABLE {
  39 + for _, model := range models {
  40 + err := DB.Model(model).CreateTable(&orm.CreateTableOptions{
  41 + Temp: false,
  42 + IfNotExists: true,
  43 + FKConstraints: true,
  44 + })
  45 + if err != nil {
  46 + panic(err)
  47 + }
  48 + comment.AddComments(DB, model)
  49 + }
  50 + //建表索引
  51 + }
  52 +
28 } 53 }
  1 +package models
  2 +
  3 +import "time"
  4 +
  5 +type Role struct {
  6 + tableName struct{} `pg:"role" comment:"角色"`
  7 + Id int64 `pg:"pk:id" comment:"ID"`
  8 + Name string `comment:"角色名称"`
  9 + Type int `comment:"角色类型(0角色可删、1系统预置角色不可删)"`
  10 + Description string `comment:"角色描述"`
  11 + CompanyId int64 `comment:"公司ID"`
  12 + CreatedAt time.Time `comment:"创建时间"`
  13 + UpdatedAt time.Time `comment:"更新时间"`
  14 + DeletedAt *time.Time `comment:"删除时间"`
  15 +}
  1 +package models
  2 +
  3 +import "time"
  4 +
  5 +type RoleUser struct {
  6 + tableName struct{} `pg:"role_user" comment:"角色用户关系"`
  7 + Id int64 `pg:"pk:id" comment:"ID"`
  8 + RoleId int64 `comment:"角色ID"`
  9 + UserId int64 `comment:"用户ID"`
  10 + CompanyId int64 `comment:"公司ID"`
  11 + CreatedAt time.Time `comment:"创建时间"`
  12 + UpdatedAt time.Time `comment:"更新时间"`
  13 + DeletedAt *time.Time `comment:"删除时间"`
  14 +}
@@ -10,6 +10,7 @@ type User struct { @@ -10,6 +10,7 @@ type User struct {
10 CompanyId int64 // 公司编号 10 CompanyId int64 // 公司编号
11 AdminType int // 1普通员工 2 主管理员 11 AdminType int // 1普通员工 2 主管理员
12 Name string // 用户姓名 12 Name string // 用户姓名
  13 + Email string // 邮箱
13 Status int // 用户状态(1正常 2禁用) 14 Status int // 用户状态(1正常 2禁用)
14 DepartmentId []int // 用户归属的部门 15 DepartmentId []int // 用户归属的部门
15 UpdateAt time.Time // 更新时间 16 UpdateAt time.Time // 更新时间
  1 +package repository
  2 +
  3 +import (
  4 + "errors"
  5 + "fmt"
  6 + "github.com/go-pg/pg/v10"
  7 + "github.com/linmadan/egglib-go/persistent/pg/sqlbuilder"
  8 + pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
  9 + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
  10 + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/infrastructure/pg/models"
  11 + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/utils"
  12 + "time"
  13 +)
  14 +
  15 +type RoleRepository struct {
  16 + transactionContext *pgTransaction.TransactionContext
  17 +}
  18 +
  19 +func NewRoleRepository(transactionContext *pgTransaction.TransactionContext) *RoleRepository {
  20 + return &RoleRepository{transactionContext: transactionContext}
  21 +}
  22 +
  23 +func (repo *RoleRepository) TransformToDomain(m *models.Role) domain.Role {
  24 + return domain.Role{
  25 + Id: m.Id,
  26 + Name: m.Name,
  27 + Type: m.Type,
  28 + Description: m.Description,
  29 + CompanyId: m.CompanyId,
  30 + CreatedAt: m.CreatedAt,
  31 + UpdatedAt: m.UpdatedAt,
  32 + DeletedAt: m.DeletedAt,
  33 + }
  34 +}
  35 +
  36 +func (repo *RoleRepository) TransformToModel(d *domain.Role) models.Role {
  37 + return models.Role{
  38 + Id: d.Id,
  39 + Name: d.Name,
  40 + Type: d.Type,
  41 + Description: d.Description,
  42 + CompanyId: d.CompanyId,
  43 + CreatedAt: d.CreatedAt,
  44 + UpdatedAt: d.UpdatedAt,
  45 + DeletedAt: d.DeletedAt,
  46 + }
  47 +}
  48 +
  49 +func (repo *RoleRepository) nextIdentify() (int64, error) {
  50 + return utils.NewSnowflakeId()
  51 +}
  52 +
  53 +func (repo *RoleRepository) Insert(d *domain.Role) (*domain.Role, error) {
  54 + var isCreate = d.Id == 0
  55 + if isCreate {
  56 + id, err := repo.nextIdentify()
  57 + if err != nil {
  58 + return d, err
  59 + }
  60 + d.Id = id
  61 + d.CreatedAt = time.Now()
  62 + d.UpdatedAt = d.CreatedAt
  63 + } else {
  64 + d.UpdatedAt = time.Now()
  65 + }
  66 + m := repo.TransformToModel(d)
  67 + tx := repo.transactionContext.PgTx
  68 + var err error
  69 + if isCreate {
  70 + _, err = tx.Model(&m).Returning("id").Insert()
  71 + } else {
  72 + _, err = tx.Model(&m).Returning("id").WherePK().Update() // 更新和删除必须增加条件
  73 + }
  74 + if err != nil {
  75 + return nil, err
  76 + }
  77 + d.Id = m.Id
  78 + return d, nil
  79 +}
  80 +
  81 +func (repo *RoleRepository) Remove(d *domain.Role) (*domain.Role, error) {
  82 + tx := repo.transactionContext.PgTx
  83 + nowTime := time.Now()
  84 + m := repo.TransformToModel(d)
  85 + m.DeletedAt = &nowTime
  86 + if _, err := tx.Model(&m).WherePK().Update(); err != nil {
  87 + return d, err
  88 + }
  89 + return d, nil
  90 +}
  91 +
  92 +func (repo *RoleRepository) FindOne(queryOptions map[string]interface{}) (*domain.Role, error) {
  93 + tx := repo.transactionContext.PgTx
  94 + m := new(models.Role)
  95 + query := tx.Model(m)
  96 + query.Where("deleted_at isnull")
  97 + if id, ok := queryOptions["id"]; ok {
  98 + query.Where("id=?", id)
  99 + }
  100 + if err := query.First(); err != nil {
  101 + if errors.Is(err, pg.ErrNoRows) {
  102 + return nil, fmt.Errorf("没有此资源")
  103 + } else {
  104 + return nil, err
  105 + }
  106 + }
  107 + u := repo.TransformToDomain(m)
  108 + return &u, nil
  109 +}
  110 +
  111 +func (repo *RoleRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.Role, error) {
  112 + tx := repo.transactionContext.PgTx
  113 + var m []*models.Role
  114 + query := tx.Model(&m).
  115 + Where("deleted_at isnull").
  116 + Limit(20)
  117 +
  118 + if name, ok := queryOptions["name"]; ok {
  119 + query.Where("name = ?", name)
  120 + }
  121 +
  122 + if companyId, ok := queryOptions["companyId"]; ok {
  123 + query.Where("company_id = ?", companyId)
  124 + }
  125 +
  126 + if v, ok := queryOptions["limit"].(int); ok {
  127 + query.Limit(v)
  128 + }
  129 + if v, ok := queryOptions["offset"].(int); ok {
  130 + query.Offset(v)
  131 + }
  132 +
  133 + count, err := query.SelectAndCount()
  134 + if err != nil {
  135 + return 0, nil, err
  136 + }
  137 + var arrays []*domain.Role
  138 + for _, v := range m {
  139 + d := repo.TransformToDomain(v)
  140 + arrays = append(arrays, &d)
  141 + }
  142 + return int64(count), arrays, nil
  143 +}
  144 +
  145 +func (repo *RoleRepository) Count(queryOptions map[string]interface{}) (int64, error) {
  146 + tx := repo.transactionContext.PgTx
  147 + m := new(models.Role)
  148 + query := sqlbuilder.BuildQuery(tx.Model(m), queryOptions)
  149 + query.Where("deleted_at isnull")
  150 +
  151 + if id, ok := queryOptions["id"]; ok {
  152 + query.Where("id = ?", id)
  153 + }
  154 +
  155 + if notId, ok := queryOptions["notId"]; ok {
  156 + query.Where("id != ?", notId)
  157 + }
  158 +
  159 + if name, ok := queryOptions["name"]; ok {
  160 + query.Where("name = ?", name)
  161 + }
  162 +
  163 + if companyId, ok := queryOptions["companyId"]; ok {
  164 + query.Where("company_id = ?", companyId)
  165 + }
  166 +
  167 + count, err := query.Count()
  168 + if err != nil {
  169 + return 0, err
  170 + }
  171 + return int64(count), nil
  172 +}
  1 +package repository
  2 +
  3 +import (
  4 + "errors"
  5 + "fmt"
  6 + "github.com/go-pg/pg/v10"
  7 + "github.com/linmadan/egglib-go/persistent/pg/sqlbuilder"
  8 + pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
  9 + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/role/adapter"
  10 + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
  11 + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/infrastructure/pg/models"
  12 + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/utils"
  13 + "time"
  14 +)
  15 +
  16 +type RoleUserRepository struct {
  17 + transactionContext *pgTransaction.TransactionContext
  18 +}
  19 +
  20 +func NewRoleUserRepository(transactionContext *pgTransaction.TransactionContext) *RoleUserRepository {
  21 + return &RoleUserRepository{transactionContext: transactionContext}
  22 +}
  23 +
  24 +func (repo *RoleUserRepository) TransformToDomain(m *models.RoleUser) domain.RoleUser {
  25 + return domain.RoleUser{
  26 + Id: m.Id,
  27 + RoleId: m.RoleId,
  28 + UserId: m.UserId,
  29 + CompanyId: m.CompanyId,
  30 + CreatedAt: m.CreatedAt,
  31 + UpdatedAt: m.UpdatedAt,
  32 + DeletedAt: m.DeletedAt,
  33 + }
  34 +}
  35 +
  36 +func (repo *RoleUserRepository) TransformToModel(d *domain.RoleUser) models.RoleUser {
  37 + return models.RoleUser{
  38 + Id: d.Id,
  39 + RoleId: d.RoleId,
  40 + UserId: d.UserId,
  41 + CompanyId: d.CompanyId,
  42 + CreatedAt: d.CreatedAt,
  43 + UpdatedAt: d.UpdatedAt,
  44 + DeletedAt: d.DeletedAt,
  45 + }
  46 +}
  47 +
  48 +func (repo *RoleUserRepository) nextIdentify() (int64, error) {
  49 + return utils.NewSnowflakeId()
  50 +}
  51 +
  52 +func (repo *RoleUserRepository) Insert(d *domain.RoleUser) (*domain.RoleUser, error) {
  53 + var isCreate = d.Id == 0
  54 + if isCreate {
  55 + id, err := repo.nextIdentify()
  56 + if err != nil {
  57 + return d, err
  58 + }
  59 + d.Id = id
  60 + d.CreatedAt = time.Now()
  61 + d.UpdatedAt = d.CreatedAt
  62 + } else {
  63 + d.UpdatedAt = time.Now()
  64 + }
  65 + m := repo.TransformToModel(d)
  66 + tx := repo.transactionContext.PgTx
  67 + var err error
  68 + if isCreate {
  69 + _, err = tx.Model(&m).Returning("id").Insert()
  70 + } else {
  71 + _, err = tx.Model(&m).Returning("id").WherePK().Update() // 更新和删除必须增加条件
  72 + }
  73 + if err != nil {
  74 + return nil, err
  75 + }
  76 + d.Id = m.Id
  77 + return d, nil
  78 +}
  79 +
  80 +func (repo *RoleUserRepository) Remove(d *domain.RoleUser) (*domain.RoleUser, error) {
  81 + tx := repo.transactionContext.PgTx
  82 + nowTime := time.Now()
  83 + m := repo.TransformToModel(d)
  84 + m.DeletedAt = &nowTime
  85 + // 真删
  86 + if _, err := tx.Model(&m).WherePK().Delete(); err != nil {
  87 + return d, err
  88 + }
  89 + return d, nil
  90 +}
  91 +
  92 +func (repo *RoleUserRepository) FindOne(queryOptions map[string]interface{}) (*domain.RoleUser, error) {
  93 + tx := repo.transactionContext.PgTx
  94 + m := new(models.RoleUser)
  95 + query := tx.Model(m)
  96 + query.Where("deleted_at isnull")
  97 + if id, ok := queryOptions["id"]; ok {
  98 + query.Where("id=?", id)
  99 + }
  100 + if err := query.First(); err != nil {
  101 + if errors.Is(err, pg.ErrNoRows) {
  102 + return nil, fmt.Errorf("没有此资源")
  103 + } else {
  104 + return nil, err
  105 + }
  106 + }
  107 + u := repo.TransformToDomain(m)
  108 + return &u, nil
  109 +}
  110 +
  111 +func (repo *RoleUserRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.RoleUser, error) {
  112 + tx := repo.transactionContext.PgTx
  113 + var m []*models.RoleUser
  114 + query := tx.Model(&m).Where("deleted_at isnull").Limit(20)
  115 +
  116 + if roleId, ok := queryOptions["roleId"]; ok {
  117 + query.Where("role_id = ?", roleId)
  118 + }
  119 +
  120 + if companyId, ok := queryOptions["companyId"]; ok {
  121 + query.Where("company_id = ?", companyId)
  122 + }
  123 +
  124 + if userId, ok := queryOptions["userId"]; ok {
  125 + query.Where("user_id = ?", userId)
  126 + }
  127 +
  128 + if userIds, ok := queryOptions["userIds"]; ok {
  129 + query.Where("user_id in (?)", userIds)
  130 + }
  131 +
  132 + if v, ok := queryOptions["limit"].(int); ok {
  133 + query.Limit(v)
  134 + }
  135 + if v, ok := queryOptions["offset"].(int); ok {
  136 + query.Offset(v)
  137 + }
  138 +
  139 + count, err := query.SelectAndCount()
  140 + if err != nil {
  141 + return 0, nil, err
  142 + }
  143 + var arrays []*domain.RoleUser
  144 + for _, v := range m {
  145 + d := repo.TransformToDomain(v)
  146 + arrays = append(arrays, &d)
  147 + }
  148 + return int64(count), arrays, nil
  149 +}
  150 +
  151 +func (repo *RoleUserRepository) Count(queryOptions map[string]interface{}) (int64, error) {
  152 + tx := repo.transactionContext.PgTx
  153 + m := new(models.RoleUser)
  154 + query := sqlbuilder.BuildQuery(tx.Model(m), queryOptions)
  155 + query.Where("deleted_at isnull")
  156 +
  157 + if id, ok := queryOptions["id"]; ok {
  158 + query.Where("id = ?", id)
  159 + }
  160 +
  161 + if notId, ok := queryOptions["notId"]; ok {
  162 + query.Where("id != ?", notId)
  163 + }
  164 +
  165 + if name, ok := queryOptions["name"]; ok {
  166 + query.Where("name = ?", name)
  167 + }
  168 +
  169 + if companyId, ok := queryOptions["companyId"]; ok {
  170 + query.Where("company_id = ?", companyId)
  171 + }
  172 +
  173 + count, err := query.Count()
  174 + if err != nil {
  175 + return 0, err
  176 + }
  177 + return int64(count), nil
  178 +}
  179 +
  180 +func (repo *RoleUserRepository) BatchDeleteById(ids []int64) error {
  181 + tx := repo.transactionContext.PgTx
  182 + _, err := tx.Model(&models.RoleUser{}).Where("id IN (?)", pg.In(ids)).Delete()
  183 + return err
  184 +}
  185 +
  186 +func (repo *RoleUserRepository) FindAllContainUser(pageSize int, pageNumber int, companyId int64, roleId int64) ([]*adapter.RoleContainUser, error) {
  187 + limit := pageSize
  188 + offset := limit * (pageNumber - 1)
  189 + if offset < 0 {
  190 + offset = 0
  191 + }
  192 + dataSql := ` SELECT
  193 + "role_user".role_id,
  194 + "role_user".user_id,
  195 + "user".name as user_name,
  196 + "user".email as user_email,
  197 +`
  198 + whereFrom := `
  199 + FROM "role_user"
  200 + LEFT JOIN "user" ON "user".id = "role_user".user_id
  201 + WHERE 1=1 AND "role_user".deleted_at ISNULL `
  202 +
  203 + var param []interface{}
  204 + if companyId > 0 {
  205 + param = append(param, companyId)
  206 + whereFrom += ` AND role_user.company_id=? `
  207 + }
  208 + if roleId >= 0 {
  209 + param = append(param, roleId)
  210 + whereFrom += ` AND role_user.role_id =? `
  211 + }
  212 + dataSql += whereFrom
  213 + dataSql = fmt.Sprintf("%s limit %d offset %d", dataSql, limit, offset)
  214 +
  215 + tx := repo.transactionContext.PgTx
  216 + var dataList = make([]*adapter.RoleContainUser, 0)
  217 + _, err := tx.Query(&dataList, dataSql, param...)
  218 + return dataList, err
  219 +}
@@ -51,6 +51,7 @@ func (repo *UserRepository) Update(user *domain.User) (*domain.User, error) { @@ -51,6 +51,7 @@ func (repo *UserRepository) Update(user *domain.User) (*domain.User, error) {
51 CompanyId: user.CompanyId, 51 CompanyId: user.CompanyId,
52 AdminType: user.AdminType, 52 AdminType: user.AdminType,
53 Name: user.Name, 53 Name: user.Name,
  54 + Email: user.Email,
54 Status: user.Status, 55 Status: user.Status,
55 UpdateAt: user.UpdateAt, 56 UpdateAt: user.UpdateAt,
56 CreateAt: user.CreateAt, 57 CreateAt: user.CreateAt,
@@ -139,6 +140,7 @@ func (repo *UserRepository) TransformToUserDomain(user *models.User) *domain.Use @@ -139,6 +140,7 @@ func (repo *UserRepository) TransformToUserDomain(user *models.User) *domain.Use
139 CompanyId: user.CompanyId, 140 CompanyId: user.CompanyId,
140 AdminType: user.AdminType, 141 AdminType: user.AdminType,
141 Name: user.Name, 142 Name: user.Name,
  143 + Email: user.Email,
142 Status: user.Status, 144 Status: user.Status,
143 UpdateAt: user.UpdateAt, 145 UpdateAt: user.UpdateAt,
144 CreateAt: user.CreateAt, 146 CreateAt: user.CreateAt,
  1 +package controllers
  2 +
  3 +import (
  4 + "github.com/linmadan/egglib-go/core/application"
  5 + "github.com/linmadan/egglib-go/web/beego"
  6 + service "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/role"
  7 + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/role/command"
  8 + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/port/beego/middlewares"
  9 +)
  10 +
  11 +type RoleController struct {
  12 + beego.BaseController
  13 +}
  14 +
  15 +func (controller *RoleController) CreateRole() {
  16 + ruService := service.NewRoleService()
  17 + in := &command.CreateRoleCommand{}
  18 + if err := controller.Unmarshal(in); err != nil {
  19 + controller.Response(nil, application.ThrowError(application.ARG_ERROR, err.Error()))
  20 + } else {
  21 + in.CompanyId = middlewares.GetCompanyId(controller.Ctx)
  22 + controller.Response(ruService.Create(in))
  23 + }
  24 +}
  25 +
  26 +func (controller *RoleController) UpdateRole() {
  27 + ruService := service.NewRoleService()
  28 + in := &command.UpdateRoleCommand{}
  29 + if err := controller.Unmarshal(in); err != nil {
  30 + controller.Response(nil, application.ThrowError(application.ARG_ERROR, err.Error()))
  31 + } else {
  32 + in.CompanyId = middlewares.GetCompanyId(controller.Ctx)
  33 + controller.Response(ruService.Update(in))
  34 + }
  35 +}
  36 +
  37 +func (controller *RoleController) RemoveRole() {
  38 + ruService := service.NewRoleService()
  39 + in := &command.DeleteRoleCommand{}
  40 + if err := controller.Unmarshal(in); err != nil {
  41 + controller.Response(nil, application.ThrowError(application.ARG_ERROR, err.Error()))
  42 + } else {
  43 + in.CompanyId = middlewares.GetCompanyId(controller.Ctx)
  44 + controller.Response(ruService.Remove(in))
  45 + }
  46 +}
  1 +package controllers
  2 +
  3 +import (
  4 + "github.com/linmadan/egglib-go/core/application"
  5 + "github.com/linmadan/egglib-go/web/beego"
  6 + service "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/role"
  7 + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/role/command"
  8 + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/port/beego/middlewares"
  9 +)
  10 +
  11 +type RoleUserController struct {
  12 + beego.BaseController
  13 +}
  14 +
  15 +func (controller *RoleUserController) CreateRoleUser() {
  16 + ruService := service.NewRoleUserService()
  17 + in := &command.UserRoleCreateCommand{}
  18 + if err := controller.Unmarshal(in); err != nil {
  19 + controller.Response(nil, application.ThrowError(application.ARG_ERROR, err.Error()))
  20 + } else {
  21 + in.CompanyId = middlewares.GetCompanyId(controller.Ctx)
  22 + controller.Response(ruService.Create(in))
  23 + }
  24 +}
  25 +
  26 +func (controller *RoleUserController) RemoveRoleUser() {
  27 + ruService := service.NewRoleUserService()
  28 + in := &command.UserRoleDeleteCommand{}
  29 + if err := controller.Unmarshal(in); err != nil {
  30 + controller.Response(nil, application.ThrowError(application.ARG_ERROR, err.Error()))
  31 + } else {
  32 + in.CompanyId = middlewares.GetCompanyId(controller.Ctx)
  33 + controller.Response(ruService.Remove(in))
  34 + }
  35 +}
  1 +package middlewares
  2 +
  3 +import (
  4 + "github.com/beego/beego/v2/server/web/context"
  5 +)
  6 +
  7 +func setUserId(userId int64, ctx *context.Context) {
  8 + ctx.Input.SetData("_UserId", userId)
  9 +}
  10 +
  11 +func GetUserId(ctx *context.Context) int64 {
  12 + userId := ctx.Input.GetData("_UserId")
  13 + return userId.(int64)
  14 +}
  15 +
  16 +func setCompanyId(companyId int64, ctx *context.Context) {
  17 + ctx.Input.SetData("_CompanyId", companyId)
  18 +}
  19 +
  20 +func GetCompanyId(ctx *context.Context) int64 {
  21 + companyId := ctx.Input.GetData("_CompanyId")
  22 + return companyId.(int64)
  23 +}
  24 +
  25 +func setCompanyType(companyId int, ctx *context.Context) {
  26 + ctx.Input.SetData("_CompanyType", companyId)
  27 +}
  28 +
  29 +func GetCompanyType(ctx *context.Context) int {
  30 + companyId := ctx.Input.GetData("_CompanyType")
  31 + return companyId.(int)
  32 +}
  33 +
  34 +func invalidOrExpired(ctx *context.Context) {
  35 + resp := map[string]interface{}{
  36 + "code": 902,
  37 + "msg": "Authorization过期或无效,需要进行重新获取令牌",
  38 + }
  39 + _ = ctx.Output.JSON(resp, false, false)
  40 +}
  41 +
  42 +func CheckToken() func(ctx *context.Context) {
  43 + return func(ctx *context.Context) {
  44 + tokenStr := ctx.Input.Header("x-mmm-accesstoken")
  45 + if tokenStr == "" { //没有带token
  46 + invalidOrExpired(ctx)
  47 + return
  48 + }
  49 +
  50 + //userServe := service.UserService{}
  51 + //userTk, err := userServe.ValidLoginToken(tokenStr)
  52 + //if err != nil {
  53 + // invalidOrExpired(ctx)
  54 + // return
  55 + //}
  56 + //setUserId(userTk.UserId, ctx)
  57 + //setCompanyId(userTk.CompanyId, ctx)
  58 + //setCompanyType(userTk.CompanyType, ctx)
  59 + }
  60 +}
  1 +package utils
  2 +
  3 +import (
  4 + "fmt"
  5 + "github.com/beego/beego/v2/core/validation"
  6 + "reflect"
  7 + "strings"
  8 +)
  9 +
  10 +// ValidateCommand 验证输入参数
  11 +func ValidateCommand(commandType interface{}) error {
  12 + valid := validation.Validation{}
  13 + b, err := valid.Valid(commandType)
  14 + if err != nil {
  15 + return err
  16 + }
  17 + if !b {
  18 + elem := reflect.TypeOf(commandType).Elem()
  19 + for _, validErr := range valid.Errors {
  20 + field, isExist := elem.FieldByName(validErr.Field)
  21 + if isExist {
  22 + if tag := field.Tag.Get("cname"); len(tag) > 0 {
  23 + return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, tag, -1))
  24 + } else {
  25 + return fmt.Errorf(validErr.Message)
  26 + }
  27 + } else {
  28 + return fmt.Errorf(validErr.Message)
  29 + }
  30 + }
  31 + }
  32 + return nil
  33 +}
  1 +package utils
  2 +
  3 +import (
  4 + "github.com/bwmarrin/snowflake"
  5 +)
  6 +
  7 +//生成新ID
  8 +var snowFlakeNode *snowflake.Node
  9 +
  10 +func NewSnowflakeId() (int64, error) {
  11 + if snowFlakeNode == nil {
  12 + node, err := snowflake.NewNode(1)
  13 + if err != nil {
  14 + return 0, err
  15 + }
  16 + snowFlakeNode = node
  17 + }
  18 + // Generate a snowflake ID.
  19 + id := snowFlakeNode.Generate()
  20 + return id.Int64(), nil
  21 +}