package report

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

// ReportChapterPermissionMapping
// @Description: 报告章节的权限关系表
type ReportChapterPermissionMapping struct {
	ReportChapterPermissionMappingId int `orm:"column(report_chapter_permission_mapping_id)"`
	ReportChapterId                  int `description:"报告章节的id"` // 报告章节的id
	ChartPermissionId                int `description:"权限id"`    // 权限id
	CreateTime                       time.Time
}

// MultiAddReportChapterPermissionMappingPermission
// @Description: 批量添加报告品种权限用户
// @author: Roc
// @receiver m
// @datetime 2024-06-04 15:36:02
// @param reportChapterId int
// @param list []*ReportChapterPermissionMapping
// @return err error
func (m ReportChapterPermissionMapping) MultiAddReportChapterPermissionMappingPermission(reportChapterId int, list []*ReportChapterPermissionMapping) (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_permission_mapping where report_chapter_id=?"
	_, err = to.Raw(sql, reportChapterId).Exec()
	if err != nil {
		return
	}

	// 新增品种权限记录
	if len(list) > 0 {
		_, tmpErr := to.InsertMulti(500, list)
		if tmpErr != nil {
			err = tmpErr
			return
		}
	}

	return
}

// GetPermissionListById
// @Description: 根据id获取品种权限列表
// @author: Roc
// @receiver m
// @datetime 2024-06-04 15:33:58
// @param reportChapterId int
// @return list []*ReportChapterPermissionMapping
// @return err error
func (m ReportChapterPermissionMapping) GetPermissionListById(reportChapterId int) (list []*ReportChapterPermissionMapping, err error) {
	o := orm.NewOrmUsingDB("rddp")
	sql := `SELECT * FROM report_chapter_permission_mapping WHERE report_chapter_id=? `
	_, err = o.Raw(sql, reportChapterId).QueryRows(&list)

	return
}

// ReportChapterPermissionItem
// @Description: 报告章节的权限关系表(带有品种名称)
type ReportChapterPermissionItem struct {
	ReportChapterPermissionMappingId int    `orm:"column(report_chapter_permission_mapping_id)"`
	ReportChapterId                  int    `description:"报告章节的id"`
	ChartPermissionId                int    `description:"权限id"`
	ChartPermissionName              string `description:"品种名称"`
	CreateTime                       time.Time
}

// GetPermissionItemListById
// @Description: 根据id获取品种权限列表(带有品种名称)
// @author: Roc
// @receiver m
// @datetime 2024-06-04 15:33:58
// @param reportChapterId int
// @return list []*ReportChapterPermissionMapping
// @return err error
func (m ReportChapterPermissionMapping) GetPermissionItemListById(reportChapterId int) (list []*ReportChapterPermissionItem, err error) {
	o := orm.NewOrmUsingDB("rddp")
	sql := `SELECT a.*,b.chart_permission_name FROM report_chapter_permission_mapping AS a 
         JOIN chart_permission AS b on a.chart_permission_id=b.chart_permission_id WHERE report_chapter_id=? `
	_, err = o.Raw(sql, reportChapterId).QueryRows(&list)

	return
}

// GetPermissionListByIdList
// @Description: 根据id列表获取品种权限列表
// @author: Roc
// @receiver m
// @datetime 2024-06-04 15:33:58
// @param reportChapterIdList []int
// @return list []*ReportChapterPermissionMapping
// @return err error
func (m ReportChapterPermissionMapping) GetPermissionListByIdList(reportChapterIdList []int) (list []*ReportChapterPermissionMapping, err error) {
	num := len(reportChapterIdList)
	if num <= 0 {
		return
	}

	o := orm.NewOrmUsingDB("rddp")
	sql := `SELECT * FROM report_chapter_permission_mapping WHERE report_chapter_id in (` + utils.GetOrmInReplace(num) + `) `
	_, err = o.Raw(sql, reportChapterIdList).QueryRows(&list)

	return
}

// MultiAdd
// @Description: 批量添加
// @author: Roc
// @receiver m
// @datetime 2024-06-20 18:04:33
// @param list []*ReportChapterPermissionMapping
// @return err error
func (m ReportChapterPermissionMapping) MultiAdd(list []*ReportChapterPermissionMapping) (err error) {
	o := orm.NewOrmUsingDB("rddp")
	to, err := o.Begin()
	if err != nil {
		return
	}
	defer func() {
		if err != nil {
			_ = to.Rollback()
		} else {
			_ = to.Commit()
		}
	}()

	// 新增品种权限记录
	if len(list) > 0 {
		_, err = to.InsertMulti(500, list)
		if err != nil {
			return
		}
	}

	return
}