package models

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

// PptV2Grant Ppt授权表
type PptV2Grant struct {
	GrantId      int64     `orm:"column(grant_id);pk;auto" description:"自增序号"`
	PptId        int64     `description:"ppt ID"`
	DepartmentId int64     `description:"授权部门id"`
	GrantAdminId int64     `description:"授权部门id"`
	CreateTime   time.Time `orm:"auto_now_add;type(datetime)" description:"创建时间"`
}

// GetPPtGrantInfo 获取已经有权限的ppt列表
func GetPPtGrantInfo(pptId int) (list []*PptV2Grant, err error) {
	o := orm.NewOrmUsingDB("rddp")
	sql := `SELECT * FROM ppt_v2_grant WHERE ppt_id=? `
	_, err = o.Raw(sql, pptId).QueryRows(&list)
	return
}

// GrantInfoResp ppt权限配置返回数据
type GrantInfoResp struct {
	PptId      int    `description:"PptId" `
	GrantType  int    `description:"分配类型;1：全部ficc研究员；2：指定成员"`
	AdminIdStr string `description:"指定成员id，多个成员用英文,隔开"`
}

// GrantPptReq 分配ppt权限
type GrantPptReq struct {
	PptId      int    `description:"PptId" `
	GrantType  int    `description:"分配类型;1：全部ficc研究员；2：指定成员"`
	AdminIdStr string `description:"指定成员id，多个成员用英文,隔开"`
}

// MultiAddPptV2Grant 批量添加授权记录
func MultiAddPptV2Grant(pptId int, list []*PptV2Grant) (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 ppt_v2_grant where ppt_id=?"
	_, err = to.Raw(sql, pptId).Exec()
	if err != nil {
		return
	}

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

	return
}

// DeletePptV2Grant 移除授权记录
func DeletePptV2Grant(pptId int) (err error) {
	o := orm.NewOrmUsingDB("rddp")
	sql := "DELETE from ppt_v2_grant where ppt_id=?"
	_, err = o.Raw(sql, pptId).Exec()

	return
}

// PptV2InfoGrantItem Ppt信息记录（包含授权信息）
type PptV2InfoGrantItem struct {
	PptId         int       `orm:"column(ppt_id);pk;auto" description:"ppt的Id"`
	TemplateType  int       `description:"模版类型"`
	BackgroundImg string    `description:"背景图片"`
	Title         string    `description:"标题"`
	ReportType    string    `description:"报告类型"`
	PptDate       string    `description:"选择日期"`
	Content       string    `description:"ppt内容"`
	PptUrl        string    `description:"ppt下载地址"`
	PptxUrl       string    `description:"pptx下载地址"`
	CreateTime    time.Time `description:"创建时间"`
	ModifyTime    time.Time `description:"修改时间"`
	AdminId       int       `description:"系统用户id"`
	AdminRealName string    `description:"系统用户名称"`
	PptVersion    int8      `description:"是否ppt的旧版本；1：旧的，2：新的"`
	ReportId      int       `description:"关联的报告ID"`
	ReportCode    string    `description:"关联的报告code"`
	IsShare       int8      `description:"是否分享，0：不分享，1：分享"`

	//GrantId      int64     `orm:"column(grant_id);pk;auto" description:"自增序号"`
	//PptId        int64     `description:"ppt ID"`
	//DepartmentId int64     `description:"授权部门id"`
	//GrantAdminId int64     `description:"授权部门id"`
	//CreateTime   time.Time `orm:"auto_now_add;type(datetime)" description:"创建时间"`
}

// GetAllGrantList 获取已经有权限的ppt列表
func GetAllGrantList(sysUserId int) (list []*PptV2InfoGrantItem, err error) {
	o := orm.NewOrmUsingDB("rddp")
	sql := `SELECT a.* FROM ppt_v2 a JOIN ppt_v2_grant b on a.ppt_id=b.ppt_id 
 WHERE a.admin_id=? OR b.department_id=1 OR b.grant_admin_id=? GROUP BY a.ppt_id`
	_, err = o.Raw(sql, sysUserId, sysUserId).QueryRows(&list)
	return
}

// GetGrantList 获取我共享出去/别人共享给我的的ppt列表
func GetGrantList(condition string, pars []interface{}) (list []*PptV2InfoGrantItem, err error) {
	o := orm.NewOrmUsingDB("rddp")
	sql := `SELECT a.* FROM ppt_v2 a JOIN ppt_v2_grant b on a.ppt_id=b.ppt_id 
 WHERE 1=1 `
	sql += condition

	sql += ` GROUP BY a.ppt_id ORDER BY a.modify_time DESC `
	_, err = o.Raw(sql, pars).QueryRows(&list)

	return
}

// GetPPtGrantConf 根据ppt_id和操作人获取ppt权限配置
func GetPPtGrantConf(pptId, sysUserId int) (item *PptV2InfoGrantItem, err error) {
	o := orm.NewOrmUsingDB("rddp")
	sql := `SELECT a.* FROM ppt_v2 a JOIN ppt_v2_grant b on a.ppt_id=b.ppt_id 
 WHERE a.ppt_id=?  AND (b.department_id=1 OR b.grant_admin_id=?) GROUP BY a.ppt_id`
	err = o.Raw(sql, pptId, sysUserId).QueryRow(&item)
	return
}
