package data_manage

import (
	"eta/eta_api/controllers/data_manage/correlation"
	"eta/eta_api/controllers/data_manage/future_good"
	"eta/eta_api/controllers/data_manage/line_equation"
	"eta/eta_api/controllers/data_manage/line_feature"
	"eta/eta_api/models"
	"eta/eta_api/models/data_manage"
	"eta/eta_api/utils"
)

// CommonChartInfoDetailFromUniqueCode
// @Title 根据编码获取图表详情
// @Description 根据编码获取图表详情接口
// @Param   UniqueCode   query   int  true       "图表唯一编码,如果是管理后台访问，传固定字符串:7c69b590249049942070ae9dcd5bf6dc"
// @Param   IsCache   query   bool  true       "是否走缓存，默认false"
// @Success 200 {object} data_manage.ChartInfoDetailFromUniqueCodeResp
// @router /chart_info/common/detail/from_unique_code [get]
func (this *ChartInfoController) CommonChartInfoDetailFromUniqueCode() {
	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
	}
	uniqueCode := this.GetString("UniqueCode")
	if uniqueCode == "" {
		br.Msg = "参数错误"
		br.ErrMsg = "参数错误,uniqueCode is empty"
		return
	}

	//是否走缓存
	isCache, _ := this.GetBool("IsCache")

	status := true
	chartInfo, err := data_manage.GetChartInfoViewByUniqueCode(uniqueCode)
	if err != nil {
		if err.Error() == utils.ErrNoRow() {
			status = false
		} else {
			br.Msg = "获取失败"
			br.ErrMsg = "获取图表信息失败，Err:" + err.Error()
			return
		}
	}

	if chartInfo == nil {
		status = false
	}

	if !status {
		resp := new(data_manage.ChartInfoDetailFromUniqueCodeResp)
		endInfoList := make([]*data_manage.ChartEdbInfoMapping, 0)
		resp.EdbInfoList = endInfoList
		resp.ChartInfo = chartInfo
		resp.Status = false

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

	switch chartInfo.Source {
	case utils.CHART_SOURCE_DEFAULT:
		resp, isOk, msg, errMsg := GetChartInfoDetailFromUniqueCode(chartInfo, isCache, sysUser)
		if !isOk {
			br.Msg = msg
			br.ErrMsg = errMsg
			return
		}
		br.Ret = 200
		br.Success = true
		br.Msg = "获取成功"
		br.Data = resp
	case utils.CHART_SOURCE_FUTURE_GOOD:
		resp, isOk, msg, errMsg := future_good.GetChartInfoDetailFromUniqueCode(chartInfo, isCache, sysUser)
		if !isOk {
			br.Msg = msg
			br.ErrMsg = errMsg
			return
		}
		br.Ret = 200
		br.Success = true
		br.Msg = "获取成功"
		br.Data = resp
	case utils.CHART_SOURCE_FUTURE_GOOD_PROFIT:
		resp, isOk, msg, errMsg := future_good.GetFutureGoodProfitChartInfoDetailFromUniqueCode(chartInfo, isCache, sysUser)
		if !isOk {
			br.Msg = msg
			br.ErrMsg = errMsg
			return
		}
		br.Ret = 200
		br.Success = true
		br.Msg = "获取成功"
		br.Data = resp
	case utils.CHART_SOURCE_CORRELATION, utils.CHART_SOURCE_ROLLING_CORRELATION:
		resp, isOk, msg, errMsg := correlation.GetChartInfoDetailFromUniqueCode(chartInfo, isCache, sysUser)
		if !isOk {
			br.Msg = msg
			br.ErrMsg = errMsg
			return
		}
		br.Ret = 200
		br.Success = true
		br.Msg = "获取成功"
		br.Data = resp
	case utils.CHART_SOURCE_LINE_EQUATION:
		resp, isOk, msg, errMsg := line_equation.GetChartInfoDetailFromUniqueCode(chartInfo, isCache, sysUser)
		if !isOk {
			br.Msg = msg
			br.ErrMsg = errMsg
			return
		}
		br.Ret = 200
		br.Success = true
		br.Msg = "获取成功"
		br.Data = resp
	case utils.CHART_SOURCE_LINE_FEATURE_STANDARD_DEVIATION, utils.CHART_SOURCE_LINE_FEATURE_PERCENTILE, utils.CHART_SOURCE_LINE_FEATURE_FREQUENCY:
		resp, isOk, msg, errMsg := line_feature.GetChartInfoDetailFromUniqueCode(chartInfo, isCache, sysUser)
		if !isOk {
			br.Msg = msg
			br.ErrMsg = errMsg
			return
		}
		br.Ret = 200
		br.Success = true
		br.Msg = "获取成功"
		br.Data = resp
	default:
		br.Msg = "错误的图表"
		br.ErrMsg = "错误的图表"
		return
	}
}
