package models

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

type CygxIndustryMap struct {
	IndustryMapId   int `orm:"column(industry_map_id);pk" description:"行业图谱id"`
	IndustryMapName string
	ParentId        int
	CreateTime      time.Time
}

func GetCygxIndustryMapByName(industryName string) (item *CygxIndustryMap, err error) {
	o := orm.NewOrm()
	sql := ` SELECT * FROM cygx_industry_map WHERE industry_map_name=? ORDER BY parent_id ASC LIMIT 1 `
	err = o.Raw(sql, industryName).QueryRow(&item)
	return
}

func GetCygxIndustryMapAll() (items []*CygxIndustryMapItems, err error) {
	o := orm.NewOrm()
	sql := ` SELECT * FROM cygx_industry_map WHERE parent_id<>0`
	_, err = o.Raw(sql).QueryRows(&items)
	return
}

type CygxIndustryMapItems struct {
	IndustryMapId   int `orm:"column(industry_map_id);" description:"行业图谱id"`
	IndustryMapName string
	ParentId        int
	Level           int
	Children        []*CygxIndustryMapItems
}

func GetCygxIndustryMapByParentId(parentId int) (items []*CygxIndustryMapItems, err error) {
	o := orm.NewOrm()
	sql := ` SELECT * FROM cygx_industry_map WHERE parent_id=?`
	_, err = o.Raw(sql, parentId).QueryRows(&items)
	return
}

func GetCygxIndustryMapByLevel(level int) (items []*CygxIndustryMapItems, err error) {
	o := orm.NewOrm()
	sql := ` SELECT * FROM cygx_industry_map WHERE level=?`
	_, err = o.Raw(sql, level).QueryRows(&items)
	return
}

//根据名称模糊匹配获取一条数据
func GetFirstCygxIndustryItemByName(industryName string) (items *CygxIndustryMapItems, err error) {
	o := orm.NewOrm()
	sql := ` SELECT * FROM cygx_industry_map WHERE industry_map_name like '%` + industryName + `%' order by industry_map_id asc`
	err = o.Raw(sql).QueryRow(&items)
	return
}

//根据名称模糊匹配获取一条数据
func GetFirstCygxIndustryListByName(industryName string) (list []*CygxIndustryMapItems, err error) {
	o := orm.NewOrm()
	sql := ` SELECT * FROM cygx_industry_map WHERE industry_map_name like '%` + industryName + `%' order by level,industry_map_id asc`
	_, err = o.Raw(sql).QueryRows(&list)
	return
}

//根据名称模糊匹配获取一条数据
func FixLevelData(industryMapId int, level int) (list []*CygxIndustryMapItems, err error) {
	o := orm.NewOrm()
	sql := ` UPDATE cygx_industry_map set level = ? WHERE industry_map_id = ?`
	_, err = o.Raw(sql, level, industryMapId).QueryRows(&list)
	return
}

//func
