package models

import (
	"time"

	"github.com/beego/beego/v2/client/orm"
)

type PptV2Group struct {
	GroupId    int64     `orm:"column(group_id);pk;auto" description:"ppt目录ID"`
	GroupName  string    `description:"目录名称"`
	AdminId    int       `description:"目录创建者账号ID"`
	IsShare    int8      `description:"是否共享，0私有，1共享"`
	GroupSort  int64     `description:"目录排序标识"`
	IsShareAdd int8      `description:"是否是通过单个ppt共享生成的目录，0不是，1-是"`
	CreateTime time.Time `orm:"auto_now_add;type(datetime)" description:"创建时间"`
	ModifyTime time.Time `orm:"auto_now;type(datetime)" description:"修改时间"`
	ShareTime  time.Time `description:"分享时间"`
}

// GetPrivatePptGroups 获取私有目录
func GetPrivatePptGroups(adminId int) (list []*PptV2Group, err error) {
	o := orm.NewOrmUsingDB("rddp")
	sql := "select * from ppt_v2_group where admin_id=? order by group_sort asc, group_id asc"
	_, err = o.Raw(sql, adminId).QueryRows(&list)
	return
}

// GetPrivatePptGroupsByAdminId 获取用户自己的私有目录
func GetPrivatePptGroupsByAdminId(adminId int) (list []*PptV2Group, err error) {
	o := orm.NewOrmUsingDB("rddp")
	sql := "select * from ppt_v2_group where admin_id=? AND is_share_add=0 order by group_sort asc, group_id asc"
	_, err = o.Raw(sql, adminId).QueryRows(&list)
	return
}

// GetPublicPptGroups 获取ppt公共目录
func GetPublicPptGroups() (list []*PptV2Group, err error) {
	o := orm.NewOrmUsingDB("rddp")
	sql := "select * from ppt_v2_group where is_share=1 order by share_time asc, group_id asc"
	_, err = o.Raw(sql).QueryRows(&list)
	return
}

// GetPptGroupByName 根据目录名称查询
func GetPptGroupByName(groupName string, adminId int) (item *PptV2Group, err error) {
	o := orm.NewOrmUsingDB("rddp")
	sql := "select * from ppt_v2_group where admin_id=? and group_name=?"
	err = o.Raw(sql, adminId, groupName).QueryRow(&item)
	return
}

// GetPptGroupById 根据目录Id查询
func GetPptGroupByGroupIdAdminId(groupId int64, adminId int) (item *PptV2Group, err error) {
	o := orm.NewOrmUsingDB("rddp")
	sql := "select * from ppt_v2_group where admin_id=? and group_id=?"
	err = o.Raw(sql, adminId, groupId).QueryRow(&item)
	return
}

// GetPptGroupByGroupId 根据目录Id查询
func GetPptGroupByGroupId(groupId int64) (item *PptV2Group, err error) {
	o := orm.NewOrmUsingDB("rddp")
	sql := "select * from ppt_v2_group where group_id=?"
	err = o.Raw(sql, groupId).QueryRow(&item)
	return
}

// AddPptGroup 新增目录
func AddPptGroup(item *PptV2Group) (lastId int64, err error) {
	o := orm.NewOrmUsingDB("rddp")
	lastId, err = o.Insert(item)
	return
}

// DeletePptGroup 删除单个ppt目录
func DeletePptGroup(groupId int64) (err error) {
	o := orm.NewOrmUsingDB("rddp")
	sql := `DELETE FROM ppt_v2_group WHERE group_id=? `
	_, err = o.Raw(sql, groupId).Exec()
	return
}

// DeletePptGroupByGroupIds 删除单个ppt目录
func DeletePptGroupByGroupIds(groupIds string) (err error) {
	o := orm.NewOrmUsingDB("rddp")
	sql := `DELETE FROM ppt_v2_group WHERE group_id in  ` + groupIds
	_, err = o.Raw(sql).Exec()
	return
}

// Update 更新ppt目录基础信息
func (item *PptV2Group) Update(cols []string) (err error) {
	o := orm.NewOrmUsingDB("rddp")
	_, err = o.Update(item, cols...)
	return
}

