package sandbox

import (
	"encoding/json"
	"eta/eta_api/controllers"
	"eta/eta_api/models"
	"eta/eta_api/models/sandbox"
	"eta/eta_api/models/sandbox/request"
	"eta/eta_api/models/sandbox/response"
	"eta/eta_api/models/system"
	sandboxService "eta/eta_api/services/sandbox"
	"eta/eta_api/utils"
	"fmt"
	"github.com/rdlucklib/rdluck_tools/paging"
)

// versionSize 版本列表第一页数据约定是：3条
var versionSize = 3

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

// List
// @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.SandboxListResp
// @router /list [get]
func (this *SandboxController) List() {
	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  b.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.SandboxListItem, 0)
	}

	if len(list) > 0 {
		sandboxIdList := make([]int, 0)
		for _, v := range list {
			sandboxIdList = append(sandboxIdList, v.SandboxId)
		}

		sandboxVersionTotalList, err := sandbox.GetTotalSandboxVersionBySandboxIdList(sandboxIdList)
		if err != nil {
			br.Success = true
			br.Msg = "获取沙盘版本数量失败"
			br.ErrMsg = "获取沙盘版本数量失败,Err:" + err.Error()
			return
		}
		sandboxVersionTotalMap := make(map[int]int)

		for _, v := range sandboxVersionTotalList {
			sandboxVersionTotalMap[v.SandboxId] = v.Total
		}

		for _, item := range list {
			/*key := fmt.Sprint(`crm:sandbox:edit:`, item.SandboxId)
			opUserId, _ := utils.Rc.RedisInt(key)
			//如果当前没有人操作，获取当前操作人是本人，那么编辑按钮可用
			if opUserId <= 0 || (opUserId == this.SysUser.AdminId) {
				item.CanEdit = true
			} else {
				adminInfo, errAdmin := system.GetSysUserById(opUserId)
				if errAdmin != nil {
					br.Msg = "获取失败"
					br.ErrMsg = "获取失败，Err:" + errAdmin.Error()
					return
				}
				item.Editor = adminInfo.RealName
			}*/
			markStatus, err := sandboxService.UpdateSandboxEditMark(item.SandboxId, this.SysUser.AdminId, 2, this.SysUser.RealName)
			if err != nil {
				br.Msg = "查询标记状态失败"
				br.ErrMsg = "查询标记状态失败，Err:" + err.Error()
				return
			}
			if markStatus.Status == 0 {
				item.CanEdit = true
			} else {
				item.Editor = markStatus.Editor
			}

			// 沙盘版本数量
			versionTotal := sandboxVersionTotalMap[item.SandboxId]
			item.VersionTotal = versionTotal
		}
	}

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

// FirstVersionList
// @Title 逻辑导图版本列表（列表页第一页）
// @Description 逻辑导图版本列表接口（列表页第一页）
// @Param   SandboxId   query   int  true       "沙盘id"
// @Success 200 {object} response.SandboxVersionListResp
// @router /version/first_list [get]
func (this *SandboxController) FirstVersionList() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()

	sandboxId, _ := this.GetInt("SandboxId")
	keyword := this.GetString("Keyword")

	currentIndex := 1
	pageSize := versionSize
	startSize := 0

	var condition string
	var pars []interface{}

	if sandboxId <= 0 {
		br.Msg = "参数错误，沙盘编号id必传！"
		br.ErrMsg = "参数错误，沙盘编号id必传"
		return
	}
	condition += " AND a.sandbox_id=? "
	pars = append(pars, sandboxId)

	//不能筛选出当前版本
	condition += " AND b.curr_version < a.curr_version "

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

	//获取指标信息
	total, list, err := sandbox.GetVersionList(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.SandboxVersionListItem, 0)
	}
	for _, v := range list {
		v.ModifyTime = v.CreateTime
	}

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

