package residual_analysis

import (
	"encoding/json"
	"eta/eta_api/controllers"
	"eta/eta_api/models"
	"eta/eta_api/models/residual_analysis_model"
	"eta/eta_api/services/residual_analysis_service"
)

// ResidualAnalysisController 残差分析
type ResidualAnalysisController struct {
	controllers.BaseAuthController
}

// ResidualAnalysisPreview
// @Title 残差分析预览
// @Description 残差分析预览
// @Success 200 {object} residual_analysis_model.ResidualAnalysisResp
// @router /residual/analysis/preview [post]
func (this *ResidualAnalysisController) ResidualAnalysisPreview() {
	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
	}

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

	if req.EdbInfoIdA == req.EdbInfoIdB {
		br.Msg = "自变量指标和因变量指标不能相同"
		br.ErrMsg = "自变量指标和因变量指标不能相同"
		return
	}

	resp, err := residual_analysis_service.ResidualAnalysisPreview(req)
	if err != nil {
		br.Ret = 403
		br.Msg = "获取失败"
		br.ErrMsg = err.Error()
		return
	}

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

// ContrastPreview
// @Title 对比指标预览
// @Description 对比指标预览
// @Success 200 {object} residual_analysis_model.ResidualAnalysisChartEdbInfoMapping
// @router /contrast/preview [get]
func (this *ResidualAnalysisController) ContrastPreview() {
	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
	}

	IndexCode := this.GetString("IndexCode")
	if IndexCode == "" {
		br.Ret = 403
		br.Msg = "IndexCode不能为空"
		br.ErrMsg = "IndexCode不能为空"
		return
	}

	resp, err := residual_analysis_service.ContrastPreview(IndexCode)
	if err != nil {
		return
	}

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

// SaveResidualAnalysis
// @Title 保存残差分析指标
// @Description 保存残差分析指标
// @Success 200
// @router /save/residual/analysis [post]
func (this *ResidualAnalysisController) SaveResidualAnalysis() {
	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
	}

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

	if req.ClassifyId == 0 {
		br.Msg = "分类id不能为空"
		br.ErrMsg = "分类id不能为空"
		return
	}

	if req.Source == 0 {
		br.Msg = "来源不能为空"
		br.ErrMsg = "来源不能为空"
		return
	}

	err = residual_analysis_service.SaveResidualAnalysis(req, sysUser)
	if err != nil {
		br.Ret = 403
		br.Msg = err.Error()
		br.ErrMsg = err.Error()
		return
	}

	br.Ret = 200
	br.Success = true
	br.Msg = "添加成功"
}

// SaveResidualAnalysisConfig
// @Title 保存残差指标配置
// @Description 保存残差指标配置
// @Success 200
// @router /save/residual/analysis/config [post]
func (this *ResidualAnalysisController) SaveResidualAnalysisConfig() {
	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
	}

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

	configId, err := residual_analysis_service.SaveResidualAnalysisConfig(req, sysUser)
	if err != nil {
		br.Ret = 403
		br.Msg = "保存失败"
		br.ErrMsg = err.Error()
		return
	}

	br.Ret = 200
	br.Success = true
	br.Msg = "添加成功"
	br.Data = configId
}

// ResidualAnalysisDetail
// @Title 残差分析指标详情
// @Description 残差分析指标详情
// @Success 200 {object} []*data_manage.EdbInfoList
// @router /residual/analysis/detail [get]
func (this *ResidualAnalysisController) ResidualAnalysisDetail() {
	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
	}

	EdbInfoId, err := this.GetInt("EdbInfoId")
	if err != nil {
		br.Msg = "EdbInfoId参数异常!"
		br.ErrMsg = "EdbInfoId参数解析失败,Err:" + err.Error()
	}
	if EdbInfoId <= 0 {
		br.Msg = "EdbInfoId参数异常!"
		br.ErrMsg = "EdbInfoId参数异常!"
	}

	resp, err := residual_analysis_service.ResidualAnalysisDetail(EdbInfoId)
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = err.Error()
		br.Ret = 403
		return
	}

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

// CheckResidualAnalysisExist
// @Title 校验残差指标是否存在
// @Description 校验残差指标是否存在
// @Success 200 {object}
// @router /check/residual/analysis/exist [get]
func (this *ResidualAnalysisController) CheckResidualAnalysisExist() {
	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
	}

	configId, err := this.GetInt("ConfigId")
	if err != nil {
		br.Msg = "EdbInfoId参数异常!"
		br.ErrMsg = "EdbInfoId参数解析失败,Err:" + err.Error()
	}
	if configId <= 0 {
		br.Msg = "ConfigId参数异常!"
		br.ErrMsg = "ConfigId参数异常!"
	}

	resp, err := residual_analysis_service.CheckResidualAnalysisExist(configId)
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = err.Error()
		br.Ret = 403
		return
	}

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