package report

import (
	"hongze/hongze_yb/models/tables/company_report_permission"
	"hongze/hongze_yb/models/tables/research_report"
	"hongze/hongze_yb/utils"
)

func GetResearchReportInfo(researchReportId, userId uint64) (result ResearchReportInfo, hasPermission bool, err error) {
	//获取报告详情
	reportInfo, err := research_report.GetByResearchReportId(researchReportId)
	if err != nil {
		return
	}
	reportType := reportInfo.Type
	//这些个报告需要做权限校验

	if utils.InArray(reportInfo.Type, []string{"month", "two_week", "other"}) {
		list, tmpErr := company_report_permission.GetReportVarietyList(userId, reportType)
		if tmpErr != nil {
			err = tmpErr
			return
		}
		for _, v := range list {
			if reportInfo.ResearchReportID == v.ReportChapterTypeId {
				hasPermission = true
				break
			}
		}
		if !hasPermission {
			//permissionName, tmpErr := company_report_permission.GetPermissionNameByReportId(reportInfo.ResearchReportID, reportType)
			//if tmpErr != nil {
			//	err = tmpErr
			//	return
			//}
			return
		}
	} else {
		hasPermission = true
	}

	researchReportTypeList := make([]*company_report_permission.ResearchReportTypeList, 0)
	tmpResearchReportTypeList, err := company_report_permission.GetResearchReportType(reportInfo.ResearchReportID, userId, reportInfo.Type)
	if err != nil {
		return
	}
	reportDate := reportInfo.ResearchReportDate
	for _, v := range tmpResearchReportTypeList {
		if reportDate.Before(v.PauseStartTime) || reportDate.After(v.PauseEndTime) {
			researchReportTypeList = append(researchReportTypeList, v)
		}
	}

	result = ResearchReportInfo{
		ResearchReportInfo:     reportInfo,
		ResearchReportTypeList: researchReportTypeList,
		HasMenu:                1,
	}
	if len(researchReportTypeList) <= 0 {

	} else if len(researchReportTypeList) == 1 {
		//只有一个章节，即没有目录的时候，需要直接返回章节详情
		result.HasMenu = 0
		researchReportTypeContent, tmpErr := research_report.GetResearchReportTypeContent(researchReportTypeList[0].ResearchReportTypeId)
		if tmpErr != nil {
			return
		}
		result.ResearchReportTypeContentList = researchReportTypeContent
	}
	return
}

type ResearchReportInfo struct {
	ResearchReportInfo            *research_report.ResearchReport                     `json:"research_report_info"`
	ResearchReportTypeList        []*company_report_permission.ResearchReportTypeList `json:"research_report_type_list"`
	HasMenu                       int                                                 `json:"has_menu"`
	ResearchReportTypeContentList []*research_report.ResearchReportTypeContent        `description:"报告详情"`
}
