package controllers

import (
	"encoding/json"
	"eta/eta_index_lib/logic"
	"eta/eta_index_lib/models"
	"eta/eta_index_lib/services"
	"eta/eta_index_lib/utils"
	"fmt"
	"strconv"
	"time"
)

// BloombergController Bloomberg
type BloombergController struct {
	BaseAuthController
}

// Add
// @Title 新增彭博指标接口
// @Description 新增彭博指标接口
// @Success 200 {object} models.AddEdbInfoReq
// @router /add [post]
func (this *BloombergController) Add() {
	br := new(models.BaseResponse).Init()
	var cacheKey string
	defer func() {
		if br.ErrMsg == "" {
			br.IsSendEmail = false
		}
		_ = utils.Rc.Delete(cacheKey)
		this.Data["json"] = br
		this.ServeJSON()
	}()
	source := utils.DATA_SOURCE_BLOOMBERG
	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
	}
	if req.Source != source {
		br.Msg = "数据源有误"
		br.ErrMsg = fmt.Sprintf("数据源ID不匹配, Source: %d", req.Source)
		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)
		err = models.AddEdbDataFromBloomberg(req.EdbCode)
		if err != nil {
			br.Msg = "获取指标信息失败！"
			br.ErrMsg = "获取指标信息失败 AddEdbDataFromBloomberg，Err:" + err.Error()
			return
		}
		br.Ret = 200
		br.Success = true
		br.Msg = "获取成功"
	} else {
		br.Ret = 501
		br.Success = true
		br.Msg = "系统处理中，请稍后重试"
	}
}

// Refresh
// @Title 刷新彭博指标接口
// @Description 刷新彭博指标接口
// @Success 200 {object} models.RefreshEdbInfoReq
// @router /refresh [post]
func (this *BloombergController) Refresh() {
	br := new(models.BaseResponse).Init()
	var cacheKey string
	defer func() {
		if br.ErrMsg == "" {
			br.IsSendEmail = false
		}
		_ = utils.Rc.Delete(cacheKey)
		this.Data["json"] = br
		this.ServeJSON()
	}()
	source := utils.DATA_SOURCE_BLOOMBERG
	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
	}

	// 获取指标详情
	edbInfo, err := models.GetEdbInfoByEdbCode(source, req.EdbCode)
	if err != nil {
		br.Msg = "指标不存在！"
		br.ErrMsg = "指标不存在"
		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
	}
	dataUpdateTime := time.Now().Format(utils.FormatDateTime)
	utils.Rc.SetNX(cacheKey, 1, 1*time.Minute)
	err = models.RefreshEdbDataFromBloomberg(req.EdbInfoId, req.EdbCode, req.StartDate)
	if err != nil && err.Error() != utils.ErrNoRow() {
		br.Msg = "刷新指标信息失败！"
		br.ErrMsg = "刷新指标信息失败 RefreshEdbDataFromBloomberg，Err:" + err.Error()
		return
	}

	// 更新指标最大最小值
	erDataUpdateDate, err, errMsg := models.UnifiedModifyEdbInfoMaxAndMinInfoDataUpdate(edbInfo, dataUpdateTime)
	if err != nil {
		br.Msg = errMsg
		br.ErrMsg = err.Error()
		return
	}
	// 添加指标刷新成功日志
	if erDataUpdateDate != "" {
		_ = services.AddEdbInfoUpdateLog(edbInfo.EdbInfoId, 1, "", 1, "", 0, 0)
	} else {
		_ = services.AddEdbInfoUpdateLog(edbInfo.EdbInfoId, 1, "", 2, "未刷新到数据", 0, 0)
	}

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

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

// PCSGRefreshDaily
// @Title 中石油新加坡-刷新日度指标
// @Description  中石油新加坡-刷新日度指标
// @Success 200 {object} models.AddEdbInfoReq
// @router /pcsg/refresh_daily [post]
func (this *BloombergController) PCSGRefreshDaily() {
	br := new(models.BaseResponse).Init()
	defer func() {
		if br.ErrMsg == "" {
			br.IsSendEmail = false
		}
		this.Data["json"] = br
		this.ServeJSON()
	}()

	// 获取数据
	indexes, e := services.GetPCSGBloombergDailyFromBridge()
	if e != nil {
		br.Msg = "刷新失败"
		br.ErrMsg = "Bridge-获取PCSG彭博日度指标失败, Err: " + e.Error()
		return
	}
	if len(indexes) == 0 {
		br.Ret = 200
		br.Success = true
		br.Msg = "操作成功"
		return
	}

	// 写入数据
	if e = services.PCSGWrite2BaseBloomberg(indexes); e != nil {
		br.Msg = "刷新失败"
		br.ErrMsg = "PCSG-写入Bloomberg数据源失败, Err: " + e.Error()
		return
	}

	br.Ret = 200
	br.Success = true
	br.Msg = "操作成功"
}

