package data_manage

import (
	"eta/eta_mobile/utils"
	"fmt"
	"github.com/beego/beego/v2/client/orm"
	"strings"
	"time"
)

// ChartFrameworkNode 图库框架节点表
type ChartFrameworkNode struct {
	ChartFrameworkNodeId int       `orm:"column(chart_framework_node_id);pk"`
	ChartFrameworkId     int       `description:"框架ID"`
	FrameworkName        string    `description:"框架名称"`
	NodeId               string    `description:"节点ID"`
	NodeName             string    `description:"节点名称"`
	MyChartClassifyId    int       `description:"我的图表分类ID"`
	CreateTime           time.Time `description:"创建时间"`
}

func (m *ChartFrameworkNode) TableName() string {
	return "chart_framework_node"
}

func (m *ChartFrameworkNode) PrimaryId() string {
	return "chart_framework_node_id"
}

func (m *ChartFrameworkNode) Create() (err error) {
	o := orm.NewOrmUsingDB("data")
	id, err := o.Insert(m)
	if err != nil {
		return
	}
	m.ChartFrameworkNodeId = int(id)
	return
}

func (m *ChartFrameworkNode) CreateMulti(items []*ChartFrameworkNode) (err error) {
	if len(items) == 0 {
		return
	}
	o := orm.NewOrmUsingDB("data")
	_, err = o.InsertMulti(len(items), items)
	return
}

func (m *ChartFrameworkNode) Update(cols []string) (err error) {
	o := orm.NewOrmUsingDB("data")
	_, err = o.Update(m, cols...)
	return
}

func (m *ChartFrameworkNode) Del() (err error) {
	o := orm.NewOrmUsingDB("data")
	sql := fmt.Sprintf(`DELETE FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.PrimaryId())
	_, err = o.Raw(sql, m.ChartFrameworkNodeId).Exec()
	return
}

func (m *ChartFrameworkNode) GetItemById(id int) (item *ChartFrameworkNode, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.PrimaryId())
	err = o.Raw(sql, id).QueryRow(&item)
	return
}

func (m *ChartFrameworkNode) GetItemByCondition(condition string, pars []interface{}) (item *ChartFrameworkNode, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := fmt.Sprintf(`SELECT * FROM %s WHERE 1=1 %s LIMIT 1`, m.TableName(), condition)
	err = o.Raw(sql, pars).QueryRow(&item)
	return
}

func (m *ChartFrameworkNode) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE 1=1 %s`, m.TableName(), condition)
	err = o.Raw(sql, pars).QueryRow(&count)
	return
}

func (m *ChartFrameworkNode) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*ChartFrameworkNode, err error) {
	o := orm.NewOrmUsingDB("data")
	fields := strings.Join(fieldArr, ",")
	if len(fieldArr) == 0 {
		fields = `*`
	}
	order := `ORDER BY create_time DESC`
	if orderRule != "" {
		order = ` ORDER BY ` + orderRule
	}
	sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
	_, err = o.Raw(sql, pars).QueryRows(&items)
	return
}

func (m *ChartFrameworkNode) GetPageItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string, startSize, pageSize int) (items []*ChartFrameworkNode, err error) {
	o := orm.NewOrmUsingDB("data")
	fields := strings.Join(fieldArr, ",")
	if len(fieldArr) == 0 {
		fields = `*`
	}
	order := `ORDER BY create_time DESC`
	if orderRule != "" {
		order = ` ORDER BY ` + orderRule
	}
	sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s LIMIT ?,?`, fields, m.TableName(), condition, order)
	_, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
	return
}

// ChartFrameworkNodeItem 图库框架节点信息
type ChartFrameworkNodeItem struct {
	ChartFrameworkNodeId int
	ChartFrameworkId     int    `description:"框架ID"`
	FrameworkName        string `description:"框架名称"`
	NodeId               string `description:"节点ID"`
	NodeName             string `description:"节点名称"`
	MyChartClassifyId    int    `description:"我的图表分类ID"`
	ChartNum             int    `description:"分类下的图表数"`
	CreateTime           string `description:"创建时间"`
}

// FormatChartFrameworkNode2Item 格式化框架节点信息
func FormatChartFrameworkNode2Item(origin *ChartFrameworkNode, chartNum int) (item *ChartFrameworkNodeItem) {
	if origin == nil {
		return
	}
	item = new(ChartFrameworkNodeItem)
	item.ChartFrameworkNodeId = origin.ChartFrameworkNodeId
	item.ChartFrameworkId = origin.ChartFrameworkId
	item.FrameworkName = origin.FrameworkName
	item.NodeId = origin.NodeId
	item.NodeName = origin.NodeName
	item.MyChartClassifyId = origin.MyChartClassifyId
	item.ChartNum = chartNum
	item.CreateTime = utils.TimeTransferString(utils.FormatDateTime, origin.CreateTime)
	return
}
