package analyst

import (
	logger "eta/eta_mini_ht_api/common/component/log"
	"eta/eta_mini_ht_api/common/exception"
	"eta/eta_mini_ht_api/common/utils/page"
	analystService "eta/eta_mini_ht_api/domian/financial_analyst"
	userService "eta/eta_mini_ht_api/domian/user"
	"sync"
)

type Analyst struct {
	Id           int    `json:"id"`
	Name         string `json:"name"`
	HeadImgUrl   string `json:"headImgUrl"`
	Introduction string `json:"introduction"`
	Following    string `json:"following"`
}

func GetAnalystCount() (total int64) {
	return analystService.GetCount()
}

func GetAnalystList(pageInfo page.PageInfo, userId int) (analysts []Analyst, err error) {
	List, err := analystService.GetAnalystList(pageInfo)
	if err != nil {
		logger.Error("查询研究员列表失败:%v", err)
		err = exception.New(exception.GetAnalystListFailed)
	}
	for _, item := range List {
		analysts = append(analysts, convertToAnalyst(item))
	}
	var wg sync.WaitGroup
	for i := 0; i < len(List); i++ {
		wg.Add(1)
		go func(analyst *Analyst) {
			defer wg.Done()
			analyst.Following = userService.GetFollowing(userId, analyst.Id)
		}(&analysts[i])
	}
	wg.Wait()
	return
}

func convertToAnalyst(analyst analystService.FinancialAnalystDTO) Analyst {
	return Analyst{
		Id:           analyst.Id,
		Name:         analyst.Name,
		HeadImgUrl:   analyst.HeadImgUrl,
		Introduction: analyst.Introduction,
		Following:    "",
	}
}
