package excel

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

// ExcelSheet excel表格详情表
type ExcelSheet struct {
	ExcelSheetId int       `orm:"column(excel_sheet_id);pk"`
	ExcelInfoId  int       `description:"excel的id"`
	SheetName    string    `description:"sheet名称"`
	PageNum      int       `description:"总页码数"`
	Index        string    `description:"excel数据中的index"`
	Sort         int       `description:"排序"`
	Config       string    `description:"配置信息"`
	CalcChain    string    `description:"计算公式"`
	ModifyTime   time.Time `description:"最近修改日期"`
	CreateTime   time.Time `description:"创建日期"`
}

// Update 更新 excel表格的sheet基础信息
func (excelSheet *ExcelSheet) Update(cols []string) (err error) {
	o := orm.NewOrmUsingDB("data")
	_, err = o.Update(excelSheet, cols...)

	return
}

// AddExcelSheet 新增excel表格的sheet基础信息
func AddExcelSheet(excelInfo *ExcelSheet) (err error) {
	o := orm.NewOrmUsingDB("data")
	// 表格信息入库
	lastId, err := o.Insert(excelInfo)
	if err != nil {
		return
	}
	excelInfo.ExcelInfoId = int(lastId)

	return
}

// GetAllSheetList 根据excel_id获取所有的sheet
func GetAllSheetList(excelInfoId int) (item []*ExcelSheet, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := ` SELECT *
FROM excel_sheet WHERE 1=1 AND excel_info_id = ? `
	sql += " ORDER BY sort asc "
	_, err = o.Raw(sql, excelInfoId).QueryRows(&item)
	return
}

// SheetItem excel表格详情表
type SheetItem struct {
	ExcelSheetId int             `orm:"column(excel_sheet_id);pk" json:"-"`
	ExcelInfoId  int             `description:"excel的id"  json:"-"`
	SheetName    string          `description:"sheet名称"`
	PageNum      int             `description:"数据总页码数"`
	Index        string          `description:"excel数据中的index"`
	Sort         int             `description:"排序"`
	Config       string          `description:"sheet配置"`
	CalcChain    string          `description:"计算公式"`
	ModifyTime   time.Time       `description:"最近修改日期" json:"-"`
	CreateTime   time.Time       `description:"创建日期"`
	Data         *ExcelSheetData `description:"excel的数据"`
}

// GetAllSheetItemList 根据excel_id获取所有的sheet详情
func GetAllSheetItemList(excelInfoId int) (item []*SheetItem, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := ` SELECT *
FROM excel_sheet WHERE 1=1 AND excel_info_id = ? `
	sql += " ORDER BY sort asc "
	_, err = o.Raw(sql, excelInfoId).QueryRows(&item)
	return
}

// GetAllNoConfigSheetItemList 根据excel_id获取所有的sheet详情
func GetAllNoConfigSheetItemList(excelInfoId int) (item []*SheetItem, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := ` SELECT excel_sheet_id,excel_info_id,sheet_name,sort,page_num,create_time
FROM excel_sheet WHERE 1=1 AND excel_info_id = ? `
	sql += " ORDER BY sort asc "
	_, err = o.Raw(sql, excelInfoId).QueryRows(&item)
	return
}

// AddExcelSheetParams excel表格详情表
type AddExcelSheetParams struct {
	ExcelSheetId int               `orm:"column(excel_sheet_id);pk"`
	ExcelInfoId  int               `description:"excel的id"`
	SheetName    string            `description:"sheet名称"`
	Index        string            `description:"excel数据中的index"`
	Sort         int               `description:"排序"`
	Config       string            `description:"配置信息"`
	CalcChain    string            `description:"计算公式"`
	DataList     []*ExcelSheetData `description:"excel的数据"`
}