// MoveUpGroupBySort 往上移动目录
func MoveUpGroupBySort(nextSort, currentSort int64) (err error) {
	o := orm.NewOrmUsingDB("rddp")
	sql := `update ppt_v2_group set group_sort = group_sort + 1 where group_sort >= ? and group_sort< ?`
	_, err = o.Raw(sql, nextSort, currentSort).Exec()
	return
}

// MoveDownGroupBySort 往下移动目录
func MoveDownGroupBySort(prevSort, currentSort int64) (err error) {
	o := orm.NewOrmUsingDB("rddp")
	sql := `update ppt_v2_group set group_sort = group_sort - 1 where group_sort <= ? and group_sort> ? `
	_, err = o.Raw(sql, prevSort, currentSort).Exec()
	return
}

// GetPptGroupNamesByAdminId 获取当前目录的所有名称
func GetPptGroupNamesByAdminId(adminId int) (names []string, err error) {
	o := orm.NewOrmUsingDB("rddp")
	sql := "select group_name from ppt_v2_group  where admin_id = ?"
	_, err = o.Raw(sql, adminId).QueryRows(&names)
	return
}

// GetPptGroupIdAndGroupNameByAdminId 查找分组名称和标识
func GetPptGroupIdAndGroupNameByAdminId(adminId int) (list []*RespPptGroupName, err error) {
	o := orm.NewOrmUsingDB("rddp")
	sql := "select group_id, group_name from ppt_v2_group where admin_id = ? and is_share_add != 1 order by group_sort asc, group_id asc"
	_, err = o.Raw(sql, adminId).QueryRows(&list)
	return
}

// AddPptGroupMappingMulti 批量新增ppt和目录的映射关系
func AddPptGroupMulti(list []*PptV2Group) (err error) {
	o := orm.NewOrmUsingDB("rddp")
	_, err = o.InsertMulti(1, list)
	return
}

// UpdatePptGroupSortMulti 批量更新初始排序标识
func UpdatePptGroupSortMulti(groupIds []int64) (err error) {
	o := orm.NewOrmUsingDB("rddp")
	p, err := o.Raw("UPDATE ppt_v2_group SET group_sort = group_id WHERE group_id = ?").Prepare()
	if err != nil {
		return
	}
	defer func() {
		_ = p.Close() // 别忘记关闭 statement
	}()
	for _, v := range groupIds {
		_, err = p.Exec(v)
		if err != nil {
			return
		}
	}
	return
}

type ReqAddPptGroup struct {
	GroupName string `description:"目录名称"`
}

type RespAddPptGroup struct {
	GroupId int64 `description:"目录id"`
}

type ReqPptGroup struct {
	GroupId int64 `description:"目录id"`
}

type ReqRenamePptGroup struct {
	GroupId   int64  `description:"目录id"`
	GroupName string `description:"目录名称"`
}

type RespPptGroupShare struct {
	GroupId int64 `description:"目录id"`
	IsShare int8  `description:"是否共享，0私有，1共享"`
}

type RespGroupList struct {
	PublicList  []*RespGroupListItem
	PrivateList []*RespGroupListItem
	GrantList   []RespGroupListItem
}

type RespGroupListItem struct {
	GroupId   int64  `description:"目录id"`
	GroupName string `description:"目录名称"`
	AdminId   int    `description:"目录创建者账号ID"`
	IsShare   int8   `description:"是否共享，0私有，1共享"`
	OnlyOld   bool   `description:"是否只包含ppt旧版本的目录,只包含旧版的ppt：true，否则 false"`
	PptList   []*RespGroupPptNameListItem
	//GroupSort  int64  `description:"目录排序标识"`
	//IsShareAdd int8   `description:"是否是通过单个ppt共享生成的目录，0不是，1-是"`
	//CreateTime string `description:"创建时间"`
	//ModifyTime string `description:"修改时间"`
}

