package data_manage

import (
	"eta/eta_api/controllers"
	"eta/eta_api/models"
	"eta/eta_api/models/data_manage"
	"eta/eta_api/services/data"
	"eta/eta_api/utils"
	"fmt"
	"github.com/rdlucklib/rdluck_tools/paging"
)

// JiaYueEdbSourceController 嘉悦数据源
type JiaYueEdbSourceController struct {
	controllers.BaseAuthController
}

// List
// @Title 数据源列表
// @Description 数据源列表
// @Success 200 string "获取成功"
// @router /edb_source/jiayue/list [get]
func (this *JiaYueEdbSourceController) List() {
	br := new(models.BaseResponse).Init()
	defer func() {
		if br.ErrMsg == "" {
			br.IsSendEmail = false
		}
		this.Data["json"] = br
		this.ServeJSON()
	}()
	sysUser := this.SysUser
	if sysUser == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录，SysUser Is Empty"
		return
	}

	cond := ` AND is_base = 1 AND from_bridge = 1`
	pars := make([]interface{}, 0)
	sources, e := data_manage.GetEdbSourceItemsByCondition(cond, pars, []string{}, "")
	if e != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取指标来源失败, Err: "
		return
	}
	br.Data = sources
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
}

// FrequencyList
// @Title 指标频度列表
// @Description 指标频度列表
// @Success 200 string "获取成功"
// @router /edb_source/jiayue/frequency_list [get]
func (this *JiaYueEdbSourceController) FrequencyList() {
	br := new(models.BaseResponse).Init()
	defer func() {
		if br.ErrMsg == "" {
			br.IsSendEmail = false
		}
		this.Data["json"] = br
		this.ServeJSON()
	}()
	sysUser := this.SysUser
	if sysUser == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录，SysUser Is Empty"
		return
	}

	frequencies, e := data.GetJiaYueFrequencyListFromBridge()
	if e != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "桥接服务获取指标频度失败, Err: " + e.Error()
		return
	}
	br.Data = frequencies
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
}

// IndexPageList
// @Title 指标列表-分页
// @Description 指标列表-分页
// @Param   PageSize  query  int  false  "每页数据条数"
// @Param   CurrentIndex  query  int  false  "当前页码"
// @Param   Source  query  int  true  "数据来源"
// @Param   Frequency  query  string  false  "频度"
// @Param   Keyword  query  string  false  "关键词: 指标ID/指标名称"
// @Param   SortField  query  int  false  "排序字段: 1-指标开始时间; 2-指标最新时间; 3-更新时间"
// @Param   SortRule  query  int  false  "排序方式: 1-正序; 2-倒序"
// @Success 200 string "获取成功"
// @router /edb_source/jiayue/index_page_list [get]
func (this *JiaYueEdbSourceController) IndexPageList() {
	br := new(models.BaseResponse).Init()
	defer func() {
		if br.ErrMsg == "" {
			br.IsSendEmail = false
		}
		this.Data["json"] = br
		this.ServeJSON()
	}()
	sysUser := this.SysUser
	if sysUser == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录，SysUser Is Empty"
		return
	}
	source, _ := this.GetInt("Source")
	frequency := this.GetString("Frequency")
	keyword := this.GetString("Keyword")
	sortField, _ := this.GetInt("SortField")
	sortRule, _ := this.GetInt("SortRule")

	if source <= 0 {
		br.Msg = "参数有误"
		br.ErrMsg = fmt.Sprintf("参数有误, Source: %d", source)
		return
	}
	cond := ` AND edb_source_id = ?`
	pars := make([]interface{}, 0)
	pars = append(pars, source)
	sourceItem, e := data_manage.GetEdbSourceItemByCondition(cond, pars)
	if e != nil {
		br.Msg = "数据源有误"
		br.ErrMsg = "获取数据源失败, Err: " + e.Error()
		return
	}

	// 分页
	pageSize, _ := this.GetInt("PageSize")
	currentIndex, _ := this.GetInt("CurrentIndex")
	if pageSize <= 0 {
		pageSize = utils.PageSize20
	}
	if currentIndex <= 0 {
		currentIndex = 1
	}

	var params data_manage.BridgeJiaYuePageIndexReq
	params.PageIndex = currentIndex
	params.PageSize = pageSize
	params.Keyword = keyword
	params.Frequency = frequency
	params.SourceExtend = sourceItem.SourceExtend
	params.SortField = sortField
	params.SortRule = sortRule
	total, list, e := data.GetJiaYueIndexPageListFromBridge(params)
	if e != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取指标分页列表失败, Err: " + e.Error()
		return
	}
	items := make([]*data_manage.DictIndexItem, 0)
	for _, v := range list {
		t := data_manage.FormatDictIndex2Item(v)
		items = append(items, t)
	}

	page := paging.GetPaging(currentIndex, pageSize, total)
	resp := new(data_manage.JiaYueIndexPageListResp)
	resp.Paging = page
	resp.List = items
	br.Data = resp
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
}