// VersionList
// @Title 逻辑导图版本列表
// @Description 逻辑导图版本列表接口
// @Param   PageSize   query   int  true       "每页数据条数"
// @Param   CurrentIndex   query   int  true       "当前页页码，从1开始"
// @Param   SandboxId   query   int  true       "沙盘id"
// @Success 200 {object} response.SandboxVersionListResp
// @router /version/list [get]
func (this *SandboxController) VersionList() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()

	sandboxId, _ := this.GetInt("SandboxId")
	keyWord := this.GetString("Keyword")

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

	var startSize int
	if pageSize <= 0 {
		pageSize = utils.PageSize20
	}
	//第一页需要返回的几条数据（目前是3条）
	startSize = paging.StartIndex(currentIndex, pageSize) + versionSize

	var condition string
	var pars []interface{}

	if sandboxId <= 0 {
		br.Msg = "参数错误，沙盘编号id必传！"
		br.ErrMsg = "参数错误，沙盘编号id必传"
		return
	}
	condition += " AND a.sandbox_id=? "
	pars = append(pars, sandboxId)

	//不能筛选出当前版本
	condition += " AND b.curr_version < a.curr_version "

	if keyWord != "" {
		condition += ` AND  ( a.op_user_name LIKE '%` + keyWord + `%'  OR  a.name LIKE '%` + keyWord + `%' or a.op_user_name LIKE '%` + keyWord + `%'  OR  a.name LIKE '%` + keyWord + `%' )`
	}

	//获取指标信息
	total, list, err := sandbox.GetVersionList(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.SandboxVersionListItem, 0)
	}
	for _, v := range list {
		v.ModifyTime = v.CreateTime
		/*key := fmt.Sprint(`crm:sandbox:edit:`, v.SandboxId)
		opUserId, _ := utils.Rc.RedisInt(key)
		//如果当前没有人操作，获取当前操作人是本人，那么编辑按钮可用
		if opUserId <= 0 || (opUserId == this.SysUser.AdminId) {
			v.CanEdit = true
		}*/
		markStatus, err := sandboxService.UpdateSandboxEditMark(v.SandboxId, this.SysUser.AdminId, 2, this.SysUser.RealName)
		if err != nil {
			br.Msg = "查询标记状态失败"
			br.ErrMsg = "查询标记状态失败，Err:" + err.Error()
			return
		}
		if markStatus.Status == 0 {
			v.CanEdit = true
		}
	}

	//过滤第一页的几条数据（目前是3条）
	total = total - versionSize
	if total <= 0 {
		total = 0
	}
	page := paging.GetPaging(currentIndex, pageSize, total)
	resp := response.SandboxVersionListResp{
		Paging: page,
		List:   list,
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = resp
}

// Save
// @Title 新增/编辑保存沙盘
// @Description 新增/编辑保存沙盘接口
// @Param	request	body request.AddAndEditSandbox true "type json string"
// @Success 200 {object} sandbox.Sandbox
// @router /save [post]
func (this *SandboxController) Save() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	sysUser := this.SysUser
	if sysUser == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录，SysUser Is Empty"
		br.Ret = 408
		return
	}
	var req request.AddAndEditSandbox
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}

	var sandboxResp *sandbox.SandboxSaveResp
	// 获取系统菜单, 如果没有对应的字段的特殊处理项, 则忽略必填
	menus, e := system.GetSysMenuItemsByCondition(` AND hidden = 0`, make([]interface{}, 0), []string{}, ``)
	if e != nil {
		br.Msg = "保存失败"
		br.ErrMsg = "获取菜单列表失败, Err: " + e.Error()
		return
	}
	menuMap := make(map[string]bool)
	for _, m := range menus {
		if m.ButtonCode != "" {
			menuMap[m.ButtonCode] = true
		}
	}
	ignoreVariety := false
	if !menuMap[system.MenuSpecialHandleSandboxVariety] {
		ignoreVariety = true
	}

	var errMsg string
	if req.SandboxVersionCode == `` {
		//新增沙盘
		sandboxResp, err = sandboxService.AddSandbox(req, sysUser.AdminId, sysUser.RealName, ignoreVariety)
	} else {
		////更新当前编辑中的状态缓存
		//err = sandboxService.UpdateSandboxEditMark(req.SandboxId, sysUser.AdminId, 1)
		//if err != nil {
		//	br.Msg = err.Error()
		//	return
		//}

		//编辑沙盘
		sandboxResp, err, errMsg = sandboxService.UpdateSandbox(req, sysUser.AdminId, sysUser.RealName, ignoreVariety)
	}
	if err != nil {
		br.Msg = "保存失败！"
		if errMsg != `` {
			br.Msg = errMsg
		}
		br.ErrMsg = "保存失败，Err:" + err.Error()
		return
	}

	msg := "保存成功"
	br.Ret = 200
	br.Success = true
	br.Msg = msg
	br.Data = sandboxResp
}

