package data_manage

import (
	"eta/eta_mobile/utils"
	"github.com/beego/beego/v2/client/orm"
	"github.com/rdlucklib/rdluck_tools/paging"
	"time"
)

type BaseFromMtjhMapping struct {
	BaseFromMtjhMappingId int       `orm:"column(base_from_mtjh_mapping_id);pk"`
	IndexName             string    `description:"持买单量指标名称"`
	IndexCode             string    `description:"持买单量指标编码"`
	CreateTime            time.Time `description:"时间"`
	Area                  string    `description:"区域"`
	Port                  string    `description:"港口或码头"`
	Variety               string    `description:"品种"`
	Unit                  string    `description:"单位"`
	Frequency             string    `description:"频率"`
}

type BaseFromMtjhIndex struct {
	BaseFromMtjhIndexId int       `orm:"column(base_from_mtjh_index_id);pk"`
	IndexName           string    `description:"持买单量指标名称"`
	IndexCode           string    `description:"持买单量指标编码"`
	DealValue           string    `description:"成交量"`
	DataTime            string    `description:"数据日期"`
	Area                string    `description:"区域"`
	Port                string    `description:"港口或码头"`
	Unit                string    `description:"单位"`
	Frequency           string    `description:"频率"`
	Variety             string    `description:"品种"`
	CreateTime          time.Time `description:"插入时间"`
	ModifyTime          time.Time `description:"修改时间"`
}

type BaseFromMtjhIndexItem struct {
	BaseFromMtjhIndexId int    `orm:"column(base_from_mtjh_index_id);pk"`
	IndexName           string `description:"持买单量指标名称"`
	IndexCode           string `description:"持买单量指标编码"`
	DealValue           string `description:"成交量"`
	DataTime            string `description:"数据日期"`
	Area                string `description:"区域"`
	Port                string `description:"港口或码头"`
	Unit                string `description:"单位"`
	Frequency           string `description:"频率"`
	Variety             string `description:"品种"`
	CreateTime          string `description:"插入时间"`
	ModifyTime          string `description:"修改时间"`
}

// 查询指标
func GetBaseFromMtjhMapping() (items []*BaseFromMtjhMapping, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM base_from_mtjh_mapping`
	_, err = o.Raw(sql).QueryRows(&items)
	return
}

// 查询指标
func GetBaseFromMtjhIndex() (items []*BaseFromMtjhIndex, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM base_from_mtjh_index`
	_, err = o.Raw(sql).QueryRows(&items)
	return
}

// 添加数据
func AddBaseFromMtjhIndex(item *BaseFromMtjhIndex) (lastId int64, err error) {
	o := orm.NewOrm()
	lastId, err = o.Insert(item)
	return
}

func AddBaseFromMtjhIndexMuti(items []*BaseFromMtjhIndex) (lastId int64, err error) {
	o := orm.NewOrm()
	lastId, err = o.InsertMulti(500, items)
	return
}

// 添加指标
func AddBaseFromMtjhMapping(item *BaseFromMtjhMapping) (lastId int64, err error) {
	o := orm.NewOrm()
	lastId, err = o.Insert(item)
	return
}

func AddBaseFromMtjhMappingMuti(items []*BaseFromMtjhMapping) (lastId int64, err error) {
	o := orm.NewOrm()
	lastId, err = o.InsertMulti(500, items)
	return
}

func UpdateBaseFromMtjhIndex(item *BaseFromMtjhIndex) (err error) {
	o := orm.NewOrm()
	sql := `UPDATE base_from_mtjh_index SET deal_value=?  WHERE index_name=?  AND  data_time = ?`
	_, err = o.Raw(sql, item.DealValue, item.IndexName, item.DataTime).Exec()
	return
}

func GetMtjhClassifyList() (list []*BaseFromMtjhMapping, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := "SELECT * FROM base_from_mtjh_mapping group by area"
	_, err = o.Raw(sql).QueryRows(&list)
	return
}

