package financial_analyst

import (
	logger "eta/eta_mini_ht_api/common/component/log"
	"eta/eta_mini_ht_api/common/utils/page"
	financialAnalystDao "eta/eta_mini_ht_api/models/financial_analyst"
)

type FinancialAnalystDTO struct {
	Id           int
	ETAId        int
	Name         string
	HeadImgUrl   string
	Introduction string
	Status       bool
	Deleted      bool
}

func GetAnalystList(pageInfo page.PageInfo) (analystsDTO []FinancialAnalystDTO, err error) {
	offset := page.StartIndex(pageInfo.Current, pageInfo.PageSize)
	anaylsts, err := financialAnalystDao.GetAnalystList(pageInfo.LatestId, offset, pageInfo.PageSize)
	for _, analyst := range anaylsts {
		analystsDTO = append(analystsDTO, convertToBaseDTO(analyst))
	}
	return
}

func GetAnalystById(id int) (financialAnalyst FinancialAnalystDTO, err error) {
	analyst, err := financialAnalystDao.GetAnalystById(id)
	if err != nil {
		logger.Error("获取研究员失败: %v", err)
		return
	}
	financialAnalyst = convertToBaseDTO(analyst)
	return
}
func GetCount() (total int64) {
	return financialAnalystDao.GetCount()
}
func GetAnalystByName(name string) (financialAnalyst FinancialAnalystDTO, err error) {
	analyst, err := financialAnalystDao.GetAnalystByName(name)
	if err != nil {
		logger.Error("获取研究员失败: %v", err)
		return
	}
	financialAnalyst = convertToBaseDTO(analyst)
	return
}
func SyncAnalyst(list []FinancialAnalystDTO) (err error) {
	var financialAnalystList []financialAnalystDao.CrmFinancialAnalyst
	for _, dto := range list {
		financialAnalyst := convert(dto)
		financialAnalystList = append(financialAnalystList, financialAnalyst)
	}
	return financialAnalystDao.BatchInsertOrUpdate(financialAnalystList)
}

func convert(dto FinancialAnalystDTO) (financialAnalyst financialAnalystDao.CrmFinancialAnalyst) {
	var status financialAnalystDao.AnalystStatus
	if dto.Status {
		status = financialAnalystDao.AnalystStatusEnabled
	} else {
		status = financialAnalystDao.AnalystStatusDisabled
	}
	return financialAnalystDao.CrmFinancialAnalyst{
		ETAId:   dto.ETAId,
		Name:    dto.Name,
		Status:  status,
		Deleted: dto.Deleted,
	}
}
func convertToBaseDTO(financialAnalyst financialAnalystDao.CrmFinancialAnalyst) (dto FinancialAnalystDTO) {
	return FinancialAnalystDTO{
		Id:           financialAnalyst.Id,
		ETAId:        financialAnalyst.ETAId,
		HeadImgUrl:   financialAnalyst.HeadImgURL,
		Name:         financialAnalyst.Name,
		Introduction: financialAnalyst.Introduction,
	}
}
