package models

import (
	"eta/eta_mobile/utils"
	"github.com/beego/beego/v2/client/orm"
	"time"
)

type ReportChapterTypePermission struct {
	Id                    int       `orm:"column(id);pk" description:"主键ID"`
	ReportChapterTypeId   int       `description:"报告章节类型ID"`
	ReportChapterTypeName string    `description:"章节名称"`
	ChartPermissionId     int       `description:"大分类ID"`
	PermissionName        string    `description:"权限名称"`
	ResearchType          string    `description:"研报类型"`
	CreatedTime           time.Time `description:"创建时间"`
}

// GetChapterTypePermissionByReportChapterTypeId
// @Description: 根据章节类型ID获取章节类型权限列表
// @author: Roc
// @datetime 2024-06-03 15:42:47
// @param typeId int
// @return list []*ReportChapterTypePermission
// @return err error
func GetChapterTypePermissionByReportChapterTypeId(typeId int) (list []*ReportChapterTypePermission, err error) {
	o := orm.NewOrmUsingDB("rddp")
	sql := ` SELECT * FROM report_chapter_type_permission WHERE report_chapter_type_id = ?  ORDER BY chart_permission_id ASC `
	_, err = o.Raw(sql, typeId).QueryRows(&list)
	return
}

// SetReportChapterTypePermission 设置报告章节类型权限
func SetReportChapterTypePermission(chapterTypeId int, newPermissions []*ReportChapterTypePermission) (err error) {
	o := orm.NewOrmUsingDB("rddp")
	to, err := o.Begin()
	if err != nil {
		return
	}
	defer func() {
		if err != nil {
			_ = to.Rollback()
		} else {
			_ = to.Commit()
		}
	}()

	// 删除原章节类型权限
	sql := `DELETE FROM report_chapter_type_permission WHERE report_chapter_type_id = ?`
	_, err = to.Raw(sql, chapterTypeId).Exec()
	if err != nil {
		return
	}

	// 新增章节类型权限
	if len(newPermissions) > 0 {
		_, err = to.InsertMulti(len(newPermissions), newPermissions)
		if err != nil {
			return
		}
	}

	// 周报章节调整chart_permission_chapter_mapping表
	//if researchType == utils.REPORT_TYPE_WEEK {
	//	// 删除原权限
	//	sql = `DELETE FROM chart_permission_chapter_mapping WHERE report_chapter_type_id = ? AND research_type = ?`
	//	_, err = to.Raw(sql, chapterTypeId, researchType).Exec()
	//	if err != nil {
	//		return
	//	}
	//
	//	// 新增权限
	//	if len(newWeekPermissions) > 0 {
	//		_, err = to.InsertMulti(len(newWeekPermissions), newWeekPermissions)
	//		if err != nil {
	//			return
	//		}
	//	}
	//}

	return
}

// GetChapterTypePermissionByResearchType 根据章节类型ID及研报类型获取章节类型权限列表
func GetChapterTypePermissionByResearchType(researchType string) (list []*ReportChapterTypePermission, err error) {
	o := orm.NewOrmUsingDB("rddp")
	sql := ` SELECT * FROM report_chapter_type_permission WHERE research_type = ? ORDER BY chart_permission_id ASC `
	_, err = o.Raw(sql, researchType).QueryRows(&list)
	return
}

// GetChapterTypePermissionByChapterTypeIdList
// @Description: 根据章节类型ID列表获取章节类型权限列表
// @author: Roc
// @datetime 2024-06-03 14:10:17
// @param chapterTypeIdList []int
// @return list []*ReportChapterTypePermission
// @return err error
func GetChapterTypePermissionByChapterTypeIdList(chapterTypeIdList []int) (list []*ReportChapterTypePermission, err error) {
	num := len(chapterTypeIdList)
	if num <= 0 {
		return
	}
	o := orm.NewOrmUsingDB("rddp")
	sql := ` SELECT * FROM report_chapter_type_permission WHERE report_chapter_type_id in (` + utils.GetOrmInReplace(num) + `) ORDER BY chart_permission_id ASC `
	_, err = o.Raw(sql, chapterTypeIdList).QueryRows(&list)
	return
}

// GetAllChapterTypePermission
// @Description: 获取所有章节类型ID获取章节类型权限列表
// @author: Roc
// @datetime 2024-06-03 15:42:47
// @return list []*ReportChapterTypePermission
// @return err error
func GetAllChapterTypePermission() (list []*ReportChapterTypePermission, err error) {
	o := orm.NewOrmUsingDB("rddp")
	sql := ` SELECT * FROM report_chapter_type_permission ORDER BY chart_permission_id ASC `
	_, err = o.Raw(sql).QueryRows(&list)
	return
}
