package controllers

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

// StockPlantController 存量装置
type StockPlantController struct {
	BaseAuthController
}

// Calculate
// @Title 计算减产数
// @Description 计算减产数
// @Success 200 {object} supply_analysis.CalculateVarietyReq
// @router /calculate [post]
func (this *StockPlantController) Calculate() {
	br := new(models.BaseResponse).Init()
	var cacheKey string
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	var req supply_analysis.CalculateVarietyReq
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}
	if req.VarietyId <= 0 {
		br.Msg = "请选择品种！"
		br.ErrMsg = "请选择品种"
		return
	}

	//cacheKey = utils.CACHE_STOCK_PLANT_CALCULATE + strconv.Itoa(req.VarietyId)
	//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)
	}()

	err = supply_analysis.Calculate(req.VarietyId, req.AdminId, req.AdminName)
	if err != nil {
		br.Msg = "获取指标信息失败！"
		br.ErrMsg = "获取指标信息失败 AddEdbDataFromManual，Err:" + err.Error()
		return
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
}

// @Title 刷新手工指标接口
// @Description 刷新手工指标接口
// @Success 200 {object} models.RefreshEdbInfoReq
// @router /refresh [post]
func (this *StockPlantController) Refresh() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	source := utils.DATA_SOURCE_STOCK_PLANT
	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)
	defer func() {
		utils.Rc.Delete(cacheKey)
	}()
	// 获取指标详情
	edbInfo, err := models.GetEdbInfoByEdbCode(source, req.EdbCode)
	if err != nil {
		br.Msg = "指标不存在！"
		br.ErrMsg = "指标不存在"
		return
	}

	req.StartDate = `` // 开始日期也给过滤吧，万一研究员补充了之前的数据呢，还是过滤掉吧
	err = models.RefreshEdbDataFromStockPlant(req.EdbInfoId, req.EdbCode, req.StartDate)
	if err != nil && err.Error() != utils.ErrNoRow() {
		br.Msg = "刷新指标信息失败！"
		br.ErrMsg = "刷新指标信息失败 RefreshEdbDataFromManual，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 = "获取成功"
}
