package data_manage

import (
	"eta/eta_api/utils"
	"fmt"
	"github.com/beego/beego/v2/client/orm"
)

type BaseFromCoalmineMapping struct {
	BaseFromCoalmineMappingId int    `orm:"column(base_from_coalmine_mapping_id);pk"`
	IndexName                 string `description:"持买单量指标名称"`
	IndexCode                 string `description:"持买单量指标编码"`
	CreateTime                string `description:"时间"`
}

// GetCoalItemList 模糊查询Smm数据库指标列表
func GetCoalItemList(keyword string) (items []*BaseFromCoalmineMapping, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := "SELECT * FROM base_from_coalmine_mapping WHERE CONCAT(index_name,index_code) LIKE ? "
	_, err = o.Raw(sql, utils.GetLikeKeyword(keyword)).QueryRows(&items)
	return
}

func GetCoalCompanyMaxCount(suffix, classifyName string) (count int, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `SELECT
	MAX( t.num ) AS count 
FROM
	(
	SELECT
		COUNT( 1 ) AS num 
	FROM
		base_from_coalmine_%s 
	WHERE
		group_name =? 
GROUP BY
	index_name ) AS t `
	sql = fmt.Sprintf(sql, suffix)
	err = o.Raw(sql, classifyName).QueryRow(&count)
	return
}

func GetCoalJsmMaxCount(classifyName string) (count int, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `SELECT
	MAX( t.num ) AS count 
FROM
	(
	SELECT
		COUNT( 1 ) AS num 
	FROM
		base_from_coalmine_jsm_index 
	WHERE
		province =? 
GROUP BY
	index_name ) AS t `
	err = o.Raw(sql, classifyName).QueryRow(&count)
	return
}

func GetCoalCount(suffix, indexCode string) (count int, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `SELECT
		COUNT( 1 ) AS num 
	FROM
		base_from_coalmine_%s 
	WHERE
		index_code =? `
	sql = fmt.Sprintf(sql, suffix)
	err = o.Raw(sql, indexCode).QueryRow(&count)
	return
}

type BaseFromCoalmineIndex struct {
	BaseFromCoalmineCompanyIndexId int
	IndexName                      string `description:"持买单量指标名称"`
	IndexCode                      string `description:"持买单量指标编码"`
	DealValue                      string `description:"成交量"`
	DataTime                       string `description:"数据日期"`
	Source                         string `description:"来源"`
	Province                       string `description:"省份"`
	City                           string `description:"城市"`
	GroupName                      string `description:"集团名称"`
	Unit                           string `description:"单位"`
	Frequency                      string `description:"频率"`
	CreateTime                     string `description:"插入时间"`
	ModifyTime                     string `description:"修改时间"`
}

func GetBaseFromCoalIndexByCode(suffix, indexCode string) (items []*BaseFromCoalmineIndex, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `SELECT * FROM base_from_coalmine_%s WHERE index_code=? ORDER BY data_time DESC`
	sql = fmt.Sprintf(sql, suffix)
	_, err = o.Raw(sql, indexCode).QueryRows(&items)
	return
}

func GetClassifyFromCoalByGroupName(suffix, groupName string) (items []*string, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `SELECT DISTINCT index_code FROM base_from_coalmine_%s WHERE group_name=? `
	sql = fmt.Sprintf(sql, suffix)
	_, err = o.Raw(sql, groupName).QueryRows(&items)
	return
}
