package sandbox

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

// Sandbox 沙盘推演主表
//type Sandbox struct {
//	SandboxId           int       `orm:"column(sandbox_id);pk" description:"沙盘id"`
//	Name                string    `description:"沙盘名称"`
//	ChartPermissionId   int       `description:"品种id"`
//	ChartPermissionName string    `description:"品种名称"`
//	CurrVersion         int       `description:"当前版本"`
//	Code                string    `description:"沙盘code"`
//	Content             string    `description:"沙盘数据"`
//	PicUrl              string    `description:"沙盘图片地址"`
//	OpUserId            int       `description:"最近一次编辑操作的用户id"`
//	OpUserName          string    `description:"最近一次编辑的用户名称(冗余字段,避免查表)"`
//	IsDelete            int8      `description:"是否删除,0:未删除,1:已删除"`
//	ModifyTime          time.Time `description:"修改时间"`
//	CreateTime          time.Time `description:"创建时间"`
//}

type Sandbox struct {
	SandboxId         int       `orm:"column(sandbox_id);pk" description:"沙盘id"`
	Name              string    `description:"沙盘名称"`
	Code              string    `description:"沙盘code"`
	Content           string    `description:"沙盘数据"`
	MindmapData       string    `description:"思维导图数据"`
	PicUrl            string    `description:"沙盘图片地址"`
	SysUserId         int       `description:"作者id"`
	SysUserName       string    `description:"作者名称"`
	IsDelete          int8      `description:"是否删除,0:未删除,1:已删除"`
	ModifyTime        time.Time `description:"修改时间"`
	CreateTime        time.Time `description:"创建时间"`
	SandboxClassifyId int       `description:"分类id"`
	Sort              int       `description:"排序"`
}

// Update 沙盘字段变更
func (sandbox *Sandbox) Update(cols []string) (err error) {
	o := orm.NewOrmUsingDB("data")
	_, err = o.Update(sandbox, cols...)
	return
}

// SandboxListItem 沙盘推演列表数据
type SandboxListItem struct {
	SandboxId           int    `description:"沙盘id"`
	Name                string `description:"沙盘名称"`
	ChartPermissionId   int    `description:"品种id"`
	ChartPermissionName string `description:"品种名称"`
	CurrVersion         int    `description:"当前版本"`
	Code                string `description:"沙盘code"`
	VersionCode         string `description:"沙盘版本code"`
	//Content             string    `description:"沙盘数据"`
	PicUrl       string    `description:"沙盘图片地址"`
	OpUserId     int       `description:"最近一次编辑操作的用户id"`
	OpUserName   string    `description:"最近一次编辑的用户名称(冗余字段,避免查表)"`
	IsDelete     int8      `description:"是否删除,0:未删除,1:已删除" json:"is_delete"`
	CanEdit      bool      `description:"是否可编辑"`
	Editor       string    `description:"编辑人"`
	VersionTotal int       `description:"历史版本数量"`
	ModifyTime   time.Time `description:"修改时间"`
	CreateTime   time.Time `description:"创建时间"`
}

// GetList 获取沙盘列表页
func GetList(condition string, pars []interface{}, startSize, pageSize int) (total int, list []*Sandbox, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := "select a.sandbox_id,a.name,a.code,a.pic_url,a.sys_user_id,a.sys_user_name,a.modify_time,a.create_time from sandbox as a where 1=1 AND a.is_delete = 0 "
	sql += condition
	sql += ` order by a.modify_time desc,a.sandbox_id desc`

	totalSql := `select count(1) total from (` + sql + `) z `
	err = o.Raw(totalSql, pars).QueryRow(&total)
	if err != nil {
		return
	}
	sql += ` LIMIT ?,? `
	_, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&list)
	return
}