user_translator.go 10.3 KB
package translator

import (
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/domain"
	"time"
)

type UserTranslator struct {
}

type UserDetail struct {
	UserId          int64  `json:"userId"`
	UserBaseId      int64  `json:"userBaseId"`
	UserType        int32  `json:"userType"`
	UserCode        string `json:"userCode"`
	EnableStatus    int32  `json:"enableStatus"`
	CooperationInfo struct {
		CooperationCompany  string    `json:"cooperationCompany"`
		CooperationDeadline time.Time `json:"cooperationDeadline"`
	} `json:"cooperationInfo,omitempty"`
	UserInfo struct {
		Phone    string `json:"phone"`
		UserCode string `json:"userCode"`
		Email    string `json:"email"`
		UserName string `json:"userName"`
		Avatar   string `json:"avatar"`
	} `json:"userInfo,omitempty"`
	Company *struct {
		CompanyId        int64     `json:"companyId"`
		CompanyName      string    `json:"companyName"`
		Scale            string    `json:"scale"`
		IndustryCategory string    `json:"industryCategory"`
		RegisteredTime   time.Time `json:"registeredTime"`
		Status           int64     `json:"status"`
	} `json:"company,omitempty"`
	Org *struct {
		OrgId   int64  `json:"orgId"`
		OrgCode string `json:"orgCode"`
		OrgName string `json:"orgName"`
	} `json:"org,omitempty"`
	Department *struct {
		DepartmentId   int64  `json:"departmentId"`
		DepartmentName string `json:"departmentName"`
	} `json:"department,omitempty"`
	UserRole []struct {
		RoleID   int64  `json:"roleId"`
		RoleName string `json:"roleName"`
		Ext      struct {
			OrgName string `json:"orgName"`
		} `json:"ext,omitempty"`
	} `json:"userRole"`
	UserOrg []struct {
		OrgID     int64     `json:"orgId"`
		CreatedAt time.Time `json:"createdAt"`
		UpdatedAt time.Time `json:"updatedAt"`
		DeletedAt time.Time `json:"deletedAt"`
		OrgName   string    `json:"orgName"`
	} `json:"userOrg"`
}

func (translator *UserTranslator) ToRelevantFromRepresentation(user *UserDetail) (*domain.Relevant, error) {
	var orgs []*domain.Org
	for _, org := range user.UserOrg {
		orgs = append(orgs, &domain.Org{
			OrgId:   org.OrgID,
			OrgName: org.OrgName,
			Company: &domain.Company{
				CompanyId:   user.Company.CompanyId,
				CompanyLogo: "",
				CompanyName: user.Company.CompanyName,
			},
		})
	}
	var roles []*domain.Role
	for _, role := range user.UserRole {
		roles = append(roles, &domain.Role{
			RoleId:   int64(role.RoleID),
			RoleName: role.RoleName,
		})
	}
	return &domain.Relevant{
		UserId:     user.UserId,
		UserBaseId: user.UserBaseId,
		Org: &domain.Org{
			OrgId:   user.Org.OrgId,
			OrgName: user.Org.OrgName,
			Company: &domain.Company{
				CompanyId:   user.Company.CompanyId,
				CompanyLogo: "",
				CompanyName: user.Company.CompanyName,
			},
		},
		Orgs: orgs,
		Department: &domain.Department{
			DepartmentId:     user.Department.DepartmentId,
			DepartmentName:   user.Department.DepartmentName,
			DepartmentNumber: "",
			IsOrganization:   false,
		},
		Roles: roles,
		UserInfo: &domain.UserInfo{
			UserAvatar:  user.UserInfo.Avatar,
			UserEmail:   user.UserInfo.Email,
			UserName:    user.UserInfo.UserName,
			UserPhone:   user.UserInfo.Phone,
			UserAccount: "",
		},
		UserType:  user.UserType,
		UserName:  user.UserInfo.UserName,
		UserPhone: user.UserInfo.Phone,
		Status:    user.EnableStatus,
		Company: &domain.Company{
			CompanyId:   user.Company.CompanyId,
			CompanyLogo: "",
			CompanyName: user.Company.CompanyName,
		},
	}, nil
}

