project_adapter.go 2.1 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:"被评估人"`
	Principal     *UserAdapter   `json:"principal" comment:"任务负责人"`
	PmpList       []*UserAdapter `json:"pmpList" comment:"项目管理员"`
}

func (adapter *EvaluationProjectAdapter) TransformRecipientAdapter(recipients []*domain.User, principalId string) {
	// 任务负责人ID
	var principalIdInt = int64(0)
	if len(principalId) > 0 {
		i, err := strconv.ParseInt(principalId, 10, 64)
		if err == nil {
			principalIdInt = i
		}
	}

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

		if ua.Id == principalIdInt {
			adapter.Principal = ua
		}
	}
}

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
}