作者 yangfu

add org

  1 +package command
  2 +
  3 +import (
  4 + "fmt"
  5 + "reflect"
  6 + "strings"
  7 +
  8 + "github.com/beego/beego/v2/core/validation"
  9 +)
  10 +
  11 +type CreateOrgCommand struct {
  12 + // 企业id
  13 + CompanyId int64 `cname:"企业id" json:"companyId,string" valid:"Required"`
  14 + // 组织编码
  15 + OrgCode string `cname:"组织编码" json:"orgCode" valid:"Required"`
  16 + // 组织名称
  17 + OrgName string `cname:"组织名称" json:"orgName" valid:"Required"`
  18 + // 是否是组织(是:1 不是:2)
  19 + IsOrg int `cname:"是否是组织(是:1 不是:2)" json:"isOrg" valid:"Required"`
  20 + // 父级ID
  21 + ParentId int64 `cname:"父级ID" json:"parentId,string" valid:"Required"`
  22 +}
  23 +
  24 +func (createOrgCommand *CreateOrgCommand) Valid(validation *validation.Validation) {
  25 + //validation.SetError("CustomValid", "未实现的自定义认证")
  26 +}
  27 +
  28 +func (createOrgCommand *CreateOrgCommand) ValidateCommand() error {
  29 + valid := validation.Validation{}
  30 + b, err := valid.Valid(createOrgCommand)
  31 + if err != nil {
  32 + return err
  33 + }
  34 + if !b {
  35 + elem := reflect.TypeOf(createOrgCommand).Elem()
  36 + for _, validErr := range valid.Errors {
  37 + field, isExist := elem.FieldByName(validErr.Field)
  38 + if isExist {
  39 + return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1))
  40 + } else {
  41 + return fmt.Errorf(validErr.Message)
  42 + }
  43 + }
  44 + }
  45 + return nil
  46 +}
  1 +package command
  2 +
  3 +import (
  4 + "fmt"
  5 + "reflect"
  6 + "strings"
  7 +
  8 + "github.com/beego/beego/v2/core/validation"
  9 +)
  10 +
  11 +type EnableOrgCommand struct {
  12 + // 组织ID
  13 + OrgId int64 `cname:"组织ID" json:"orgId,string" valid:"Required"`
  14 + // 组织状态 1:启用 2:禁用 3.删除
  15 + OrgStatus int `cname:"组织状态 1:启用 2:禁用 3.删除" json:"orgStatus" valid:"Required"`
  16 +}
  17 +
  18 +func (enableOrgCommand *EnableOrgCommand) Valid(validation *validation.Validation) {
  19 + //validation.SetError("CustomValid", "未实现的自定义认证")
  20 +}
  21 +
  22 +func (enableOrgCommand *EnableOrgCommand) ValidateCommand() error {
  23 + valid := validation.Validation{}
  24 + b, err := valid.Valid(enableOrgCommand)
  25 + if err != nil {
  26 + return err
  27 + }
  28 + if !b {
  29 + elem := reflect.TypeOf(enableOrgCommand).Elem()
  30 + for _, validErr := range valid.Errors {
  31 + field, isExist := elem.FieldByName(validErr.Field)
  32 + if isExist {
  33 + return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1))
  34 + } else {
  35 + return fmt.Errorf(validErr.Message)
  36 + }
  37 + }
  38 + }
  39 + return nil
  40 +}
  1 +package command
  2 +
  3 +import (
  4 + "fmt"
  5 + "reflect"
  6 + "strings"
  7 +
  8 + "github.com/beego/beego/v2/core/validation"
  9 +)
  10 +
  11 +type RemoveOrgCommand struct {
  12 + // 组织ID
  13 + OrgId int64 `cname:"组织ID" json:"orgId,string" valid:"Required"`
  14 +}
  15 +
  16 +func (removeOrgCommand *RemoveOrgCommand) Valid(validation *validation.Validation) {
  17 + //validation.SetError("CustomValid", "未实现的自定义认证")
  18 +}
  19 +
  20 +func (removeOrgCommand *RemoveOrgCommand) ValidateCommand() error {
  21 + valid := validation.Validation{}
  22 + b, err := valid.Valid(removeOrgCommand)
  23 + if err != nil {
  24 + return err
  25 + }
  26 + if !b {
  27 + elem := reflect.TypeOf(removeOrgCommand).Elem()
  28 + for _, validErr := range valid.Errors {
  29 + field, isExist := elem.FieldByName(validErr.Field)
  30 + if isExist {
  31 + return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1))
  32 + } else {
  33 + return fmt.Errorf(validErr.Message)
  34 + }
  35 + }
  36 + }
  37 + return nil
  38 +}
  1 +package command
  2 +
  3 +import (
  4 + "fmt"
  5 + "reflect"
  6 + "strings"
  7 +
  8 + "github.com/beego/beego/v2/core/validation"
  9 +)
  10 +
  11 +type UpdateOrgCommand struct {
  12 + // 组织ID
  13 + OrgId int64 `cname:"组织ID" json:"orgId,string" valid:"Required"`
  14 + // 组织编码
  15 + OrgCode string `cname:"组织编码" json:"orgCode" valid:"Required"`
  16 + // 组织名称
  17 + OrgName string `cname:"组织名称" json:"orgName" valid:"Required"`
  18 + // 是否是组织(是:1 不是:2)
  19 + IsOrg int `cname:"是否是组织(是:1 不是:2)" json:"isOrg" valid:"Required"`
  20 + // 父级ID
  21 + ParentId int64 `cname:"父级ID" json:"parentId,string" valid:"Required"`
  22 +}
  23 +
  24 +func (updateOrgCommand *UpdateOrgCommand) Valid(validation *validation.Validation) {
  25 + //validation.SetError("CustomValid", "未实现的自定义认证")
  26 +}
  27 +
  28 +func (updateOrgCommand *UpdateOrgCommand) ValidateCommand() error {
  29 + valid := validation.Validation{}
  30 + b, err := valid.Valid(updateOrgCommand)
  31 + if err != nil {
  32 + return err
  33 + }
  34 + if !b {
  35 + elem := reflect.TypeOf(updateOrgCommand).Elem()
  36 + for _, validErr := range valid.Errors {
  37 + field, isExist := elem.FieldByName(validErr.Field)
  38 + if isExist {
  39 + return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1))
  40 + } else {
  41 + return fmt.Errorf(validErr.Message)
  42 + }
  43 + }
  44 + }
  45 + return nil
  46 +}
  1 +package query
  2 +
  3 +import (
  4 + "fmt"
  5 + "reflect"
  6 + "strings"
  7 +
  8 + "github.com/beego/beego/v2/core/validation"
  9 +)
  10 +
  11 +type GetOrgQuery struct {
  12 + // 组织ID
  13 + OrgId int64 `cname:"组织ID" json:"orgId,string" valid:"Required"`
  14 +}
  15 +
  16 +func (getOrgQuery *GetOrgQuery) Valid(validation *validation.Validation) {
  17 + //validation.SetError("CustomValid", "未实现的自定义认证")
  18 +}
  19 +
  20 +func (getOrgQuery *GetOrgQuery) ValidateQuery() error {
  21 + valid := validation.Validation{}
  22 + b, err := valid.Valid(getOrgQuery)
  23 + if err != nil {
  24 + return err
  25 + }
  26 + if !b {
  27 + elem := reflect.TypeOf(getOrgQuery).Elem()
  28 + for _, validErr := range valid.Errors {
  29 + field, isExist := elem.FieldByName(validErr.Field)
  30 + if isExist {
  31 + return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1))
  32 + } else {
  33 + return fmt.Errorf(validErr.Message)
  34 + }
  35 + }
  36 + }
  37 + return nil
  38 +}
  1 +package query
  2 +
  3 +import (
  4 + "fmt"
  5 + "reflect"
  6 + "strings"
  7 +
  8 + "github.com/beego/beego/v2/core/validation"
  9 +)
  10 +
  11 +type GetOrgSubDepartmentQuery struct {
  12 + // 组织ID
  13 + OrgId int64 `cname:"组织ID" json:"orgId,string" valid:"Required"`
  14 + // 获取所有子节点 (1:获取当前下级子节点 2:获取当前下级所有子节点)
  15 + ChildFlag int `cname:"获取所有子节点 (1:获取当前下级子节点 2:获取当前下级所有子节点)" json:"childFlag,omitempty"`
  16 +}
  17 +
  18 +func (getOrgSubDepartmentQuery *GetOrgSubDepartmentQuery) Valid(validation *validation.Validation) {
  19 + //validation.SetError("CustomValid", "未实现的自定义认证")
  20 +}
  21 +
  22 +func (getOrgSubDepartmentQuery *GetOrgSubDepartmentQuery) ValidateQuery() error {
  23 + valid := validation.Validation{}
  24 + b, err := valid.Valid(getOrgSubDepartmentQuery)
  25 + if err != nil {
  26 + return err
  27 + }
  28 + if !b {
  29 + elem := reflect.TypeOf(getOrgSubDepartmentQuery).Elem()
  30 + for _, validErr := range valid.Errors {
  31 + field, isExist := elem.FieldByName(validErr.Field)
  32 + if isExist {
  33 + return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1))
  34 + } else {
  35 + return fmt.Errorf(validErr.Message)
  36 + }
  37 + }
  38 + }
  39 + return nil
  40 +}
  1 +package query
  2 +
  3 +import (
  4 + "fmt"
  5 + "reflect"
  6 + "strings"
  7 +
  8 + "github.com/beego/beego/v2/core/validation"
  9 +)
  10 +
  11 +type ListOrgQuery struct {
  12 + // 查询偏离量
  13 + Offset int `cname:"查询偏离量" json:"offset,omitempty"`
  14 + // 查询限制
  15 + Limit int `cname:"查询限制" json:"limit,omitempty"`
  16 + // 企业id
  17 + CompanyId int64 `cname:"企业id" json:"companyId,string" valid:"Required"`
  18 + // 组织编码
  19 + OrgCode string `cname:"组织编码" json:"orgCode,omitempty"`
  20 + // 部门名称
  21 + DepName string `cname:"部门名称" json:"depName,omitempty"`
  22 + // 父级ID
  23 + ParentId int64 `cname:"父级ID" json:"parentId,string,omitempty"`
  24 + // 是否是组织(是:1 不是:2)
  25 + IsOrg int `cname:"是否是组织(是:1 不是:2)" json:"isOrg,omitempty"`
  26 +}
  27 +
  28 +func (listOrgQuery *ListOrgQuery) Valid(validation *validation.Validation) {
  29 + //validation.SetError("CustomValid", "未实现的自定义认证")
  30 +}
  31 +
  32 +func (listOrgQuery *ListOrgQuery) ValidateQuery() error {
  33 + valid := validation.Validation{}
  34 + b, err := valid.Valid(listOrgQuery)
  35 + if err != nil {
  36 + return err
  37 + }
  38 + if !b {
  39 + elem := reflect.TypeOf(listOrgQuery).Elem()
  40 + for _, validErr := range valid.Errors {
  41 + field, isExist := elem.FieldByName(validErr.Field)
  42 + if isExist {
  43 + return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1))
  44 + } else {
  45 + return fmt.Errorf(validErr.Message)
  46 + }
  47 + }
  48 + }
  49 + return nil
  50 +}
  1 +package service
  2 +
  3 +import (
  4 + "fmt"
  5 + "github.com/linmadan/egglib-go/core/application"
  6 + "github.com/linmadan/egglib-go/utils/tool_funs"
  7 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/factory"
  8 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/org/command"
  9 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/org/query"
  10 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain"
  11 +)
  12 +
  13 +// 组织管理
  14 +type OrgService struct {
  15 +}
  16 +
  17 +// 创建组织
  18 +func (orgService *OrgService) CreateOrg(createOrgCommand *command.CreateOrgCommand) (interface{}, error) {
  19 + if err := createOrgCommand.ValidateCommand(); err != nil {
  20 + return nil, application.ThrowError(application.ARG_ERROR, err.Error())
  21 + }
  22 + transactionContext, err := factory.CreateTransactionContext(nil)
  23 + if err != nil {
  24 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  25 + }
  26 + if err := transactionContext.StartTransaction(); err != nil {
  27 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  28 + }
  29 + defer func() {
  30 + transactionContext.RollbackTransaction()
  31 + }()
  32 + newOrg := &domain.Org{
  33 + CompanyId: createOrgCommand.CompanyId,
  34 + OrgCode: createOrgCommand.OrgCode,
  35 + OrgName: createOrgCommand.OrgName,
  36 + IsOrg: createOrgCommand.IsOrg,
  37 + ParentId: createOrgCommand.ParentId,
  38 + }
  39 + var orgRepository domain.OrgRepository
  40 + if value, err := factory.CreateOrgRepository(map[string]interface{}{
  41 + "transactionContext": transactionContext,
  42 + }); err != nil {
  43 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  44 + } else {
  45 + orgRepository = value
  46 + }
  47 + if org, err := orgRepository.Save(newOrg); err != nil {
  48 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  49 + } else {
  50 + if err := transactionContext.CommitTransaction(); err != nil {
  51 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  52 + }
  53 + return org, nil
  54 + }
  55 +}
  56 +
  57 +// 设置组织启用状态
  58 +func (orgService *OrgService) EnableOrg(enableOrgCommand *command.EnableOrgCommand) (interface{}, error) {
  59 + if err := enableOrgCommand.ValidateCommand(); err != nil {
  60 + return nil, application.ThrowError(application.ARG_ERROR, err.Error())
  61 + }
  62 + transactionContext, err := factory.CreateTransactionContext(nil)
  63 + if err != nil {
  64 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  65 + }
  66 + if err := transactionContext.StartTransaction(); err != nil {
  67 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  68 + }
  69 + defer func() {
  70 + transactionContext.RollbackTransaction()
  71 + }()
  72 + if err := transactionContext.CommitTransaction(); err != nil {
  73 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  74 + }
  75 + return nil, nil
  76 +}
  77 +
  78 +// 返回组织
  79 +func (orgService *OrgService) GetOrg(getOrgQuery *query.GetOrgQuery) (interface{}, error) {
  80 + if err := getOrgQuery.ValidateQuery(); err != nil {
  81 + return nil, application.ThrowError(application.ARG_ERROR, err.Error())
  82 + }
  83 + transactionContext, err := factory.CreateTransactionContext(nil)
  84 + if err != nil {
  85 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  86 + }
  87 + if err := transactionContext.StartTransaction(); err != nil {
  88 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  89 + }
  90 + defer func() {
  91 + transactionContext.RollbackTransaction()
  92 + }()
  93 + var orgRepository domain.OrgRepository
  94 + if value, err := factory.CreateOrgRepository(map[string]interface{}{
  95 + "transactionContext": transactionContext,
  96 + }); err != nil {
  97 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  98 + } else {
  99 + orgRepository = value
  100 + }
  101 + org, err := orgRepository.FindOne(map[string]interface{}{"orgId": getOrgQuery.OrgId})
  102 + if err != nil {
  103 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  104 + }
  105 + if org == nil {
  106 + return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(getOrgQuery.OrgId)))
  107 + } else {
  108 + if err := transactionContext.CommitTransaction(); err != nil {
  109 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  110 + }
  111 + return org, nil
  112 + }
  113 +}
  114 +
  115 +// 获取组织的子部门(通用部门列表使用)
  116 +func (orgService *OrgService) GetOrgSubDepartment(getOrgSubDepartmentQuery *query.GetOrgSubDepartmentQuery) (interface{}, error) {
  117 + if err := getOrgSubDepartmentQuery.ValidateQuery(); err != nil {
  118 + return nil, application.ThrowError(application.ARG_ERROR, err.Error())
  119 + }
  120 + transactionContext, err := factory.CreateTransactionContext(nil)
  121 + if err != nil {
  122 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  123 + }
  124 + if err := transactionContext.StartTransaction(); err != nil {
  125 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  126 + }
  127 + defer func() {
  128 + transactionContext.RollbackTransaction()
  129 + }()
  130 + if err := transactionContext.CommitTransaction(); err != nil {
  131 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  132 + }
  133 + return nil, nil
  134 +}
  135 +
  136 +// 返回组织列表
  137 +func (orgService *OrgService) ListOrg(listOrgQuery *query.ListOrgQuery) (interface{}, error) {
  138 + if err := listOrgQuery.ValidateQuery(); err != nil {
  139 + return nil, application.ThrowError(application.ARG_ERROR, err.Error())
  140 + }
  141 + transactionContext, err := factory.CreateTransactionContext(nil)
  142 + if err != nil {
  143 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  144 + }
  145 + if err := transactionContext.StartTransaction(); err != nil {
  146 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  147 + }
  148 + defer func() {
  149 + transactionContext.RollbackTransaction()
  150 + }()
  151 + var orgRepository domain.OrgRepository
  152 + if value, err := factory.CreateOrgRepository(map[string]interface{}{
  153 + "transactionContext": transactionContext,
  154 + }); err != nil {
  155 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  156 + } else {
  157 + orgRepository = value
  158 + }
  159 + if count, orgs, err := orgRepository.Find(tool_funs.SimpleStructToMap(listOrgQuery)); err != nil {
  160 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  161 + } else {
  162 + if err := transactionContext.CommitTransaction(); err != nil {
  163 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  164 + }
  165 + return map[string]interface{}{
  166 + "count": count,
  167 + "orgs": orgs,
  168 + }, nil
  169 + }
  170 +}
  171 +
  172 +// 移除组织
  173 +func (orgService *OrgService) RemoveOrg(removeOrgCommand *command.RemoveOrgCommand) (interface{}, error) {
  174 + if err := removeOrgCommand.ValidateCommand(); err != nil {
  175 + return nil, application.ThrowError(application.ARG_ERROR, err.Error())
  176 + }
  177 + transactionContext, err := factory.CreateTransactionContext(nil)
  178 + if err != nil {
  179 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  180 + }
  181 + if err := transactionContext.StartTransaction(); err != nil {
  182 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  183 + }
  184 + defer func() {
  185 + transactionContext.RollbackTransaction()
  186 + }()
  187 + var orgRepository domain.OrgRepository
  188 + if value, err := factory.CreateOrgRepository(map[string]interface{}{
  189 + "transactionContext": transactionContext,
  190 + }); err != nil {
  191 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  192 + } else {
  193 + orgRepository = value
  194 + }
  195 + org, err := orgRepository.FindOne(map[string]interface{}{"orgId": removeOrgCommand.OrgId})
  196 + if err != nil {
  197 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  198 + }
  199 + if org == nil {
  200 + return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(removeOrgCommand.OrgId)))
  201 + }
  202 + if org, err := orgRepository.Remove(org); err != nil {
  203 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  204 + } else {
  205 + if err := transactionContext.CommitTransaction(); err != nil {
  206 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  207 + }
  208 + return org, nil
  209 + }
  210 +}
  211 +
  212 +// 更新组织
  213 +func (orgService *OrgService) UpdateOrg(updateOrgCommand *command.UpdateOrgCommand) (interface{}, error) {
  214 + if err := updateOrgCommand.ValidateCommand(); err != nil {
  215 + return nil, application.ThrowError(application.ARG_ERROR, err.Error())
  216 + }
  217 + transactionContext, err := factory.CreateTransactionContext(nil)
  218 + if err != nil {
  219 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  220 + }
  221 + if err := transactionContext.StartTransaction(); err != nil {
  222 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  223 + }
  224 + defer func() {
  225 + transactionContext.RollbackTransaction()
  226 + }()
  227 + var orgRepository domain.OrgRepository
  228 + if value, err := factory.CreateOrgRepository(map[string]interface{}{
  229 + "transactionContext": transactionContext,
  230 + }); err != nil {
  231 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  232 + } else {
  233 + orgRepository = value
  234 + }
  235 + org, err := orgRepository.FindOne(map[string]interface{}{"orgId": updateOrgCommand.OrgId})
  236 + if err != nil {
  237 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  238 + }
  239 + if org == nil {
  240 + return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(updateOrgCommand.OrgId)))
  241 + }
  242 + if err := org.Update(tool_funs.SimpleStructToMap(updateOrgCommand)); err != nil {
  243 + return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
  244 + }
  245 + if org, err := orgRepository.Save(org); err != nil {
  246 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  247 + } else {
  248 + if err := transactionContext.CommitTransaction(); err != nil {
  249 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  250 + }
  251 + return org, nil
  252 + }
  253 +}
  254 +
  255 +func NewOrgService(options map[string]interface{}) *OrgService {
  256 + newOrgService := &OrgService{}
  257 + return newOrgService
  258 +}
