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"
	"math"
	"strconv"
	"time"
)

// 有色
type SmmController struct {
	BaseAuthController
}

// @Title 新增有色指标接口
// @Description 新增有色指标接口
// @Success 200 {object} models.AddEdbInfoReq
// @router /add [post]
func (this *SmmController) 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_YS
	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)
		smmBaseDataAllList, err := services.GetEdbDataFromSmm(req.EdbCode)
		if err!= nil {
			br.Msg = "获取指标信息失败！"
			br.ErrMsg = "获取指标信息失败 GetEdbDataFromSmm，Err:" + err.Error()
			return
		}
		for _, item := range smmBaseDataAllList {
			err = models.AddEdbDataFromSmm(item.IndexCode, item.Data)
			if err != nil {
				br.Msg = "获取指标信息失败！"
				br.ErrMsg = "获取指标信息失败 AddEdbDataFromSmm，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 *SmmController) Refresh() {
	br := new(models.BaseResponse).Init()
	var cacheKey string
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	source := utils.DATA_SOURCE_YS
	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
	}
	utils.Rc.SetNX(cacheKey, 1, 1*time.Minute)
	defer func() {
		utils.Rc.Delete(cacheKey)
	}()
	if req.EdbInfoId <= 0 {
		req.EdbInfoId = edbInfo.EdbInfoId
	}
	if utils.BusinessCode == "E2023110300" {
		// 中基宁波走API更新
		err = services.GetSmmIndexLatestFromBridge(req.EdbInfoId, req.EdbCode, req.StartDate)
		if err != nil && err.Error() != utils.ErrNoRow() {
			br.Msg = "刷新指标信息失败！"
			br.ErrMsg = "刷新指标信息失败 RefreshEdbDataFromSmm，Err:" + err.Error()
			return
		}
	} else {
		err = models.RefreshEdbDataFromSmm(req.EdbInfoId, req.EdbCode, req.StartDate)
		if err != nil && err.Error() != utils.ErrNoRow() {
			br.Msg = "刷新指标信息失败！"
			br.ErrMsg = "刷新指标信息失败 RefreshEdbDataFromSmm，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 = "获取成功"
}

// HandleExcelData
// @Title 处理有色指标的接口
// @Description 处理有色指标的接口
// @Success 200 string "操作成功"
// @router /handle/excel_data [post]
func (this *SmmController) HandleExcelData() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()

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

	// 处理excel表数据
	e, errMsg := services.SmmIndexHandle(req.BaseFilePath, req.RenameFilePath, req.IndexName, req.IndexCode, req.Unit, req.Frequency, req.Source, req.ExcelDataMap, req.TerminalCode)
	if e != nil {
		br.Msg = "处理失败"
		br.ErrMsg = fmt.Sprintf("处理有色excel表数据失败, Err: %s, ErrMsg: %s", e.Error(), errMsg)
		return
	}

	br.Ret = 200
	br.Success = true
	br.Msg = "处理成功"
}

// AddIndexToDataSource
// @Title 添加指标到数据源-SMM
// @Description 添加指标到数据源-SMM
// @Success 200 {object} models.AddSmmIndexToDataSourceResp
// @router /add/index/to_data_source [post]
func (this *SmmController) AddIndexToDataSource() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	var req models.AddSmmIndexToDataSourceResp
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}
	smmIndexObj := new(models.BaseFromSmmIndex)
	indexItem, err := smmIndexObj.GetSmmIndexItem(req.EdbCode)
	var isAdd bool
	if err != nil {
		if err.Error() == utils.ErrNoRow() {
			isAdd = true
		} else {
			br.Msg = "判断指标是否存在失败"
			br.ErrMsg = "判断指标是否存在失败,Err:" + err.Error()
			return
		}
	}
	if indexItem == nil {
		isAdd = true
	}
	if isAdd {
		smmIndexObj.IndexCode = req.EdbCode
		smmIndexObj.ClassifyId = req.ClassifyId
		smmIndexObj.CreateTime = time.Now()
		smmIndexObj.ModifyTime = time.Now()
		err = smmIndexObj.Add()
		if err != nil {
			br.Msg = "新增指标失败"
			br.ErrMsg = "新增指标失败,Err:" + err.Error()
			return
		}
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "新增成功"
}

// GetIndexDetailByCode
// @Title 获取指标详情
// @Description 获取指标详情
// @Success 200 {object} models.GetIndexDetailReq
// @router /index_detail/from_data_source [post]
func (this *SmmController) GetIndexDetailFromDataSourceByCode() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	var req models.GetIndexDetailReq
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}
	if req.IndexCode == "" {
		br.Msg = "参数有误"
		br.ErrMsg = "参数有误"
		return
	}

	indexObj := new(models.BaseFromSmmIndex)
	item, err := indexObj.GetIndexItem(req.IndexCode)
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取指标失败, Err: " + err.Error()
		return
	}
	br.Data = item
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
}

// @Title 新增有色指标接口
// @Description 新增有色指标接口
// @Success 200 {object} models.AddEdbInfoReq
// @router /zhongji/list [post]
func (this *SmmController) List() {
	br := new(models.BaseResponse).Init()
	var cacheKey string
	defer func() {
		utils.Rc.Delete(cacheKey)
		this.Data["json"] = br
		this.ServeJSON()
	}()

	list, err := models.GetBaseFromSmmIndex()
	if err!= nil {
		br.Msg = "获取指标失败"
		br.ErrMsg = "获取指标失败, Err: " + err.Error()
		return
	}

	indexCodeMap := make(map[string]*models.BaseFromSmmIndex)
	for _, item := range list {
		indexCodeMap[item.IndexCode] = item
	}

	// 从桥接服务获取指标和数据
	var params models.BridgeZhongJiIndexListParams
	params.Page = 1
	params.PageSize = 500
	total, err := services.GetSmmNewIndexListTotalFromBridge(params)
	if err!= nil {
		br.Msg = "获取指标失败"
		br.ErrMsg = "获取指标数据失败, Err: " + err.Error()
		return
	}
	if total <= 0 {
		br.Msg = "没有指标数据"
		br.ErrMsg = "没有指标数据"
		return
	}
	var addList []*models.BaseFromSmmIndex
	for i := 1; i <= int(math.Ceil(float64(total)/float64(params.PageSize))); i++ {
		time.Sleep(time.Second * 2)
		params.Page = i
		indexList, err := services.GetSmmNewIndexListFromBridge(params)
		if err!= nil {
			br.Msg = "获取指标失败"
			br.ErrMsg = "获取指标数据失败, Err: " + err.Error()
		}
		for _, item := range indexList {
			if _, ok := indexCodeMap[item.IndexCode]; !ok {
				addList = append(addList, item)
				indexCodeMap[item.IndexCode] = item
			} else {
				if indexCodeMap[item.IndexCode].DataState != item.DataState || indexCodeMap[item.IndexCode].EndDate != item.EndDate {
					err = models.ModifyBaseFromSmmIndex(item)
					if err != nil {
						br.Msg = "更新指标失败"
						br.ErrMsg = "更新指标失败,Err:" + err.Error()
						return
					}
					fmt.Println("更新数据：",item.IndexCode)
				}
			}
		}

		if len(addList) > 0 {
			_,err = models.AddBaseFromSmmIndex(addList)
			if err!= nil {
				br.Msg = "新增指标失败"
				br.ErrMsg = "新增指标失败,Err:" + err.Error()
				return
			}
			//fmt.Println("新增数据：",len(addList))
			addList = []*models.BaseFromSmmIndex{}
		}
	}


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