package report_chapter_permission_mapping

import (
	"hongze/hongze_yb/global"
	"time"
)

// GetReportChapterListByPermissionIdsAndReportId
// @Description: 根据报告ID和品种ID列表获取章节
// @author: Roc
// @datetime 2024-06-24 11:00:16
// @param permissionIds []int
// @param reportId int
// @return reportChapterIdList []int
// @return err error
func GetReportChapterListByPermissionIdsAndReportId(permissionIds []int, reportId int) (reportChapterIdList []int, err error) {
	var items []*ReportChapterPermissionMapping
	obj := ReportChapterPermissionMapping{}
	err = global.MYSQL["rddp"].Table(obj.TableName()+" AS a ").
		Joins("INNER JOIN report_chapter AS b ON a.report_chapter_id = b.report_chapter_id").
		Select("DISTINCT a.report_chapter_id ").
		Where("a.chart_permission_id in (?) AND b.report_id = ? ", permissionIds, reportId).
		Scan(&items).Error
	if err != nil {
		return
	}
	for _, v := range items {
		reportChapterIdList = append(reportChapterIdList, v.ReportChapterID)
	}

	return
}

// ReportChapterPermissionMappingItem 报告章节的权限关系表
type ReportChapterPermissionMappingItem struct {
	ReportChapterPermissionMappingID int       `gorm:"primaryKey;column:report_chapter_permission_mapping_id" json:"report_chapter_permission_mapping_id"`
	ReportChapterID                  int       `gorm:"column:report_chapter_id" json:"report_chapter_id"`     // 报告章节的id
	ChartPermissionID                int       `gorm:"column:chart_permission_id" json:"chart_permission_id"` // 权限id
	TypeId                           int       `gorm:"column:type_id" json:"type_id"`                         // 报告章节类型id
	CreateTime                       time.Time `gorm:"column:create_time" json:"create_time"`
}

// GetReportChapterPermissionMappingItemListByReportId
// @Description: 根据报告ID列表获取章节
// @author: Roc
// @datetime 2024-06-24 11:00:16
// @param permissionIds []int
// @param reportId int
// @return reportChapterIdList []int
// @return err error
func GetReportChapterPermissionMappingItemListByReportId(reportId int) (items []*ReportChapterPermissionMappingItem, err error) {
	obj := ReportChapterPermissionMapping{}
	err = global.MYSQL["rddp"].Table(obj.TableName()+" AS a ").
		Joins("INNER JOIN report_chapter AS b ON a.report_chapter_id = b.report_chapter_id").
		Select("a.*,b.type_id").
		Where(" b.report_id = ? ", reportId).
		Scan(&items).Error

	return
}

// GetReportChapterListByReportId
// @Description: 根据报告ID获取所有章节id
// @author: Roc
// @datetime 2024-06-24 11:02:23
// @param reportId int
// @return reportChapterIdList []int
// @return err error
func GetReportChapterListByReportId(reportId int) (reportChapterIdList []int, err error) {
	var items []*ReportChapterPermissionMapping
	obj := ReportChapterPermissionMapping{}
	err = global.MYSQL["rddp"].Table(obj.TableName()+" AS a ").
		Joins("INNER JOIN report_chapter AS b ON a.report_chapter_id = b.report_chapter_id").
		Select("DISTINCT report_chapter_id ").
		Where(" b.report_id = ? ", reportId).
		Scan(&items).Error
	if err != nil {
		return
	}
	for _, v := range items {
		reportChapterIdList = append(reportChapterIdList, v.ReportChapterID)
	}

	return
}