package controllers

import (
	"hongze/hongze_api/models"
	"hongze/hongze_api/utils"
)

// @Title 研报列表
// @Description 研报列表接口
// @Param   ClassifyId   query   int  true       "分类id"
// @Success 200 {object} models.ResearchReportListResp
// @router /pc/research/report/list [get]
func (this *ReportCommonController) ResearchReportList() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()

	classifyId, _ := this.GetInt("ClassifyId")
	if classifyId <= 0 {
		br.Msg = "参数错误"
		br.ErrMsg = "参数错误,分类id小于等于0"
		return
	}
	list, err := models.GetResearchReportList(classifyId)
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取失败,Err:" + err.Error()
		return
	}
	resp := new(models.ResearchReportListResp)
	resp.List = list
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = resp
}

// @Title 研报PC端搜索接口
// @Param   KeyWord   query   string  true       "搜索关键词"
// @Success 200 {object} models.PcSearchReportListResp
// @router /pc/search/report/list [get]
func (this *ReportCommonController) PcSearchReportList() {
	// @Description 研报PC端搜索接口
	// @Param   PageSize   query   int  true       "每页数据条数"
	// @Param   CurrentIndex   query   int  true       "当前页页码,从1开始"
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	keyWord := this.GetString("KeyWord")

	//pageSize, _ := this.GetInt("PageSize")
	//currentIndex, _ := this.GetInt("CurrentIndex")
	//classifyId, _ := this.GetInt("ClassifyId")
	//if classifyId <= 0 {
	//	br.Msg = "参数错误"
	//	br.ErrMsg = "参数错误,分类id小于等于0"
	//	return
	//}
	//
	//page := paging.GetPaging(currentIndex, pageSize, 0)
	resp := new(models.PcSearchReportListResp)
	var condition string
	//var pars []interface{}

	if keyWord != "" {
		condition += ` AND (a.title LIKE '%` + keyWord + `%' OR a.abstract LIKE '%` + keyWord + `%') `
	} else {
		list := make([]*models.ReportList, 0)
		resp.List = list
		//resp.Paging = page
		br.Ret = 200
		br.Success = true
		br.Msg = "获取成功"
		br.Data = resp
		return
	}

	//var startSize int
	//if pageSize <= 0 {
	//	pageSize = utils.PageSize20
	//}
	//if currentIndex <= 0 {
	//	currentIndex = 1
	//}
	//startSize = utils.StartIndex(currentIndex, pageSize)

	//total, err := models.GetSearchReportListCount(condition)
	//if err != nil {
	//	br.Msg = "获取数据失败"
	//	br.ErrMsg = "获取数据失败,Err:" + err.Error()
	//	return
	//}
	var uid int
	if this.Token != "" && this.User != nil {
		uid = this.User.UserId
	}
	var hasPermission bool
	reportType := "rddp"
	reportPermissionList, err := models.GetReportVarietyListByUserIdExt(uid, reportType) //获取客户拥有权限的报告id
	if err != nil {
		if err.Error() != utils.ErrNoRow() {
			br.Msg = "获取报告详情失败"
			br.ErrMsg = "获取客户信息失败,Err:" + err.Error()
			return
		} else {
			hasPermission = false
		}
	}
	if len(reportPermissionList) == 0 {
		hasPermission = false
	}
	permissionMap := make(map[int]int)
	for _, v := range reportPermissionList {
		if _, ok := permissionMap[v.ReportChapterTypeId]; !ok {
			permissionMap[v.ReportChapterTypeId] = v.ReportChapterTypeId
		}
	}

	list, err := models.GetSearchReportList(condition)
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取失败,Err:" + err.Error()
		return
	}
	listLen := len(list)
	for i := 0; i < listLen; i++ {
		item := list[i]
		if this.Token != "" {
			count, err := models.GetReportPermission(uid, item.ClassifyNameSecond)
			if err != nil && err.Error()!=utils.ErrNoRow() {
				br.Msg = "获取失败"
				br.ErrMsg = "判断报告是否拥有权限失败,Err:" + err.Error()
				return
			}
			if count > 0 {
				list[i].HasPermission = 1
			} else {
				list[i].VideoUrl = ""
				list[i].VideoName = ""
				list[i].VideoPlaySeconds = ""
				list[i].VideoSize = ""
			}
		} else {
			list[i].VideoUrl = ""
			list[i].VideoName = ""
			list[i].VideoPlaySeconds = ""
			list[i].VideoSize = ""
		}

		if hasPermission == false {
			list[i].VideoUrl = ""
			list[i].VideoName = ""
			list[i].VideoPlaySeconds = ""
			list[i].VideoSize = ""
		}
		if item.ClassifyNameFirst == "权益研报" {
			list[i].TitleType = "图说逻辑"
		} else {
			list[i].TitleType = "FICC"
		}
	}
	//page = paging.GetPaging(currentIndex, pageSize, total)
	resp.List = list
	//resp.Paging = page
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = resp
}