package services

import (
	"errors"
	"eta/eta_api/models/system"
	"strconv"
	"strings"
)

// ResearchGroup 研究方向分组表
type ResearchGroupItem struct {
	ResearchGroupId   int                    `json:"research_group_id" description:"研究方向分组ID"`
	ResearchGroupName string                 `json:"research_group_name" description:"研究方向分组名称"`
	ParentId          int                    `json:"parent_id" description:"父类ID"`
	ChartPermissionId int                    `json:"chart_permission_id" description:"品种权限ID"`
	Sort              int                    `json:"sort" description:"排序"`
	Members           []*ResearchGroupMember `json:"members"`
	Children          []*ResearchGroupItem   `json:"children"`
}

// ResearchGroupMember 研究方向组员信息
type ResearchGroupMember struct {
	AdminId   int    `json:"admin_id"`
	AdminName string `json:"admin_name"`
}

// GetResearchGroupTree 获取研究方向分组及组员列表
func GetResearchGroupTree(include int) (respList []*ResearchGroupItem, err error) {
	respList = make([]*ResearchGroupItem, 0)
	list, e := system.GetResearchGroupList()
	if e != nil {
		err = errors.New("获取研究方向分组失败, Err:" + e.Error())
		return
	}
	listLen := len(list)
	if listLen == 0 {
		return
	}
	// 分类
	firstList := make([]*ResearchGroupItem, 0)
	secondList := make([]*ResearchGroupItem, 0)
	for i := 0; i < listLen; i++ {
		item := new(ResearchGroupItem)
		item.ResearchGroupId = list[i].ResearchGroupId
		item.ResearchGroupName = list[i].ResearchGroupName
		item.ParentId = list[i].ParentId
		item.ChartPermissionId = list[i].ChartPermissionId
		item.Sort = list[i].Sort
		if list[i].ParentId == 0 {
			firstList = append(firstList, item)
		} else {
			secondList = append(secondList, item)
		}
	}
	if len(firstList) == 0 {
		return
	}
	// 匹配成员
	relationList, e := system.GetResearchGroupRelationList(include)
	if e != nil {
		err = errors.New("获取研究方向关系失败, Err:" + e.Error())
		return
	}
	for _, v := range secondList {
		members := make([]*ResearchGroupMember, 0)
		for _, r := range relationList {
			if v.ResearchGroupId == r.ResearchGroupId {
				members = append(members, &ResearchGroupMember{
					AdminId:   r.AdminId,
					AdminName: r.AdminName,
				})
			}
		}
		v.Members = members
	}
	// 匹配子分类
	for _, p := range firstList {
		children := make([]*ResearchGroupItem, 0)
		for _, child := range secondList {
			if p.ResearchGroupId == child.ParentId {
				children = append(children, child)
			}
		}
		p.Children = children
	}
	respList = firstList
	return
}

// UpdateAdminResearchGroup 更新研究员研究方向分组
func UpdateAdminResearchGroup(adminId int, groupIds string) (err error) {
	if adminId == 0 {
		return
	}
	items := make([]*system.ResearchGroupRelation, 0)
	groupIdArr := strings.Split(groupIds, ",")
	if groupIds != "" {
		for _, v := range groupIdArr {
			groupId, e := strconv.Atoi(v)
			if e != nil {
				err = errors.New("分组ID有误")
				return
			}
			items = append(items, &system.ResearchGroupRelation{
				ResearchGroupId: groupId,
				AdminId:         adminId,
			})
		}
	}
	if e := system.UpdateAdminResearchGroup(adminId, items); e != nil {
		err = errors.New("更新研究方向分组失败, Err:" + e.Error())
	}
	return
}
