You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

121 lines
2.8 KiB
Go

package svc
import (
"context"
"git.noahlan.cn/n-admin/n-admin-server/rpc/core/ent"
"git.noahlan.cn/n-admin/n-admin-server/rpc/core/types/core"
"github.com/jinzhu/copier"
"time"
)
type Convert struct {
svcCtx *ServiceContext
}
func NewConvert(svcCtx *ServiceContext) *Convert {
return &Convert{
svcCtx: svcCtx,
}
}
func (c *Convert) ConvertRoleRpc(_ context.Context, v *ent.Role) *core.RoleInfo {
if v == nil {
return nil
}
var tmp core.RoleInfo
_ = copier.Copy(&tmp, v)
tmp.CreatedAt = v.CreatedAt.Format(time.DateTime)
tmp.UpdatedAt = v.UpdatedAt.Format(time.DateTime)
tmp.Status = v.Status.String()
return &tmp
}
func (c *Convert) ConvertUserRpc(ctx context.Context, v *ent.User) *core.UserInfo {
if v == nil {
return nil
}
var tmp core.UserInfo
_ = copier.Copy(&tmp, v)
tmp.Status = v.Status.String()
tmp.CreatedAt = v.CreatedAt.Format(time.DateTime)
tmp.UpdatedAt = v.UpdatedAt.Format(time.DateTime)
// Metadata
if len(v.Edges.Metas) > 0 {
// 导出meta数据
metaMap := make(map[string]string)
for _, item := range v.Edges.Metas {
metaMap[item.Key] = item.Value
}
tmp.Metas = metaMap
}
// Relationship
if len(v.Edges.Roles) > 0 {
tmp.Roles = make([]*core.RoleInfo, len(v.Edges.Roles))
for i, item := range v.Edges.Roles {
tmp.Roles[i] = c.ConvertRoleRpc(ctx, item)
}
}
if v.Edges.LoginRecord != nil {
tmp.LoginRecord = c.ConvertLoginRecordRpc(ctx, v.Edges.LoginRecord)
}
if len(v.Edges.Departments) > 0 {
tmp.Departments = make([]*core.DepartmentInfo, len(v.Edges.Departments))
for i, item := range v.Edges.Departments {
tmp.Departments[i] = c.ConvertDepartmentRpc(ctx, item)
}
}
if len(v.Edges.Token) > 0 {
}
if len(v.Edges.Socials) > 0 {
}
return &tmp
}
func (c *Convert) ConvertLoginRecordRpc(_ context.Context, v *ent.LoginRecord) *core.LoginRecordInfo {
if v == nil {
return nil
}
var tmp core.LoginRecordInfo
_ = copier.Copy(&tmp, v)
tmp.UserId = v.UserID
tmp.LastLoginUa = v.LastLoginUA
return &tmp
}
func (c *Convert) ConvertDepartmentRpc(ctx context.Context, v *ent.Department) *core.DepartmentInfo {
if v == nil {
return nil
}
var tmp core.DepartmentInfo
_ = copier.Copy(&tmp, v)
tmp.LeaderId = v.LeaderID
tmp.ParentId = v.ParentID
// relationship
if v.Edges.Parent != nil {
tmp.Parent = c.ConvertDepartmentRpc(ctx, v.Edges.Parent)
}
if len(v.Edges.Children) > 0 {
tmp.Children = make([]*core.DepartmentInfo, len(v.Edges.Children))
for i, item := range v.Edges.Children {
tmp.Children[i] = c.ConvertDepartmentRpc(ctx, item)
}
}
if len(v.Edges.Users) > 0 {
tmp.Users = make([]*core.UserInfo, len(v.Edges.Users))
for i, item := range v.Edges.Users {
tmp.Users[i] = c.ConvertUserRpc(ctx, item)
}
}
if v.Edges.Leader != nil {
tmp.Leader = c.ConvertUserRpc(ctx, v.Edges.Leader)
}
return &tmp
}