func (translator *UserTranslator) ToReferrerFromRepresentation(user *UserDetail) (*domain.Referrer, error) {
	var orgs []*domain.Org
	for _, org := range user.UserOrg {
		orgs = append(orgs, &domain.Org{
			OrgId:   org.OrgID,
			OrgName: org.OrgName,
			Company: &domain.Company{
				CompanyId:   user.Company.CompanyId,
				CompanyLogo: "",
				CompanyName: user.Company.CompanyName,
			},
		})
	}
	var roles []*domain.Role
	for _, role := range user.UserRole {
		roles = append(roles, &domain.Role{
			RoleId:   int64(role.RoleID),
			RoleName: role.RoleName,
		})
	}
	return &domain.Referrer{
		UserId:     user.UserId,
		UserBaseId: user.UserBaseId,
		Roles:      roles,
		Orgs:       orgs,
		Org: &domain.Org{
			OrgId:   user.Org.OrgId,
			OrgName: user.Org.OrgName,
			Company: nil,
		},
		Department: &domain.Department{
			DepartmentId:     user.Department.DepartmentId,
			DepartmentName:   user.Department.DepartmentName,
			DepartmentNumber: "",
			IsOrganization:   false,
		},
		Company: &domain.Company{
			CompanyId:   user.Company.CompanyId,
			CompanyLogo: "",
			CompanyName: user.Company.CompanyName,
		},
		UserInfo:  nil,
		UserType:  user.UserType,
		UserName:  user.UserInfo.UserName,
		UserPhone: user.UserInfo.Phone,
	}, nil
}

func (translator *UserTranslator) ToUndertakerFromRepresentation(user *UserDetail) (*domain.Undertaker, error) {
	var orgs []*domain.Org
	for _, org := range user.UserOrg {
		orgs = append(orgs, &domain.Org{
			OrgId:   org.OrgID,
			OrgName: org.OrgName,
			Company: &domain.Company{
				CompanyId:   user.Company.CompanyId,
				CompanyLogo: "",
				CompanyName: user.Company.CompanyName,
			},
		})
	}
	var roles []*domain.Role
	for _, role := range user.UserRole {
		roles = append(roles, &domain.Role{
			RoleId:   int64(role.RoleID),
			RoleName: role.RoleName,
		})
	}
	return &domain.Undertaker{
		UserId:     user.UserId,
		UserBaseId: user.UserBaseId,
		Org: &domain.Org{
			OrgId:   user.Org.OrgId,
			OrgName: user.Org.OrgName,
			Company: nil,
		},
		Orgs: orgs,
		Department: &domain.Department{
			DepartmentId:     user.Department.DepartmentId,
			DepartmentName:   user.Department.DepartmentName,
			DepartmentNumber: "",
			IsOrganization:   false,
		},
		Roles: roles,
		UserInfo: &domain.UserInfo{
			UserAvatar:  user.UserInfo.Avatar,
			UserEmail:   user.UserInfo.Email,
			UserName:    user.UserInfo.UserName,
			UserPhone:   user.UserInfo.Phone,
			UserAccount: "",
			UserCode:    user.UserCode,
		},
		UserType:  user.UserType,
		UserName:  user.UserInfo.UserName,
		UserPhone: user.UserInfo.Phone,
		Status:    user.EnableStatus,
		Company: &domain.Company{
			CompanyId:   user.Company.CompanyId,
			CompanyLogo: "",
			CompanyName: user.Company.CompanyName,
		},
		ContractAttachment: nil,
	}, nil
}

func (translator *UserTranslator) ToSalesmanFromRepresentation(user *UserDetail) (*domain.Salesman, error) {
	var orgs []*domain.Org
	for _, org := range user.UserOrg {
		orgs = append(orgs, &domain.Org{
			OrgId:   org.OrgID,
			OrgName: org.OrgName,
			Company: &domain.Company{
				CompanyId:   user.Company.CompanyId,
				CompanyLogo: "",
				CompanyName: user.Company.CompanyName,
			},
		})
	}
	var roles []*domain.Role
	for _, role := range user.UserRole {
		roles = append(roles, &domain.Role{
			RoleId:   int64(role.RoleID),
			RoleName: role.RoleName,
		})
	}
	return &domain.Salesman{
		UserId:     user.UserId,
		UserBaseId: user.UserBaseId,
		Roles:      roles,
		Orgs:       orgs,
		Org: &domain.Org{
			OrgId:   user.Org.OrgId,
			OrgName: user.Org.OrgName,
			Company: nil,
		},
		Department: &domain.Department{
			DepartmentId:     user.Department.DepartmentId,
			DepartmentName:   user.Department.DepartmentName,
			DepartmentNumber: "",
			IsOrganization:   false,
		},
		Company: &domain.Company{
			CompanyId:   user.Company.CompanyId,
			CompanyLogo: "",
			CompanyName: user.Company.CompanyName,
		},
		UserInfo: &domain.UserInfo{
			UserAvatar:  user.UserInfo.Avatar,
			UserEmail:   user.UserInfo.Email,
			UserName:    user.UserInfo.UserName,
			UserPhone:   user.UserInfo.Phone,
			UserAccount: "",
		},
		UserType:  user.UserType,
		UserName:  user.UserInfo.UserName,
		UserPhone: user.UserInfo.Phone,
	}, nil
}

