package sandbox

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

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

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

// GetSandboxVersionBySandbox2VersionId 根据沙盘id和版本id获取沙盘版本详情
func GetSandboxVersionBySandbox2VersionId(sandboxId, sandboxVersion int) (sandboxVersionInfo *SandboxVersion, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `select b.* from sandbox a join sandbox_version b on a.sandbox_id=b.sandbox_id where a.sandbox_id = ? and a.is_delete = 0 and b.is_delete = 0 and b.curr_version = ?`
	err = o.Raw(sql, sandboxId, sandboxVersion).QueryRow(&sandboxVersionInfo)
	return
}

// GetSandboxVersionBySandboxVersionCode 根据沙盘版本code获取沙盘版本详情
func GetSandboxVersionBySandboxVersionCode(sandboxVersionCode string) (sandboxVersionInfo *SandboxVersion, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `select b.* from sandbox a join sandbox_version b on a.sandbox_id=b.sandbox_id where a.is_delete = 0 and b.is_delete = 0  and b.version_code = ?`
	err = o.Raw(sql, sandboxVersionCode).QueryRow(&sandboxVersionInfo)
	return
}

// GetLastSandboxVersionBySandbox2VersionId 根据沙盘id和版本id获取距离最新的沙盘版本详情
func GetLastSandboxVersionBySandbox2VersionId(sandboxId, sandboxVersion int) (sandboxVersionInfo *SandboxVersion, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `select b.* from sandbox a join sandbox_version b on a.sandbox_id=b.sandbox_id where a.sandbox_id = ? and a.is_delete = 0 and b.is_delete = 0  and b.curr_version < ? order by b.curr_version desc`
	err = o.Raw(sql, sandboxId, sandboxVersion).QueryRow(&sandboxVersionInfo)
	return
}

// SandboxVersionListItem 沙盘推演版本列表数据
type SandboxVersionListItem struct {
	SandboxVersionId    int    `orm:"column(sandbox_version_id);pk" description:"沙盘版本记录id"`
	SandboxId           int    `description:"沙盘id"`
	Name                string `description:"沙盘名称"`
	ChartPermissionId   int    `description:"品种id"`
	ChartPermissionName string `description:"品种名称"`
	CurrVersion         int    `description:"当前版本"`
	//Content             string    `description:"沙盘数据"`
	PicUrl      string    `description:"沙盘图片地址"`
	OpUserId    int       `description:"最近一次编辑操作的用户id"`
	OpUserName  string    `description:"最近一次编辑的用户名称(冗余字段,避免查表)"`
	VersionCode string    `description:"沙盘版本code,对外开放使用"`
	IsDelete    int8      `description:"是否删除,0:未删除,1:已删除" json:"-"`
	CanEdit     bool      `description:"是否可编辑"`
	ModifyTime  time.Time `description:"最后一次修改时间"`
	CreateTime  time.Time `description:"创建时间"`
}

// GetVersionList 获取沙盘版本列表页
func GetVersionList(condition string, pars []interface{}, startSize, pageSize int) (total int, list []*SandboxVersionListItem, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := "select b.sandbox_version_id,b.sandbox_id,b.name,b.chart_permission_id,b.chart_permission_name,b.curr_version,b.version_code,b.pic_url,b.op_user_id,b.op_user_name,b.create_time from sandbox as a join sandbox_version b on a.sandbox_id=b.sandbox_id where 1=1 AND a.is_delete = 0  AND b.is_delete = 0 "
	sql += condition
	sql += ` order by b.sandbox_version_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
}

// GetTotalSandboxVersionBySandboxId 根据沙盘id获取沙盘版本数量
func GetTotalSandboxVersionBySandboxId(sandboxId int) (sandboxVersionInfo *SandboxVersion, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `select count(1) total from sandbox_version b where b.sandbox_id = ? and b.is_delete = 0 `
	err = o.Raw(sql, sandboxId).QueryRow(&sandboxVersionInfo)
	return
}

type TotalSandboxVersion struct {
	SandboxId int `description:"沙盘id"`
	Total     int `description:"沙盘版本数量"`
}

// GetTotalSandboxVersionBySandboxIdList 根据沙盘id获取沙盘版本数量
func GetTotalSandboxVersionBySandboxIdList(sandboxIdList []int) (items []*TotalSandboxVersion, err error) {
	num := len(sandboxIdList)
	if num <= 0 {
		return
	}
	o := orm.NewOrmUsingDB("data")
	sql := `select sandbox_id,count(1) AS total from sandbox_version b where b.sandbox_id in (` + utils.GetOrmInReplace(num) + `) and b.is_delete = 0 GROUP BY sandbox_id`
	_, err = o.Raw(sql, sandboxIdList).QueryRows(&items)
	return
}