package data_manage

import (
	"encoding/json"
	"eta/eta_mobile/models"
	"eta/eta_mobile/models/data_manage"
	"eta/eta_mobile/services/data"
	"eta/eta_mobile/utils"
	"strings"
)

// GetChartTypeList
// @Title 获取所有图表类型
// @Description 获取所有图表类型
// @Success 200 {object} data_manage.AddChartInfoResp
// @router /chart_info/type_list [get]
func (this *ChartInfoController) GetChartTypeList() {
	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
	}

	listTmp, err := data_manage.GetChartTypeList()
	if err != nil {
		br.Msg = "获取图表类型失败"
		br.ErrMsg = "获取图表类型失败，Err:" + err.Error()
		return
	}
	//遍历list，将id和name组成map
	chartTypeMap := make(map[int][]data_manage.ChartType)
	for _, v := range listTmp {
		if v.ParentId > 0 {
			chartTypeMap[v.ParentId] = append(chartTypeMap[v.ParentId], v)
		}
	}
	list := make([]data_manage.ChartTypeList, 0)
	for _, v := range listTmp {
		if v.ParentId == 0 {
			tmp := data_manage.ChartTypeList{
				ChartTypeId:     v.ChartTypeId,
				ChartTypeName:   v.ChartTypeName,
				ChartTypeNameEn: v.ChartTypeNameEn,
				ParentId:        v.ParentId,
			}
			child, ok := chartTypeMap[v.ChartTypeId]
			if ok {
				tmp.Child = child
			}
			list = append(list, tmp)
		}
	}

	resp := data_manage.ChartTypeListResp{List: list}
	br.Data = resp
	br.Ret = 200
	br.Success = true
	br.Msg = "获取图表类型成功"

}

// PreviewSectionCombineChartInfo
// @Title 图表-获取预览的截面组合图数据
// @Description 图表-获取预览的截面组合图数据
// @Param	request	body data_manage.PreviewSectionCombineChartReq true "type json string"
// @Success 200 {object} data_manage.ChartEdbInfoDetailResp
// @router /chart_info/preview/section_combine [post]
func (this *ChartInfoController) PreviewSectionCombineChartInfo() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()

	var req data_manage.PreviewSectionCombineChartReq
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}

	chartInfo := new(data_manage.ChartInfoView)

	sysUser := this.SysUser
	if sysUser == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录，SysUser Is Empty"
		br.Ret = 408
		return
	}

	chartType := utils.CHART_TYPE_SECTION_COMBINE
	edbInfoIdArr, err, errMsg := data.CheckChartExtraConfig(chartType, req.ExtraConfig)
	if err != nil {
		br.Msg = errMsg
		br.ErrMsg = "添加失败:" + err.Error()
		return
	}
	chartInfo.ChartType = chartType
	mappingList, err := data_manage.GetChartEdbMappingListByEdbInfoIdList(edbInfoIdArr)

	// 获取图表中的指标数据
	edbList, xEdbIdValue, yDataList, dataResp, err, errMsg := data.GetChartEdbData(0, chartType, "", "", "", mappingList, req.ExtraConfig, "")
	if err != nil {
		br.Msg = "获取失败"
		if errMsg != `` {
			br.Msg = errMsg
		}
		br.ErrMsg = "获取图表，指标信息失败，Err:" + err.Error()
		return
	}
	warnEdbList := make([]string, 0)
	for _, v := range edbList {
		if v.IsNullData {
			warnEdbList = append(warnEdbList, v.EdbName+"("+v.EdbCode+")")
		}
	}
	if len(warnEdbList) > 0 {
		chartInfo.WarnMsg = `图表引用指标异常，异常指标：` + strings.Join(warnEdbList, "，")
	}

	// 图表的指标来源
	sourceNameList, sourceNameEnList := data.GetEdbSourceByEdbInfoIdList(edbList)
	chartInfo.ChartSource = strings.Join(sourceNameList, ",")
	chartInfo.ChartSourceEn = strings.Join(sourceNameEnList, ",")

	//图表操作权限
	chartInfo.IsEdit = data.CheckOpChartPermission(sysUser, chartInfo.SysUserId, true)
	//判断是否需要展示英文标识
	chartInfo.IsEnChart = data.CheckIsEnChart(chartInfo.ChartNameEn, edbList, utils.CHART_SOURCE_DEFAULT, chartType)
	chartInfo.Button = data_manage.ChartViewButton{
		IsEdit:    chartInfo.IsEdit,
		IsEnChart: chartInfo.IsEnChart,
		IsAdd:     chartInfo.IsAdd,
		IsCopy:    true,
		IsSetName: chartInfo.IsSetName,
	}

	resp := new(data_manage.ChartInfoDetailResp)
	resp.ChartInfo = chartInfo
	resp.EdbInfoList = edbList
	resp.XEdbIdValue = xEdbIdValue
	resp.YDataList = yDataList
	resp.DataResp = dataResp
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = resp
}
