package excel

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

// ExcelDraft 沙盘推演草稿表
type ExcelDraft struct {
	ExcelDraftId int       `orm:"column(excel_draft_id);pk" description:"excel表格草稿记录id"`
	ExcelId      int       `description:"excel表格id"`
	Name         string    `description:"excel表格名称"`
	Content      string    `description:"excel数据"`
	OpUserId     int       `description:"最近一次编辑操作的用户id"`
	OpUserName   string    `description:"最近一次编辑的用户名称（冗余字段，避免查表）"`
	CreateTime   time.Time `description:"创建时间"`
}

// AddExcelDraft 添加一个新的excel表格草稿
func AddExcelDraft(excelDraft *ExcelDraft) (err error) {
	o := orm.NewOrmUsingDB("data")
	// 新增草稿
	id, err := o.Insert(excelDraft)
	if err != nil {
		return
	}
	excelDraft.ExcelDraftId = int(id)
	return
}

// GetLastExcelDraftById 根据沙盘id获取最后一条沙盘草稿详情
func GetLastExcelDraftById(excelId int) (excelDraft *ExcelDraft, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `select * from excel_draft where excel_id = ? order by excel_draft_id desc `
	err = o.Raw(sql, excelId).QueryRow(&excelDraft)
	return
}
