作者 yangfu

导入用户 组织修改

... ... @@ -99,6 +99,8 @@ spec:
value: "true"
- name: HTTP_PORT
value: "8082"
- name: SERVICE_ENV
value: "dev"
- name: REDIS_HOST
valueFrom:
configMapKeyRef:
... ...
... ... @@ -102,3 +102,11 @@ func CreatePgBatchRemoveRoleService(options map[string]interface{}) (service.PgB
}
return domainService.NewPgBatchRemoveRoleService(transactionContext)
}
func CreatePgBatchAddOrgService(options map[string]interface{}) (service.PgBatchAddOrgService, error) {
var transactionContext *pgTransaction.TransactionContext
if value, ok := options["transactionContext"]; ok {
transactionContext = value.(*pgTransaction.TransactionContext)
}
return domainService.NewPgBatchAddOrgService(transactionContext)
}
... ...
package command
import (
"fmt"
"github.com/beego/beego/v2/core/validation"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain"
"reflect"
"strings"
)
type BatchAddCommand struct {
OperateInfo *domain.OperateInfo `json:"-"`
OrgList []*domain.BatchAddOrgItem `json:"orgList"`
}
func (batchAddCommand *BatchAddCommand) Valid(validation *validation.Validation) {
//validation.SetError("CustomValid", "未实现的自定义认证")
}
func (batchAddCommand *BatchAddCommand) ValidateCommand() error {
valid := validation.Validation{}
b, err := valid.Valid(batchAddCommand)
if err != nil {
return err
}
if !b {
elem := reflect.TypeOf(batchAddCommand).Elem()
for _, validErr := range valid.Errors {
field, isExist := elem.FieldByName(validErr.Field)
if isExist {
return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1))
} else {
return fmt.Errorf(validErr.Message)
}
}
}
return nil
}
... ...
... ... @@ -16,6 +16,35 @@ import (
type OrgService struct {
}
// 批量添加用户
func (orgService *OrgService) BatchAdd(batchAddCommand *command.BatchAddCommand) (interface{}, error) {
if err := batchAddCommand.ValidateCommand(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
batchAddOrgService, _ := factory.CreatePgBatchAddOrgService(map[string]interface{}{
"transactionContext": transactionContext,
})
if err = batchAddOrgService.BatchAddOrg(batchAddCommand.OperateInfo, batchAddCommand.OrgList); 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 struct{}{}, nil
}
// 创建组织
func (orgService *OrgService) CreateOrg(createOrgCommand *command.CreateOrgCommand) (interface{}, error) {
if err := createOrgCommand.ValidateCommand(); err != nil {
... ...
... ... @@ -12,7 +12,7 @@ import (
type BatchAddCommand struct {
OperateInfo *domain.OperateInfo `json:"-"`
// 用户类型 1:企业内部用户(内部添加) 2:共创用户 1024:企业注册用户(注册添加)
UserType int `cname:"用户类型" json:"userType" valid:"Required"`
//UserType int `cname:"用户类型" json:"userType" valid:"Required"`
// 用户列表
Users []*domain.User `cname:"用户列表" json:"users,omitempty"`
// 密码
... ...
package command
import (
"fmt"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain"
"reflect"
"strings"
"github.com/beego/beego/v2/core/validation"
)
type BatchAdd2Command struct {
OperateInfo *domain.OperateInfo `json:"-"`
// 用户列表
Users []*domain.BatchAddUserItem `cname:"用户列表" json:"users,omitempty"`
// 密码
Password string `cname:"密码" json:"password" valid:"Required"`
}
func (batchAddCommand *BatchAdd2Command) Valid(validation *validation.Validation) {
//validation.SetError("CustomValid", "未实现的自定义认证")
}
func (batchAddCommand *BatchAdd2Command) ValidateCommand() error {
valid := validation.Validation{}
b, err := valid.Valid(batchAddCommand)
if err != nil {
return err
}
if !b {
elem := reflect.TypeOf(batchAddCommand).Elem()
for _, validErr := range valid.Errors {
field, isExist := elem.FieldByName(validErr.Field)
if isExist {
return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1))
} else {
return fmt.Errorf(validErr.Message)
}
}
}
return nil
}
... ...
... ... @@ -45,6 +45,35 @@ func (userService *UserService) BatchAdd(batchAddCommand *command.BatchAddComman
return nil, nil
}
// 批量添加用户
func (userService *UserService) BatchAdd2(batchAddCommand *command.BatchAdd2Command) (interface{}, error) {
if err := batchAddCommand.ValidateCommand(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
batchAddUserService, _ := factory.CreateBatchAddUserService(map[string]interface{}{
"transactionContext": transactionContext,
})
if err = batchAddUserService.BatchAddUser2(batchAddCommand.OperateInfo, batchAddCommand.Users, batchAddCommand.Password); 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 nil, nil
}
// 批量修改启用状态
func (userService *UserService) BatchEnable(batchEnableCommand *command.BatchEnableCommand) (interface{}, error) {
if err := batchEnableCommand.ValidateCommand(); err != nil {
... ...
... ... @@ -47,5 +47,6 @@ func init() {
if os.Getenv("HTTP_PORT") != "" {
HTTP_PORT, _ = strconv.Atoi(os.Getenv("HTTP_PORT"))
}
CACHE_PREFIX = fmt.Sprintf("%v-%v", SERVICE_NAME, SERVICE_ENV)
SERVICE_NAME = fmt.Sprintf("%v-%v", SERVICE_NAME, SERVICE_ENV)
CACHE_PREFIX = SERVICE_NAME
}
... ...
... ... @@ -205,3 +205,16 @@ func (m *Org) CacheKeyFunc() string {
}
return fmt.Sprintf("%v:cache:org:id:%v", constant.CACHE_PREFIX, m.OrgId)
}
/***** 3.批量添加组织项 *****/
type BatchAddOrgItem struct {
// 组织编码
OrgCode string `json:"orgCode,omitempty"`
// 父级组织编码
ParentOrgCode string `json:"parentOrgCode,omitempty"`
// 组织名称
OrgName string `json:"orgName,omitempty"`
// 企业id
CompanyId int64 `json:"companyId,omitempty"`
}
... ...
package service
import "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain"
// PgBatchAddUserService 批量添加用户服务
type PgBatchAddOrgService interface {
BatchAddOrg(optUser *domain.OperateInfo, orgList []*domain.BatchAddOrgItem) error
}
... ...
... ... @@ -5,4 +5,5 @@ import "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain"
// PgBatchAddUserService 批量添加用户服务
type PgBatchAddUserService interface {
BatchAddUser(optUser *domain.OperateInfo, users []*domain.User, password string) error
BatchAddUser2(optUser *domain.OperateInfo, users []*domain.BatchAddUserItem, password string) error
}
... ...
... ... @@ -284,3 +284,30 @@ func (user *User) CacheKeyFunc() string {
func (user *User) BelongOrg() int64 {
return user.OrganizationId
}
/***** 4.批量添加组织项 *****/
type BatchAddUserItem struct {
// 企业id
CompanyId int64 `json:"companyId,omitempty"`
// 用户类型 1:企业内部用户(内部添加) 2:共创用户 1024:企业注册用户(注册添加)
UserType int `json:"userType,omitempty"`
// 用户姓名
UserName string `json:"userName,omitempty"`
// 手机号码
Phone string `json:"phone,omitempty"`
// 邮箱
Email string `json:"email,omitempty"`
// 用户编号 企业内标识
UserCode string `json:"userCode,omitempty"`
// 组织编码
Org string `json:"org,omitempty"`
// 部门编码
Department string `json:"department,omitempty"`
// 状态(1:启用 2:禁用 3:注销)
EnableStatus int `json:"enableStatus,omitempty"`
// 共创公司 cooperationCompany
CooperationCompany string `json:"cooperationCompany"`
// 共创到期时间 (yyyy-MM-dd) cooperationDeadline
CooperationDeadline time.Time `json:"cooperationDeadline"`
}
... ...
package domainService
import (
"fmt"
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/repository"
"time"
)
// PgBatchAddOrgService 批量创建组织服务
type PgBatchAddOrgService struct {
transactionContext *pgTransaction.TransactionContext
}
func (ptr *PgBatchAddOrgService) BatchAddOrg(optUser *domain.OperateInfo, orgList []*domain.BatchAddOrgItem) error {
var (
err error
)
orgRepository, err := repository.NewOrgRepository(ptr.transactionContext)
if err != nil {
return err
}
_, orgs, err := orgRepository.Find(map[string]interface{}{"companyId": optUser.CompanyId})
if err != nil {
return err
}
var mapOrg = make(map[string]*domain.Org)
for i := range orgs {
mapOrg[orgs[i].OrgCode] = orgs[i]
}
createOrgService, _ := NewPgCreateOrgService(ptr.transactionContext)
for i := range orgList {
item := orgList[i]
orgItem := &domain.Org{
CompanyId: optUser.CompanyId,
OrgCode: item.OrgCode,
OrgName: item.OrgName,
IsOrg: domain.IsOrgFlag,
ParentId: 0,
OrgStatus: domain.OrgStatusEnable,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
Ext: &domain.Ext{},
}
if v, ok := mapOrg[item.ParentOrgCode]; !ok {
return fmt.Errorf(fmt.Sprintf("找不到组织:%v", item.OrgCode))
} else {
orgItem.ParentId = v.OrgId
}
orgItem, err = createOrgService.CreateOrg(optUser, orgItem)
if err != nil {
return err
}
if _, ok := mapOrg[orgItem.OrgCode]; !ok {
mapOrg[orgItem.OrgCode] = orgItem
}
}
return err
}
func NewPgBatchAddOrgService(transactionContext *pgTransaction.TransactionContext) (*PgBatchAddOrgService, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
return &PgBatchAddOrgService{
transactionContext: transactionContext,
}, nil
}
}
... ...
... ... @@ -82,6 +82,73 @@ func (ptr *PgBatchAddUserService) BatchAddUser(optUser *domain.OperateInfo, user
return nil
}
func (ptr *PgBatchAddUserService) BatchAddUser2(optUser *domain.OperateInfo, users []*domain.BatchAddUserItem, password string) error {
var (
err error
)
orgRepository, err := repository.NewOrgRepository(ptr.transactionContext)
if err != nil {
return err
}
_, orgs, err := orgRepository.Find(map[string]interface{}{"companyId": optUser.CompanyId})
if err != nil {
return err
}
var mapOrg = make(map[string]*domain.Org)
for i := range orgs {
mapOrg[orgs[i].OrgCode] = orgs[i]
}
createUserService, _ := NewPgCreateUserService(ptr.transactionContext)
for i := range users {
user := users[i]
if err = ptr.preCheck2(user); err != nil {
return err
}
var org, dep *domain.Org
var ok bool
if org, ok = mapOrg[user.Org]; !ok {
return fmt.Errorf("导入的组织机构不存在:" + user.Org)
}
if dep, ok = mapOrg[user.Department]; !ok {
return fmt.Errorf("导入的所属部门不存在:" + user.Department)
}
newUser := &domain.User{
CompanyId: user.CompanyId,
UserType: user.UserType,
UserCode: user.UserCode,
OrganizationId: org.OrgId,
DepartmentId: dep.OrgId,
UserOrg: []*domain.Org{},
UserRole: []*domain.Role{},
FavoriteMenus: []string{},
CooperationInfo: &domain.CooperationInfo{
CooperationCompany: user.CooperationCompany,
CooperationDeadline: user.CooperationDeadline,
},
UserInfo: &domain.UserInfo{
UserName: user.UserName,
Phone: user.Phone,
Avatar: "",
Email: user.Email,
},
EnableStatus: int(domain.UserStatusEnable),
Ext: &domain.Ext{
Phone: user.Phone,
UserName: user.UserName,
OrgName: org.OrgName,
DepName: dep.OrgName,
},
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
}
if newUser, err = createUserService.CreateUser(nil, newUser, password); err != nil {
return err
}
}
return nil
}
func (ptr *PgBatchAddUserService) preCheck(user *domain.User) error {
if len(user.UserInfo.UserName) == 0 {
return fmt.Errorf("导入的用户姓名为空值")
... ... @@ -98,6 +165,22 @@ func (ptr *PgBatchAddUserService) preCheck(user *domain.User) error {
return nil
}
func (ptr *PgBatchAddUserService) preCheck2(user *domain.BatchAddUserItem) error {
if len(user.UserName) == 0 {
return fmt.Errorf("导入的用户姓名为空值")
}
if len(user.Phone) == 0 {
return fmt.Errorf("导入的手机号不是有效手机号")
}
//if len(user.Org) == 0 {
// return fmt.Errorf("导入的组织机构不存在")
//}
//if len(user.Department) == 0 && user.UserType == domain.UserTypeEmployee {
// return fmt.Errorf("导入的所属部门不存在")
//}
return nil
}
func NewPgBatchAddUserService(transactionContext *pgTransaction.TransactionContext) (*PgBatchAddUserService, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
... ...
... ... @@ -4,7 +4,7 @@ import (
"context"
"fmt"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/pg/models"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/log"
"log"
"reflect"
"github.com/go-pg/pg/v10"
... ... @@ -61,7 +61,8 @@ func (hook SqlGeneratePrintHook) AfterQuery(c context.Context, q *pg.QueryEvent)
if err != nil {
return err
}
log.Logger.Debug(string(sqlStr))
//log.Logger.Debug(string(sqlStr))
log.Println(string(sqlStr))
return nil
}
... ...
... ... @@ -89,3 +89,12 @@ func (controller *OrgController) SearchOrg() {
data, err := orgService.ListOrg(listOrgQuery)
controller.Response(data, err)
}
func (controller *OrgController) BatchAdd() {
orgService := service.NewOrgService(nil)
batchAddCommand := &command.BatchAddCommand{}
Must(controller.Unmarshal(batchAddCommand))
batchAddCommand.OperateInfo = ParseOperateInfo(controller.BaseController)
data, err := orgService.BatchAdd(batchAddCommand)
controller.Response(data, err)
}
... ...
... ... @@ -93,6 +93,15 @@ func (controller *UserController) BatchAdd() {
controller.Response(data, err)
}
func (controller *UserController) BatchAdd2() {
userService := service.NewUserService(nil)
batchAddCommand := &command.BatchAdd2Command{}
Must(controller.Unmarshal(batchAddCommand))
batchAddCommand.OperateInfo = ParseOperateInfo(controller.BaseController)
data, err := userService.BatchAdd2(batchAddCommand)
controller.Response(data, err)
}
func (controller *UserController) BatchEnable() {
userService := service.NewUserService(nil)
batchEnableCommand := &command.BatchEnableCommand{}
... ...
... ... @@ -13,4 +13,5 @@ func init() {
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")
web.Router("/org/batch-add", &controllers.OrgController{}, "Post:BatchAdd")
}
... ...
... ... @@ -14,6 +14,7 @@ func init() {
web.Router("/user/:userId/access-menus", &controllers.UserController{}, "Get:GetUserAccessMenus")
web.Router("/user/:userId/profile", &controllers.UserController{}, "Get:GetUserProfile")
web.Router("/user/batch-add", &controllers.UserController{}, "Post:BatchAdd")
web.Router("/user/batch-add2", &controllers.UserController{}, "Post:BatchAdd2")
web.Router("/user/batch-enable", &controllers.UserController{}, "Post:BatchEnable")
web.Router("/user/batch-reset-password", &controllers.UserController{}, "Post:BatchResetPassword")
web.Router("/user/:userId/base-info", &controllers.UserController{}, "Put:UpdateUsersBase")
... ...