package company_report_permission

import (
	"fmt"
	"github.com/beego/beego/v2/client/orm"
	"strings"
	"time"
)

type ReportChapterTypeIdList struct {
	ReportChapterTypeId uint64
}

type ResearchReportTypeInfo struct {
	ResearchReportTypeTitle string `json:"research_report_type_title" description:"研究报告标题"`
	BannerUrl               string `json:"banner_url" description:"banner url"`
	ReportChapterTypeName   string `json:"report_chapter_type_name" description:"章节名称"`
	ResearchReportID        uint64 `orm:"column(research_report_id)" json:"research_report_id" description:"报告id"`
	ResearchReportTypeID    uint64 `orm:"column(research_report_type_id)" json:"research_report_type_id" description:"研究报告id"`
	TypeID                  int    `orm:"column(type_id)" json:"type_id" description:"分类id"`
	ReportChapterTypeId     uint64 `json:"report_chapter_type_id" description:"章节名称"`
	Type                    string ` json:"type";description:"day 晨报  week 周报 two_week双周报 month 月报"`
}

//type ResearchReportTypeList struct {
//	ResearchReportTypeId    uint64    ` description:"章节ID" json:"research_report_type_id" `
//	ResearchReportId        uint64    ` description:"研究报告id"  json:"research_report_id" `
//	ResearchReportTypeTitle string    `description:"研究报告标题"  json:"research_report_type_title" `
//	TypeId                  int       `description:"分类id"  json:"type_id" `
//	Edit                    int8      `description:"是否编辑过" json:"edit" `
//	Trend                   string    `description:"趋势观点" json:"trend" `
//	ReportChapterTypeKey    string    `description:"章节key" json:"report_chapter_typeKey" `
//	ReportChapterTypeThumb  string    `description:"H5展示的图片"  json:"report_chapter_type_thumb" `
//	BannerUrl               string    `description:"banner显示图片"  json:"banner_url" `
//	ReportChapterTypeName   string    `description:"报告章节类型名称"  json:"report_chapter_type_name" `
//	Sort                    int       `description:"排序字段"  json:"sort" `
//	EditImgUrl              string    `description:"管理后台编辑时选用的图"  json:"edit_img_url" `
//	PauseStartTime          time.Time `description:"暂停开始日期"  json:"pause_start_time" `
//	PauseEndTime            time.Time `description:"暂停结束日期" json:"pause_end_time" `
//	LastUpdatedTime         time.Time `description:"最后更新时间" json:"last_updated_time" `
//	HttpUrl                 string    `json:"http_url",description:"报告详情"`
//}

type ResearchReportTypeList struct {
	ResearchReportTypeId    uint64    ` description:"章节ID"  `
	ResearchReportId        uint64    ` description:"研究报告id"  `
	ResearchReportTypeTitle string    `description:"研究报告标题" `
	TypeId                  int       `description:"分类id"   `
	Edit                    int8      `description:"是否编辑过"`
	Trend                   string    `description:"趋势观点"  `
	ReportChapterTypeKey    string    `description:"章节key" `
	ReportChapterTypeThumb  string    `description:"H5展示的图片"   `
	BannerUrl               string    `description:"banner显示图片"  `
	ReportChapterTypeName   string    `description:"报告章节类型名称"  `
	Sort                    int       `description:"排序字段"   `
	EditImgUrl              string    `description:"管理后台编辑时选用的图" `
	PauseStartTime          time.Time `description:"暂停开始日期"  `
	PauseEndTime            time.Time `description:"暂停结束日期" `
	LastUpdatedTime         time.Time `description:"最后更新时间"  `
	HttpUrl                 string    `json:"http_url",description:"报告详情"`
}

