allied-creation-user.go 2.4 KB
package domainService

import (
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/constant"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/allied-lib/gateway/allied_creation_user"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/allied-lib/models"
)

type UserService struct {
	internalUserService *allied_creation_user.HttpLibAlliedCreationUser
}

func (svr *UserService) User(id int) (*domain.User, error) {
	rsp, err := svr.internalUserService.User(id)
	if err != nil {
		return nil, err
	}
	return svr.ToUser(rsp), nil
}

func (svr *UserService) UserByCode(companyId, orgId int, userCode string) (*domain.User, error) {
	rsp, err := svr.internalUserService.UserByCode(companyId, orgId, userCode)
	if err != nil {
		return nil, err
	}
	return svr.ToUser(rsp), nil
}

func (svr *UserService) UserByICCode(companyId, orgId int, userCode string) (*domain.User, error) {
	rsp, err := svr.internalUserService.UserByICCode(companyId, orgId, userCode)
	if err != nil {
		return nil, err
	}
	return svr.ToUser(rsp), nil
}

func (svr *UserService) Users(id []int) ([]*domain.User, error) {
	rsp, err := svr.internalUserService.Users(id)
	if err != nil {
		return nil, err
	}
	result := make([]*domain.User, 0)
	for i := range rsp {
		result = append(result, svr.ToUser(rsp[i]))
	}
	return result, nil
}

func (svr *UserService) Organization(id int) (*domain.Org, error) {
	rsp, err := svr.internalUserService.Organization(id)
	if err != nil {
		return nil, err
	}
	return svr.ToOrg(rsp), nil
}

func (svr *UserService) ToUser(from *models.User) *domain.User {
	user := &domain.User{
		UserId:       from.UserId,
		UserName:     from.UserInfo.UserName,
		EmployeeType: from.EmployeeType,
		IcCardNumber: from.IcCardNumber,
		Avatar:       from.UserInfo.Avatar,
		Phone:        from.UserInfo.Phone,
	}
	if user.EmployeeType == 0 {
		user.EmployeeType = 1 //默认正式员工
	}
	return user
}

func (svr *UserService) ToOrg(from *models.Organization) *domain.Org {
	return &domain.Org{
		OrgId:   from.OrgId,
		OrgName: from.OrgName,
	}
}

//func(svr *UserService) ToOrg(from *models.Organization)*domain.Org{
//
//}

func NewUserService() *UserService {
	return &UserService{
		internalUserService: allied_creation_user.NewHttpLibAlliedCreationUser(constant.ALLIED_CREATION_USER_HOST),
	}
}