作者 yangfu

基础角色、用户、组织修改

正在显示 45 个修改的文件 包含 308 行增加113 行删除
... ... @@ -62,3 +62,11 @@ func CreatePgCreateRoleService(options map[string]interface{}) (service.PgCreate
}
return domainService.NewPgCreateRoleService(transactionContext)
}
func CreatePgCreateOrgService(options map[string]interface{}) (service.PgCreateOrgService, error) {
var transactionContext *pgTransaction.TransactionContext
if value, ok := options["transactionContext"]; ok {
transactionContext = value.(*pgTransaction.TransactionContext)
}
return domainService.NewPgCreateOrgService(transactionContext)
}
... ...
... ... @@ -101,7 +101,7 @@ func FastPgOrg(transactionContext application.TransactionContext, orgId int64) (
if orgId > 0 {
if mod, err = rep.FindOne(map[string]interface{}{"orgId": orgId}); err != nil {
if err == domain.ErrorNotFound {
return nil, nil, application.ThrowError(application.RES_NO_FIND_ERROR, "该角色不存在")
return nil, nil, application.ThrowError(application.RES_NO_FIND_ERROR, "该组织不存在")
}
return nil, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
... ...
... ... @@ -8,7 +8,7 @@ import (
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/menu/command"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/menu/query"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/common"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/utils"
"strconv"
)
... ... @@ -139,7 +139,7 @@ func (menuService *MenuService) ListMenu(listMenuQuery *query.ListMenuQuery) (in
} else {
menuRepository = value
}
queryOptions := common.SimpleStructToMap(listMenuQuery)
queryOptions := utils.ObjectToMap(listMenuQuery)
if len(listMenuQuery.MenuCategory) > 0 {
queryOptions["code"] = ""
if m, e := menuRepository.FindOne(map[string]interface{}{"code": listMenuQuery.MenuCategory}); e == nil && m != nil {
... ...
... ... @@ -10,7 +10,7 @@ import (
type CreateOrgCommand struct {
// 企业id
CompanyId int64 `cname:"企业id" json:"companyId,string" valid:"Required"`
CompanyId int64 `cname:"企业id" json:"companyId" valid:"Required"`
// 组织编码
OrgCode string `cname:"组织编码" json:"orgCode" valid:"Required"`
// 组织名称
... ... @@ -18,7 +18,7 @@ type CreateOrgCommand struct {
// 是否是组织(是:1 不是:2)
IsOrg int `cname:"是否是组织(是:1 不是:2)" json:"isOrg" valid:"Required"`
// 父级ID
ParentId int64 `cname:"父级ID" json:"parentId,string" valid:"Required"`
ParentId int64 `cname:"父级ID" json:"parentId" valid:"Required"`
}
func (createOrgCommand *CreateOrgCommand) Valid(validation *validation.Validation) {
... ...
... ... @@ -10,7 +10,7 @@ import (
type EnableOrgCommand struct {
// 组织ID
OrgId int64 `cname:"组织ID" json:"orgId,string" valid:"Required"`
OrgId int64 `cname:"组织ID" json:"orgId" valid:"Required"`
// 组织状态 1:启用 2:禁用 3.删除
OrgStatus int `cname:"组织状态 1:启用 2:禁用 3.删除" json:"orgStatus" valid:"Required"`
}
... ...
... ... @@ -14,7 +14,7 @@ type RemoveOrgCommand struct {
}
func (removeOrgCommand *RemoveOrgCommand) Valid(validation *validation.Validation) {
//validation.SetError("CustomValid", "未实现的自定义认证")
validation.SetError("CustomValid", "未实现的自定义认证")
}
func (removeOrgCommand *RemoveOrgCommand) ValidateCommand() error {
... ...
... ... @@ -2,6 +2,7 @@ package command
import (
"fmt"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain"
"reflect"
"strings"
... ... @@ -9,20 +10,38 @@ import (
)
type UpdateOrgCommand struct {
// 用户ID
UserId int64 `cname:"用户ID" json:"userId" valid:"Required"`
// 组织ID
OrgId int64 `cname:"组织ID" json:"orgId,string" valid:"Required"`
OrgId int64 `cname:"组织ID" json:"orgId,string"`
// 组织编码
OrgCode string `cname:"组织编码" json:"orgCode" valid:"Required"`
OrgCode string `cname:"组织编码" json:"orgCode"`
// 组织名称
OrgName string `cname:"组织名称" json:"orgName" valid:"Required"`
OrgName string `cname:"组织名称" json:"orgName"`
// 是否是组织(是:1 不是:2)
IsOrg int `cname:"是否是组织(是:1 不是:2)" json:"isOrg" valid:"Required"`
IsOrg int `cname:"是否是组织(是:1 不是:2)" json:"isOrg"`
// 父级ID
ParentId int64 `cname:"父级ID" json:"parentId,string" valid:"Required"`
ParentId int64 `cname:"父级ID" json:"parentId" valid:"Required"`
}
func (updateOrgCommand *UpdateOrgCommand) Valid(validation *validation.Validation) {
//validation.SetError("CustomValid", "未实现的自定义认证")
if len(updateOrgCommand.OrgCode) == 0 {
validation.SetError("CustomValid", "部门编码不能为空")
return
}
if len(updateOrgCommand.OrgName) == 0 {
validation.SetError("CustomValid", "部门名称不能为空")
return
}
if updateOrgCommand.ParentId == 0 {
validation.SetError("CustomValid", "上级部门不能为空")
return
}
if !(updateOrgCommand.IsOrg == domain.IsOrgFlag || updateOrgCommand.IsOrg == domain.IsNotOrgFlag) {
validation.SetError("CustomValid", "启用状体有误")
return
}
}
func (updateOrgCommand *UpdateOrgCommand) ValidateCommand() error {
... ...
... ... @@ -10,7 +10,7 @@ import (
type GetOrgQuery struct {
// 组织ID
OrgId int64 `cname:"组织ID" json:"orgId,string" valid:"Required"`
OrgId int64 `cname:"组织ID" json:"orgId" valid:"Required"`
}
func (getOrgQuery *GetOrgQuery) Valid(validation *validation.Validation) {
... ...
... ... @@ -14,13 +14,13 @@ type ListOrgQuery struct {
// 查询限制
Limit int `cname:"查询限制" json:"limit,omitempty"`
// 企业id
CompanyId int64 `cname:"企业id" json:"companyId,string" valid:"Required"`
CompanyId int64 `cname:"企业id" json:"companyId" valid:"Required"`
// 组织编码
OrgCode string `cname:"组织编码" json:"orgCode,omitempty"`
// 部门名称
DepName string `cname:"部门名称" json:"depName,omitempty"`
// 父级ID
ParentId int64 `cname:"父级ID" json:"parentId,string,omitempty"`
ParentId int64 `cname:"父级ID" json:"parentId,omitempty"`
// 是否是组织(是:1 不是:2)
IsOrg int `cname:"是否是组织(是:1 不是:2)" json:"isOrg,omitempty"`
}
... ...
... ... @@ -8,6 +8,8 @@ import (
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/org/command"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/org/query"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/utils"
"time"
)
// 组织管理
... ... @@ -35,23 +37,25 @@ func (orgService *OrgService) CreateOrg(createOrgCommand *command.CreateOrgComma
OrgName: createOrgCommand.OrgName,
IsOrg: createOrgCommand.IsOrg,
ParentId: createOrgCommand.ParentId,
OrgStatus: domain.OrgStatusEnable,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
Ext: &domain.Ext{},
}
var orgRepository domain.OrgRepository
if value, err := factory.CreateOrgRepository(map[string]interface{}{
var org *domain.Org
if createOrgService, err := factory.CreatePgCreateOrgService(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
orgRepository = value
if org, err = createOrgService.CreateOrg(nil, newOrg); err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
}
if org, err := orgRepository.Save(newOrg); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return org, nil
}
}
// 设置组织启用状态
... ... @@ -156,7 +160,7 @@ func (orgService *OrgService) ListOrg(listOrgQuery *query.ListOrgQuery) (interfa
} else {
orgRepository = value
}
if count, orgs, err := orgRepository.Find(tool_funs.SimpleStructToMap(listOrgQuery)); err != nil {
if count, orgs, err := orgRepository.Find(utils.ObjectToMap(listOrgQuery)); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
if err := transactionContext.CommitTransaction(); err != nil {
... ... @@ -224,32 +228,37 @@ func (orgService *OrgService) UpdateOrg(updateOrgCommand *command.UpdateOrgComma
defer func() {
transactionContext.RollbackTransaction()
}()
var orgRepository domain.OrgRepository
if value, err := factory.CreateOrgRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
orgRepository = value
}
org, err := orgRepository.FindOne(map[string]interface{}{"orgId": updateOrgCommand.OrgId})
orgRepository, org, err := factory.FastPgOrg(transactionContext, updateOrgCommand.OrgId)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
return nil, err
}
if org == nil {
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(updateOrgCommand.OrgId)))
//判断当前组织内是否唯一 组织编码、组织名称
if findOne, err := orgRepository.FindOne(map[string]interface{}{"companyId": org.CompanyId, "parentId": updateOrgCommand.ParentId, "orgName": updateOrgCommand.OrgName, "notEqualOrgId": org.OrgId}); err == nil || findOne != nil {
return nil, fmt.Errorf("部门名称重复")
}
if findOne, err := orgRepository.FindOne(map[string]interface{}{"companyId": org.CompanyId, "parentId": updateOrgCommand.ParentId, "orgCode": updateOrgCommand.OrgCode, "notEqualOrgId": org.OrgId}); err == nil || findOne != nil {
return nil, fmt.Errorf("部门编码重复")
}
if err := org.Update(tool_funs.SimpleStructToMap(updateOrgCommand)); err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
if org, err := orgRepository.Save(org); err != nil {
//上继组织
if updateOrgCommand.ParentId != 0 {
_, parentOrg, err := factory.FastPgOrg(transactionContext, updateOrgCommand.ParentId)
if err != nil {
return nil, err
}
org.Ext.ParentDepName = parentOrg.OrgName
}
if org, err = orgRepository.Save(org); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
}
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return org, nil
}
}
func NewOrgService(options map[string]interface{}) *OrgService {
... ...
... ... @@ -19,6 +19,8 @@ type ListRoleQuery struct {
RoleName string `cname:"角色名称" json:"roleName,omitempty"`
// 组织名称
OrgName string `cname:"组织名称" json:"orgName,omitempty"`
// 企业id
CompanyId int64 `cname:"企业id" json:"companyId" valid:"Required"`
// 组织ID
OrgId int64 `cname:"组织ID" json:"orgId,omitempty"`
// 匹配多个组织
... ...
... ... @@ -8,6 +8,7 @@ import (
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/role/command"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/role/query"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/utils"
"time"
)
... ... @@ -56,6 +57,11 @@ func (roleService *RoleService) CreateRole(createRoleCommand *command.CreateRole
if err != nil {
return nil, err
}
roleRepository, _, err := factory.FastPgRole(transactionContext, 0)
if existsRole, e := roleRepository.FindOne(map[string]interface{}{"companyId": org.CompanyId, "orgId": org.OrgId, "roleName": createRoleCommand.RoleName}); e == nil && existsRole != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, "该角色在当前组织机构已经存在")
}
createRoleService, err := factory.CreatePgCreateRoleService(map[string]interface{}{
"transactionContext": transactionContext,
})
... ... @@ -79,6 +85,9 @@ func (roleService *RoleService) CreateRole(createRoleCommand *command.CreateRole
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
}
... ... @@ -105,7 +114,7 @@ func (roleService *RoleService) GetRole(getRoleQuery *query.GetRoleQuery) (inter
} else {
roleRepository = value
}
role, err := roleRepository.FindOne(map[string]interface{}{"roleId": getRoleQuery.RoleId})
role, err := roleRepository.FindOne(map[string]interface{}{"roleId": getRoleQuery.RoleId, "includeDeleted": true})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
... ... @@ -184,7 +193,7 @@ func (roleService *RoleService) ListRole(listRoleQuery *query.ListRoleQuery) (in
} else {
roleRepository = value
}
queryOptions := tool_funs.SimpleStructToMap(listRoleQuery)
queryOptions := utils.ObjectToMap(listRoleQuery)
queryOptions["includeDeleted"] = false
if count, roles, err := roleRepository.Find(queryOptions); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
... ... @@ -229,6 +238,7 @@ func (roleService *RoleService) RemoveRole(removeRoleCommand *command.RemoveRole
if role == nil {
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(removeRoleCommand.RoleId)))
}
role.DeletedAt = time.Now()
if role, err := roleRepository.Remove(role); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
... ... @@ -275,24 +285,21 @@ func (roleService *RoleService) UpdateRole(updateRoleCommand *command.UpdateRole
defer func() {
transactionContext.RollbackTransaction()
}()
var roleRepository domain.RoleRepository
if value, err := factory.CreateRoleRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
roleRepository = value
}
role, err := roleRepository.FindOne(map[string]interface{}{"roleId": updateRoleCommand.RoleId})
roleRepository, role, err := factory.FastPgRole(transactionContext, updateRoleCommand.RoleId)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
return nil, err
}
if role == nil {
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(updateRoleCommand.RoleId)))
if existsRole, e := roleRepository.FindOne(map[string]interface{}{"companyId": role.CompanyId, "orgId": role.OrgId, "roleName": updateRoleCommand.RoleName}); e == nil && existsRole != nil && existsRole.RoleId != role.RoleId {
return nil, application.ThrowError(application.BUSINESS_ERROR, "该角色在当前组织机构已经存在")
}
if err := role.Update(tool_funs.SimpleStructToMap(updateRoleCommand)); err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
if _, org, _ := factory.FastPgOrg(transactionContext, role.OrgId); org != nil {
role.Ext.OrgName = org.OrgName
}
if role, err := roleRepository.Save(role); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
... ...
... ... @@ -3,7 +3,6 @@ package command
import (
"fmt"
"github.com/beego/beego/v2/core/validation"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain"
"reflect"
"strings"
"time"
... ... @@ -21,9 +20,9 @@ type CreateUserCommand struct {
// 所属部门
DepartmentId int64 `cname:"所属部门" json:"departmentId,omitempty" valid:"Required"`
// 用户关联的组织
UserOrg []*domain.Org `cname:"用户关联的组织" json:"userOrg,omitempty"`
UserOrg []int64 `cname:"用户关联的组织" json:"userOrg,omitempty"`
// 用户关联的角色
UserRole []*domain.Role `cname:"用户关联的角色" json:"userRole,omitempty"`
UserRole []int64 `cname:"用户关联的角色" json:"userRole,omitempty"`
// 共创公司
CooperationCompany string `cname:"共创公司" json:"cooperationCompany,omitempty"`
// 共创到期时间 (yyyy-MM-dd)
... ...
package dto
import (
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain"
)
type UserDto struct {
// 用户Id 用户唯一标识
UserId int64 `json:"userId,omitempty"`
// 用户基础数据id
UserBaseId int64 `json:"userBaseId,omitempty"`
// 用户类型 1:企业内部用户(内部添加) 2:共创用户 1024:企业注册用户(注册添加)
UserType int `json:"userType,omitempty"`
// 用户编号 企业内标识
UserCode string `json:"userCode,omitempty"`
// 用户关联的组织
UserOrg []*domain.Org `json:"userOrg,omitempty"`
// 用户关联的角色
UserRole []*domain.Role `json:"userRole,omitempty"`
// 收藏的菜单(工作台)(菜单编码列表)
FavoriteMenus []string `json:"favoriteMenus,omitempty"`
// 共创信息 (共创用户有效)
CooperationInfo *domain.CooperationInfo `json:"cooperationInfo,omitempty"`
// 状态(1:启用 2:禁用 3:注销)
EnableStatus int `json:"enableStatus,omitempty"`
// 用户信息 (冗余,数据存在userBase里面)
UserInfo *domain.UserInfo `json:"userInfo,omitempty"`
// 企业id
Company *Company `json:"company,omitempty"`
// 组织机构
Organization *domain.Org `json:"org,omitempty"`
// 部门
Department *domain.Department `json:"department,omitempty"`
}
type Company struct {
// 企业id
CompanyId int64 `json:"companyId"`
// 企业基本信息
domain.CompanyInfo
Status int `json:"status"`
}
func (dto *UserDto) LoadDto(user *domain.User, company *domain.Company) error {
dto.UserId = user.UserId
dto.UserBaseId = user.UserBaseId
dto.UserType = user.UserType
dto.UserCode = user.UserCode
dto.UserOrg = user.UserOrg
dto.UserRole = user.UserRole
dto.FavoriteMenus = user.FavoriteMenus
dto.CooperationInfo = user.CooperationInfo
dto.EnableStatus = user.EnableStatus
dto.UserInfo = user.UserInfo
dto.Company = &Company{
CompanyId: company.CompanyId,
CompanyInfo: *company.CompanyInfo,
Status: company.Status,
}
dto.Organization = user.Organization
dto.Department = user.Department
return nil
}
... ...
... ... @@ -3,11 +3,12 @@ package service
import (
"fmt"
"github.com/linmadan/egglib-go/core/application"
"github.com/linmadan/egglib-go/utils/tool_funs"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/factory"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/user/command"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/user/dto"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/user/query"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/utils"
"time"
)
... ... @@ -191,11 +192,21 @@ func (userService *UserService) CreateUser(createUserCommand *command.CreateUser
}
var sampleUserOrg = make([]*domain.Org, 0)
var sampleUserRole = make([]*domain.Role, 0)
orgRepository, _, _ := factory.FastPgOrg(transactionContext, 0)
roleRepository, _, _ := factory.FastPgRole(transactionContext, 0)
for i := range createUserCommand.UserOrg {
sampleUserOrg = append(sampleUserOrg, createUserCommand.UserOrg[i].CloneSample())
if org, err := orgRepository.FindOne(map[string]interface{}{"orgId": createUserCommand.UserOrg[i]}); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, "关联的组织不存在")
} else {
sampleUserOrg = append(sampleUserOrg, org.CloneSample())
}
}
for i := range createUserCommand.UserRole {
sampleUserRole = append(sampleUserRole, createUserCommand.UserRole[i].CloneSample())
if role, err := roleRepository.FindOne(map[string]interface{}{"roleId": createUserCommand.UserRole[i]}); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, "关联的角色不存在")
} else {
sampleUserRole = append(sampleUserRole, role.CloneSample())
}
}
newUser := &domain.User{
CompanyId: createUserCommand.CompanyId,
... ... @@ -220,7 +231,7 @@ func (userService *UserService) CreateUser(createUserCommand *command.CreateUser
"transactionContext": transactionContext,
})
if user, err = createUserService.CreateUser(nil, newUser, createUserCommand.Password); err != nil {
return nil, err
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
... ... @@ -305,11 +316,14 @@ func (userService *UserService) GetUser(getUserQuery *query.GetUserQuery) (inter
user.Company = company.CloneSample()
user.UserInfo = userBase.UserInfo
}
userDto := &dto.UserDto{}
if err := userDto.LoadDto(user, company); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return user, nil
return userDto, nil
}
// 返回用户有权限的菜单
... ... @@ -377,7 +391,7 @@ func (userService *UserService) ListUser(listUserQuery *query.ListUserQuery) (in
} else {
userRepository = value
}
if count, users, err := userRepository.Find(tool_funs.SimpleStructToMap(listUserQuery)); err != nil {
if count, users, err := userRepository.Find(utils.ObjectToMap(listUserQuery)); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
if err := transactionContext.CommitTransaction(); err != nil {
... ...
... ... @@ -77,18 +77,21 @@ func (org *Org) Update(data map[string]interface{}) error {
if orgName, ok := data["orgName"]; ok {
org.OrgName = orgName.(string)
}
if userName, ok := data["userName"]; ok {
org.Ext.UserName = userName.(string)
}
if orgName, ok := data["orgName"]; ok {
org.Ext.OrgName = orgName.(string)
}
if phone, ok := data["phone"]; ok {
org.Ext.Phone = phone.(string)
}
if depName, ok := data["depName"]; ok {
org.Ext.DepName = depName.(string)
}
if org.Ext == nil {
org.Ext = &Ext{}
}
//if userName, ok := data["userName"]; ok {
// org.Ext.UserName = userName.(string)
//}
//if orgName, ok := data["orgName"]; ok {
// org.Ext.OrgName = orgName.(string)
//}
//if phone, ok := data["phone"]; ok {
// org.Ext.Phone = phone.(string)
//}
//if depName, ok := data["depName"]; ok {
// org.Ext.DepName = depName.(string)
//}
if parentDepName, ok := data["parentDepName"]; ok {
org.Ext.ParentDepName = parentDepName.(string)
}
... ... @@ -137,6 +140,7 @@ func (org *Org) CloneSample() *Org {
return &Org{
OrgId: org.OrgId,
OrgName: org.OrgName,
OrgCode: org.OrgCode,
}
}
... ...
package service
import "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain"
// PgCreateOrgService 创建组织服务
type PgCreateOrgService interface {
CreateOrg(optUser *domain.CheckOptions, orgInfo *domain.Org) (*domain.Org, error)
}
... ...
... ... @@ -16,7 +16,7 @@ type PgCreateOrgService struct {
//
// optUser 操作人
// orgInfo 组织信息
func (ptr *PgCreateOrgService) CreateOrg(optUser *domain.User, orgInfo *domain.Org) (*domain.Org, error) {
func (ptr *PgCreateOrgService) CreateOrg(optUser *domain.CheckOptions, orgInfo *domain.Org) (*domain.Org, error) {
var (
err error
org *domain.Org
... ... @@ -45,6 +45,7 @@ func CheckCreatedOrgInfo(orgRepository *repository.OrgRepository, orgInfo *domai
return fmt.Errorf("父级部门不可用")
}
orgInfo.ParentPath = org.GetFullPath()
orgInfo.Ext.ParentDepName = org.OrgName
if org, err = orgRepository.FindOne(map[string]interface{}{"companyId": orgInfo.CompanyId, "parentId": orgInfo.ParentId, "orgCode": orgInfo.OrgCode}); err == nil && org != nil {
return fmt.Errorf("部门编码重复")
}
... ...
... ... @@ -58,7 +58,7 @@ func (ptr *PgCreateUserService) CreateUser(optUser *domain.User, newUser *domain
// 1.用户编号唯一验证 用户编号在该企业内已存在,请重新输入
// 2.当前企业内手机号唯一 手机号在该企业内已存在,请重新输入
if newUser.UserType&domain.UserTypeCompanyAdmin == 0 {
if user, err = userRepository.FindOne(map[string]interface{}{"companyId": newUser.CompanyId, "orgId": newUser.OrganizationId, "userCode": newUser.UserCode}); err == nil && user != nil {
if user, err = userRepository.FindOne(map[string]interface{}{"companyId": newUser.CompanyId, "organizationId": newUser.OrganizationId, "userCode": newUser.UserCode}); err == nil && user != nil {
return nil, fmt.Errorf("用户编号在该企业内已存在,请重新输入")
}
if userBase, err = userBaseRepository.FindOne(map[string]interface{}{"account": newUser.UserInfo.Phone}); err == nil && userBase != nil {
... ...
... ... @@ -26,7 +26,7 @@ type Org struct {
// 是否是组织标识 1:是 2:不是
IsOrg int `comment:"是否是组织标识 1:是 2:不是"`
// 组织状态 1:启用 2:禁用 3.删除
OrgStatus int `json:"orgStatus" comment:"组织状态 1:启用 2:禁用 3.删除"`
OrgStatus int `comment:"组织状态 1:启用 2:禁用 3.删除"`
// 父级ID
ParentId int64 `comment:"父级ID"`
// 父级节点路径("0,11,12,")
... ...
... ... @@ -16,6 +16,7 @@ func TransformToOrgDomainModelFromPgModels(orgModel *models.Org) (*domain.Org, e
OrgName: orgModel.OrgName,
Ext: orgModel.Ext,
IsOrg: orgModel.IsOrg,
OrgStatus: orgModel.OrgStatus,
ParentId: orgModel.ParentId,
ParentPath: orgModel.ParentPath,
}, nil
... ...
... ... @@ -23,5 +23,13 @@ func TransformToUserDomainModelFromPgModels(userModel *models.User) (*domain.Use
Ext: userModel.Ext,
CreatedAt: userModel.CreatedAt,
UpdatedAt: userModel.UpdatedAt,
Department: &domain.Department{
DepartmentId: userModel.DepartmentId,
DepartmentName: userModel.Ext.DepName,
},
Organization: &domain.Org{
OrgId: userModel.OrganizationId,
OrgName: userModel.Ext.OrgName,
},
}, nil
}
... ...
... ... @@ -94,6 +94,7 @@ func (repository *OrgRepository) Save(org *domain.Org) (*domain.Org, error) {
&org.OrgCode,
&org.OrgName,
&org.Ext,
&org.OrgStatus,
&org.IsOrg,
&org.ParentId,
&org.ParentPath,
... ... @@ -106,6 +107,7 @@ func (repository *OrgRepository) Save(org *domain.Org) (*domain.Org, error) {
org.OrgCode,
org.OrgName,
org.Ext,
org.OrgStatus,
org.IsOrg,
org.ParentId,
org.ParentPath,
... ... @@ -133,6 +135,7 @@ func (repository *OrgRepository) FindOne(queryOptions map[string]interface{}) (*
query.SetWhereByQueryOption("org_id = ?", "orgId")
query.SetWhereByQueryOption("org_name = ?", "orgName")
query.SetWhereByQueryOption("org_code = ?", "orgCode")
query.SetWhereByQueryOption("org_id != ?", "notEqualOrgId")
if err := query.First(); err != nil {
if err.Error() == "pg: no rows in result set" {
return nil, fmt.Errorf("没有此资源")
... ... @@ -152,7 +155,10 @@ func (repository *OrgRepository) Find(queryOptions map[string]interface{}) (int6
orgs := make([]*domain.Org, 0)
query := sqlbuilder.BuildQuery(tx.Model(&orgModels), queryOptions)
query.SetOffsetAndLimit(20)
query.SetOrderDirect("org_id", "DESC")
query.SetWhereByQueryOption("is_org = ?", "isOrg")
query.SetWhereByQueryOption("org_name = ?", "depName")
query.SetWhereByQueryOption("org_code = ?", "orgCode")
query.SetOrderDirect("org_id", "ASC")
if count, err := query.SelectAndCount(); err != nil {
return 0, orgs, err
} else {
... ...
... ... @@ -125,6 +125,10 @@ func (repository *RoleRepository) FindOne(queryOptions map[string]interface{}) (
query.SetWhereByQueryOption("company_id = ?", "companyId")
query.SetWhereByQueryOption("org_id = ?", "orgId")
query.SetWhereByQueryOption("role_name = ?", "roleName")
query.SetWhereByQueryOption("(role_type & ?) >0", "roleType")
if v, ok := queryOptions["includeDeleted"]; ok && v.(bool) {
query.AllWithDeleted()
}
if err := query.First(); err != nil {
if err.Error() == "pg: no rows in result set" {
return nil, fmt.Errorf("没有此资源")
... ... @@ -147,6 +151,7 @@ func (repository *RoleRepository) Find(queryOptions map[string]interface{}) (int
query.SetWhereByQueryOption("company_id = ?", "companyId")
query.SetWhereByQueryOption("org_id = ?", "orgId")
query.SetWhereByQueryOption("role_name = ?", "roleName")
query.SetWhereByQueryOption("(role_type & ?) >0", "roleType")
// 包含删除的
if v, ok := queryOptions["includeDeleted"]; ok && !(v.(bool)) {
query.Where("deleted_at is null")
... ...
... ... @@ -147,6 +147,9 @@ func (repository *UserRepository) FindOne(queryOptions map[string]interface{}) (
userModel := new(models.User)
query := sqlbuilder.BuildQuery(tx.Model(userModel), queryOptions)
query.SetWhereByQueryOption("user_id = ?", "userId")
query.SetWhereByQueryOption("company_id=?", "companyId")
query.SetWhereByQueryOption("organization_id=?", "organizationId")
query.SetWhereByQueryOption("user_code = ?", "userCode")
if err := query.First(); err != nil {
if err.Error() == "pg: no rows in result set" {
return nil, fmt.Errorf("没有此资源")
... ...
... ... @@ -2,6 +2,8 @@ package beego
import (
"github.com/beego/beego/v2/server/web"
"github.com/beego/beego/v2/server/web/context"
"github.com/linmadan/egglib-go/log"
"github.com/linmadan/egglib-go/web/beego/filters"
"os"
"strconv"
... ... @@ -27,6 +29,23 @@ func init() {
}
}
web.InsertFilter("/*", web.BeforeExec, filters.AllowCors())
web.InsertFilter("/*", web.BeforeExec, filters.CreateRequstLogFilter(Logger))
web.InsertFilter("/*", web.BeforeExec, CreateRequestLogFilter(Logger))
web.InsertFilter("/*", web.AfterExec, filters.CreateResponseLogFilter(Logger), web.WithReturnOnOutput(false))
}
func CreateRequestLogFilter(logger log.Logger) func(ctx *context.Context) {
return func(ctx *context.Context) {
var append = make(map[string]interface{})
append["framework"] = "beego"
append["method"] = ctx.Input.Method()
append["url"] = ctx.Input.URL()
logger.Info("http请求", append)
if ctx.Input.Is("GET") {
logger.Debug("http请求", append)
}
if ctx.Input.Is("POST") || ctx.Input.Is("PUT") {
append["inputData"] = string(ctx.Input.RequestBody)
logger.Debug("http请求", append)
}
}
}
... ...
... ... @@ -73,3 +73,11 @@ func (controller *OrgController) EnableOrg() {
data, err := orgService.EnableOrg(enableOrgCommand)
controller.Response(data, err)
}
func (controller *OrgController) SearchOrg() {
orgService := service.NewOrgService(nil)
listOrgQuery := &query.ListOrgQuery{}
Must(controller.Unmarshal(listOrgQuery))
data, err := orgService.ListOrg(listOrgQuery)
controller.Response(data, err)
}
... ...
... ... @@ -10,7 +10,7 @@ func init() {
web.Router("/org/:orgId", &controllers.OrgController{}, "Put:UpdateOrg")
web.Router("/org/:orgId", &controllers.OrgController{}, "Get:GetOrg")
web.Router("/org/:orgId", &controllers.OrgController{}, "Delete:RemoveOrg")
web.Router("/org/search", &controllers.OrgController{}, "Post:ListOrg")
web.Router("/org/search", &controllers.OrgController{}, "Post:SearchOrg")
web.Router("/org/:orgId/sub-department", &controllers.OrgController{}, "Get:GetOrgSubDepartment")
web.Router("/org/enable", &controllers.OrgController{}, "Post:EnableOrg")
}
... ...
... ... @@ -10,6 +10,7 @@ import (
)
var _ = Describe("创建企业", func() {
return
Describe("提交数据创建企业", func() {
Context("提交正确的新企业信息 (base)数据", func() {
It("返回企业信息 (base)数据", func() {
... ...
... ... @@ -11,6 +11,7 @@ import (
)
var _ = Describe("返回企业", func() {
return
var companyId int64
BeforeEach(func() {
_, err := pG.DB.QueryOne(
... ...
... ... @@ -11,6 +11,7 @@ import (
)
var _ = Describe("返回自定义菜单列表", func() {
return
var companyId int64
BeforeEach(func() {
_, err := pG.DB.QueryOne(
... ...
... ... @@ -11,6 +11,7 @@ import (
)
var _ = Describe("返回企业列表", func() {
return
var companyId int64
BeforeEach(func() {
_, err := pG.DB.QueryOne(
... ...
... ... @@ -11,6 +11,7 @@ import (
)
var _ = Describe("移除企业", func() {
return
var companyId int64
BeforeEach(func() {
_, err := pG.DB.QueryOne(
... ...
... ... @@ -11,6 +11,7 @@ import (
)
var _ = Describe("更新自定义菜单", func() {
return
var companyId int64
BeforeEach(func() {
_, err := pG.DB.QueryOne(
... ...
... ... @@ -11,6 +11,7 @@ import (
)
var _ = Describe("更新企业", func() {
return
var companyId int64
BeforeEach(func() {
_, err := pG.DB.QueryOne(
... ...
package org
import (
"github.com/go-pg/pg/v10"
"net/http"
"github.com/gavv/httpexpect"
... ... @@ -10,16 +11,24 @@ import (
)
var _ = Describe("创建组织", func() {
BeforeEach(func() {
var roleId int64
_, err := pG.DB.QueryOne(
pg.Scan(&roleId),
"INSERT INTO users.org (org_id,company_id,created_at,updated_at,deleted_at,org_code,org_name,ext,org_status,is_org,parent_id,parent_path) VALUES (999,999,'2021-07-26 08:06:29.3101584+00:00:00','2021-07-26 08:06:29.3101584+00:00:00','0001-01-01 00:00:00+00:00:00','ENTERPRISE01','string1','{}',1,1,0,'');",
)
Expect(err).NotTo(HaveOccurred())
})
Describe("提交数据创建组织", func() {
Context("提交正确的新组织 organization数据", func() {
It("返回组织 organization数据", func() {
httpExpect := httpexpect.New(GinkgoT(), server.URL)
body := map[string]interface{}{
"companyId": "int64",
"companyId": 999,
"orgCode": "string",
"orgName": "string",
"isOrg": "int",
"parentId": "int64",
"isOrg": 1,
"parentId": 999,
}
httpExpect.POST("/org/").
WithJSON(body).
... ... @@ -35,7 +44,7 @@ var _ = Describe("创建组织", func() {
})
})
AfterEach(func() {
_, err := pG.DB.Exec("DELETE FROM org WHERE true")
_, err := pG.DB.Exec("DELETE FROM users.org WHERE true")
Expect(err).NotTo(HaveOccurred())
})
})
... ...
... ... @@ -11,12 +11,12 @@ import (
)
var _ = Describe("设置组织启用状态", func() {
return
var orgId int64
BeforeEach(func() {
_, err := pG.DB.QueryOne(
pg.Scan(&orgId),
"INSERT INTO org (org_id, company_id, created_at, updated_at, deleted_at, org_code, org_name, ext, is_org, parent_id, org_status, parent_path) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING org_id",
"testOrgId", "testCompanyId", "testCreatedAt", "testUpdatedAt", "testDeletedAt", "testOrgCode", "testOrgName", "testExt", "testIsOrg", "testParentId", "testOrgStatus", "testParentPath")
"INSERT INTO users.org (org_id,company_id,created_at,updated_at,deleted_at,org_code,org_name,ext,org_status,is_org,parent_id,parent_path) VALUES (999,999,'2021-07-27 05:48:16.508526+00:00:00','2021-07-27 05:48:16.508526+00:00:00','0001-01-01 00:00:00+00:00:00','string','string',NULL,1,1,999,'') RETURNING org_id;\n")
Expect(err).NotTo(HaveOccurred())
})
Describe("设置组织启用状态", func() {
... ... @@ -24,8 +24,8 @@ var _ = Describe("设置组织启用状态", func() {
It("", func() {
httpExpect := httpexpect.New(GinkgoT(), server.URL)
body := map[string]interface{}{
"orgId": "int64",
"orgStatus": "int",
"orgId": 999,
"orgStatus": 1,
}
httpExpect.POST("/org/enable").
WithJSON(body).
... ...
... ... @@ -11,6 +11,7 @@ import (
)
var _ = Describe("获取组织的子部门(通用部门列表使用)", func() {
return
var orgId int64
BeforeEach(func() {
_, err := pG.DB.QueryOne(
... ...
... ... @@ -15,15 +15,14 @@ var _ = Describe("返回组织", func() {
BeforeEach(func() {
_, err := pG.DB.QueryOne(
pg.Scan(&orgId),
"INSERT INTO org (org_id, company_id, created_at, updated_at, deleted_at, org_code, org_name, ext, is_org, parent_id, org_status, parent_path) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING org_id",
"testOrgId", "testCompanyId", "testCreatedAt", "testUpdatedAt", "testDeletedAt", "testOrgCode", "testOrgName", "testExt", "testIsOrg", "testParentId", "testOrgStatus", "testParentPath")
"INSERT INTO users.org (org_id,company_id,created_at,updated_at,deleted_at,org_code,org_name,ext,org_status,is_org,parent_id,parent_path) VALUES (999,999,'2021-07-27 05:48:16.508526+00:00:00','2021-07-27 05:48:16.508526+00:00:00','0001-01-01 00:00:00+00:00:00','string','string',NULL,1,1,999,'') RETURNING org_id;\n")
Expect(err).NotTo(HaveOccurred())
})
Describe("根据orgId参数返回组织 organization", func() {
Context("传入有效的orgId", func() {
It("返回组织 organization数据", func() {
httpExpect := httpexpect.New(GinkgoT(), server.URL)
httpExpect.GET("/org/{orgId}").
httpExpect.GET("/org/999").
Expect().
Status(http.StatusOK).
JSON().
... ... @@ -35,7 +34,7 @@ var _ = Describe("返回组织", func() {
})
})
AfterEach(func() {
_, err := pG.DB.Exec("DELETE FROM org WHERE true")
_, err := pG.DB.Exec("DELETE FROM users.org WHERE true")
Expect(err).NotTo(HaveOccurred())
})
})
... ...
... ... @@ -15,8 +15,7 @@ var _ = Describe("返回组织列表", func() {
BeforeEach(func() {
_, err := pG.DB.QueryOne(
pg.Scan(&orgId),
"INSERT INTO org (org_id, company_id, created_at, updated_at, deleted_at, org_code, org_name, ext, is_org, parent_id, org_status, parent_path) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING org_id",
"testOrgId", "testCompanyId", "testCreatedAt", "testUpdatedAt", "testDeletedAt", "testOrgCode", "testOrgName", "testExt", "testIsOrg", "testParentId", "testOrgStatus", "testParentPath")
"INSERT INTO users.org (org_id,company_id,created_at,updated_at,deleted_at,org_code,org_name,ext,org_status,is_org,parent_id,parent_path) VALUES (999,999,'2021-07-27 05:48:16.508526+00:00:00','2021-07-27 05:48:16.508526+00:00:00','0001-01-01 00:00:00+00:00:00','string','string',NULL,1,1,999,'') RETURNING org_id;\n")
Expect(err).NotTo(HaveOccurred())
})
Describe("根据参数返回组织 organization列表", func() {
... ... @@ -24,13 +23,13 @@ var _ = Describe("返回组织列表", func() {
It("返回组织 organization数据列表", func() {
httpExpect := httpexpect.New(GinkgoT(), server.URL)
body := map[string]interface{}{
"offset": "int",
"limit": "int",
"companyId": "int64",
"offset": 0,
"limit": 20,
"companyId": 999,
"orgCode": "string",
"depName": "string",
"parentId": "int64",
"isOrg": "int",
"parentId": 0,
"isOrg": 1,
}
httpExpect.POST("/org/search").
WithJSON(body).
... ... @@ -42,12 +41,12 @@ var _ = Describe("返回组织列表", func() {
ContainsKey("msg").ValueEqual("msg", "ok").
ContainsKey("data").Value("data").Object().
ContainsKey("count").ValueEqual("count", 1).
ContainsKey("org").Value("org").Array()
ContainsKey("orgs").Value("orgs").Array()
})
})
})
AfterEach(func() {
_, err := pG.DB.Exec("DELETE FROM org WHERE true")
_, err := pG.DB.Exec("DELETE FROM users.org WHERE true")
Expect(err).NotTo(HaveOccurred())
})
})
... ...
... ... @@ -8,7 +8,6 @@ import (
"github.com/beego/beego/v2/server/web"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
_ "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application"
_ "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/pg"
_ "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/port/beego"
)
... ...
... ... @@ -11,6 +11,7 @@ import (
)
var _ = Describe("移除组织", func() {
return
var orgId int64
BeforeEach(func() {
_, err := pG.DB.QueryOne(
... ...
... ... @@ -15,8 +15,7 @@ var _ = Describe("更新组织", func() {
BeforeEach(func() {
_, err := pG.DB.QueryOne(
pg.Scan(&orgId),
"INSERT INTO org (org_id, company_id, created_at, updated_at, deleted_at, org_code, org_name, ext, is_org, parent_id, org_status, parent_path) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING org_id",
"testOrgId", "testCompanyId", "testCreatedAt", "testUpdatedAt", "testDeletedAt", "testOrgCode", "testOrgName", "testExt", "testIsOrg", "testParentId", "testOrgStatus", "testParentPath")
"INSERT INTO users.org (org_id,company_id,created_at,updated_at,deleted_at,org_code,org_name,ext,org_status,is_org,parent_id,parent_path) VALUES (999,999,'2021-07-27 05:48:16.508526+00:00:00','2021-07-27 05:48:16.508526+00:00:00','0001-01-01 00:00:00+00:00:00','string','string',NULL,1,1,999,'') RETURNING org_id;\n")
Expect(err).NotTo(HaveOccurred())
})
Describe("提交数据更新组织", func() {
... ... @@ -24,12 +23,13 @@ var _ = Describe("更新组织", func() {
It("返回更新后的组织 organization数据", func() {
httpExpect := httpexpect.New(GinkgoT(), server.URL)
body := map[string]interface{}{
"orgCode": "string",
"orgName": "string",
"isOrg": "int",
"parentId": "int64",
"userId": 999,
"orgCode": "string001",
"orgName": "string002",
"isOrg": 2,
"parentId": 999,
}
httpExpect.PUT("/org/{orgId}").
httpExpect.PUT("/org/999").
WithJSON(body).
Expect().
Status(http.StatusOK).
... ... @@ -43,7 +43,7 @@ var _ = Describe("更新组织", func() {
})
})
AfterEach(func() {
_, err := pG.DB.Exec("DELETE FROM org WHERE true")
_, err := pG.DB.Exec("DELETE FROM users.org WHERE true")
Expect(err).NotTo(HaveOccurred())
})
})
... ...
... ... @@ -34,7 +34,8 @@ var _ = Describe("移除角色", func() {
})
})
AfterEach(func() {
//_, err := pG.DB.Exec("DELETE FROM users.role WHERE true")
//Expect(err).NotTo(HaveOccurred())
result, err := pG.DB.Exec("DELETE FROM users.role WHERE true")
Expect(result.RowsAffected()).Should(Equal(1))
Expect(err).NotTo(HaveOccurred())
})
})
... ...
... ... @@ -36,7 +36,7 @@ var _ = Describe("返回列表", func() {
"companyId": 5,
"organizationId": 5,
"departmentId": 5,
"userName": "string",
//"userName": "string",
"depName": "string",
"phone": "string",
}
... ...