package data_manage

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

type ExcelStyle struct {
	ExcelId          int `orm:"column(excel_id);pk"`
	Name             string
	Color            string
	Row              int
	Colum            int
	DefaultRowHeight int
	DefaultColWidth  int
	CellData         string
	Config           string
	Data             string
	CreateTime       time.Time
}

type ExcelStyleReq struct {
	ExcelId          int
	Name             string
	Color            string
	Row              int
	Colum            int
	DefaultRowHeight int
	DefaultColWidth  int
	CellData         []interface{}
	Config           interface{}
	Data             [][]interface{}
	Frequency        string `description:"频度"`
	ClassifyId       int    `description:"分类id"`
}

type CellData struct {
	R int
	C int
	V struct {
		Bg interface{} `json:"bg"`
		Bl int         `json:"bl"`
		It int         `json:"it"`
		Ff int         `json:"ff"`
		Fs int         `json:"fs"`
		Fc string      `json:"fc"`
		Ht int         `json:"ht"`
		Vt int         `json:"vt"`
		V  int         `json:"-"`
		Ct struct {
			Fa string `json:"fa"`
			T  string `json:"t"`
		} `json:"ct"`
		M string `json:"m"`
	} `json:"v"`
}

type Data struct {
	Bg interface{} `json:"bg"`
	Bl int         `json:"bl"`
	It int         `json:"it"`
	Ff int         `json:"ff"`
	Fs int         `json:"fs"`
	Fc string      `json:"fc"`
	Ht int         `json:"ht"`
	Vt int         `json:"vt"`
	V  int         `json:"-"`
	Ct struct {
		Fa string `json:"fa"`
		T  string `json:"t"`
	} `json:"ct"`
	M string `json:"m"`
}

type config struct {
	Merge      Merge
	BorderInfo BorderInfo
	Rowlen     Rowlen
}

type Merge struct {
	One35 struct {
		R  int `json:"r"`
		C  int `json:"c"`
		Rs int `json:"rs"`
		Cs int `json:"cs"`
	} `json:"13_5"`
	One37 struct {
		R  int `json:"r"`
		C  int `json:"c"`
		Rs int `json:"rs"`
		Cs int `json:"cs"`
	} `json:"13_7"`
	One42 struct {
		R  int `json:"r"`
		C  int `json:"c"`
		Rs int `json:"rs"`
		Cs int `json:"cs"`
	} `json:"14_2"`
	One510 struct {
		R  int `json:"r"`
		C  int `json:"c"`
		Rs int `json:"rs"`
		Cs int `json:"cs"`
	} `json:"15_10"`
}

type BorderInfo struct {
	RangeType string `json:"rangeType"`
	Value     struct {
		RowIndex int `json:"row_index"`
		ColIndex int `json:"col_index"`
		L        struct {
			Style int    `json:"style"`
			Color string `json:"color"`
		} `json:"l"`
		R struct {
			Style int    `json:"style"`
			Color string `json:"color"`
		} `json:"r"`
		T struct {
			Style int    `json:"style"`
			Color string `json:"color"`
		} `json:"t"`
		B struct {
			Style int    `json:"style"`
			Color string `json:"color"`
		} `json:"b"`
	} `json:"value"`
}

type Rowlen struct {
	Num0  int `json:"0"`
	Num1  int `json:"1"`
	Num2  int `json:"2"`
	Num3  int `json:"3"`
	Num4  int `json:"4"`
	Num5  int `json:"5"`
	Num6  int `json:"6"`
	Num7  int `json:"7"`
	Num8  int `json:"8"`
	Num9  int `json:"9"`
	Num10 int `json:"10"`
	Num11 int `json:"11"`
	Num12 int `json:"12"`
	Num13 int `json:"13"`
	Num14 int `json:"14"`
	Num15 int `json:"15"`
	Num16 int `json:"16"`
	Num17 int `json:"17"`
	Num18 int `json:"18"`
	Num19 int `json:"19"`
	Num20 int `json:"20"`
	Num21 int `json:"21"`
	Num22 int `json:"22"`
	Num23 int `json:"23"`
	Num24 int `json:"24"`
	Num25 int `json:"25"`
	Num26 int `json:"26"`
	Num27 int `json:"27"`
	Num28 int `json:"28"`
	Num29 int `json:"29"`
}

type ExcelEdbdataMapping struct {
	MappingId  int `orm:"column(mapping_id);pk"`
	ExcelId    int64
	ClassifyId int
	Frequency  string
	TradeCode  string
	CreateTime time.Time
}

func AddExcelEdbdataMapping(item *ExcelEdbdataMapping) (err error) {
	o := orm.NewOrmUsingDB("edb")
	_, err = o.Insert(item)
	return
}

func GetExcelEdbdataMappingByTradeCode(classifyId int, frequency string) (excelId *int, err error) {
	o := orm.NewOrmUsingDB("edb")
	sql := `SELECT excel_id FROM excel_edbdata_mapping WHERE classify_id = ? AND frequency=?`
	err = o.Raw(sql, classifyId, frequency).QueryRow(&excelId)
	return
}

func GetExcelStyleById(excelId int) (item *ExcelStyle, err error) {
	o := orm.NewOrmUsingDB("edb")
	sql := `SELECT * FROM excel_style WHERE excel_id = ? `
	err = o.Raw(sql,excelId).QueryRow(&item)
	return
}

func GetExcelEdbdataMappingCount(classifyId int, tradeCode, frequency string) (count int, err error) {
	sql := `SELECT COUNT(1) AS count FROM excel_edbdata_mapping WHERE classify_id=? AND trade_code=? AND frequency=? `
	o := orm.NewOrmUsingDB("edb")
	err = o.Raw(sql, classifyId, tradeCode, frequency).QueryRow(&count)
	return
}

//func ModifyExcelEdbdataMapping(classifyId int, tradeCode, frequency string) (err error) {
//	sql := `UPDATE  excel_edbdata_mapping SET CLOSE=?,modify_time=NOW() WHERE  TRADE_CODE=? AND DT=? `
//	o := orm.NewOrmUsingDB("edb")
//	_, err = o.Raw(sql, close, tradeCode, dt).Exec()
//	return
//}