package help_doc

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

type HelpDocClassify struct {
	ClassifyId      int       `orm:"column(classify_id);pk"`
	ClassifyName    string    `description:"分类名称"`
	ParentId        int       `description:"父级id"`
	CreateTime      time.Time `description:"创建时间"`
	ModifyTime      time.Time `description:"修改时间"`
	SysUserId       int       `description:"创建人id"`
	SysUserRealName string    `description:"创建人姓名"`
	Level           int       `description:"层级"`
	Sort            int       `description:"排序字段,越小越靠前,默认值:10"`
}

func AddHelpDocClassify(item *HelpDocClassify) (lastId int64, err error) {
	o := orm.NewOrm()
	lastId, err = o.Insert(item)
	return
}

type AddHelpDocClassifyReq struct {
	HelpDocClassifyName string `description:"分类名称"`
	ParentId            int    `description:"父级id,第一级传0"`
	Level               int    `description:"层级,第一级传0,其余传上一级的层级"`
}

func GetHelpDocClassifyCount(HelpDocClassifyName string, parentId int) (count int, err error) {
	o := orm.NewOrm()
	sql := `SELECT COUNT(1) AS count FROM help_doc_classify WHERE parent_id=? AND classify_name=? `
	err = o.Raw(sql, parentId, HelpDocClassifyName).QueryRow(&count)
	return
}

type EditHelpDocClassifyReq struct {
	HelpDocClassifyName string `description:"分类名称"`
	HelpDocClassifyId   int    `description:"分类id"`
}

