package cygx

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
	Level           int `description:"层级深度"`
	CreateTime      time.Time
	ModifyTime      time.Time
}

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

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

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

type CygxIndustryMapResp struct {
	Node *CygxIndustryMapItems
}

// 新增图谱
type IndustryMapAddReq struct {
	IndustryMapId   int    `orm:"column(industry_map_id);" description:"行业图谱id"`
	IndustryMapName string `description:"图谱名称"`
	Level           int    `description:"层级深度"`
}

func GetCygxIndustryMapByName(name string) (count int, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := ` SELECT COUNT(1) AS count FROM cygx_industry_map WHERE industry_map_name=?`
	err = o.Raw(sql, name).QueryRow(&count)
	return
}

func AddCygxIndustryMap(item *CygxIndustryMap) (err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	_, err = o.Insert(item)
	return
}

// 编辑图谱
type IndustryMapEditReq struct {
	IndustryMapId   int    `orm:"column(industry_map_id);" description:"行业图谱id"`
	IndustryMapName string `description:"图谱名称"`
}

func GetCygxIndustryMapById(industryMapId int) (items *CygxIndustryMapItems, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := ` SELECT * FROM cygx_industry_map WHERE industry_map_id=?`
	err = o.Raw(sql, industryMapId).QueryRow(&items)
	return
}

func EditCygxIndustryMap(item *IndustryMapEditReq) (err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `UPDATE cygx_industry_map SET industry_map_name=?,modify_time = NOW()  WHERE industry_map_id=? `
	_, err = o.Raw(sql, item.IndustryMapName, item.IndustryMapId).Exec()
	return
}

// 编辑图谱
type IndustryMapDeleteReq struct {
	IndustryMapId int `orm:"column(industry_map_id);" description:"行业图谱id"`
}

func DeleteCygxIndustryMap(industryMapId int) (err error) {
	o := orm.NewOrm()
	sql := `DELETE FROM cygx_industry_map WHERE industry_map_id=? `
	_, err = o.Raw(sql, industryMapId).Exec()
	return
}

// 列表
func GetIndustrialMapListName(condition string) (items []*IndustryMapAddReq, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT * FROM cygx_industry_map  WHERE 1= 1 `
	if condition != "" {
		sql += condition
	}
	sql += ` GROUP BY  industry_map_name `
	_, err = o.Raw(sql).QueryRows(&items)
	return
}
