审查视图

pkg/application/evaluation_project/adapter/project_adapter.go 2.1 KB
郑周 authored
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
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:"被评估人"`
16
	Principal     *UserAdapter   `json:"principal" comment:"任务负责人"`
郑周 authored
17 18 19
	PmpList       []*UserAdapter `json:"pmpList" comment:"项目管理员"`
}
20 21 22 23 24 25 26 27 28 29
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
		}
	}
30
	adapter.RecipientList = make([]*UserAdapter, 0)
郑周 authored
31
	for i := range recipients {
32
		ua := &UserAdapter{
郑周 authored
33 34
			Id:   recipients[i].Id,
			Name: recipients[i].Name,
35 36 37 38 39 40
		}
		adapter.RecipientList = append(adapter.RecipientList, ua)

		if ua.Id == principalIdInt {
			adapter.Principal = ua
		}
郑周 authored
41 42 43 44
	}
}

func (adapter *EvaluationProjectAdapter) TransformPmpAdapter(pms []*domain.User) {
45
	adapter.PmpList = make([]*UserAdapter, 0)
郑周 authored
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
	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
66 67
		epa.PmpList = make([]*UserAdapter, 0)
郑周 authored
68 69 70 71 72 73 74 75 76 77 78 79 80 81
		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
}