package report

import (
	"github.com/gin-gonic/gin"
	"hongze/hongze_yb/controller/response"
	"hongze/hongze_yb/models/request"
	"hongze/hongze_yb/models/tables/user_report_chapter_set"
	"hongze/hongze_yb/services"
	"hongze/hongze_yb/services/report"
	userService "hongze/hongze_yb/services/user"
	"hongze/hongze_yb/utils"
	"strconv"
)

// Detail 报告详情
func Detail(c *gin.Context) {
	reqReportId := c.DefaultQuery("report_id", "")
	if reqReportId == "" {
		response.Fail("请输入报告ID", c)
		return
	}
	reportId, err := strconv.Atoi(reqReportId)
	if err != nil {
		response.Fail("报告ID格式有误", c)
		return
	}
	userinfo := userService.GetInfoByClaims(c)

	reportDetail, err := report.GetReportDetail(userinfo, reportId)
	if err != nil {
		if err.Error() == "报告未发布" {
			response.NotReleaseMsg("您查看的报告不存在，请试试其他报告吧", err.Error(), c)
			return
		} else {
			response.FailMsg(err.Error(), err.Error(), c)
			return
		}
	}

	response.OkData("查询成功", reportDetail, c)
	return
}

// ChapterDetail 章节详情
func ChapterDetail(c *gin.Context) {
	reqReportChapterId := c.DefaultQuery("report_chapter_id", "")
	if reqReportChapterId == "" {
		response.Fail("请输入章节ID", c)
		return
	}
	reportChapterId, err := strconv.Atoi(reqReportChapterId)
	if err != nil {
		response.Fail("章节ID格式有误", c)
		return
	}
	userinfo := userService.GetInfoByClaims(c)

	chapterDetail, err := report.GetChapterDetail(userinfo, reportChapterId)
	if err != nil {
		response.FailMsg("查看章节详情出错", err.Error(), c)
		return
	}

	response.OkData("查询成功", chapterDetail, c)
	return
}

// List 研报列表
func List(c *gin.Context) {
	var req request.ReportListReq
	if err := c.Bind(&req); err != nil {
		response.Fail("参数有误", c)
		return
	}
	if req.ClassifyIdFirst <= 0 {
		response.Fail("分类ID有误", c)
		return
	}
	if req.CurrentIndex == 0 {
		req.CurrentIndex = 1
	}
	if req.PageSize == 0 {
		req.PageSize = utils.PageSize20
	}

	userInfo := userService.GetInfoByClaims(c)

	list, err := report.GetReportList(userInfo, req.KeyWord, req.ClassifyIdFirst, req.ClassifyIdSecond, req.CurrentIndex, req.PageSize, req.ClassifyMenuId)
	if err != nil {
		response.FailMsg("查看研报列表出错", err.Error(), c)
		return
	}
	response.OkData("查询成功", list, c)
	return
}

// CollectReportList 首页研报汇总
func CollectReportList(c *gin.Context) {
	reqChartPermissionId := c.DefaultQuery("chart_permission_id", "")
	reqPageIndex := c.DefaultQuery("current_index", "1")
	reqPageSize := c.DefaultQuery("page_size", strconv.Itoa(utils.PageSize20))

	pageIndex, err := strconv.Atoi(reqPageIndex)
	if err != nil {
		response.Fail("请输入正确的条数限制", c)
		return
	}

	pageSize, err := strconv.Atoi(reqPageSize)
	if err != nil {
		response.Fail("请输入正确的页码", c)
		return
	}

	if reqChartPermissionId == "" {
		response.Fail("请输入权限ID", c)
		return
	}
	chartPermissionId, err := strconv.Atoi(reqChartPermissionId)
	if err != nil {
		response.Fail("权限ID格式错误", c)
		return
	}
	userinfo := userService.GetInfoByClaims(c)

	list, err := report.GetCollectReportList(userinfo, chartPermissionId, pageIndex, pageSize)
	if err != nil {
		response.FailMsg("汇总报告查询出错", err.Error(), c)
		return
	}
	response.OkData("查询成功", list, c)
	return
}

// Search 首页研报搜索
func Search(c *gin.Context) {
	keyWord := c.DefaultQuery("key_word", "")
	reqPageIndex := c.DefaultQuery("current_index", "1")
	reqPageSize := c.DefaultQuery("page_size", strconv.Itoa(utils.PageSize20))

	pageIndex, err := strconv.Atoi(reqPageIndex)
	if err != nil {
		response.Fail("请输入正确的条数限制", c)
		return
	}

	pageSize, err := strconv.Atoi(reqPageSize)
	if err != nil {
		response.Fail("请输入正确的页码", c)
		return
	}

	if keyWord == "" {
		response.Fail("请输入搜索词", c)
		return
	}
	userinfo := userService.GetInfoByClaims(c)
	data, err := report.SearchReport(userinfo, keyWord, pageIndex, pageSize)
	if err != nil {
		response.FailMsg("搜索报告出错", err.Error(), c)
		return
	}
	response.OkData("查询成功", data, c)
	return
}