// AddSandboxDraft
// @Title 添加沙盘草稿
// @Description 添加沙盘草稿接口
// @Param	request	body request.AddAndEditSandbox true "type json string"
// @Success 200 {object} sandbox.SandboxDraft
// @Fail 202 另外的人在操作，不要重复添加草稿；204 错误了，当时不是必要性的错误，不用将错误信息暴露给用户
// @router /draft/add [post]
func (this *SandboxController) AddSandboxDraft() {
	br := new(models.BaseResponse).Init()
	br.IsSendEmail = false
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	sysUser := this.SysUser
	if sysUser == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录，SysUser Is Empty"
		br.Ret = 408
		return
	}
	var req request.AddAndEditSandbox
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}

	if req.SandboxVersionCode == `` {
		br.Msg = "请传入沙盘版本！"
		return
	}

	// 获取沙盘版本信息
	sandboxVersion, err := sandbox.GetSandboxVersionBySandboxVersionCode(req.SandboxVersionCode)
	if err != nil {
		if err.Error() == utils.ErrNoRow() {
			br.Msg = "找不到该版本！"
			br.ErrMsg = "找不到该版本"
			return
		}
		br.Msg = "找不到该版本！"
		br.ErrMsg = "找不到该版本，Err:" + err.Error()
		br.IsSendEmail = false
		return
	}
	//更新标记key
	markStatus, err := sandboxService.UpdateSandboxEditMark(sandboxVersion.SandboxId, sysUser.AdminId, 1, sysUser.RealName)
	if err != nil {
		br.Msg = err.Error()
		return
	}
	if markStatus.Status == 1 {
		br.Msg = markStatus.Msg
		return
	}

	//新增沙盘草稿
	sandboxDraftInfo, err := sandboxService.AddSandboxDraft(sandboxVersion.SandboxId, req, sysUser.AdminId, sysUser.RealName)
	if err != nil {
		br.Msg = "保存失败！"
		br.ErrMsg = "保存失败，Err:" + err.Error()
		br.Ret = 204 //204 （无内容） ：服务器成功处理了请求，但没有返回任何内容。
		return
	}
	msg := "保存成功"
	br.Ret = 200
	br.Success = true
	br.Msg = msg
	br.Data = sandboxDraftInfo
}

// MarkEditStatus
// @Title 标记沙盘编辑状态
// @Description 标记沙盘编辑状态接口
// @Param	request	body request.MarkEditSandbox true "type json string"
// @Success 200 标记成功 ；202 标记成功
// @router /mark [post]
func (this *SandboxController) MarkEditStatus() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	sysUser := this.SysUser
	if sysUser == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录，SysUser Is Empty"
		br.Ret = 408
		return
	}
	var req request.MarkEditSandbox
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}

	if req.SandboxId <= 0 {
		br.Msg = "缺少沙盘编号"
		return
	}
	if req.Status <= 0 {
		br.Msg = "标记状态异常"
		return
	}
	//更新标记key
	data, err := sandboxService.UpdateSandboxEditMark(req.SandboxId, sysUser.AdminId, req.Status, sysUser.RealName)
	if err != nil {
		br.Msg = err.Error()
		return
	}

	msg := "标记成功"
	br.Ret = 200
	br.Success = true
	br.Msg = msg
	br.Data = data
}

// GetSandboxVersionDetail
// @Title 获取沙盘版本数据详情（已保存的）
// @Description 获取沙盘版本数据详情接口（已保存的）
// @Param   SandboxVersionCode   query   string  true       "沙盘版本code"
// @Success 200 {object} sandbox.SandboxVersion
// @router /version/detail [get]
func (this *SandboxController) GetSandboxVersionDetail() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	sysUser := this.SysUser
	if sysUser == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录，SysUser Is Empty"
		br.Ret = 408
		return
	}

	sandboxVersionCode := this.GetString("SandboxVersionCode")
	if sandboxVersionCode == "" {
		br.Msg = "缺少沙盘版本编号"
		return
	}

	//获取沙盘数据详情（已保存的）
	sandboxVersionInfo, err := sandboxService.GetSandboxVersionDetailByCode(sandboxVersionCode)
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取失败，Err：" + err.Error()
		return
	}
	msg := "获取成功"
	br.Ret = 200
	br.Success = true
	br.Msg = msg
	br.Data = sandboxVersionInfo
}

// GetLastSandboxInfo
// @Title 获取最后一次编辑的沙盘数据详情
// @Description 获取最后一次编辑的沙盘数据详情接口
// @Param   SandboxId   query   int  true       "沙盘编号id"
// @Success 200 {object} sandbox.Sandbox
// @router /last_info [get]
func (this *SandboxController) GetLastSandboxInfo() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	sysUser := this.SysUser
	if sysUser == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录，SysUser Is Empty"
		br.Ret = 408
		return
	}

	sandboxId, _ := this.GetInt("SandboxId", 0)
	if sandboxId <= 0 {
		br.Msg = "缺少沙盘编号"
		return
	}

	//获取最后一次操作的沙盘数据
	sandboxInfo, err := sandboxService.GetLastSandboxInfo(sandboxId)
	if err != nil {
		br.Msg = err.Error()
		return
	}
	msg := "获取成功"
	br.Ret = 200
	br.Success = true
	br.Msg = msg
	br.Data = sandboxInfo
}