func (translator *UserTranslator) ToOperatorFromRepresentation(user *UserDetail) (*domain.User, error) {
	var orgs []*domain.Org
	for _, org := range user.UserOrg {
		orgs = append(orgs, &domain.Org{
			OrgId:   org.OrgID,
			OrgName: org.OrgName,
			Company: &domain.Company{
				CompanyId:   user.Company.CompanyId,
				CompanyLogo: "",
				CompanyName: user.Company.CompanyName,
			},
		})
	}
	var roles []*domain.Role
	for _, role := range user.UserRole {
		roles = append(roles, &domain.Role{
			RoleId:   int64(role.RoleID),
			RoleName: role.RoleName,
		})
	}
	return &domain.User{
		UserId:     user.UserId,
		UserBaseId: user.UserBaseId,
		Org: &domain.Org{
			OrgId:   user.Org.OrgId,
			OrgName: user.Org.OrgName,
			Company: nil,
		},
		Orgs: orgs,
		Department: &domain.Department{
			DepartmentId:     user.Department.DepartmentId,
			DepartmentName:   user.Department.DepartmentName,
			DepartmentNumber: "",
			IsOrganization:   false,
		},
		Roles: roles,
		UserInfo: &domain.UserInfo{
			UserName:   user.UserInfo.UserName,
			UserPhone:  user.UserInfo.Phone,
			UserAvatar: user.UserInfo.Avatar,
		},
		UserType:  user.UserType,
		UserName:  user.UserInfo.UserName,
		UserPhone: user.UserInfo.Phone,
		Status:    user.EnableStatus,
		Company: &domain.Company{
			CompanyId:   user.Company.CompanyId,
			CompanyLogo: "",
			CompanyName: user.Company.CompanyName,
		},
	}, nil
}

func (translator *UserTranslator) ToUserFromRepresentation(user *UserDetail) (*domain.User, error) {
	var orgs []*domain.Org
	for _, org := range user.UserOrg {
		orgs = append(orgs, &domain.Org{
			OrgId:   org.OrgID,
			OrgName: org.OrgName,
			Company: &domain.Company{
				CompanyId:   user.Company.CompanyId,
				CompanyLogo: "",
				CompanyName: user.Company.CompanyName,
			},
		})
	}
	var roles []*domain.Role
	for _, role := range user.UserRole {
		roles = append(roles, &domain.Role{
			RoleId:   int64(role.RoleID),
			RoleName: role.RoleName,
		})
	}
	return &domain.User{
		UserId:     user.UserId,
		UserBaseId: user.UserBaseId,
		Org: &domain.Org{
			OrgId:   user.Org.OrgId,
			OrgName: user.Org.OrgName,
			Company: nil,
		},
		Orgs: orgs,
		Department: &domain.Department{
			DepartmentId:     user.Department.DepartmentId,
			DepartmentName:   user.Department.DepartmentName,
			DepartmentNumber: "",
			IsOrganization:   false,
		},
		Roles: roles,
		UserInfo: &domain.UserInfo{
			UserName:   user.UserInfo.UserName,
			UserPhone:  user.UserInfo.Phone,
			UserAvatar: user.UserInfo.Avatar,
		},
		UserType:  user.UserType,
		UserName:  user.UserInfo.UserName,
		UserPhone: user.UserInfo.Phone,
		Status:    user.EnableStatus,
		Company: &domain.Company{
			CompanyId:   user.Company.CompanyId,
			CompanyLogo: "",
			CompanyName: user.Company.CompanyName,
		},
	}, nil
}

func NewUserTranslator() (*UserTranslator, error) {
	return &UserTranslator{}, nil
}