// TickerData 报告详情获取指标数据
func TickerData(c *gin.Context) {
	reqReportChapterId := c.DefaultQuery("report_chapter_id", "")
	if reqReportChapterId == "" {
		response.Fail("请输入章节ID", c)
		return
	}
	reportChapterId, err := strconv.Atoi(reqReportChapterId)
	if err != nil {
		response.Fail("章节ID格式有误", c)
		return
	}
	userinfo := userService.GetInfoByClaims(c)

	chapterDetail, err := report.GetTickerData(userinfo, reportChapterId)
	if err != nil {
		response.Fail(err.Error(), c)
		return
	}
	response.OkData("查询成功", chapterDetail, c)
	return
}

// RddpShareImg 获取报告分享图
func RddpShareImg(c *gin.Context) {
	var req request.RddpReportShareImgReq
	if c.ShouldBind(&req) != nil {
		response.Fail("参数异常", c)
		return
	}
	defaultImg := utils.DEFAULT_REPORT_SHARE_BG_IMG
	if req.Source == "" {
		response.OkData("获取成功", defaultImg, c)
		return
	}
	imgUrl, err := services.GetDynamicShareImg(req.Source, req.Pars, req.ReportId, req.ReportChapterId, req.Version)
	if err != nil {
		response.FailData("获取分享海报失败", "获取分享海报失败, Err: "+err.Error(), c)
		return
	}
	if imgUrl == "" {
		imgUrl = defaultImg
	}
	response.OkData("获取成功", imgUrl, c)
}

// ReportPptImgList 报告中的ppt图片列表
func ReportPptImgList(c *gin.Context) {
	reqReportId := c.DefaultQuery("report_id", "")
	if reqReportId == "" {
		response.Fail("请输入报告ID", c)
		return
	}
	reportId, err := strconv.Atoi(reqReportId)
	if err != nil {
		response.Fail("报告ID格式有误", c)
		return
	}

	reqReportChapterId := c.DefaultQuery("report_chapter_id", "0")
	reportChapterId, err := strconv.Atoi(reqReportChapterId)
	if err != nil {
		response.Fail("章节ID格式有误", c)
		return
	}

	pptImgList, err := report.GetReportPptImgListByReportIdAndReportChapterId(reportId, reportChapterId)
	if err != nil {
		response.FailMsg("获取ppt图片成功", err.Error(), c)
		return
	}
	response.OkData("获取成功", pptImgList, c)
	return
}

// VarietyReportList 品种类型研报列表
func VarietyReportList(c *gin.Context) {
	reqClassifyId := c.DefaultQuery("classify_id", "")
	reqChartPermissionId := c.DefaultQuery("chart_permission_id", "")
	reqPageIndex := c.DefaultQuery("current_index", "1")
	reqPageSize := c.DefaultQuery("page_size", strconv.Itoa(utils.PageSize20))

	pageIndex, err := strconv.Atoi(reqPageIndex)
	if err != nil {
		response.Fail("请输入正确的条数限制", c)
		return
	}

	pageSize, err := strconv.Atoi(reqPageSize)
	if err != nil {
		response.Fail("请输入正确的页码", c)
		return
	}

	if reqChartPermissionId == "" {
		response.Fail("请输入权限ID", c)
		return
	}
	chartPermissionId, err := strconv.Atoi(reqChartPermissionId)
	if err != nil {
		response.Fail("权限ID格式错误", c)
		return
	}

	classifyId, err := strconv.Atoi(reqClassifyId)
	if err != nil {
		response.Fail("种类ID格式错误", c)
		return
	}

	userinfo := userService.GetInfoByClaims(c)

	list, err := report.GetVarietyReportList(userinfo, classifyId, chartPermissionId, pageIndex, pageSize)
	if err != nil {
		response.FailMsg("查看研报列表出错", err.Error(), c)
		return
	}

	response.OkData("查询成功", list, c)
	return
}

// RddpShareImg 获取报告分享图
func ReportChapterSet(c *gin.Context) {
	var req request.ReportChapterSetReq
	if c.ShouldBind(&req) != nil {
		response.Fail("参数异常", c)
		return
	}

	userinfo := userService.GetInfoByClaims(c)
	err := user_report_chapter_set.ModifyReportChapterSet(req.IsClose, userinfo.UserID, req.TypeId)
	if err != nil {
		response.Fail("保存失败,err:"+err.Error(), c)
		return
	}
	response.Ok("保存成功", c)
}
