package author

import (
	"encoding/json"
	logger "eta/eta_mini_ht_api/common/component/log"
	"eta/eta_mini_ht_api/common/contants"
	"eta/eta_mini_ht_api/domian/financial_analyst"
	"eta/eta_mini_ht_api/models/eta"
	"eta/eta_mini_ht_api/task/base"
)

var (
	taskName base.TaskType = "ETAAuthorSyncTask"
	cron                   = "0/60 * * * * *"
)

// Execute Task ETA取研报的数据
func (au *AuthorTask) Execute(taskDetail *base.TaskDetail) error {
	logger.Info(contants.TaskFormat, "同步ETA作者开始")
	authors, err := eta.GetReportAuthorList()
	if err != nil {
		logger.Error("获取ETA作者列表失败：%v", err)
		return err
	}
	if len(authors) > 0 {
		var list []byte
		list, err = json.Marshal(authors)
		if err == nil {
			taskDetail.Content = string(list)
		}

		var financialAnalystList []financial_analyst.FinancialAnalystDTO
		for _, author := range authors {
			financialAnalystList = append(financialAnalystList, convert(author))
		}
		err = financial_analyst.SyncAnalyst(financialAnalystList)
		if err != nil {
			logger.Error("同步ETA研报列表失败：%v", err)
			return err
		}
	}
	logger.Info(contants.TaskFormat, "同步ETA作者结束")
	return nil
}

type AuthorTask struct {
}

func convert(author eta.ReportAuthor) financial_analyst.FinancialAnalystDTO {
	return financial_analyst.FinancialAnalystDTO{
		Deleted: author.IsDelete,
		ETAId:   author.Id,
		Name:    author.ReportAuthor,
		Status:  author.Enable,
	}
}
func init() {
	authorTask := base.NewTask(taskName, cron, new(AuthorTask), base.DEV)
	base.RegisterTask(&authorTask)
}