// PCSGRefreshWeekly
// @Title 中石油新加坡-刷新周度指标
// @Description  中石油新加坡-刷新周度指标
// @Success 200 {object} models.AddEdbInfoReq
// @router /pcsg/refresh_weekly [post]
func (this *BloombergController) PCSGRefreshWeekly() {
	br := new(models.BaseResponse).Init()
	defer func() {
		if br.ErrMsg == "" {
			br.IsSendEmail = false
		}
		this.Data["json"] = br
		this.ServeJSON()
	}()

	// 获取数据
	indexes, e := services.GetPCSGBloombergWeeklyFromBridge()
	if e != nil {
		br.Msg = "刷新失败"
		br.ErrMsg = "Bridge-获取PCSG彭博周度指标失败, Err: " + e.Error()
		return
	}
	if len(indexes) == 0 {
		br.Ret = 200
		br.Success = true
		br.Msg = "操作成功"
		return
	}

	// 写入数据
	if e = services.PCSGWrite2BaseBloomberg(indexes); e != nil {
		br.Msg = "刷新失败"
		br.ErrMsg = "PCSG-写入Bloomberg数据源失败, Err: " + e.Error()
		return
	}

	br.Ret = 200
	br.Success = true
	br.Msg = "操作成功"
}

// PCSGRefreshMonthly
// @Title 中石油新加坡-刷新月度指标
// @Description  中石油新加坡-刷新周度指标
// @Success 200 {object} models.AddEdbInfoReq
// @router /pcsg/refresh_monthly [post]
func (this *BloombergController) PCSGRefreshMonthly() {
	br := new(models.BaseResponse).Init()
	defer func() {
		if br.ErrMsg == "" {
			br.IsSendEmail = false
		}
		this.Data["json"] = br
		this.ServeJSON()
	}()

	// 获取数据
	indexes, e := services.GetPCSGBloombergMonthlyFromBridge()
	if e != nil {
		br.Msg = "刷新失败"
		br.ErrMsg = "Bridge-获取PCSG彭博月度指标失败, Err: " + e.Error()
		return
	}
	if len(indexes) == 0 {
		br.Ret = 200
		br.Success = true
		br.Msg = "操作成功"
		return
	}

	// 写入数据
	if e = services.PCSGWrite2BaseBloomberg(indexes); e != nil {
		br.Msg = "刷新失败"
		br.ErrMsg = "PCSG-写入Bloomberg数据源失败, Err: " + e.Error()
		return
	}

	br.Ret = 200
	br.Success = true
	br.Msg = "操作成功"
}

// PCSGImportHistoryData
// @Title 中石油新加坡-导入历史数据
// @Description  中石油新加坡-导入历史数据
// @Success 200 {object} models.PCSGImportHistoryDataReq
// @router /pcsg/import_history_data [post]
func (this *BloombergController) PCSGImportHistoryData() {
	br := new(models.BaseResponse).Init()
	defer func() {
		if br.ErrMsg == "" {
			br.IsSendEmail = false
		}
		this.Data["json"] = br
		this.ServeJSON()
	}()
	var req models.PCSGImportHistoryDataReq
	if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + e.Error()
		return
	}

	var indexes []models.BaseFromBloombergApiIndexAndData
	var index models.BaseFromBloombergApiIndexAndData
	var indexData []models.BaseFromBloombergApiIndexData
	index.IndexCode = req.IndexCode
	for k, v := range req.DataMap {
		indexData = append(indexData, models.BaseFromBloombergApiIndexData{
			DataTime: k,
			Value:    v,
		})
	}
	index.Data = indexData
	indexes = append(indexes, index)

	// 写入数据
	if e := services.PCSGWrite2BaseBloomberg(indexes); e != nil {
		br.Msg = "刷新失败"
		br.ErrMsg = "PCSG-写入Bloomberg数据源失败, Err: " + e.Error()
		return
	}

	br.Ret = 200
	br.Success = true
	br.Msg = "操作成功"
}
