users.go 14.8 KB
package service

import (
	"strconv"
	"time"

	"github.com/linmadan/egglib-go/core/application"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/web/users/command"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/web/users/dto"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/web/users/query"

	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/infrastructure/service_gateway/allied_creation_user"
)

// 用户信息
type UsersService struct {
}

// 获取公司用户信息
func (usersService *UsersService) CompanyUserGet(companyUserGetQuery *query.CompanyUserGetQuery) (interface{}, error) {
	creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(
		companyUserGetQuery.Operator.CompanyId,
		companyUserGetQuery.Operator.OrgId,
		companyUserGetQuery.Operator.UserId)
	userid, _ := strconv.Atoi(companyUserGetQuery.UsersId)
	resultUser, err := creationUserGateway.UserGet(allied_creation_user.ReqGateUser{
		UserId: userid,
	})
	if err != nil {
		return nil, err
	}
	resultMenu, err := creationUserGateway.UserAccessMenus(allied_creation_user.ReqUserAccessMenus{})
	if err != nil {
		return nil, err
	}
	var (
		usersOrg []dto.UserOrg
		userRole []dto.UserRole
		userMenu []dto.UserMenu
	)

	for _, v := range resultMenu.Menus {
		userMenu = append(userMenu, dto.UserMenu{
			Category: v.Category,
			Code:     v.Code,
			Icon:     v.Icon,
			MenuName: v.MenuName,
			MenuID:   strconv.Itoa(v.MenuID),
			MenuType: v.MenuType,
			ParentID: strconv.Itoa(v.ParentID),
			Remark:   v.Remark,
			Sort:     v.Sort,
		})
	}
	for _, v := range resultUser.UserOrg {
		usersOrg = append(usersOrg, dto.UserOrg{
			OrgID:   strconv.Itoa(v.OrgID),
			OrgName: v.OrgName,
		})
	}
	for _, v := range resultUser.UserRole {
		userRole = append(userRole, dto.UserRole{
			RoleID:   strconv.Itoa(v.RoleID),
			RoleName: v.RoleName,
			OrgName:  v.Ext.OrgName,
		})
	}
	user := dto.CompanyUserInfo{
		Email:          resultUser.UserInfo.Email,
		Phone:          resultUser.UserInfo.Phone,
		Avatar:         resultUser.UserInfo.Avatar,
		EnableStatus:   resultUser.EnableStatus,
		UsersCode:      resultUser.UserCode,
		UsersID:        strconv.Itoa(resultUser.UserId),
		UsersName:      resultUser.UserInfo.UserName,
		OrgID:          strconv.Itoa(resultUser.Org.OrgId),
		OrgName:        resultUser.Org.OrgName,
		DepartmentID:   strconv.Itoa(resultUser.Department.DepartmentId),
		DepartmentName: resultUser.Department.DepartmentName,
		UsersOrg:       usersOrg,
		UsersRole:      userRole,
	}

	datas := map[string]interface{}{
		"user":     user,
		"userMenu": userMenu,
	}
	return datas, err
}

// 创建公司用户信息
func (usersService *UsersService) CompanyUserAdd(companyUserAddCommand *command.CompanyUserAddCommand) (interface{}, error) {
	creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(
		companyUserAddCommand.Operator.CompanyId,
		companyUserAddCommand.Operator.OrgId,
		companyUserAddCommand.Operator.UserId)
	departmentId, _ := strconv.Atoi(companyUserAddCommand.OrgId)
	orgId, _ := strconv.Atoi(companyUserAddCommand.OrgId)
	userOrg := []int64{}
	userRole := []int64{}
	for _, v := range companyUserAddCommand.UsersOrg {
		id, err := strconv.Atoi(v)
		if err == nil {
			userOrg = append(userOrg, int64(id))
		}
	}
	for _, v := range companyUserAddCommand.UsersRole {
		id, err := strconv.Atoi(v)
		if err == nil {
			userRole = append(userRole, int64(id))
		}
	}
	result, err := creationUserGateway.UserCreate(allied_creation_user.ReqCreateUser{
		CompanyId: companyUserAddCommand.Operator.CompanyId,
		// 用户类型  1:企业内部用户(内部添加) 2:共创用户   1024:企业注册用户(注册添加)
		UserType:       1,
		UserCode:       companyUserAddCommand.UsersCode,
		OrganizationId: int64(orgId),
		DepartmentId:   int64(departmentId),
		UserOrg:        userOrg,
		UserRole:       userRole,
		// 启用状态(启用:1 禁用:2)
		EnableStatus: companyUserAddCommand.EnableStatus,
		Password:     "",
		UserName:     companyUserAddCommand.UsersName,
		Phone:        companyUserAddCommand.Phone,
		Avatar:       companyUserAddCommand.Avator,
		Email:        companyUserAddCommand.Avator,
	})

	data := struct {
		UserId string `json:"userId"`
		command.CompanyUserAddCommand
	}{
		UserId:                strconv.Itoa(result.UserId),
		CompanyUserAddCommand: *companyUserAddCommand,
	}
	return data, err
}

