package report

import (
	"eta_gn/eta_api/global"
	"eta_gn/eta_api/utils"
	"time"
)

type ReportChapterGrant struct {
	GrantId         int       `gorm:"primaryKey;column:grant_id;type:int(9) unsigned;not null"`                                    // 授权id
	ReportChapterId int       `gorm:"index:idx_report_chapterid;column:report_chapter_id;type:int(9) unsigned;not null;default:0"` // 报告章节id
	AdminId         int       `gorm:"column:admin_id;type:int(9) unsigned;default:0"`                                              // 授权的用户id
	CreateTime      time.Time `gorm:"column:create_time;type:timestamp;default:CURRENT_TIMESTAMP"`                                 // 授权时间
}

// MultiAddReportChapterGrantGrant
// @Description: 批量添加报告授权用户
// @author: Roc
// @receiver m
// @datetime 2024-06-04 15:36:02
// @param reportChapterId int
// @param list []*ReportChapterGrant
// @return err error
func (m ReportChapterGrant) MultiAddReportChapterGrantGrant(reportChapterId int, list []*ReportChapterGrant) (err error) {
	tx := global.DmSQL["rddp"].Begin()
	defer func() {
		if err != nil {
			_ = tx.Rollback()
			return
		}
		_ = tx.Commit()
	}()

	sql := "DELETE from report_chapter_grant where report_chapter_id=?"
	err = tx.Exec(sql, reportChapterId).Error
	if err != nil {
		return
	}

	// 新增授权记录
	if len(list) > 0 {
		e := tx.CreateInBatches(list, utils.MultiAddNum).Error
		if e != nil {
			err = e
			return
		}
	}

	return
}

// GetGrantListById
// @Description: 根据id获取授权列表
// @author: Roc
// @receiver m
// @datetime 2024-06-04 15:33:58
// @param reportChapterId int
// @return list []*ReportChapterGrant
// @return err error
func (m ReportChapterGrant) GetGrantListById(reportChapterId int) (list []*ReportChapterGrant, err error) {
	sql := `SELECT * FROM report_chapter_grant WHERE report_chapter_id=? `
	err = global.DmSQL["rddp"].Raw(sql, reportChapterId).Find(&list).Error
	return
}

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

	sql := `SELECT * FROM report_chapter_grant WHERE report_chapter_id in (` + utils.GetOrmInReplace(num) + `) `
	err = global.DmSQL["rddp"].Raw(sql, reportChapterIdList).Find(&list).Error
	return
}

// GetGrantByIdAndAdmin
// @Description: 根据reportId和操作人获取报告章节权限配置
// @author: Roc
// @receiver m
// @datetime 2024-06-04 15:49:59
// @param reportChapterId int
// @param sysUserId int
// @return item *ReportGrant
// @return err error
func (m ReportChapterGrant) GetGrantByIdAndAdmin(reportChapterId, sysUserId int) (item *ReportGrant, err error) {
	sql := `SELECT * FROM report_chapter_grant WHERE report_chapter_id = ? AND admin_id = ? `
	err = global.DmSQL["rddp"].Raw(sql, reportChapterId, sysUserId).First(&item).Error
	return
}