package pc

import (
	"errors"
	"fmt"
	"hongze/hongze_yb/global"
	"hongze/hongze_yb/models/response/pc"
	"hongze/hongze_yb/models/tables/chart_permission_search_key_word_mapping"
	"hongze/hongze_yb/models/tables/rddp/chart_permission"
	"hongze/hongze_yb/models/tables/rddp/classify"
	"hongze/hongze_yb/models/tables/rddp/customer_comment"
	"hongze/hongze_yb/models/tables/rddp/report"
	"hongze/hongze_yb/services/company"
	"hongze/hongze_yb/services/user"
	"hongze/hongze_yb/utils"
)

func GetClassifyDetail(user user.UserInfo, classifyIdSecond int) (detail *pc.ClassifyDetail, err error) {
	var errMsg string
	defer func() {
		if err != nil {
			global.LOG.Critical(fmt.Sprintf("GetClassifyDetail: userId=%d, err:%s， errMsg:%s", user.UserID, err.Error(), errMsg))
		}
	}()
	classifyInfo, err := classify.GetByClassifyId(classifyIdSecond)
	if err != nil {
		errMsg = err.Error()
		err = errors.New("分类查询出错")
		return
	}

	if classifyInfo.Id == 0 {
		err = errors.New("分类不存在")
		return
	}
	if classifyInfo.ParentId == 0 {
		err = errors.New("只允许查询二级分类")
		return
	}
	//查询权限, 查询当前分类下的最新的报告，
	permissionIds, err := chart_permission_search_key_word_mapping.GetChartPermissionIdsByKeyWord(classifyIdSecond)
	if err != nil {
		errMsg = err.Error()
		err = errors.New("分类权限查询出错")
		return
	}
	authOk, permissionCheckInfo, _, err := company.GetCheckPermission(user.CompanyID, int(user.UserID), permissionIds)
	if err != nil && err != utils.ErrNoRow {
		errMsg = err.Error()
		err = errors.New("权限查询出错")
		return
	}

	comments, err := customer_comment.GetCustomerComment()
	if err != nil {
		errMsg = err.Error()
		err = errors.New("客户评价查询出错")
		return
	}

	detail = new(pc.ClassifyDetail)
	detail.ClassifyIdSecond = classifyInfo.Id
	detail.ParentId = classifyInfo.ParentId
	detail.AuthorDescript = classifyInfo.AuthorDescript
	detail.ReportAuthor = classifyInfo.ReportAuthor
	detail.AvatarImgUrl = classifyInfo.AvatarImgUrl
	detail.ClassifyNameSecond = classifyInfo.ClassifyName
	detail.Abstract = classifyInfo.Abstract
	detail.Descript = classifyInfo.Descript
	detail.VipTitle = classifyInfo.VipTitle
	detail.PermissionCheck = &permissionCheckInfo
	detail.AuthOk = authOk
	detail.Comments = comments

	return
}

// GetLatestReportList 首页最新资讯
func GetLatestReportList(user user.UserInfo, chartPermissionId int) (ret []*pc.LatestReport, err error) {
	var errMsg string
	defer func() {
		if err != nil {
			global.LOG.Critical(fmt.Sprintf("GetCollectReportList: userId=%d, err:%s， errMsg:%s", user.UserID, err.Error(), errMsg))
		}
	}()

	// 查询权限的基本信息
	permissionInfo, err := chart_permission.GetByChartPermissionId(chartPermissionId)
	if err != nil {
		errMsg = err.Error()
		err = errors.New("查询权限出错")
		return
	}

	if permissionInfo.ChartPermissionID == 0 {
		err = errors.New("权限不存在")
		return
	}
	// 除了晨报和周报以外的其他报告
	classifySecondIds, err := chart_permission_search_key_word_mapping.GetClassifyIdsByChartPermissionId(chartPermissionId, "rddp")
	if err != nil {
		errMsg = err.Error()
		err = errors.New("查询权限对应的分类出错")
		return
	}

	//var classifySecondIds []int
	//if len(classifyNames) > 0 {
	//	classifySecondIds, err = classify.GetSecondIdsByClassifyNames(classifyNames)
	//	if err != nil {
	//		errMsg = err.Error()
	//		err = errors.New("查询分类出错")
	//		return
	//	}
	//}

	var reportList []*pc.LatestReport
	if len(classifySecondIds) > 0 {
		reportList, err = report.GetLatestReportByPermission(classifySecondIds)
		if err != nil {
			errMsg = err.Error()
			err = errors.New("查询报告信息出错")
			return
		}
	}
	//文章不足三篇时用下一品种补全
	//if len(reportList) < 3 {
	//
	//	reportList, err = report.GetLatestReportByPermission(classifySecondIds)
	//	if err != nil {
	//		errMsg = err.Error()
	//		err = errors.New("查询报告信息出错")
	//		return
	//	}
	//}
	ret = reportList
	return
}