// 启用禁用公司用户信息
func (usersService *UsersService) CompanyUserEnable(companyUserEnableCommand *command.CompanyUserEnableCommand) (interface{}, error) {
	creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(
		companyUserEnableCommand.Operator.CompanyId,
		companyUserEnableCommand.Operator.OrgId,
		companyUserEnableCommand.Operator.UserId)
	_, err := creationUserGateway.UserBatchEnable(allied_creation_user.ReqBatchEnableUser{
		UserIds:      companyUserEnableCommand.UsersIds,
		EnableStatus: companyUserEnableCommand.EnableStatus,
	})
	return companyUserEnableCommand, err
}

// 返回公司用户信息列表
func (usersService *UsersService) CompanyUserList(companyUserListQuery *query.CompanyUserListQuery) (int64, interface{}, error) {
	creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(0, 0, 0)
	result, err := creationUserGateway.UserSearch(allied_creation_user.ReqUserSearch{
		Offset:         (companyUserListQuery.PageNumber - 1) * companyUserListQuery.PageSize,
		Limit:          companyUserListQuery.PageSize,
		CompanyId:      companyUserListQuery.Operator.CompanyId,
		OrganizationId: companyUserListQuery.Operator.OrgId,
		DepartmentId:   0,
		UserName:       companyUserListQuery.UserName,
		DepName:        companyUserListQuery.DepartmentName,
		Phone:          "",
	})

	if err != nil {
		return 0, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	//数据转换
	cnt := int64(result.Count)
	var (
		listData []dto.CompanyUserItem
		item     dto.CompanyUserItem
	)
	for _, v := range result.Users {
		item = dto.CompanyUserItem{
			DepName:      v.Department.DepartmentName,
			OrgName:      v.Org.OrgName,
			Phone:        v.UserInfo.Phone,
			EnableStatus: v.EnableStatus,
			UserCode:     v.UserCode,
			UserId:       strconv.Itoa(v.UserId),
			UserName:     v.UserInfo.UserName,
		}
		listData = append(listData, item)
	}
	return cnt, listData, nil
}

// 批量重置密码
func (usersService *UsersService) CompanyUserResetPassword(companyUserResetPasswordCommand *command.CompanyUserResetPasswordCommand) (interface{}, error) {
	creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(
		companyUserResetPasswordCommand.Operator.CompanyId,
		companyUserResetPasswordCommand.Operator.OrgId,
		companyUserResetPasswordCommand.Operator.UserId)
	_, err := creationUserGateway.UserBatchResetPassword(allied_creation_user.ReqBatchResetPasswordUser{
		Password: "",
		UserIds:  companyUserResetPasswordCommand.UsersIds,
	})
	return companyUserResetPasswordCommand, err
}

// 更新公司用户信息
func (usersService *UsersService) CompanyUserUpdate(companyUserUpdateCommand *command.CompanyUserUpdateCommand) (interface{}, error) {
	creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(
		companyUserUpdateCommand.Operator.CompanyId,
		companyUserUpdateCommand.Operator.OrgId,
		companyUserUpdateCommand.Operator.UserId)
	departmentId, _ := strconv.Atoi(companyUserUpdateCommand.OrganizationId)
	orgId, _ := strconv.Atoi(companyUserUpdateCommand.OrganizationId)
	userOrg := []int64{}
	userRole := []int64{}
	for _, v := range companyUserUpdateCommand.UsersOrg {
		id, err := strconv.Atoi(v)
		if err == nil {
			userOrg = append(userOrg, int64(id))
		}
	}
	for _, v := range companyUserUpdateCommand.UsersRole {
		id, err := strconv.Atoi(v)
		if err == nil {
			userRole = append(userRole, int64(id))
		}
	}
	userId, _ := strconv.Atoi(companyUserUpdateCommand.UsersId)
	_, err := creationUserGateway.UserUpdate(allied_creation_user.ReqUpdateUser{
		UserId:         int64(userId),
		CompanyId:      companyUserUpdateCommand.Operator.CompanyId,
		UserCode:       companyUserUpdateCommand.UsersCode,
		OrganizationId: int64(orgId),
		DepartmentId:   int64(departmentId),
		UserOrg:        userOrg,
		UserRole:       userRole,
		EnableStatus:   companyUserUpdateCommand.EnableStatus,
		UserName:       companyUserUpdateCommand.UsersName,
		Phone:          companyUserUpdateCommand.Phone,
		Avatar:         companyUserUpdateCommand.Avator,
		Email:          companyUserUpdateCommand.Avator,
	})
	return companyUserUpdateCommand, err
}

// 创建共创用户信息
func (usersService *UsersService) CooperationUserAdd(cooperationUserAddCommand *command.CooperationUserAddCommand) (interface{}, error) {
	creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(
		cooperationUserAddCommand.Operator.CompanyId,
		cooperationUserAddCommand.Operator.OrgId,
		cooperationUserAddCommand.Operator.UserId)
	result, err := creationUserGateway.CooperatorUserCreate(allied_creation_user.ReqCreateCooperatorUser{
		CooperationCompany:  cooperationUserAddCommand.CooperationCompany,
		CooperationDeadline: time.Unix(cooperationUserAddCommand.CooperationDeadline, 0),
		Email:               cooperationUserAddCommand.Email,
		EnableStatus:        cooperationUserAddCommand.EnableStatus,
		UserCode:            cooperationUserAddCommand.UsersCode,
		UserName:            cooperationUserAddCommand.UsersName,
		Avatar:              cooperationUserAddCommand.Avatar,
		OrgId:               cooperationUserAddCommand.Operator.OrgId,
		Phone:               cooperationUserAddCommand.Phone,
		Password:            "", //TODO 填充默认密码
	})
	data := struct {
		UserId string `json:"userId"`
		command.CooperationUserAddCommand
	}{
		UserId:                    strconv.Itoa(result.UserId),
		CooperationUserAddCommand: *cooperationUserAddCommand,
	}
	return data, err
}

// 启用禁用共创用户信息
func (usersService *UsersService) CooperationUserEnable(cooperationUserEnableCommand *command.CooperationUserEnableCommand) (interface{}, error) {
	creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(
		cooperationUserEnableCommand.Operator.CompanyId,
		cooperationUserEnableCommand.Operator.OrgId,
		cooperationUserEnableCommand.Operator.UserId)
	_, err := creationUserGateway.UserBatchEnable(allied_creation_user.ReqBatchEnableUser{
		UserIds:      cooperationUserEnableCommand.UsersIds,
		EnableStatus: cooperationUserEnableCommand.EnableStatus,
	})
	return cooperationUserEnableCommand, err
}

// 获取共创用户信息
func (usersService *UsersService) CooperationUserGet(cooperationUserGetQuery *query.CooperationUserGetQuery) (interface{}, error) {
	creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(
		cooperationUserGetQuery.Operator.CompanyId,
		cooperationUserGetQuery.Operator.OrgId,
		cooperationUserGetQuery.Operator.UserId)
	userId, _ := strconv.Atoi(cooperationUserGetQuery.UsersId)
	result, err := creationUserGateway.UserGet(allied_creation_user.ReqGateUser{
		UserId: userId,
	})
	if err != nil {
		return nil, err
	}
	deadline := result.CooperationInfo.CooperationDeadline.Unix()
	userInfo := dto.CooperationUserInfo{
		UserId:              strconv.Itoa(result.UserId),
		UserCode:            result.UserCode,
		EnableStatus:        int32(result.EnableStatus),
		CooperationCompany:  result.CooperationInfo.CooperationCompany,
		CooperationDeadline: deadline,
		UserName:            result.UserInfo.UserName,
		Email:               result.UserInfo.Email,
		Phone:               result.UserInfo.Phone,
		Avatar:              result.UserInfo.Avatar,
	}
	return userInfo, nil
}

// 返回共创用户信息列表
func (usersService *UsersService) CooperationUserList(cooperationUserListQuery *query.CooperationUserListQuery) (int64, interface{}, error) {
	creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(0, 0, 0)
	result, err := creationUserGateway.UserSearch(allied_creation_user.ReqUserSearch{
		Offset:         (cooperationUserListQuery.PageNumber - 1) * cooperationUserListQuery.PageSize,
		Limit:          cooperationUserListQuery.PageSize,
		CompanyId:      cooperationUserListQuery.Operator.CompanyId,
		OrganizationId: cooperationUserListQuery.Operator.OrgId,
		DepartmentId:   0,
		UserName:       cooperationUserListQuery.UserName,
		DepName:        "",
		Phone:          "",
	})
	var (
		listData []dto.CooperationUserItem
		item     dto.CooperationUserItem
	)
	cnt := result.Count
	for _, v := range result.Users {
		item = dto.CooperationUserItem{
			CooperationCompany:  v.CooperationInfo.CooperationCompany,
			UserId:              strconv.Itoa(v.UserId),
			CooperationDeadline: v.CooperationInfo.CooperationDeadline.Unix(),
			Phone:               v.UserInfo.Phone,
			EnableStatus:        v.EnableStatus,
			UserCode:            v.UserCode,
			UserName:            v.UserInfo.UserName,
			OrgName:             v.Org.OrgName,
			OrgId:               strconv.Itoa(v.Org.OrgId),
		}
		listData = append(listData, item)
	}
	return cnt, listData, err
}

// 批量重置密码
func (usersService *UsersService) CooperationUserResetPassword(cooperationUserResetPasswordCommand *command.CooperationUserResetPasswordCommand) (interface{}, error) {
	creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(
		cooperationUserResetPasswordCommand.Operator.CompanyId,
		cooperationUserResetPasswordCommand.Operator.OrgId,
		cooperationUserResetPasswordCommand.Operator.UserId)
	_, err := creationUserGateway.UserBatchResetPassword(allied_creation_user.ReqBatchResetPasswordUser{
		Password: "",
		UserIds:  cooperationUserResetPasswordCommand.UsersIds,
	})
	return cooperationUserResetPasswordCommand, err
}

// 编辑共创用户信息
func (usersService *UsersService) CooperationUserUpdate(cooperationUserUpdateCommand *command.CooperationUserUpdateCommand) (interface{}, error) {
	creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(
		cooperationUserUpdateCommand.Operator.CompanyId,
		cooperationUserUpdateCommand.Operator.OrgId,
		cooperationUserUpdateCommand.Operator.UserId)
	_, err := creationUserGateway.CooperatorUserUpdate(allied_creation_user.ReqUpdateCooperatorUser{
		UserId:              cooperationUserUpdateCommand.UsersId,
		CooperationCompany:  cooperationUserUpdateCommand.CooperationCompany,
		CooperationDeadline: time.Unix(cooperationUserUpdateCommand.CooperationDeadline, 0),
		Email:               cooperationUserUpdateCommand.Email,
		EnableStatus:        cooperationUserUpdateCommand.EnableStatus,
		UserCode:            cooperationUserUpdateCommand.UsersCode,
		UserName:            cooperationUserUpdateCommand.UsersName,
		Avatar:              cooperationUserUpdateCommand.Avatar,
		OrgId:               cooperationUserUpdateCommand.Operator.OrgId,
		Phone:               cooperationUserUpdateCommand.Phone,
	})
	return cooperationUserUpdateCommand, err
}

func NewUsersService(options map[string]interface{}) *UsersService {
	newUsersService := &UsersService{}
	return newUsersService
}