package data_manage

import (
	"encoding/json"
	"eta/eta_api/controllers/data_manage/correlation"
	"eta/eta_api/controllers/data_manage/cross_variety"
	"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/models/system"
	"eta/eta_api/services/data"
	"eta/eta_api/services/data/excel"
	"eta/eta_api/utils"
	"fmt"
	"time"
)

// 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
	case utils.CHART_SOURCE_CROSS_HEDGING:
		resp, isOk, msg, errMsg := cross_variety.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_BALANCE_EXCEL:
		resp, isOk, msg, errMsg := getBalanceChartInfoDetailFromUniqueCode(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
	}
}

func getBalanceChartInfoDetailFromUniqueCode(chartInfo *data_manage.ChartInfoView, isCache bool, sysUser *system.Admin) (resp *data_manage.ChartInfoDetailFromUniqueCodeResp, isOk bool, msg, errMsg string) {
	resp = new(data_manage.ChartInfoDetailFromUniqueCodeResp)
	resp, isOk, msg, errMsg = data.CheckBalanceChartCacheAndPermission(chartInfo, isCache, sysUser)
	if isOk {
		return
	}
	msg = `获取失败`
	// 相关联指标
	mappingListTmp, dataListMap, err, errMsg := excel.GetBalanceExcelChartSingle(chartInfo.ChartInfoId, 0, "")
	if err != nil {
		errMsg = "获取失败"
		err = fmt.Errorf(" 获取图表，指标信息失败 Err:%s", err.Error())
		return
	}
	var chartInfoResp *data_manage.ChartInfoDetailResp
	chartInfoResp, err, errMsg = data.GetBalanceExcelChartDetail(chartInfo, mappingListTmp, sysUser, dataListMap)
	if err != nil {
		msg = "查询图表详情失败"
		errMsg = "查询图表详情失败，Err:" + err.Error()
		return
	}
	resp = &data_manage.ChartInfoDetailFromUniqueCodeResp{
		ChartInfo:            chartInfoResp.ChartInfo,
		Status:               true,
		EdbInfoList:          chartInfoResp.EdbInfoList,
		XEdbIdValue:          chartInfoResp.XEdbIdValue,
		YDataList:            chartInfoResp.YDataList,
		XDataList:            chartInfoResp.XDataList,
		BarChartInfo:         chartInfoResp.BarChartInfo,
		CorrelationChartInfo: chartInfoResp.CorrelationChartInfo,
		DataResp:             chartInfoResp.DataResp,
	}
	// 将数据加入缓存
	if utils.Re == nil {
		cacheData, _ := json.Marshal(resp)
		key := data.GetChartInfoDataKey(chartInfo.ChartInfoId)
		utils.Rc.Put(key, cacheData, 2*time.Hour)
	}
	isOk = true

	return
}