type RespGroupPptNameListItem struct {
	GroupPptId    int64  `description:"目录和ppt绑定序号"`
	PptId         int64  `description:"ppt ID"`
	Title         string `description:"标题"`
	AdminId       int    `description:"移动ppt到该目录的系统用户id"`
	AdminRealName string `description:"系统用户名称"`
	PptVersion    int8   `description:"是否ppt的旧版本；1：旧的，2：新的"`
	IsSingleShare int8   `description:"是否是单个共享ppt，0未单个共享，1共享"`
	PptxUrl       string `description:"pptx下载地址"`
	ReportId      int    `description:"关联的报告ID"`
	ReportCode    string `description:"关联的报告code"`
	PptCreateTime string `description:"ppt创建时间"`
	PptModifyTime string `description:"ppt修改时间"`
	PublishTime   string `description:"发布时间"`
	PptPage       int    `description:"PPT总页数"`
	IsReceived    int8   `description:"是否收到的共享，0：不是，1：是"`
	IsGrant       int8   `description:"是否分配了权限，0：不是，1：是"`
}

type RespGroupPptListItem struct {
	GroupPptId    int64  `description:"目录和ppt绑定序号"`
	PptId         int64  `description:"ppt ID"`
	TemplateType  int    `description:"模版类型"`
	BackgroundImg string `description:"背景图片"`
	Title         string `description:"标题"`
	PptCreateTime string `description:"ppt创建时间"`
	PptModifyTime string `description:"ppt修改时间"`
	AdminId       int    `description:"移动ppt到该目录的系统用户id"`
	AdminRealName string `description:"系统用户名称"`
	PptVersion    int8   `description:"是否ppt的旧版本；1：旧的，2：新的"`
	IsSingleShare int8   `description:"是否是单个共享ppt，0未单个共享，1共享"`
	PptxUrl       string `description:"pptx下载地址"`
	ReportId      int    `description:"关联的报告ID"`
	ReportCode    string `description:"关联的报告code"`
	PublishTime   string `description:"发布时间"`
	PptPage       int    `description:"PPT总页数"`
}

func (p RespGroupPptList) Len() int {
	return len(p.List)
}

func (p RespGroupPptList) Less(i, j int) bool {
	if p.List[i].PptCreateTime < p.List[j].PptCreateTime {
		return false
	}
	return true
}

func (p RespGroupPptList) Swap(i, j int) {
	p.List[i], p.List[j] = p.List[j], p.List[i]
}

type ReqPptShare struct {
	GroupPptId int64 `description:"目录和ppt绑定序号"`
}

type RespPptShare struct {
	GroupPptId    int64 `description:"目录和ppt绑定自增序号"`
	IsSingleShare int8  `description:"是否是单个共享ppt，0未单个共享，1共享"`
}

type RespGroupPptList struct {
	List  []*RespGroupPptListItem
	Total int `description:"ppt总数"`
}

type ReqMoveGroupPpt struct {
	GroupId        int64 `description:"目标目录id"`
	GroupPptId     int64 `description:"当前被移动的目录里的ppt绑定序号"`
	PrevGroupPptId int64 `description:"上一个目录里的ppt绑定序号，ppt最终在目录中置顶，则传0值"`
	NextGroupPptId int64 `description:"下一个目录里的ppt绑定序号，ppt最终在目录末尾，则传0值"`
}

type ReqMoveGroup struct {
	GroupId     int64 `description:"目标目录id"`
	PrevGroupId int64 `description:"上一个目录Id，置顶，则传0值"`
	NextGroupId int64 `description:"下一个目录Id，放到末尾，则传0值"`
}

type ReqCopyPpt struct {
	GroupId int64 `description:"目录id"`
	PptId   int   `description:"PptId" `
}

type RespPptGroupNameList struct {
	List []*RespPptGroupName
}

type RespPptGroupName struct {
	GroupId   int64  `description:"目录id"`
	GroupName string `description:"目录名称"`
}

type RespSearchGroupPptList struct {
	List []*RespSearchGroupPptListItem
}

type RespSearchGroupPptListItem struct {
	GroupPptId int64  `description:"目录和ppt绑定序号"`
	PptId      int64  `description:"ppt ID"`
	Title      string `description:"标题"`
}

// ReqPptShareV2 共享ppt请求参数
type ReqPptShareV2 struct {
	PptId int `description:"pptId"`
}

// RespPptShareV2  共享ppt返回参数
type RespPptShareV2 struct {
	PptId   int  `description:"目录和ppt绑定自增序号"`
	IsShare int8 `description:"是否是单个共享ppt，0未单个共享，1共享"`
}
