package data_manage

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

// SmmIndexList
// @Title 有色api数据指标列表
// @Description 有色api数据指标列表
// @Success 200 {object} data_manage.SmmClassify
// @router /smm/api/list [get]
func (this *EdbInfoController) SmmApiList() {
	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
	}
	types := this.GetString("Types")
	frequency := this.GetString("Frequency")
	dataState := this.GetString("DataState")
	keyword := this.GetString("Keyword")
	indexCodes := this.GetString("IndexCodes")
	sortType := this.GetString("SortType", "desc")
	sortParam := this.GetString("SortParam")
	pageSize, _ := this.GetInt("PageSize")
	currentIndex, _ := this.GetInt("CurrentIndex")

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

	var condition string
	var pars []interface{}

	if types != "" {
		typeArr := strings.Split(types,",")
		for i, v := range typeArr {
			typeStr := "type_"
			typeStr += fmt.Sprintf("%d", i+1)
			condition += " AND "+typeStr+" =? "
			pars = append(pars, v)
		}
	}

	if frequency != "" {
		condition += " AND frequency = ? "
		pars = append(pars, frequency)
	}

	if dataState != "" {
		if dataState == "normal" {
			condition += " AND (data_state = 'normal' OR data_state = '') "
			pars = append(pars)
		} else {
			condition += " AND data_state = ? "
			pars = append(pars, dataState)
		}
	}

	if keyword != "" {
		condition += " AND (index_name LIKE ? OR index_code LIKE ?) "
		pars = utils.GetLikeKeywordPars(pars, keyword, 2)
	}

	if indexCodes != "" {
		indexCodeArr := strings.Split(indexCodes,",")
		indexCodeStr := ""
		for _, v := range indexCodeArr {
			indexCodeStr += "'" + v + "',"
		}
		indexCodeStr = strings.TrimRight(indexCodeStr, ",")
		condition += " AND index_code IN (" + indexCodeStr + ") "
	}

	sortStr := ``
	if sortParam != `` {
		sortStr = fmt.Sprintf("%s %s,modify_time desc ", utils.PascalToSnake(sortParam), sortType)
	} else {
		sortStr = " modify_time desc "
	}

	total, err := data_manage.GetSmmIndexDataListCount(condition, pars)
	if err!= nil {
		br.Msg = "获取指标总数失败"
		br.ErrMsg = "获取指标总数失败，Err:" + err.Error()
		return
	}

	indexList, err := data_manage.GetSmmIndexDataList(condition, sortStr, pars, startSize, pageSize)
	if err != nil {
		br.Msg = "获取指标列表失败"
		br.ErrMsg = "获取指标列表失败，Err:" + err.Error()
		return
	}

	for _, v := range indexList {
		v.TypeAll = v.Type1 + "/" + v.Type2 + "/" + v.Type3
	}

	page := paging.GetPaging(currentIndex, pageSize, total)
	var ret data_manage.BaseFromSmmIndexListResp
	ret.List = indexList
	ret.Paging = page
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = ret
}

// SmmApiTypeList
// @Title 有色api数据分类列表
// @Description 有色api数据分类列表
// @Success 200 {object} data_manage.SmmClassify
// @router /smm/api/type/list [get]
func (this *EdbInfoController) SmmApiTypeList() {
	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
	}

	typeList, err := data_manage.GetBaseFromSmmIndexTypeList()
	if err != nil {
		br.Msg = "获取指标列表失败"
		br.ErrMsg = "获取指标列表失败，Err:" + err.Error()
		return
	}

	resp := make([]data_manage.TypeListRespItem, 0)

	typeMap := make(map[string]map[string][]string)
	//type2Map := make(map[string][]string)

	//type2Map := make(map[string]data_manage.TypeListRespItem)

	// 初始化
	for _, v := range typeList {
		if v.Type1 != ""{
			if _, ok := typeMap[v.Type1];!ok {
				typeMap[v.Type1] = make(map[string][]string)
			} else {
				if _, ok := typeMap[v.Type1][v.Type2];!ok {
					typeMap[v.Type1][v.Type2] = make([]string, 0)
				}
			}
		}
	}

	for _, v := range typeList {
		if v.Type1 != ""{
			typeMap[v.Type1][v.Type2] = append(typeMap[v.Type1][v.Type2], v.Type3)
		}
	}

	for type1, type2Map := range typeMap {
		var item data_manage.TypeListRespItem
		item.Type = type1
		for type2, type3List := range type2Map {
			var child data_manage.TypeListRespItem
			child.Type = type2
			for _, type3 := range type3List {
				child.Child = append(child.Child, data_manage.TypeListRespItem{type3,nil})
			}
			item.Child = append(item.Child, child)
		}
		resp = append(resp, item)
	}

	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = resp
}
