package sandbox

import (
	sandboxModel "hongze/hongze_yb/models/tables/sandbox"
	"time"
)

// SandboxItem 沙盘图
type SandboxItem struct {
	SandboxID           uint32    `gorm:"primaryKey;column:sandbox_id;type:int(9) unsigned;not null" json:"sandbox_id"`                                                   // 沙盘id
	Name                string    `gorm:"index:idx_name;column:name;type:varchar(64);not null;default:''" json:"name"`                                                    // 沙盘名称
	ChartPermissionID   uint32    `gorm:"column:chart_permission_id;type:int(9) unsigned;not null;default:0" json:"chart_permission_id"`                                  // 品种id
	ChartPermissionName string    `gorm:"index:idx_chart_permission_name;column:chart_permission_name;type:varchar(32);not null;default:''" json:"chart_permission_name"` // 品种名称（冗余字段，避免列表页查询时再去关联表查询）
	CurrVersion         uint32    `gorm:"column:curr_version;type:int(9) unsigned;not null;default:0" json:"curr_version"`                                                // 当前版本
	Code                string    `gorm:"column:code;type:varchar(255);not null" json:"code"`                                                                             // 沙盘code
	PicURL              string    `gorm:"column:pic_url;type:varchar(255);not null;default:''" json:"pic_url"`                                                            // 沙盘图片
	OpUserName          string    `gorm:"index:idx_op_user_name;column:op_user_name;type:varchar(32);not null;default:''" json:"op_user_name"`                            // 最近一次编辑的用户名称（冗余字段，避免查表）
	ModifyTime          time.Time `gorm:"column:modify_time;type:timestamp;default:CURRENT_TIMESTAMP" json:"modify_time"`                                                 // 最近一次更新时间
}

// GetList 获取沙盘列表数据
func GetList(chartPermissionId int, companyPermissionIdList []int, keyword string, page, pageSize int) (total int64, list []SandboxItem, err error, errMsg string) {
	errMsg = `获取失败`
	var condition string
	var pars []interface{}

	condition = ` s.is_delete = ? `
	pars = append(pars, 0)

	// 客户拥有的品种权限
	if len(companyPermissionIdList) > 0 {
		condition += " AND c.chart_permission_id in (?) "
		pars = append(pars, companyPermissionIdList)
	}

	if chartPermissionId > 0 {
		condition += " AND c.chart_permission_id=? "
		pars = append(pars, chartPermissionId)
	}

	if keyword != "" {
		//condition += ` AND  ( name LIKE '%` + keyword + `%'  OR  chart_permission_name LIKE '%` + keyword + `%' )`
		condition += ` AND  ( s.name LIKE '%` + keyword + `%' )`
	}

	//获取指标信息
	tmpTotal, tmpList, tmpErr := sandboxModel.GetPageListByWhere(condition, pars, (page-1)*pageSize, pageSize)
	if tmpErr != nil {
		err = tmpErr
		return
	}

	total = tmpTotal

	for _, v := range tmpList {
		tmpSandBox := SandboxItem{
			SandboxID:           v.SandboxID,
			Name:                v.Name,
			ChartPermissionID:   v.ChartPermissionID,
			ChartPermissionName: v.ChartPermissionName,
			CurrVersion:         v.CurrVersion,
			Code:                v.Code,
			PicURL:              v.PicURL,
			OpUserName:          v.OpUserName,
			ModifyTime:          v.ModifyTime,
			//CreateTime:          time.Time{},
		}
		list = append(list, tmpSandBox)
	}
	return
}

// GetPermissionCountList 获取沙盘列表数据
func GetPermissionCountList(companyPermissionIdList []int) (list []*sandboxModel.PermissionSandboxCount, err error, errMsg string) {
	errMsg = `获取失败`
	var condition string
	var pars []interface{}

	condition = ` is_delete = ? `
	pars = append(pars, 0)

	// 客户拥有的品种权限
	if len(companyPermissionIdList) > 0 {
		condition += " AND chart_permission_id in (?) "
		pars = append(pars, companyPermissionIdList)
	}

	//获取指标信息
	list, err = sandboxModel.GetPermissionCountListByWhere(condition, pars)
	return
}