// Delete
// @Title 删除沙盘
// @Description 删除沙盘接口
// @Param	request	body request.DeleteSandbox true "type json string"
// @Success 200 标记成功
// @router /delete [post]
func (this *SandboxController) Delete() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	sysUser := this.SysUser
	if sysUser == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录，SysUser Is Empty"
		br.Ret = 408
		return
	}
	var req request.DeleteSandbox
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}

	if req.SandboxId <= 0 {
		br.Msg = "缺少沙盘编号"
		return
	}

	/*key := fmt.Sprint(`crm:sandbox:edit:`, req.SandboxId)
	opUserId, _ := utils.Rc.RedisInt(key)
	//如果当前有人操作，且获取当前操作人不是本人，那么不允许删除
	if opUserId > 0 && opUserId != this.SysUser.AdminId {
		br.Msg = "当前有其他人正在编辑，不允许删除该沙盘"
		return
	}*/
	markStatus, err := sandboxService.UpdateSandboxEditMark(req.SandboxId, this.SysUser.AdminId, 2, this.SysUser.RealName)
	if err != nil {
		br.Msg = "查询标记状态失败"
		br.ErrMsg = "查询标记状态失败，Err:" + err.Error()
		return
	}
	if markStatus.Status == 1 {
		br.Msg = fmt.Sprintf("当前%s正在编辑，不允许删除该沙盘", markStatus.Editor)
		return
	}

	//删除沙盘
	err = sandboxService.DeleteSandbox(req.SandboxId)
	if err != nil {
		br.Msg = err.Error()
		return
	}

	msg := "删除成功"
	br.Ret = 200
	br.Success = true
	br.Msg = msg
}

// DeleteVersion
// @Title 删除沙盘版本
// @Description 删除沙盘版本接口
// @Param	request	body request.DeleteSandbox true "type json string"
// @Success 200 标记成功
// @router /version/delete [post]
func (this *SandboxController) DeleteVersion() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	sysUser := this.SysUser
	if sysUser == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录，SysUser Is Empty"
		br.Ret = 408
		return
	}
	var req request.DeleteSandboxVersion
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}

	if req.SandboxVersionCode == `` {
		br.Msg = "缺少沙盘版本号"
		return
	}
	//删除沙盘
	err, errMsg := sandboxService.DeleteSandboxVersion(req.SandboxVersionCode, this.SysUser.AdminId)
	if err != nil {
		br.Msg = "删除版本失败"
		if errMsg != `` {
			br.Msg = errMsg
		}
		br.ErrMsg = err.Error()
		return
	}

	msg := "删除成功"
	br.Ret = 200
	br.Success = true
	br.Msg = msg
}

// ResetDraftToLastVersion
// @Title 重置沙盘草稿至最新版本
// @Description 重置沙盘草稿至最新版本接口
// @Param	request	body request.DeleteSandbox true "type json string"
// @Success 200 {object} sandbox.SandboxDraft
// @Fail 202 另外的人在操作，不要重复添加草稿；204 错误了，当时不是必要性的错误，不用将错误信息暴露给用户
// @router /draft/reset [post]
func (this *SandboxController) ResetDraftToLastVersion() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	sysUser := this.SysUser
	if sysUser == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录，SysUser Is Empty"
		br.Ret = 408
		return
	}
	var req request.DeleteSandbox
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}

	if req.SandboxId <= 0 {
		br.Msg = "缺少沙盘编号"
		return
	}

	//更新标记key
	markStatus, err := sandboxService.UpdateSandboxEditMark(req.SandboxId, sysUser.AdminId, 0, sysUser.RealName)
	if err != nil {
		br.Msg = err.Error()
		return
	}
	if markStatus.Status == 1 {
		br.Msg = markStatus.Msg
		return
	}

	//重置沙盘草稿至最新版本
	sandboxDraftInfo, err := sandboxService.ResetDraftToLastVersion(req.SandboxId, sysUser.AdminId, sysUser.RealName)
	if err != nil {
		br.Msg = "保存失败！"
		br.ErrMsg = "保存失败，Err:" + err.Error()
		return
	}
	msg := "保存成功"
	br.Ret = 200
	br.Success = true
	br.Msg = msg
	br.Data = sandboxDraftInfo
}

// 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.SandboxListResp
// @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  b.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.SandboxListItem, 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
}