func GetHelpDocClassifyById(classifyId int) (item *HelpDocClassify, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM help_doc_classify WHERE classify_id=? `
	err = o.Raw(sql, classifyId).QueryRow(&item)
	return
}

func GetHelpDocClassifyCountById(classifyId int) (count int, err error) {
	o := orm.NewOrm()
	sql := `SELECT count(1) AS count FROM help_doc_classify WHERE classify_id=? `
	err = o.Raw(sql, classifyId).QueryRow(&count)
	return
}

func EditHelpDocClassify(classifyId int, HelpDocClassifyName string) (err error) {
	o := orm.NewOrm()
	sql := `UPDATE help_doc_classify SET classify_name=?, modify_time=NOW() WHERE classify_id=? `
	_, err = o.Raw(sql, HelpDocClassifyName, classifyId).Exec()
	return
}

// GetHelpDocClassifyByParentId
func GetHelpDocClassifyByParentId(parentId int, keyWord string) (items []HelpDocClassifyItems, err error) {
	o := orm.NewOrm()
	sql := ``
	if keyWord != "" {
		sql = ` SELECT * FROM help_doc_classify WHERE parent_id=? AND (classify_name LIKE '%` + keyWord + `%' ) order by sort asc,classify_id asc`
	} else {
		sql = ` SELECT * FROM help_doc_classify WHERE parent_id=? order by sort asc,classify_id asc`
	}
	_, err = o.Raw(sql, parentId).QueryRows(&items)
	return
}

// GetHelpDocClassifyAll
func GetHelpDocClassifyAll(keyWord string) (items []*HelpDocClassifyItems, err error) {
	o := orm.NewOrm()
	sql := ``
	if keyWord != "" {
		sql = ` SELECT * FROM help_doc_classify WHERE parent_id<>0 AND (classify_name LIKE '%` + keyWord + `%' ) order by sort asc,classify_id asc`
	} else {
		sql = ` SELECT * FROM help_doc_classify WHERE parent_id<>0 order by sort asc,classify_id asc`
	}
	_, err = o.Raw(sql).QueryRows(&items)
	return
}

type HelpDocClassifyItems struct {
	ClassifyId         int `description:"分类id"`
	ClassifyName       string
	ParentId           int
	Level              int    `description:"层级"`
	Sort               int    `description:"排序字段,越小越靠前,默认值:10"`
	SysUserId          int    `description:"创建人id"`
	SysUserRealName    string `description:"创建人姓名"`
	VisibleBusinessIds string
	Disabled           bool `description:"是否能选 true不能 false可以"`
	Children           []*HelpDocClassifyItems
}

type HelpDocClassifyListResp struct {
	AllNodes      []*HelpDocClassifyItems
	TwoLevelNodes []*HelpDocClassifyItems
}

type HelpDocClassifyDeleteCheckResp struct {
	DeleteStatus int    `description:"检测状态:0:默认值,如果为0,继续走其他校验,1:该分类下关联不可删除,2:确认删除当前目录及包含的子目录吗"`
	TipsMsg      string `description:"提示信息"`
}

type HelpDocClassifyDeleteCheckReq struct {
	HelpDocClassifyId int `description:"分类id"`
	ChartInfoId       int `description:"指标id"`
}

func GetHelpDocClassifyByCondition(condition string, pars []interface{}) (item *HelpDocClassify, err error) {
	o := orm.NewOrm()
	sql := ` SELECT * FROM help_doc_classify WHERE 1=1 `
	if condition != "" {
		sql += condition
	}
	err = o.Raw(sql, pars).QueryRow(&item)
	return
}

// MoveHelpDocClassifyReq 移动分类请求参数
type MoveHelpDocClassifyReq struct {
	ClassifyId       int `description:"分类id"`
	ParentClassifyId int `description:"父级分类id"`
	PrevClassifyId   int `description:"上一个兄弟节点分类id"`
	NextClassifyId   int `description:"下一个兄弟节点分类id"`
}

// GetFirstHelpDocClassifyByParentId 获取当前父级分类下的排序第一条的数据
func GetFirstHelpDocClassifyByParentId(parentId int) (item *HelpDocClassify, err error) {
	o := orm.NewOrm()
	sql := ` SELECT * FROM help_doc_classify WHERE parent_id=? order by sort asc,classify_id asc limit 1`
	err = o.Raw(sql, parentId).QueryRow(&item)
	return
}

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

// Update 更新分类基础信息
func (HelpDocClassify *HelpDocClassify) Update(cols []string) (err error) {
	o := orm.NewOrm()
	_, err = o.Update(HelpDocClassify, cols...)
	return
}

// GetHelpDocClassifyMaxSort 获取分类下最大的排序数
func GetHelpDocClassifyMaxSort(parentId int) (sort int, err error) {
	o := orm.NewOrm()
	sql := `SELECT Max(sort) AS sort FROM help_doc_classify WHERE parent_id=?  `
	err = o.Raw(sql, parentId).QueryRow(&sort)
	return
}

type HelpDocClassifyView struct {
	HelpDocClassifyId   int    `orm:"column(classify_id);pk"`
	HelpDocClassifyName string `description:"分类名称"`
	ParentId            int    `description:"父级id"`
}

func EditHelpDocClassifySysUser(classifyId, sysUserId int, HelpDocClassifyName string) (err error) {
	o := orm.NewOrm()
	sql := `UPDATE help_doc_classify SET sys_user_id=?,sys_user_real_name=?,modify_time=NOW() WHERE parent_id=?  AND level=3 `
	_, err = o.Raw(sql, sysUserId, HelpDocClassifyName, classifyId).Exec()
	return
}

func GetHelpDocClassifyChildCounts(parentId int) (count int, err error) {
	o := orm.NewOrm()
	sql := `SELECT COUNT(1) AS count FROM help_doc_classify WHERE parent_id=? `
	err = o.Raw(sql, parentId).QueryRow(&count)
	return
}

func GetHelpDocCounts(classifyId int) (count int, err error) {
	o := orm.NewOrm()
	sql := `SELECT COUNT(1) AS count FROM help_doc WHERE classify_id=? `
	err = o.Raw(sql, classifyId).QueryRow(&count)
	return
}

// DeleteHelpDocClassify 删除分类
func DeleteHelpDocClassify(classifyId int) (err error) {
	o := orm.NewOrm()
	sql := ` DELETE FROM help_doc_classify WHERE classify_id =? `
	_, err = o.Raw(sql, classifyId).Exec()
	return
}

// MoveClassifyReq 移动分类请求参数
type MoveClassifyReq struct {
	ClassifyId       int `description:"分类id"`
	ParentClassifyId int `description:"父级分类id"`
	PrevClassifyId   int `description:"上一个兄弟节点分类id"`
	NextClassifyId   int `description:"下一个兄弟节点分类id"`
}

// GetHelpDocClassifyLevel
func GetHelpDocClassifyLevel() (items []*HelpDocClassifyItems, err error) {
	o := orm.NewOrm()
	sql := ``
	sql = ` SELECT * FROM help_doc_classify WHERE level IN (1,2) `
	_, err = o.Raw(sql).QueryRows(&items)
	return
}

type EditHelpDocClassifyVisibleReq struct {
	HelpDocClassifyId  int `description:"分类id"`
	VisibleBusinessIds string
}

func EditHelpDocClassifyVisible(classifyId int, visibleBusinessIds string) (err error) {
	o := orm.NewOrm()
	sql := `UPDATE help_doc_classify SET visible_business_ids=?, modify_time=NOW() WHERE classify_id=? `
	_, err = o.Raw(sql, visibleBusinessIds, classifyId).Exec()
	return
}

func AddAllHelpDocClassifyVisible(businessId int) (err error) {
	o := orm.NewOrm()
	sql := `UPDATE help_doc_classify set visible_business_ids=CONCAT(visible_business_ids,",", ?)  where visible_business_ids !=""`
	_, err = o.Raw(sql, businessId).Exec()
	if err != nil {
		return
	}
	sql = `UPDATE help_doc_classify set visible_business_ids=CONCAT(visible_business_ids, ?)  where visible_business_ids =""`
	_, err = o.Raw(sql, businessId).Exec()
	return
}

func GetAllHelpDocClassify() (items []*HelpDocClassifyItems, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM help_doc_classify `
	_, err = o.Raw(sql).QueryRows(&items)
	return
}

func GetAllHelpDocClassifyByKeyword(keyword string) (items []*HelpDocClassifyItems, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM help_doc_classify WHERE classify_name LIKE '%` + keyword + `%' `
	_, err = o.Raw(sql).QueryRows(&items)
	return
}