project_adapter.go 1.7 KB
package adapter

import (
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
	"strconv"
)

type UserAdapter struct {
	Id   int64  `json:"id,string" comment:"用户Id"`
	Name string `json:"name" comment:"用户名称"`
}

type EvaluationProjectAdapter struct {
	*domain.EvaluationProject
	RecipientList []*UserAdapter `json:"recipientList" comment:"被评估人"`
	PmpList       []*UserAdapter `json:"pmpList" comment:"项目管理员"`
}

func (adapter *EvaluationProjectAdapter) TransformRecipientAdapter(recipients []*domain.User) {
	adapter.RecipientList = make([]*UserAdapter, 0)
	for i := range recipients {
		adapter.RecipientList = append(adapter.RecipientList, &UserAdapter{
			Id:   recipients[i].Id,
			Name: recipients[i].Name,
		})
	}
}

func (adapter *EvaluationProjectAdapter) TransformPmpAdapter(pms []*domain.User) {
	adapter.PmpList = make([]*UserAdapter, 0)
	for i := range pms {
		adapter.PmpList = append(adapter.PmpList, &UserAdapter{
			Id:   pms[i].Id,
			Name: pms[i].Name,
		})
	}
}

func TransformProjectListAdapter(projects []*domain.EvaluationProject, users []*domain.User) []*EvaluationProjectAdapter {
	pmpUserMap := map[int64]*domain.User{}
	for i := range users {
		pmpUserMap[users[i].Id] = users[i]
	}

	projectAdapters := make([]*EvaluationProjectAdapter, 0)
	for i := range projects {
		project := projects[i]

		epa := &EvaluationProjectAdapter{}
		epa.EvaluationProject = project
		epa.PmpList = make([]*UserAdapter, 0)

		projectAdapters = append(projectAdapters, epa)

		for j := range project.PmpIds {
			userId, _ := strconv.ParseInt(project.PmpIds[j], 10, 64)
			if v, ok := pmpUserMap[userId]; ok {
				epa.PmpList = append(epa.PmpList, &UserAdapter{
					Id:   v.Id,
					Name: v.Name,
				})
			}
		}
	}
	return projectAdapters
}