package controllers

import (
	"encoding/json"
	"hongze/hongze_edb_lib/logic"
	"hongze/hongze_edb_lib/models"
	"hongze/hongze_edb_lib/services"
	"hongze/hongze_edb_lib/utils"
	"strconv"
	"strings"
	"time"
)

// 富宝-数据对接
type FuBaoController struct {
	BaseAuthController
}

// @Title 新增富宝指标接口
// @Description  新增富宝指标接口
// @Success 200 {object} models.AddEdbInfoReq
// @router /add [post]
func (this *FuBaoController) Add() {
	br := new(models.BaseResponse).Init()
	var cacheKey string
	defer func() {
		utils.Rc.Delete(cacheKey)
		this.Data["json"] = br
		this.ServeJSON()
	}()
	source := utils.DATA_SOURCE_FUBAO
	var req models.AddEdbInfoReq
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}
	if req.EdbCode == "" {
		br.Msg = "请输入指标编码！"
		br.ErrMsg = "请输入指标编码，指标编码为空"
		return
	}
	cacheKey = utils.CACHE_EDB_DATA_ADD + strconv.Itoa(source) + "_" + req.EdbCode
	if !utils.Rc.IsExist(cacheKey) {
		utils.Rc.SetNX(cacheKey, 1, 1*time.Minute)
		dataItem, err := services.GetEdbDataFromFuBao(req.EdbCode, utils.BASE_START_DATE, utils.BASE_END_DATE)
		if err != nil {
			br.Msg = "获取指标信息失败！"
			br.ErrMsg = "获取指标信息失败 GetEdbDataFromLt，Err:" + err.Error()
			return
		}
		if dataItem.Code != 200 {
			br.Msg = "获取富宝指标数据失败！"
			br.ErrMsg = "获取富宝指标数据失败 GetEdbDataFromLt，Code:" + strconv.Itoa(dataItem.Code)
			return
		}
		if len(dataItem.ReturnValue.Data) > 0 {
			priceStr := dataItem.ReturnValue.Data[0].PriceStr
			strIndex := strings.Index(priceStr, "-")
			if strIndex > 0 {
				br.Msg = "此指标的数据格式暂不支持！"
				br.ErrMsg = "此指标的数据格式暂不支持，Val:" + priceStr + ";pid:" + req.EdbCode
				return
			}
		}

		err = models.AddEdbDataFromFubao(req.EdbCode, dataItem)
		if err != nil {
			br.Msg = "获取指标信息失败！"
			br.ErrMsg = "获取指标信息失败 AddEdbDataFromFubao，Err:" + err.Error()
			return
		}
		br.Ret = 200
		br.Success = true
		br.Msg = "获取成功"
	} else {
		br.Ret = 501
		br.Success = true
		br.Msg = "系统处理中，请稍后重试"
	}
}

// @Title 刷新富宝指标接口
// @Description 刷新路透社指标接口
// @Success 200 {object} models.RefreshEdbInfoReq
// @router /refresh [post]
func (this *FuBaoController) Refresh() {
	br := new(models.BaseResponse).Init()
	var cacheKey string
	defer func() {
		utils.Rc.Delete(cacheKey)
		this.Data["json"] = br
		this.ServeJSON()
	}()
	source := utils.DATA_SOURCE_FUBAO
	var req models.RefreshEdbInfoReq
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}
	if req.EdbCode == "" {
		br.Msg = "请输入指标编码！"
		br.ErrMsg = "请输入指标编码，指标编码为空"
		return
	}
	if req.EdbInfoId <= 0 {
		br.Msg = "请输入指标ID！"
		br.ErrMsg = "请输入指标ID"
		return
	}
	cacheKey = utils.CACHE_EDB_DATA_REFRESH + strconv.Itoa(source) + "_" + req.EdbCode
	if utils.Rc.IsExist(cacheKey) {
		br.Ret = 501
		br.Success = true
		br.Msg = "系统处理中，请稍后重试"
		return
	}
	utils.Rc.SetNX(cacheKey, 1, 1*time.Minute)

	// 获取指标详情
	edbInfo, err := models.GetEdbInfoByEdbCode(source, req.EdbCode)
	if err != nil {
		br.Msg = "指标不存在！"
		br.ErrMsg = "指标不存在"
		return
	}

	dataItem, err := services.GetEdbDataFromFuBao(req.EdbCode, req.StartDate, utils.BASE_END_DATE)
	if err != nil {
		br.Msg = "获取指标信息失败！"
		br.ErrMsg = "获取指标信息失败 GetEdbDataFromFuBao，Err:" + err.Error()
		return
	}
	err = models.RefreshEdbDataFromFubao(req.EdbInfoId, req.EdbCode, req.StartDate, dataItem)
	if err != nil && err.Error() != utils.ErrNoRow() {
		br.Msg = "刷新指标信息失败！"
		br.ErrMsg = "刷新指标信息失败 RefreshEdbDataFromFubao，Err:" + err.Error()
		return
	}
	// 更新指标最大最小值
	err, errMsg := models.UnifiedModifyEdbInfoMaxAndMinInfo(edbInfo)
	if err != nil {
		br.Msg = errMsg
		br.ErrMsg = err.Error()
		return
	}

	// 更新ES
	go logic.UpdateEs(edbInfo.EdbInfoId)

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