package sandbox

import (
	"eta/eta_mobile/controllers"
	"eta/eta_mobile/models"
	"eta/eta_mobile/models/sandbox"
	"eta/eta_mobile/models/sandbox/response"
	"eta/eta_mobile/utils"
	"github.com/rdlucklib/rdluck_tools/paging"
)

// SandboxController 逻辑导图
type SandboxController struct {
	controllers.BaseAuthController
}

// ListByQuote
// @Title 逻辑导图列表（其他地方引用到的，莫名其妙要根据输入的关键字匹配品种）
// @Description 逻辑导图列表接口（其他地方引用到的，莫名其妙要根据输入的关键字匹配品种）
// @Param   PageSize   query   int  true       "每页数据条数"
// @Param   CurrentIndex   query   int  true       "当前页页码，从1开始"
// @Param   ChartPermissionId   query   int  true       "权限编号id"
// @Param   Keyword   query   string  false       "搜索关键词:沙盘名称/编辑人名称"
// @Success 200 {object} response.ListResp
// @router /list_by_quote [get]
func (this *SandboxController) ListByQuote() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()

	chartPermissionId, _ := this.GetInt("ChartPermissionId")
	keyword := this.GetString("Keyword")

	pageSize, _ := this.GetInt("PageSize")
	currentIndex, _ := this.GetInt("CurrentIndex")

	var startSize int
	if pageSize <= 0 {
		pageSize = utils.PageSize20
	}
	if currentIndex <= 0 {
		currentIndex = 1
	}
	startSize = paging.StartIndex(currentIndex, pageSize)

	var condition string
	var pars []interface{}

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

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

	//获取指标信息
	total, list, err := sandbox.GetList(condition, pars, startSize, pageSize)
	if err != nil && err.Error() != utils.ErrNoRow() {
		br.Success = true
		br.Msg = "获取沙盘列表失败"
		br.ErrMsg = "获取沙盘列表失败,Err:" + err.Error()
		return
	}

	if list == nil || (err != nil && err.Error() == utils.ErrNoRow()) {
		list = make([]*sandbox.Sandbox, 0)
	}

	page := paging.GetPaging(currentIndex, pageSize, total)
	resp := response.SandboxListResp{
		Paging: page,
		List:   list,
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = resp
}
