package data_manage

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

// EdbTerminal 指标终端
type EdbTerminal struct {
	TerminalId   int       `orm:"column(terminal_id);pk"`
	Source       int       `orm:"column(source)" description:"指标来源类型"`
	Name         string    `description:"终端名称"`
	TerminalCode string    `description:"终端编码，用于配置在机器上"`
	ServerUrl    string    `description:"终端地址"`
	DirPath      string    `description:"终端存放的文件夹路径"`
	Num          int       `description:"终端最大指标数"`
	Status       int       `description:"状态，1启用，2禁用"`
	Value        string    `description:"终端相关的token"`
	ModifyTime   time.Time `description:"修改时间"`
	CreateTime   time.Time `description:"创建时间"`
}

type EdbTerminalItem struct {
	TerminalId   int    `orm:"column(terminal_id);pk"`
	Source       int    `description:"指标来源类型"`
	Name         string `description:"终端别名"`
	TerminalCode string `description:"终端编码，用于配置在机器上"`
	ServerUrl    string `description:"终端地址"`
	DirPath      string `description:"终端存放的文件夹路径"`
	Num          int    `description:"终端最大指标数"`
	Status       int    `description:"状态，1启用，2禁用"`
	Value        string `description:"终端相关的token"`
	ModifyTime   string `description:"修改时间"`
	CreateTime   string `description:"创建时间"`
}

type EdbTerminalCode struct {
	TerminalCode string `description:"终端编码，用于配置在机器上"`
	Source       int    `description:"指标来源类型"`
	Name         string `description:"终端别名"`
}

// GetEdbTerminalListBySource 根据指标来源类型获取所有的终端列表
func GetEdbTerminalListBySource(source int) (items []*EdbTerminal, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := ` SELECT *  FROM edb_terminal WHERE source = ? and status=1 ORDER BY terminal_id ASC `
	_, err = o.Raw(sql, source).QueryRows(&items)
	return
}

type BaseIndexTerminalCode struct {
	TerminalCode string `description:"终端编码，用于配置在机器上"`
	IndexName    string
}

type EdbTerminalListResp struct {
	List []*EdbTerminalItem `description:"列表数据"`
}

type EdbTerminalCodeResp struct {
	List []*EdbTerminalCode `description:"终端编码列表"`
}

type AddEdbTerminalListReq struct {
	TerminalId int    `description:"终端id"`
	Source     int    `description:"指标来源类型"`
	Name       string `description:"终端别名"`
	//TerminalCode string `description:"终端编码，用于配置在机器上"`
	ServerUrl string `description:"终端地址"`
	DirPath   string `description:"终端存放的文件夹路径"`
	Num       int    `description:"终端最大指标数"`
	//Status    int    `description:"状态，1启用，2禁用"`
	Value string `description:"终端相关的token"`
}

type SetEdbTerminalStatusReq struct {
	TerminalId int `description:"终端id"`
	Status     int `description:"状态，1启用，2禁用"`
}

// GetBaseIndexTerminalCode 获取数据源的终端code
func GetBaseIndexTerminalCode(edbCode, tableName string) (item BaseIndexTerminalCode, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := fmt.Sprintf(`select terminal_code, index_name from %s where index_code = ? `, tableName)
	err = o.Raw(sql, edbCode).QueryRow(&item)
	return
}

// GetEdbTerminalById 主键获取终端
func GetEdbTerminalById(id int) (item *EdbTerminal, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `SELECT * FROM edb_terminal WHERE terminal_id = ? LIMIT 1`
	err = o.Raw(sql, id).QueryRow(&item)
	return
}

func GetEdbTerminalByTerminalCode(terminalCode string) (item *EdbTerminal, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `SELECT * FROM edb_terminal WHERE terminal_code = ? LIMIT 1`
	err = o.Raw(sql, terminalCode).QueryRow(&item)
	return
}

func GetEdbTerminalList() (item []*EdbTerminalItem, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := ` SELECT * FROM edb_terminal ORDER BY terminal_id ASC`
	_, err = o.Raw(sql).QueryRows(&item)
	return
}

func GetEdbTerminalBySource(source int) (item []*EdbTerminalItem, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := ` SELECT * FROM edb_terminal where source=? ORDER BY terminal_id ASC`
	_, err = o.Raw(sql, source).QueryRows(&item)
	return
}

func (item *EdbTerminal) Add() (err error) {
	o := orm.NewOrmUsingDB("data")
	_, err = o.Insert(item)
	return
}

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

// GetEdbTerminalByCode 根据终端编码获取终端信息
func GetEdbTerminalByCode(terminalCode string) (item *EdbTerminal, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := ` SELECT *  FROM edb_terminal WHERE terminal_code = ? `
	err = o.Raw(sql, terminalCode).QueryRow(&item)
	return
}

type TerminalCodeCountGroup struct {
	TerminalCode string
	Total        int
}

// GetEdbCountGroupByTerminal 获取终端code分组总数
func GetEdbCountGroupByTerminal(source int) (list []TerminalCodeCountGroup, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `select terminal_code,count(1) total from edb_info where source = ? AND no_update=0 AND terminal_code != "" group by terminal_code; `
	_, err = o.Raw(sql, source).QueryRows(&list)
	return
}
