...
|
...
|
@@ -6,7 +6,6 @@ import ( |
|
|
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/factory"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/role/adapter"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/role/command"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
|
|
|
)
|
|
|
|
|
|
type RoleService struct {
|
...
|
...
|
@@ -17,122 +16,122 @@ func NewRoleService() *RoleService { |
|
|
return newRoleService
|
|
|
}
|
|
|
|
|
|
// Create 创建
|
|
|
func (rs *RoleService) Create(in *command.CreateRoleCommand) (interface{}, error) {
|
|
|
transactionContext, err := factory.ValidateStartTransaction(in)
|
|
|
if err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
defer func() {
|
|
|
transactionContext.RollbackTransaction()
|
|
|
}()
|
|
|
roleRepository := factory.CreateRoleRepository(map[string]interface{}{"transactionContext": transactionContext})
|
|
|
|
|
|
// 检测名称重复
|
|
|
count, err := roleRepository.Count(map[string]interface{}{"name": in.Name, "companyId": in.CompanyId})
|
|
|
if err != nil {
|
|
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
}
|
|
|
if count > 0 {
|
|
|
return nil, application.ThrowError(application.BUSINESS_ERROR, "角色名称已存在")
|
|
|
}
|
|
|
newRole := &domain.Role{
|
|
|
Id: 0,
|
|
|
Name: in.Name,
|
|
|
Type: domain.RoleTypeCommon,
|
|
|
Description: in.Description,
|
|
|
CompanyId: in.CompanyId,
|
|
|
}
|
|
|
role, err := roleRepository.Insert(newRole)
|
|
|
if err != nil {
|
|
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
}
|
|
|
if err := transactionContext.CommitTransaction(); err != nil {
|
|
|
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
}
|
|
|
return role, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
func (rs *RoleService) Update(in *command.UpdateRoleCommand) (interface{}, error) {
|
|
|
transactionContext, err := factory.ValidateStartTransaction(in)
|
|
|
if err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
defer func() {
|
|
|
transactionContext.RollbackTransaction()
|
|
|
}()
|
|
|
|
|
|
roleRepository := factory.CreateRoleRepository(map[string]interface{}{"transactionContext": transactionContext})
|
|
|
|
|
|
// 检测名称重复(排除自己)
|
|
|
count, err := roleRepository.Count(map[string]interface{}{"name": in.Name, "companyId": in.CompanyId, "notId": in.Id})
|
|
|
if err != nil {
|
|
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
}
|
|
|
if count > 0 {
|
|
|
return nil, application.ThrowError(application.BUSINESS_ERROR, "角色名称已存在")
|
|
|
}
|
|
|
|
|
|
role, err := roleRepository.FindOne(map[string]interface{}{"id": in.Id})
|
|
|
if err != nil {
|
|
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
}
|
|
|
|
|
|
role.Name = in.Name
|
|
|
role.Description = in.Description
|
|
|
|
|
|
role, err = roleRepository.Insert(role)
|
|
|
if err != nil {
|
|
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
}
|
|
|
if err := transactionContext.CommitTransaction(); err != nil {
|
|
|
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
}
|
|
|
return role, nil
|
|
|
}
|
|
|
|
|
|
func (rs *RoleService) Remove(in *command.DeleteRoleCommand) (interface{}, error) {
|
|
|
transactionContext, err := factory.ValidateStartTransaction(in)
|
|
|
if err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
defer func() {
|
|
|
transactionContext.RollbackTransaction()
|
|
|
}()
|
|
|
|
|
|
roleRepository := factory.CreateRoleRepository(map[string]interface{}{"transactionContext": transactionContext})
|
|
|
roleUserRepository := factory.CreateRoleUserRepository(map[string]interface{}{"transactionContext": transactionContext})
|
|
|
|
|
|
role, err := roleRepository.FindOne(map[string]interface{}{"id": in.Id})
|
|
|
if err != nil {
|
|
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
}
|
|
|
if _, err := roleRepository.Remove(role); err != nil {
|
|
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
}
|
|
|
|
|
|
// 获取角色所有关联的用户,并删除
|
|
|
_, roleUsers, err := roleUserRepository.Find(map[string]interface{}{"roleId": in.Id, "companyId": in.CompanyId})
|
|
|
if err != nil {
|
|
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
}
|
|
|
ids := make([]int64, 0)
|
|
|
for i := range roleUsers {
|
|
|
ids = append(ids, roleUsers[i].Id)
|
|
|
}
|
|
|
if len(ids) > 0 {
|
|
|
err := roleUserRepository.BatchDeleteById(ids)
|
|
|
if err != nil {
|
|
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if err := transactionContext.CommitTransaction(); err != nil {
|
|
|
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
}
|
|
|
return role, nil
|
|
|
}
|
|
|
//// Create 创建
|
|
|
//func (rs *RoleService) Create(in *command.CreateRoleCommand) (interface{}, error) {
|
|
|
// transactionContext, err := factory.ValidateStartTransaction(in)
|
|
|
// if err != nil {
|
|
|
// return nil, err
|
|
|
// }
|
|
|
// defer func() {
|
|
|
// transactionContext.RollbackTransaction()
|
|
|
// }()
|
|
|
// roleRepository := factory.CreateRoleRepository(map[string]interface{}{"transactionContext": transactionContext})
|
|
|
//
|
|
|
// // 检测名称重复
|
|
|
// count, err := roleRepository.Count(map[string]interface{}{"name": in.Name, "companyId": in.CompanyId})
|
|
|
// if err != nil {
|
|
|
// return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
// }
|
|
|
// if count > 0 {
|
|
|
// return nil, application.ThrowError(application.BUSINESS_ERROR, "角色名称已存在")
|
|
|
// }
|
|
|
// newRole := &domain.Role{
|
|
|
// Id: 0,
|
|
|
// Name: in.Name,
|
|
|
// Type: domain.RoleTypeCommon,
|
|
|
// Description: in.Description,
|
|
|
// CompanyId: in.CompanyId,
|
|
|
// }
|
|
|
// role, err := roleRepository.Insert(newRole)
|
|
|
// if err != nil {
|
|
|
// return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
// }
|
|
|
// if err := transactionContext.CommitTransaction(); err != nil {
|
|
|
// return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
// }
|
|
|
// return role, nil
|
|
|
//
|
|
|
//}
|
|
|
//
|
|
|
//func (rs *RoleService) Update(in *command.UpdateRoleCommand) (interface{}, error) {
|
|
|
// transactionContext, err := factory.ValidateStartTransaction(in)
|
|
|
// if err != nil {
|
|
|
// return nil, err
|
|
|
// }
|
|
|
// defer func() {
|
|
|
// transactionContext.RollbackTransaction()
|
|
|
// }()
|
|
|
//
|
|
|
// roleRepository := factory.CreateRoleRepository(map[string]interface{}{"transactionContext": transactionContext})
|
|
|
//
|
|
|
// // 检测名称重复(排除自己)
|
|
|
// count, err := roleRepository.Count(map[string]interface{}{"name": in.Name, "companyId": in.CompanyId, "notId": in.Id})
|
|
|
// if err != nil {
|
|
|
// return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
// }
|
|
|
// if count > 0 {
|
|
|
// return nil, application.ThrowError(application.BUSINESS_ERROR, "角色名称已存在")
|
|
|
// }
|
|
|
//
|
|
|
// role, err := roleRepository.FindOne(map[string]interface{}{"id": in.Id})
|
|
|
// if err != nil {
|
|
|
// return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
// }
|
|
|
//
|
|
|
// role.Name = in.Name
|
|
|
// role.Description = in.Description
|
|
|
//
|
|
|
// role, err = roleRepository.Insert(role)
|
|
|
// if err != nil {
|
|
|
// return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
// }
|
|
|
// if err := transactionContext.CommitTransaction(); err != nil {
|
|
|
// return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
// }
|
|
|
// return role, nil
|
|
|
//}
|
|
|
//
|
|
|
//func (rs *RoleService) Remove(in *command.DeleteRoleCommand) (interface{}, error) {
|
|
|
// transactionContext, err := factory.ValidateStartTransaction(in)
|
|
|
// if err != nil {
|
|
|
// return nil, err
|
|
|
// }
|
|
|
// defer func() {
|
|
|
// transactionContext.RollbackTransaction()
|
|
|
// }()
|
|
|
//
|
|
|
// roleRepository := factory.CreateRoleRepository(map[string]interface{}{"transactionContext": transactionContext})
|
|
|
// roleUserRepository := factory.CreateRoleUserRepository(map[string]interface{}{"transactionContext": transactionContext})
|
|
|
//
|
|
|
// role, err := roleRepository.FindOne(map[string]interface{}{"id": in.Id})
|
|
|
// if err != nil {
|
|
|
// return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
// }
|
|
|
// if _, err := roleRepository.Remove(role); err != nil {
|
|
|
// return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
// }
|
|
|
//
|
|
|
// // 获取角色所有关联的用户,并删除
|
|
|
// _, roleUsers, err := roleUserRepository.Find(map[string]interface{}{"roleId": in.Id, "companyId": in.CompanyId})
|
|
|
// if err != nil {
|
|
|
// return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
// }
|
|
|
// ids := make([]int64, 0)
|
|
|
// for i := range roleUsers {
|
|
|
// ids = append(ids, roleUsers[i].Id)
|
|
|
// }
|
|
|
// if len(ids) > 0 {
|
|
|
// err := roleUserRepository.BatchDeleteById(ids)
|
|
|
// if err != nil {
|
|
|
// return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
// }
|
|
|
// }
|
|
|
//
|
|
|
// if err := transactionContext.CommitTransaction(); err != nil {
|
|
|
// return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
// }
|
|
|
// return role, nil
|
|
|
//}
|
|
|
|
|
|
func (rs *RoleService) ListForUser(in *command.QueryRoleUserCommand) (interface{}, error) {
|
|
|
transactionContext, err := factory.StartTransaction()
|
...
|
...
|
|