@@ -7,9 +7,9 @@ var POSTGRESQL_USER = "postgres" @@ -7,9 +7,9 @@ var POSTGRESQL_USER = "postgres"
7 var POSTGRESQL_PASSWORD = "123456" 7 var POSTGRESQL_PASSWORD = "123456"
8 var POSTGRESQL_HOST = "127.0.0.1" 8 var POSTGRESQL_HOST = "127.0.0.1"
9 var POSTGRESQL_PORT = "5432" 9 var POSTGRESQL_PORT = "5432"
10 -var DISABLE_CREATE_TABLE = false  
11 -var DISABLE_SQL_GENERATE_PRINT = false  
12 -var DISABLE_SQL_GENERATE_COMMENT = false 10 +var DISABLE_CREATE_TABLE = true
  11 +var DISABLE_SQL_GENERATE_PRINT = true
  12 +var DISABLE_SQL_GENERATE_COMMENT = true
13 13
14 func init() { 14 func init() {
15 if os.Getenv("POSTGRESQL_DB_NAME") != "" { 15 if os.Getenv("POSTGRESQL_DB_NAME") != "" {
@@ -67,7 +67,7 @@ func (repository *OrgRepository) Save(org *domain.Org) (*domain.Org, error) { @@ -67,7 +67,7 @@ func (repository *OrgRepository) Save(org *domain.Org) (*domain.Org, error) {
67 &org.ParentId, 67 &org.ParentId,
68 &org.ParentPath, 68 &org.ParentPath,
69 ), 69 ),
70 - fmt.Sprintf("INSERT INTO orgs (%s) VALUES (%s) RETURNING %s", insertFieldsSnippet, insertPlaceHoldersSnippet, returningFieldsSnippet), 70 + fmt.Sprintf("INSERT INTO org (%s) VALUES (%s) RETURNING %s", insertFieldsSnippet, insertPlaceHoldersSnippet, returningFieldsSnippet),
71 org.OrgId, 71 org.OrgId,
72 org.CompanyId, 72 org.CompanyId,
73 org.CreatedAt, 73 org.CreatedAt,
@@ -98,7 +98,7 @@ func (repository *OrgRepository) Save(org *domain.Org) (*domain.Org, error) { @@ -98,7 +98,7 @@ func (repository *OrgRepository) Save(org *domain.Org) (*domain.Org, error) {
98 &org.ParentId, 98 &org.ParentId,
99 &org.ParentPath, 99 &org.ParentPath,
100 ), 100 ),
101 - fmt.Sprintf("UPDATE orgs SET %s WHERE org_id=? RETURNING %s", updateFieldsSnippet, returningFieldsSnippet), 101 + fmt.Sprintf("UPDATE org SET %s WHERE org_id=? RETURNING %s", updateFieldsSnippet, returningFieldsSnippet),
102 org.CompanyId, 102 org.CompanyId,
103 org.CreatedAt, 103 org.CreatedAt,
104 org.UpdatedAt, 104 org.UpdatedAt,
  1 +package controllers
  2 +
  3 +import (
  4 + "github.com/linmadan/egglib-go/web/beego"
  5 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/org/command"
  6 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/org/query"
  7 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/org/service"
  8 +)
  9 +
  10 +type OrgController struct {
  11 + beego.BaseController
  12 +}
  13 +
  14 +func (controller *OrgController) CreateOrg() {
  15 + orgService := service.NewOrgService(nil)
  16 + createOrgCommand := &command.CreateOrgCommand{}
  17 + controller.Unmarshal(createOrgCommand)
  18 + data, err := orgService.CreateOrg(createOrgCommand)
  19 + controller.Response(data, err)
  20 +}
  21 +
  22 +func (controller *OrgController) UpdateOrg() {
  23 + orgService := service.NewOrgService(nil)
  24 + updateOrgCommand := &command.UpdateOrgCommand{}
  25 + controller.Unmarshal(updateOrgCommand)
  26 + orgId, _ := controller.GetInt64(":orgId")
  27 + updateOrgCommand.OrgId = orgId
  28 + data, err := orgService.UpdateOrg(updateOrgCommand)
  29 + controller.Response(data, err)
  30 +}
  31 +
  32 +func (controller *OrgController) GetOrg() {
  33 + orgService := service.NewOrgService(nil)
  34 + getOrgQuery := &query.GetOrgQuery{}
  35 + orgId, _ := controller.GetInt64(":orgId")
  36 + getOrgQuery.OrgId = orgId
  37 + data, err := orgService.GetOrg(getOrgQuery)
  38 + controller.Response(data, err)
  39 +}
  40 +
  41 +func (controller *OrgController) RemoveOrg() {
  42 + orgService := service.NewOrgService(nil)
  43 + removeOrgCommand := &command.RemoveOrgCommand{}
  44 + controller.Unmarshal(removeOrgCommand)
  45 + orgId, _ := controller.GetInt64(":orgId")
  46 + removeOrgCommand.OrgId = orgId
  47 + data, err := orgService.RemoveOrg(removeOrgCommand)
  48 + controller.Response(data, err)
  49 +}
  50 +
  51 +func (controller *OrgController) ListOrg() {
  52 + orgService := service.NewOrgService(nil)
  53 + listOrgQuery := &query.ListOrgQuery{}
  54 + data, err := orgService.ListOrg(listOrgQuery)
  55 + controller.Response(data, err)
  56 +}
  57 +
  58 +func (controller *OrgController) GetOrgSubDepartment() {
  59 + orgService := service.NewOrgService(nil)
  60 + getOrgSubDepartmentQuery := &query.GetOrgSubDepartmentQuery{}
  61 + orgId, _ := controller.GetInt64(":orgId")
  62 + getOrgSubDepartmentQuery.OrgId = orgId
  63 + childFlag, _ := controller.GetInt("childFlag")
  64 + getOrgSubDepartmentQuery.ChildFlag = childFlag
  65 + data, err := orgService.GetOrgSubDepartment(getOrgSubDepartmentQuery)
  66 + controller.Response(data, err)
  67 +}
  68 +
  69 +func (controller *OrgController) EnableOrg() {
  70 + orgService := service.NewOrgService(nil)
  71 + enableOrgCommand := &command.EnableOrgCommand{}
  72 + controller.Unmarshal(enableOrgCommand)
  73 + data, err := orgService.EnableOrg(enableOrgCommand)
  74 + controller.Response(data, err)
  75 +}
  1 +package routers
  2 +
  3 +import (
  4 + "github.com/beego/beego/v2/server/web"
  5 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/port/beego/controllers"
  6 +)
  7 +
  8 +func init() {
  9 + web.Router("/org/", &controllers.OrgController{}, "Post:CreateOrg")
  10 + web.Router("/org/:orgId", &controllers.OrgController{}, "Put:UpdateOrg")
  11 + web.Router("/org/:orgId", &controllers.OrgController{}, "Get:GetOrg")
  12 + web.Router("/org/:orgId", &controllers.OrgController{}, "Delete:RemoveOrg")
  13 + web.Router("/org/search", &controllers.OrgController{}, "Post:ListOrg")
  14 + web.Router("/org/:orgId/sub-department", &controllers.OrgController{}, "Get:GetOrgSubDepartment")
  15 + web.Router("/org/enable", &controllers.OrgController{}, "Post:EnableOrg")
  16 +}
  1 +package org
  2 +
  3 +import (
  4 + "net/http"
  5 +
  6 + "github.com/gavv/httpexpect"
  7 + . "github.com/onsi/ginkgo"
  8 + . "github.com/onsi/gomega"
  9 + pG "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/pg"
  10 +)
  11 +
  12 +var _ = Describe("创建组织", func() {
  13 + Describe("提交数据创建组织", func() {
  14 + Context("提交正确的新组织 organization数据", func() {
  15 + It("返回组织 organization数据", func() {
  16 + httpExpect := httpexpect.New(GinkgoT(), server.URL)
  17 + body := map[string]interface{}{
  18 + "companyId": "int64",
  19 + "orgCode": "string",
  20 + "orgName": "string",
  21 + "isOrg": "int",
  22 + "parentId": "int64",
  23 + }
  24 + httpExpect.POST("/org/").
  25 + WithJSON(body).
  26 + Expect().
  27 + Status(http.StatusOK).
  28 + JSON().
  29 + Object().
  30 + ContainsKey("code").ValueEqual("code", 0).
  31 + ContainsKey("msg").ValueEqual("msg", "ok").
  32 + ContainsKey("data").Value("data").Object().
  33 + ContainsKey("orgId").ValueNotEqual("orgId", BeZero())
  34 + })
  35 + })
  36 + })
  37 + AfterEach(func() {
  38 + _, err := pG.DB.Exec("DELETE FROM org WHERE true")
  39 + Expect(err).NotTo(HaveOccurred())
  40 + })
  41 +})
  1 +package org
  2 +
  3 +import (
  4 + "github.com/go-pg/pg/v10"
  5 + "net/http"
  6 +
  7 + "github.com/gavv/httpexpect"
  8 + . "github.com/onsi/ginkgo"
  9 + . "github.com/onsi/gomega"
  10 + pG "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/pg"
  11 +)
  12 +
  13 +var _ = Describe("设置组织启用状态", func() {
  14 + var orgId int64
  15 + BeforeEach(func() {
  16 + _, err := pG.DB.QueryOne(
  17 + pg.Scan(&orgId),
  18 + "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",
  19 + "testOrgId", "testCompanyId", "testCreatedAt", "testUpdatedAt", "testDeletedAt", "testOrgCode", "testOrgName", "testExt", "testIsOrg", "testParentId", "testOrgStatus", "testParentPath")
  20 + Expect(err).NotTo(HaveOccurred())
  21 + })
  22 + Describe("设置组织启用状态", func() {
  23 + Context("", func() {
  24 + It("", func() {
  25 + httpExpect := httpexpect.New(GinkgoT(), server.URL)
  26 + body := map[string]interface{}{
  27 + "orgId": "int64",
  28 + "orgStatus": "int",
  29 + }
  30 + httpExpect.POST("/org/enable").
  31 + WithJSON(body).
  32 + Expect().
  33 + Status(http.StatusOK).
  34 + JSON().
  35 + Object().
  36 + ContainsKey("code").ValueEqual("code", 0).
  37 + ContainsKey("msg").ValueEqual("msg", "ok").
  38 + ContainsKey("data").Value("data").Object()
  39 + })
  40 + })
  41 + })
  42 + AfterEach(func() {
  43 + _, err := pG.DB.Exec("DELETE FROM org WHERE true")
  44 + Expect(err).NotTo(HaveOccurred())
  45 + })
  46 +})
  1 +package org
  2 +
  3 +import (
  4 + "github.com/go-pg/pg/v10"
  5 + "net/http"
  6 +
  7 + "github.com/gavv/httpexpect"
  8 + . "github.com/onsi/ginkgo"
  9 + . "github.com/onsi/gomega"
  10 + pG "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/pg"
  11 +)
  12 +
  13 +var _ = Describe("获取组织的子部门(通用部门列表使用)", func() {
  14 + var orgId int64
  15 + BeforeEach(func() {
  16 + _, err := pG.DB.QueryOne(
  17 + pg.Scan(&orgId),
  18 + "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",
  19 + "testOrgId", "testCompanyId", "testCreatedAt", "testUpdatedAt", "testDeletedAt", "testOrgCode", "testOrgName", "testExt", "testIsOrg", "testParentId", "testOrgStatus", "testParentPath")
  20 + Expect(err).NotTo(HaveOccurred())
  21 + })
  22 + Describe("获取组织的子部门(通用部门列表使用)", func() {
  23 + Context("", func() {
  24 + It("", func() {
  25 + httpExpect := httpexpect.New(GinkgoT(), server.URL)
  26 + httpExpect.GET("/org/{orgId}/sub-department").
  27 + WithQuery("childFlag", "int").
  28 + Expect().
  29 + Status(http.StatusOK).
  30 + JSON().
  31 + Object().
  32 + ContainsKey("code").ValueEqual("code", 0).
  33 + ContainsKey("msg").ValueEqual("msg", "ok").
  34 + ContainsKey("data").Value("data").Object()
  35 + })
  36 + })
  37 + })
  38 + AfterEach(func() {
  39 + _, err := pG.DB.Exec("DELETE FROM org WHERE true")
  40 + Expect(err).NotTo(HaveOccurred())
  41 + })
  42 +})
  1 +package org
  2 +
  3 +import (
  4 + "github.com/go-pg/pg/v10"
  5 + "net/http"
  6 +
  7 + "github.com/gavv/httpexpect"
  8 + . "github.com/onsi/ginkgo"
  9 + . "github.com/onsi/gomega"
  10 + pG "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/pg"
  11 +)
  12 +
  13 +var _ = Describe("返回组织", func() {
  14 + var orgId int64
  15 + BeforeEach(func() {
  16 + _, err := pG.DB.QueryOne(
  17 + pg.Scan(&orgId),
  18 + "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",
  19 + "testOrgId", "testCompanyId", "testCreatedAt", "testUpdatedAt", "testDeletedAt", "testOrgCode", "testOrgName", "testExt", "testIsOrg", "testParentId", "testOrgStatus", "testParentPath")
  20 + Expect(err).NotTo(HaveOccurred())
  21 + })
  22 + Describe("根据orgId参数返回组织 organization", func() {
  23 + Context("传入有效的orgId", func() {
  24 + It("返回组织 organization数据", func() {
  25 + httpExpect := httpexpect.New(GinkgoT(), server.URL)
  26 + httpExpect.GET("/org/{orgId}").
  27 + Expect().
  28 + Status(http.StatusOK).
  29 + JSON().
  30 + Object().
  31 + ContainsKey("code").ValueEqual("code", 0).
  32 + ContainsKey("msg").ValueEqual("msg", "ok").
  33 + ContainsKey("data").Value("data").Object()
  34 + })
  35 + })
  36 + })
  37 + AfterEach(func() {
  38 + _, err := pG.DB.Exec("DELETE FROM org WHERE true")
  39 + Expect(err).NotTo(HaveOccurred())
  40 + })
  41 +})
  1 +package org
  2 +
  3 +import (
  4 + "github.com/go-pg/pg/v10"
  5 + "net/http"
  6 +
  7 + "github.com/gavv/httpexpect"
  8 + . "github.com/onsi/ginkgo"
  9 + . "github.com/onsi/gomega"
  10 + pG "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/pg"
  11 +)
  12 +
  13 +var _ = Describe("返回组织列表", func() {
  14 + var orgId int64
  15 + BeforeEach(func() {
  16 + _, err := pG.DB.QueryOne(
  17 + pg.Scan(&orgId),
  18 + "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",
  19 + "testOrgId", "testCompanyId", "testCreatedAt", "testUpdatedAt", "testDeletedAt", "testOrgCode", "testOrgName", "testExt", "testIsOrg", "testParentId", "testOrgStatus", "testParentPath")
  20 + Expect(err).NotTo(HaveOccurred())
  21 + })
  22 + Describe("根据参数返回组织 organization列表", func() {
  23 + Context("传入有效的参数", func() {
  24 + It("返回组织 organization数据列表", func() {
  25 + httpExpect := httpexpect.New(GinkgoT(), server.URL)
  26 + body := map[string]interface{}{
  27 + "offset": "int",
  28 + "limit": "int",
  29 + "companyId": "int64",
  30 + "orgCode": "string",
  31 + "depName": "string",
  32 + "parentId": "int64",
  33 + "isOrg": "int",
  34 + }
  35 + httpExpect.POST("/org/search").
  36 + WithJSON(body).
  37 + Expect().
  38 + Status(http.StatusOK).
  39 + JSON().
  40 + Object().
  41 + ContainsKey("code").ValueEqual("code", 0).
  42 + ContainsKey("msg").ValueEqual("msg", "ok").
  43 + ContainsKey("data").Value("data").Object().
  44 + ContainsKey("count").ValueEqual("count", 1).
  45 + ContainsKey("org").Value("org").Array()
  46 + })
  47 + })
  48 + })
  49 + AfterEach(func() {
  50 + _, err := pG.DB.Exec("DELETE FROM org WHERE true")
  51 + Expect(err).NotTo(HaveOccurred())
  52 + })
  53 +})
  1 +package org
  2 +
  3 +import (
  4 + "net/http"
  5 + "net/http/httptest"
  6 + "testing"
  7 +
  8 + "github.com/beego/beego/v2/server/web"
  9 + . "github.com/onsi/ginkgo"
  10 + . "github.com/onsi/gomega"
  11 + _ "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application"
  12 + _ "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/pg"
  13 + _ "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/port/beego"
  14 +)
  15 +
  16 +func TestOrg(t *testing.T) {
  17 + RegisterFailHandler(Fail)
  18 + RunSpecs(t, "Beego Port Org Correlations Test Case Suite")
  19 +}
  20 +
  21 +var handler http.Handler
  22 +var server *httptest.Server
  23 +
  24 +var _ = BeforeSuite(func() {
  25 + handler = web.BeeApp.Handlers
  26 + server = httptest.NewServer(handler)
  27 +})
  28 +
  29 +var _ = AfterSuite(func() {
  30 + server.Close()
  31 +})
  1 +package org
  2 +
  3 +import (
  4 + "github.com/go-pg/pg/v10"
  5 + "net/http"
  6 +
  7 + "github.com/gavv/httpexpect"
  8 + . "github.com/onsi/ginkgo"
  9 + . "github.com/onsi/gomega"
  10 + pG "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/pg"
  11 +)
  12 +
  13 +var _ = Describe("移除组织", func() {
  14 + var orgId int64
  15 + BeforeEach(func() {
  16 + _, err := pG.DB.QueryOne(
  17 + pg.Scan(&orgId),
  18 + "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",
  19 + "testOrgId", "testCompanyId", "testCreatedAt", "testUpdatedAt", "testDeletedAt", "testOrgCode", "testOrgName", "testExt", "testIsOrg", "testParentId", "testOrgStatus", "testParentPath")
  20 + Expect(err).NotTo(HaveOccurred())
  21 + })
  22 + Describe("根据参数移除组织", func() {
  23 + Context("传入有效的orgId", func() {
  24 + It("返回被移除组织 organization的数据", func() {
  25 + httpExpect := httpexpect.New(GinkgoT(), server.URL)
  26 + httpExpect.DELETE("/org/{orgId}").
  27 + Expect().
  28 + Status(http.StatusOK).
  29 + JSON().
  30 + Object().
  31 + ContainsKey("code").ValueEqual("code", 0).
  32 + ContainsKey("msg").ValueEqual("msg", "ok").
  33 + ContainsKey("data").Value("data").Object()
  34 + })
  35 + })
  36 + })
  37 + AfterEach(func() {
  38 + _, err := pG.DB.Exec("DELETE FROM org WHERE true")
  39 + Expect(err).NotTo(HaveOccurred())
  40 + })
  41 +})
  1 +package org
  2 +
  3 +import (
  4 + "github.com/go-pg/pg/v10"
  5 + "net/http"
  6 +
  7 + "github.com/gavv/httpexpect"
  8 + . "github.com/onsi/ginkgo"
  9 + . "github.com/onsi/gomega"
  10 + pG "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/pg"
  11 +)
  12 +
  13 +var _ = Describe("更新组织", func() {
  14 + var orgId int64
  15 + BeforeEach(func() {
  16 + _, err := pG.DB.QueryOne(
  17 + pg.Scan(&orgId),
  18 + "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",
  19 + "testOrgId", "testCompanyId", "testCreatedAt", "testUpdatedAt", "testDeletedAt", "testOrgCode", "testOrgName", "testExt", "testIsOrg", "testParentId", "testOrgStatus", "testParentPath")
  20 + Expect(err).NotTo(HaveOccurred())
  21 + })
  22 + Describe("提交数据更新组织", func() {
  23 + Context("提交正确的组织 organization数据", func() {
  24 + It("返回更新后的组织 organization数据", func() {
  25 + httpExpect := httpexpect.New(GinkgoT(), server.URL)
  26 + body := map[string]interface{}{
  27 + "orgCode": "string",
  28 + "orgName": "string",
  29 + "isOrg": "int",
  30 + "parentId": "int64",
  31 + }
  32 + httpExpect.PUT("/org/{orgId}").
  33 + WithJSON(body).
  34 + Expect().
  35 + Status(http.StatusOK).
  36 + JSON().
  37 + Object().
  38 + ContainsKey("code").ValueEqual("code", 0).
  39 + ContainsKey("msg").ValueEqual("msg", "ok").
  40 + ContainsKey("data").Value("data").Object().
  41 + ContainsKey("orgId").ValueEqual("orgId", orgId)
  42 + })
  43 + })
  44 + })
  45 + AfterEach(func() {
  46 + _, err := pG.DB.Exec("DELETE FROM org WHERE true")
  47 + Expect(err).NotTo(HaveOccurred())
  48 + })
  49 +})