package models

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

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

type BaseFromCoalmineJsmIndex struct {
	BaseFromCoalmineJsmIndexId int       `orm:"column(base_from_coalmine_jsm_index_id);pk"`
	IndexName                  string    `description:"持买单量指标名称"`
	IndexCode                  string    `description:"持买单量指标编码"`
	Exchange                   string    `description:"样本统计类别"`
	DealValue                  string    `description:"成交量"`
	DataTime                   string    `description:"数据日期"`
	Source                     string    `description:"来源"`
	Province                   string    `description:"省份"`
	Description                string    `description:"描述"`
	Unit                       string    `description:"单位"`
	Frequency                  string    `description:"频率"`
	CreateTime                 time.Time `description:"插入时间"`
	ModifyTime                 time.Time `description:"修改时间"`
}

type BaseFromCoalmineCompanyIndex struct {
	BaseFromCoalmineCompanyIndexId int       `orm:"column(base_from_coalmine_company_index_id);pk"`
	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                     time.Time `description:"插入时间"`
	ModifyTime                     time.Time `description:"修改时间"`
}

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

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

// 查询数据
func GetBaseFromCoalmineIndex() (items []*BaseFromCoalmineJsmIndex, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM base_from_coalmine_jsm_index`
	_, err = o.Raw(sql).QueryRows(&items)
	return
}

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

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

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

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

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

func (m *BaseFromCoalmineMapping) GetMaxAndMinDateByIndexCode(indexCode string) (item *EdbInfoMaxAndMinInfo, err error) {
	o := orm.NewOrm()
	sql := ` SELECT MIN(data_time) AS min_date,MAX(data_time) AS max_date,MIN(value) AS min_value,MAX(value) AS max_value FROM base_from_sci99_data WHERE index_code=? `
	err = o.Raw(sql, indexCode).QueryRow(&item)
	if err != nil {
		return
	}

	// 获取最新值
	var latest_value float64
	sql = ` SELECT value AS latest_value FROM base_from_sci99_data WHERE index_code=? ORDER BY data_time DESC LIMIT 1 `
	err = o.Raw(sql, indexCode).QueryRow(&latest_value)
	if err != nil {
		return
	}
	item.LatestValue = latest_value

	return
}

func (m *BaseFromCoalmineMapping) ModifyIndexMaxAndMinDate(indexCode string, item *EdbInfoMaxAndMinInfo) (err error) {
	o := orm.NewOrm()
	sql := ` UPDATE base_from_sci99_index SET start_date=?,end_date=?,latest_value=?,modify_time=NOW() WHERE index_code=? `
	_, err = o.Raw(sql, item.MinDate, item.MaxDate, item.LatestValue, indexCode).Exec()
	return
}

type CoalMineDataReq struct {
	SheetData []SheetData
}

type SheetData struct {
	Name     string
	Rows     []Row
	Cols     []*Col
	MaxRow   int
	MaxCol   int
	Hidden   bool
	Selected bool
}

type Row struct {
	Cells        []Cell
	Hidden       bool
	Height       float64
	OutlineLevel uint8
	isCustom     bool
}

type Col struct {
	Min          int
	Max          int
	Hidden       bool
	Width        float64
	Collapsed    bool
	OutlineLevel uint8
	numFmt       string
}

type Cell struct {
	Value string
}