//GetResearchReportType 获取研究报告的章节详情
func GetResearchReportType(researchReportId uint64, userId int, reportType string) (list []*ResearchReportTypeList, err error) {
	var condition string
	//whereVals := make([]interface{}, 0)

	//如果是周报，并且是H5页面
	if "week" == reportType && userId > 0 {
		condition += ` and rrt.edit=1 `
		reportChapterTypeList, tmpErr := GetReportVarietyList(userId, reportType)
		if tmpErr != nil {
			err = tmpErr
			return
		}
		if len(reportChapterTypeList) > 0 {
			reportChapterTypeIdList := make([]string, 0)
			for _, v := range reportChapterTypeList {
				reportChapterTypeIdList = append(reportChapterTypeIdList, fmt.Sprint(v.ReportChapterTypeId))
			}
			condition += ` and rct.report_chapter_type_id in (` + strings.Join(reportChapterTypeIdList, ",") + `) `
			//whereVals = append(whereVals, strings.Join(reportChapterTypeIdList, ","))

		}
	}

	if strings.Contains("day,week", reportType) {
		condition += ` and rct.is_show=1 `
	}

	sql := `select
	rrt.research_report_type_id,
	rrt.research_report_id,
	rrt.research_report_type_title,
	rrt.type_id,
	rrt.edit,
	rrt.trend,
	rct.report_chapter_type_key,
	rct.report_chapter_type_thumb,
	rct.banner_url,
	rct.report_chapter_type_name,
	rct.sort,
	rct.edit_img_url,
	rct.pause_start_time,
	rct.pause_end_time,
	rrt.last_updated_time
	from research_report_type rrt
	left JOIN  report_chapter_type rct on rct.report_chapter_type_id  = rrt.type_id
	where rrt.research_report_id  = ? `
	sql += condition
	sql += ` order by rct.sort,rrt.research_report_type_id `
	o := orm.NewOrm()
	_, err = o.Raw(sql, researchReportId).QueryRows(&list)
	return
}

type PermissionName struct {
	ChartPermissionName string
	ResearchType        string
}

//权限
// GetReportVarietyList
func GetReportVarietyList(userId int, reportType string) (list []*ReportChapterTypeIdList, err error) {
	o := orm.NewOrm()
	var condition string
	//whereVals := make([]interface{}, 0)
	if reportType != "" {
		condition += ` and cpcm.research_type = '` + reportType + `' `
		//whereVals = append(whereVals, reportType)
	}
	sql := ` SELECT cpcm.report_chapter_type_id FROM company_report_permission crp
            INNER JOIN  chart_permission_chapter_mapping cpcm ON crp.chart_permission_id = cpcm.chart_permission_id
            INNER JOIN wx_user wu ON wu.company_id = crp.company_id
            WHERE wu.user_id = ? `
	sql += condition
	sql += ` GROUP BY cpcm.report_chapter_type_id `
	_, err = o.Raw(sql, userId).QueryRows(&list)
	return
}

type ResearchReportTypeContent struct {
	ResearchReportTypeTitle     string    `json:"research_report_type_title" description:"标题"`
	ResearchReportTypeContentId int       `json:"research_report_type_content_id" description:"研究报告内容id"`
	ResearchReportTypeId        int       `json:"research_report_id" description:"报告id"`
	Sort                        int       `json:"sort" description:"排序"`
	ContentType                 string    `json:"content_type" description:"内容分类类型"`
	Content                     string    `json:"content" description:"内容"`
	ImgUrl                      string    `json:"img_url" description:"图片路径"`
	CreatedTime                 time.Time `json:"created_time" description:"创建时间"`
	LastUpdatedTime             time.Time `json:"last_updated_time" description:"最近一次更新时间"`
}

// GetResearchReportTypeContentList 获取研究报告章节详情
func GetResearchReportTypeContentList(researchReportTypeId uint64) (items []*ResearchReportTypeContent, err error) {
	o := orm.NewOrm()
	sql := `select rrt.research_report_type_title,rrtc.*,rrt.research_report_type_id
from research_report_type rrt
inner join research_report_type_content rrtc on rrtc.research_report_type_id = rrt.research_report_type_id
where rrt.research_report_type_id = ? `
	_, err = o.Raw(sql, researchReportTypeId).QueryRows(&items)
	return
}

// GetResearchReportTypeInfo 获取研究报告类型详情
func GetResearchReportTypeInfo(researchReportTypeId uint64) (item *ResearchReportTypeInfo, err error) {
	sql := ` select rrt.research_report_type_title,rct.banner_url,rct.report_chapter_type_name,rrt.research_report_id,rrt.research_report_type_id,rrt.type_id,rct.report_chapter_type_name,rct.report_chapter_type_id
            from research_report_type rrt
            left join report_chapter_type rct on rct.report_chapter_type_id = rrt.type_id
            where rrt.research_report_type_id =? limit 1`
	o := orm.NewOrm()
	err = o.Raw(sql, researchReportTypeId).QueryRow(&item)
	return
}
