package user

import (
	"eta/eta_mini_ht_api/common/exception"
	"eta/eta_mini_ht_api/common/utils/page"
	"eta/eta_mini_ht_api/controllers"
	"eta/eta_mini_ht_api/service/analyst"
	"eta/eta_mini_ht_api/service/media"
	"eta/eta_mini_ht_api/service/report"
	"eta/eta_mini_ht_api/service/user"
	"fmt"
)

type AnalystController struct {
	controllers.ListController
}

// AnalystList  研究员列表
// @Summary 研究员列表
// @Description 研究员列表
// @Success 200 {object} controllers.BaseResponse
// @router /list [get]
func (an *AnalystController) AnalystList() {
	controllers.Wrap(&an.BaseController, func() (result *controllers.WrapData, err error) {
		result = an.InitWrapData("获取研究员列表失败")
		var userInfo user.User
		userInfo = an.Data["user"].(user.User)
		pageRes := page.Page{
			Current:  an.PageInfo.Current,
			PageSize: an.PageInfo.PageSize,
		}
		if an.PageInfo.LatestId == 0 {
			pageRes.Total = analyst.GetAnalystCount()
			pageRes.LatestId = pageRes.Total
			an.PageInfo.LatestId = pageRes.LatestId
			an.PageInfo.Total = pageRes.Total
		} else {
			pageRes.LatestId = an.PageInfo.LatestId
			pageRes.Total = an.PageInfo.Total
		}
		pageRes.Total = analyst.GetAnalystCount()
		an.PageInfo.Total = pageRes.Total
		pageRes.TotalPage = page.TotalPages(pageRes.Total, pageRes.PageSize)
		list, err := analyst.GetAnalystList(an.PageInfo, userInfo.Id)
		if err != nil {
			an.FailedResult("获取研究员列表失败", result)
			return
		}
		analystList := new(page.PageResult)
		analystList.Data = list
		analystList.Page = pageRes
		an.SuccessResult("获取研究员列表成功", analystList, result)
		return
	})
}

// AnalystDetail  研究员详情
// @Summary 研究员详情
// @Description 研究员详情
// @Success 200 {object} controllers.BaseResponse
// @router /analystDetail [get]
func (an *AnalystController) AnalystDetail(analystId int) {
	controllers.Wrap(&an.BaseController, func() (result *controllers.WrapData, err error) {
		result = an.InitWrapData("获取研究员详情失败")
		fmt.Println(analystId)
		userInfo := an.Data["user"].(user.User)
		detail, err := user.GetAnalystDetail(userInfo.Id, analystId)
		if err != nil {
			an.FailedResult("获取研究员详情失败", result)
			return
		}
		an.SuccessResult("获取研究员详情成功", detail, result)
		return
	})
}

// AnalystReportList @Title 获取研究员报告列表
// @Description 获取研究员报告列表
// @Success 200 {object}
// @router /reportList [get]
func (an *AnalystController) AnalystReportList(analystName string) {
	controllers.Wrap(&an.BaseController, func() (result *controllers.WrapData, err error) {
		result = an.InitWrapData("分页获取研究员报告列表失败")
		pageRes := page.Page{
			Current:  an.PageInfo.Current,
			PageSize: an.PageInfo.PageSize,
		}
		if an.PageInfo.LatestId == 0 {
			pageRes.Total, pageRes.LatestId = report.GetTotalPageCountByAnalyst(analystName)
			an.PageInfo.LatestId = pageRes.LatestId
		} else {
			pageRes.LatestId = an.PageInfo.LatestId
			pageRes.Total = an.PageInfo.Total
		}
		pageRes.TotalPage = page.TotalPages(pageRes.Total, pageRes.PageSize)
		list, err := report.GetReportPageByAnalyst(an.PageInfo, analystName)
		if err != nil {
			an.FailedResult("分页获取研究员报告列表失败", result)
			return
		}
		reports := new(page.PageResult)
		reports.Data = list
		reports.Page = pageRes
		an.SuccessResult("分页获取研究员报告列表成功", reports, result)
		return
	})
}

// MediaList 获取研究员媒体列表
// @Description 获取研究员媒体列表
// @Success 200 {object}
// @router /mediaList [get]
func (an *AnalystController) MediaList(mediaType string, analystId int) {
	controllers.Wrap(&an.BaseController, func() (result *controllers.WrapData, err error) {
		result = an.InitWrapData("分页查询研究员媒体列表失败")
		if mediaType == "" || !an.CheckMediaType(mediaType) {
			err = exception.New(exception.MediaTypeError)
			an.FailedResult("分页查询研究员媒体列表失败", result)
			return
		}
		pageRes := page.Page{
			Current:  an.PageInfo.Current,
			PageSize: an.PageInfo.PageSize,
		}
		if an.PageInfo.LatestId == 0 {
			pageRes.Total, pageRes.LatestId = media.GetTotalPageCountByAnalystId(mediaType, analystId)
			an.PageInfo.LatestId = pageRes.LatestId
			an.PageInfo.Total = pageRes.Total
		} else {
			pageRes.LatestId = an.PageInfo.LatestId
			pageRes.Total = an.PageInfo.Total
		}
		pageRes.TotalPage = page.TotalPages(pageRes.Total, pageRes.PageSize)
		list, err := media.GetMediaPageByAnalystId(mediaType, an.PageInfo, analystId)
		if err != nil {
			an.FailedResult("分页查询研究员媒体列表失败", result)
			return
		}
		mediaList := new(page.PageResult)
		mediaList.Data = list
		mediaList.Page = pageRes
		an.SuccessResult("分页查询研究员媒体列表成功", mediaList, result)
		return
	})
}
