package semantic_analysis

import (
	"eta/eta_mobile/controllers"
	"eta/eta_mobile/models"
	saModel "eta/eta_mobile/models/semantic_analysis"
	"eta/eta_mobile/utils"
	"fmt"
	"github.com/rdlucklib/rdluck_tools/paging"
)

// SaCompareController 语义分析-文档比对
type SaCompareController struct {
	controllers.BaseAuthController
}

// Search
// @Title 文档对比搜索（从es获取）
// @Description  图表模糊搜索（从es获取）
// @Param   Keyword   query   string  true       "文档对比标题"
// @Success 200 {object} saModel.CompareListByEsResp
// @router /compare/search [get]
func (this *SaCompareController) Search() {
	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
	}
	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)

	keyword := this.GetString("Keyword")

	var searchList []*saModel.SaCompareElastic
	var total int
	var err error

	var list []*saModel.SaCompare
	saCompare := new(saModel.SaCompare)
	existCond := fmt.Sprintf(` AND result_img != ""`)
	existPars := make([]interface{}, 0)
	if keyword != "" {
		existCond += ` AND  ( title LIKE ? )`
		existPars = append(existPars, `%`+keyword+`%`)
	}
	total, list, err = saCompare.GetPageItemsByCondition(startSize, pageSize, existCond, existPars, []string{}, "")
	if err != nil && err.Error() != utils.ErrNoRow() {
		br.Msg = "获取失败"
		br.ErrMsg = "获取图表信息失败，Err:" + err.Error()
		return
	}

	for _, v := range list {
		tmp := new(saModel.SaCompareElastic)
		tmp.SaCompareId = v.SaCompareId
		tmp.ResultImg = v.ResultImg
		tmp.CreateTime = v.CreateTime.Format(utils.FormatDateTime)
		tmp.ModifyTime = v.ModifyTime.Format(utils.FormatDateTime)
		tmp.SysAdminId = v.SysAdminId
		tmp.SysAdminName = v.SysAdminName
		tmp.ClassifyId = v.ClassifyId
		tmp.ClassifyName = v.ClassifyName
		tmp.Title = v.Title
		searchList = append(searchList, tmp)
	}

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