package excel

import (
	"eta_gn/eta_api/global"
	"eta_gn/eta_api/utils"
	"time"
)

// ExcelSheetData excel表格详情表
type ExcelSheetData struct {
	ExcelDataId  int       `orm:"column(excel_data_id);pk" gorm:"primaryKey" `
	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) {
	err = global.DmSQL["data"].Select(cols).Updates(ExcelSheetData).Error
	return
}

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

	return
}

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