package excel

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

// ExcelSheetData excel表格详情表
type ExcelSheetData struct {
	ExcelDataId  int       `orm:"column(excel_data_id);pk"`
	ExcelInfoId  int       `description:"数据归属的excel_info的id"`
	ExcelSheetId int       `description:"数据归属sheet"`
	Sort         int       `description:"数据排序"`
	Data         string    `description:"数据，分页存储"`
	ModifyTime   time.Time `description:"最近修改日期"`
	CreateTime   time.Time `description:"创建日期"`
}

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

	return
}

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

	return
}

// GetSheetDataListBySheetIdListAndPage 根据sheet_id列表和页码获取所有的sheet数据详情
func GetSheetDataListBySheetIdListAndPage(excelSheetIdList []int, page int) (items []*ExcelSheetData, err error) {
	num := len(excelSheetIdList)
	if num <= 0 {
		return
	}
	o := orm.NewOrmUsingDB("data")
	sql := ` SELECT *
FROM excel_sheet_data WHERE 1=1 AND excel_sheet_id in (` + utils.GetOrmInReplace(num) + `) AND sort = ? `
	_, err = o.Raw(sql, excelSheetIdList, page).QueryRows(&items)

	return
}

// GetAllSheetDataListByExcelInfoId 根据表格id获取所有的sheet的所有数据详情
func GetAllSheetDataListByExcelInfoId(excelInfoId int) (items []*ExcelSheetData, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := ` SELECT *
FROM excel_sheet_data WHERE 1=1 AND excel_info_id = ? ORDER BY sort ASC `
	_, err = o.Raw(sql, excelInfoId).QueryRows(&items)

	return
}
