package data_manage

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

type EdbdataClassify struct {
	ClassifyId   int       `orm:"column(classify_id);pk" description:"分类id"`
	ClassifyName string    `description:"分类名称"`
	ParentId     int       `description:"父级id"`
	CreateTime   time.Time `description:"创建时间"`
	Sort         int       `description:"排序"`
	IsShow       int       `description:"是否显示"`
}

type EdbdataClassifyList struct {
	ClassifyId   int       `description:"分类id"`
	ClassifyName string    `description:"分类名称"`
	ParentId     int       `description:"父级id"`
	CreateTime   time.Time `description:"创建时间"`
	Sort         int       `description:"排序"`
	IsShow       int       `description:"是否显示"`
	Total        int       `description:"指标总数"`
}

func AddEdbdataClassify(item *EdbdataClassify) (lastId int64, err error) {
	o := orm.NewOrmUsingDB("edb")
	lastId, err = o.Insert(item)
	return
}

type ManualClassifyList struct {
	ClassifyId   int
	ClassifyName string
	ParentId     int
	CheckList    []int
	Child        []*ManualClassifyList
}

func GetManualClassifyAll(adminId int) (items []*ManualClassifyList, err error) {
	var newItems []*ManualClassifyList
	o := orm.NewOrmUsingDB("edb")
	sql := ` SELECT classify_id,classify_name,parent_id FROM edbdata_classify WHERE parent_id=0 AND is_show=1 ORDER BY sort ASC ,classify_id ASC `
	_, err = o.Raw(sql).QueryRows(&newItems)
	if err != nil {
		return
	}
	classifyLen := len(newItems)
	for i := 0; i < classifyLen; i++ {
		var childItems []*ManualClassifyList
		parentId := newItems[i].ClassifyId
		childSql := ``
		childSql = "SELECT classify_id,classify_name,parent_id FROM edbdata_classify WHERE parent_id=? AND is_show=1 ORDER BY sort ASC ,classify_id ASC "
		_, err = o.Raw(childSql, parentId).QueryRows(&childItems)
		if err != nil {
			return
		}
		checkList := make([]int, 0)
		for _, v := range childItems {
			if v.ParentId > 0 {
				count, err := GetManualUserClassifyCount(adminId, v.ClassifyId)
				if err != nil {
					return items, err
				}
				if count > 0 {
					checkList = append(checkList, v.ClassifyId)
				}
			}
		}
		newItems[i].CheckList = checkList

		newItems[i].Child = childItems
	}

	for _, v := range newItems {
		if v.ParentId > 0 {
			count, err := GetManualUserClassifyCount(adminId, v.ClassifyId)
			if err != nil {
				return items, err
			}
			if count > 0 {
				v.CheckList = append(v.CheckList, v.ClassifyId)
			}
		}
		childLen := len(v.Child)
		if childLen > 0 {
			items = append(items, v)
		}
	}
	return
}

func GetManualClassifyAllV2() (items []*ManualClassifyList, err error) {
	var newItems []*ManualClassifyList
	o := orm.NewOrmUsingDB("edb")
	sql := ` SELECT classify_id,classify_name,parent_id FROM edbdata_classify WHERE parent_id=0 AND is_show=1 ORDER BY sort ASC ,classify_id ASC `
	_, err = o.Raw(sql).QueryRows(&newItems)
	if err != nil {
		return
	}
	classifyLen := len(newItems)
	for i := 0; i < classifyLen; i++ {
		var childItems []*ManualClassifyList
		parentId := newItems[i].ClassifyId
		childSql := ``
		childSql = "SELECT classify_id,classify_name,parent_id FROM edbdata_classify WHERE parent_id=? AND is_show=1 ORDER BY sort ASC ,classify_id ASC "
		_, err = o.Raw(childSql, parentId).QueryRows(&childItems)
		if err != nil {
			return
		}
		newItems[i].Child = childItems
	}

	for _, v := range newItems {
		items = append(items, v)
	}
	return
}

type AddManualClassifyReq struct {
	ClassifyName string `description:"分类名称"`
	ParentId     int    `description:"父级id,第一级传0"`
}

func GetManualClassifyCount(classifyName string, parentId int) (count int, err error) {
	o := orm.NewOrmUsingDB("edb")
	sql := `SELECT COUNT(1) AS count FROM edbdata_classify WHERE parent_id=? AND classify_name=? `
	err = o.Raw(sql, parentId, classifyName).QueryRow(&count)
	return
}

func GetManualClassifyByClassifyName(classifyName string) (item *EdbdataClassify, err error) {
	o := orm.NewOrmUsingDB("edb")
	sql := `SELECT * FROM edbdata_classify WHERE classify_name=? `
	err = o.Raw(sql, classifyName).QueryRow(&item)
	return
}

func ModifyManualClassifyIsShow(isShow, classifyId, parentId int) (err error) {
	o := orm.NewOrmUsingDB("edb")
	sql := `UPDATE edbdata_classify SET is_show=?,parent_id=? WHERE classify_id=? `
	_, err = o.Raw(sql, isShow, parentId, classifyId).Exec()
	return
}

type EditManualClassifyReq struct {
	ClassifyName string `description:"分类名称"`
	ClassifyId   int    `orm:"column(classify_id);pk" description:"分类id"`
	ParentId     int    `description:"上级id"`
}

// DelManualClassifyReq 删除手工指标分类请求
type DelManualClassifyReq struct {
	ClassifyId int `description:"分类id"`
}

func GetManualClassifyById(classifyId int) (item *EdbdataClassify, err error) {
	o := orm.NewOrmUsingDB("edb")
	sql := ` SELECT * FROM edbdata_classify WHERE classify_id=? `
	err = o.Raw(sql, classifyId).QueryRow(&item)
	return
}

func ModifyManualClassifyName(classifyName string, classifyId, parentId int) (err error) {
	o := orm.NewOrmUsingDB("edb")
	updateStr := ` classify_name=? `
	pars := []interface{}{classifyName}
	if parentId > 0 {
		updateStr += ` , parent_id=? `
		pars = append(pars, parentId)
	}
	sql := `UPDATE edbdata_classify SET ` + updateStr + ` WHERE classify_id=? `

	pars = append(pars, classifyId)
	_, err = o.Raw(sql, pars).Exec()
	return
}

func GetManualClassifyAllByCondition(condition string) (items []*EdbdataClassifyList, err error) {
	o := orm.NewOrmUsingDB("edb")
	sql := ` SELECT classify_id,classify_name,parent_id FROM edbdata_classify WHERE is_show=1 `
	if condition != "" {
		sql += condition
	}
	sql += `  ORDER BY sort ASC `
	_, err = o.Raw(sql).QueryRows(&items)
	return
}

// GetChildManualClassifyCount 根据分类id获取下面存在的子分类数量（展示的）
func GetChildManualClassifyCount(parentId int) (count int, err error) {
	o := orm.NewOrmUsingDB("edb")
	sql := `SELECT COUNT(1) AS count FROM edbdata_classify WHERE parent_id=? AND is_show = 1`
	err = o.Raw(sql, parentId).QueryRow(&count)
	return
}
