package models

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

// UpdateEnglishClassifySortByParentId 根据父类id更新排序
func UpdateEnglishClassifySortByParentId(parentId, permissionId, nowSort int, updateSort string) (err error) {
	o := orm.NewOrmUsingDB("rddp")
	sql := ` update english_classify set sort = ` + updateSort + ` WHERE parent_id=? AND sort > ? `
	if permissionId > 0 {
		sql += ` or ( id > ` + fmt.Sprint(permissionId) + ` and sort = ` + fmt.Sprint(nowSort) + `)`
	}
	_, err = o.Raw(sql, parentId, nowSort).Exec()
	return
}

// GetMaxSortByParentId 获取最大的排序值
func (classifyInfo *EnglishClassify) GetMaxSortByParentId(parentId int) (maxSort int, err error) {
	o := orm.NewOrmUsingDB("rddp")
	sql := `SELECT max(sort) AS sort FROM english_classify WHERE parent_id = ? `
	err = o.Raw(sql, parentId).QueryRow(&maxSort)
	return
}

// GetMaxSort 获取最大的排序值
func (classifyInfo *EnglishClassify) GetMaxSort() (maxSort int, err error) {
	o := orm.NewOrmUsingDB("rddp")
	sql := `SELECT max(sort) AS sort FROM english_classify`
	err = o.Raw(sql).QueryRow(&maxSort)
	return
}

// GetFirstClassifyByParentId 获取当前父级分类下，且排序数相同 的排序第一条的数据
func (classifyInfo *EnglishClassify) GetFirstClassifyByParentId(parentId int) (item *Classify, err error) {
	o := orm.NewOrmUsingDB("rddp")
	sql := `SELECT * FROM english_classify WHERE parent_id = ? order by sort asc, id asc limit 1`
	err = o.Raw(sql, parentId).QueryRow(&item)
	return
}

type EnglishClassifyMoveReq struct {
	ClassifyId     int `description:"分类ID"`
	PrevClassifyId int `description:"上一个兄弟节点分类id"`
	NextClassifyId int `description:"下一个兄弟节点分类id"`
}

type EnClassifyAddReq struct {
	EnPermissions []int  `description:"权限IDs"`
	ClassifyName  string `description:"分类名称"`
	ParentId      int    `description:"父级分类id"`
}

type EnClassifyEditReq struct {
	ClassifyId int `description:"分类ID"`
	EnClassifyAddReq
}