type BaseFromMtjhMappingItem struct {
	BaseFromMtjhMappingId int    `orm:"column(base_from_mtjh_mapping_id);pk"`
	IndexName             string `description:"持买单量指标名称"`
	IndexCode             string `description:"持买单量指标编码"`
	CreateTime            string `description:"时间"`
	Area                  string `description:"区域"`
	Port                  string `description:"港口或码头"`
	Variety               string `description:"品种"`
	Unit                  string `description:"单位"`
	Frequency             string `description:"频率"`
}

func GetMtjhMapping(condition string, pars interface{}) (items []*BaseFromMtjhMappingItem, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := ` SELECT * FROM base_from_mtjh_mapping WHERE 1=1  `
	if condition != "" {
		sql += condition
	}
	sql += ` ORDER BY base_from_mtjh_mapping_id asc`
	_, err = o.Raw(sql, pars).QueryRows(&items)
	return
}

type BaseFromMtjhIndexList struct {
	BaseFromMtjhMappingId int    `orm:"column(base_from_mtjh_mapping_id);pk"`
	IndexName             string `description:"持买单量指标名称"`
	IndexCode             string `description:"持买单量指标编码"`
	Area                  string `description:"区域"`
	Port                  string `description:"港口或码头"`
	Unit                  string `description:"单位"`
	Frequency             string `description:"频率"`
	CreateTime            string `description:"插入时间"`
	ModifyTime            string `description:"修改时间"`
	Variety               string `description:"品种"`
	DataList              []*BaseFromMtjhIndexItem
	Paging                *paging.PagingItem `description:"分页数据"`
}

func GetMtjhIndexDataCount(indexCode string) (count int, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := ` SELECT COUNT(1) AS count  FROM base_from_mtjh_index WHERE index_code=? `
	err = o.Raw(sql, indexCode).QueryRow(&count)
	return
}

func GetMtjhIndexData(indexCode string, startSize, pageSize int) (items []*BaseFromMtjhIndexItem, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := ` SELECT *  FROM base_from_mtjh_index WHERE index_code=? ORDER BY data_time DESC LIMIT ?,? `
	_, err = o.Raw(sql, indexCode, startSize, pageSize).QueryRows(&items)
	return
}

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

// 查询数据
func GetBaseFromMtjhIndexByCode(indexCode string) (items []*BaseFromMtjhIndexItem, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `SELECT * FROM base_from_mtjh_index WHERE index_code=? ORDER BY data_time DESC `
	_, err = o.Raw(sql, indexCode).QueryRows(&items)
	return
}

// GetMtjhMappingItemByCode
func GetMtjhMappingItemByCode(indexCode string) (item *BaseFromMtjhMappingItem, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := "SELECT * FROM base_from_mtjh_mapping WHERE index_code=? "
	err = o.Raw(sql, indexCode).QueryRow(&item)
	return
}

// GetMtjhFrequencyByArea
func GetMtjhFrequencyByArea(area string) (items []*string, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := "SELECT frequency FROM base_from_mtjh_index WHERE area=?  group by area "
	_, err = o.Raw(sql, area).QueryRows(&items)
	return
}

func GetClassifyMtjhByArea(area string) (items []*string, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `SELECT DISTINCT index_code FROM base_from_mtjh_index WHERE area=? `
	_, err = o.Raw(sql, area).QueryRows(&items)
	return
}

func GetCoalMtjhMaxCount(area 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_mtjh_index 
	WHERE
		area =? 
GROUP BY
	index_name ) AS t `
	err = o.Raw(sql, area).QueryRow(&count)
	return
}

func GetMtjhCount(indexCode string) (count int, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `SELECT
		COUNT( 1 ) AS num 
	FROM
		base_from_mtjh_index
	WHERE
		index_code =? `
	err = o.Raw(sql, indexCode).QueryRow(&count)
	return
}

func GetMtjhIndexLatestDate(indexCode string) (ModifyTime string, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := ` SELECT modify_time FROM base_from_mtjh_index WHERE index_code=? ORDER BY modify_time DESC limit 1 `
	err = o.Raw(sql, indexCode).QueryRow(&ModifyTime)
	return
}
