package controllers

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

// 指标
type EdbInfoController struct {
	BaseAuthController
}

// @Title 新增指标接口
// @Description  新增指标接口
// @Success 200 {object} models.AddEdbInfoParams
// @router /add [post]
func (this *EdbInfoController) Add() {
	br := new(models.BaseResponse).Init()
	var cacheKey string
	defer func() {
		utils.Rc.Delete(cacheKey)
		this.Data["json"] = br
		this.ServeJSON()
	}()

	var req models.AddEdbInfoParams
	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
	}
	deleteCache := true
	cacheKey = "CACHE_EDB_INFO_ADD_" + req.EdbCode
	defer func() {
		if deleteCache {
			utils.Rc.Delete(cacheKey)
		}
	}()
	if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
		deleteCache = false
		br.Msg = "系统处理中，请稍后重试！"
		br.ErrMsg = "系统处理中，请稍后重试！"
		return
	}

	if req.ClassifyId <= 0 {
		br.Msg = "请输入指标分类！"
		br.ErrMsg = "请输入指标分类"
		return
	}
	if req.Source <= 0 {
		br.Msg = "请输入指标来源！"
		br.ErrMsg = "请输入指标来源"
		return
	}
	if req.EdbName == "" {
		br.Msg = "请输入指标名称！"
		br.ErrMsg = "请输入指标名称"
		return
	}
	if req.Frequency == "" {
		br.Msg = "请输入指标频度！"
		br.ErrMsg = "请输入指标频度"
		return
	}
	if req.Unit == "" {
		br.Msg = "请输入指标单位！"
		br.ErrMsg = "请输入指标单位"
		return
	}
	var isAdd bool
	item, err := models.GetEdbInfoByEdbCode(req.Source, req.EdbCode)
	if err != nil {
		if err.Error() == utils.ErrNoRow() {
			isAdd = true
		} else {
			br.Msg = "判断指标是否存在失败！"
			br.ErrMsg = "判断指标是否存在失败"
			return
		}
	}
	if item != nil && item.EdbInfoId <= 0 {
		isAdd = true
	}
	var windUrl string
	if isAdd {
		if req.Source == utils.DATA_SOURCE_WIND {
			windUrl, err = GetServerUrl()
			if err != nil {
				br.Msg = "判断失败！"
				br.ErrMsg = "判断失败,Err:" + err.Error()
				return
			}
		}
	}

	adminId, _ := strconv.Atoi(utils.InitAdminId)
	if req.AdminId > 0 {
		adminId = req.AdminId
	}
	adminName := utils.InitAdminName
	if req.AdminName != "" {
		adminName = strings.TrimSpace(req.AdminName)
	}
	edbInfo, err := models.EdbInfoAdd(&req, windUrl, adminId, adminName)
	if err != nil {
		br.Msg = "新增指标失败！"
		br.ErrMsg = "新增指标失败,Err:" + err.Error()
		return
	}
	// 更新ES
	go logic.UpdateEs(edbInfo.EdbInfoId)
	br.Ret = 200
	br.Msg = "新增成功"
	br.Data = edbInfo
}

// @Title 获取基础指标详情
// @Description 获取基础指标详情
// @Param	request	body models.EdbInfoDetailReq true "type json string"
// @Success 200 {object} models.EdbInfo
// @router /detail [post]
func (this *EdbInfoController) EdbIndexDetail() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	var req models.EdbInfoDetailReq
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}

	if req.EdbCode == "" {
		br.Msg = "请输入指标编码！"
		return
	}
	detail, err := models.GetEdbInfoOnlyByEdbCode(req.EdbCode)
	if err != nil {
		if err.Error() == utils.ErrNoRow() {
			br.Msg = "指标数据不存在！"
			br.ErrMsg = "指标数据不存在，Err:" + err.Error()
			br.ErrCode = "400"
			return
		}
		br.Msg = "获取数据失败！"
		br.ErrMsg = "获取数据失败，Err:" + err.Error()
		return
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = detail
}

// @Title 复制新增来自弘则的指标接口
// @Description  复制新增来自弘则的指标接口
// @Success 200 {object} models.EdbInfo
// @router /copy/from/hz [post]
func (this *EdbInfoController) CopyFromHz() {
	br := new(models.BaseResponse).Init()
	var cacheKey string
	defer func() {
		utils.Rc.Delete(cacheKey)
		this.Data["json"] = br
		this.ServeJSON()
	}()

	req := new(models.EdbInfo)
	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
	}
	deleteCache := true
	cacheKey = "CACHE_EDB_INFO_COPY_FROM_HZ_" + req.EdbCode
	defer func() {
		if deleteCache {
			utils.Rc.Delete(cacheKey)
		}
	}()
	if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
		deleteCache = false
		br.Msg = "系统处理中，请稍后重试！"
		br.ErrMsg = "系统处理中，请稍后重试！"
		return
	}

	if req.ClassifyId <= 0 {
		br.Msg = "请输入指标分类！"
		br.ErrMsg = "请输入指标分类"
		return
	}
	if req.Source <= 0 {
		br.Msg = "请输入指标来源！"
		br.ErrMsg = "请输入指标来源"
		return
	}
	if req.EdbName == "" {
		br.Msg = "请输入指标名称！"
		br.ErrMsg = "请输入指标名称"
		return
	}
	if req.Frequency == "" {
		br.Msg = "请输入指标频度！"
		br.ErrMsg = "请输入指标频度"
		return
	}
	if req.Unit == "" {
		br.Msg = "请输入指标单位！"
		br.ErrMsg = "请输入指标单位"
		return
	}
	var isAdd bool
	item, err := models.GetEdbInfoByEdbCode(req.Source, req.EdbCode)
	if err != nil {
		if err.Error() == utils.ErrNoRow() {
			isAdd = true
		} else {
			br.Msg = "判断指标是否存在失败！"
			br.ErrMsg = "判断指标是否存在失败"
			return
		}
	}
	if item != nil && item.EdbInfoId <= 0 {
		isAdd = true
	}
	if isAdd {
		req.EdbInfoId = 0
		req.StartDate = "1990-01-01"
		err = req.Add()
		if err != nil {
			br.Msg = "新增指标失败！"
			br.ErrMsg = "新增指标失败"
			return
		}
	}
	edbInfo, err := models.GetEdbInfoOnlyByEdbCode(req.EdbCode)
	// 更新ES
	go logic.UpdateEs(edbInfo.EdbInfoId)
	br.Ret = 200
	br.Msg = "新增成功"
	br.Data = edbInfo
}

// RefreshCheck @Title 指标数据更新检查
// @Description 指标数据更新检查
// @Param	request	body models.EdbInfoRefreshCheckReq true "type json string"
// @Success 200 {object} models.EdbInfo
// @router /refresh_check [post]
func (this *EdbInfoController) RefreshCheck() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	var req models.EdbInfoRefreshCheckReq
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}

	if req.Source == 0 {
		br.Msg = "请输入指标来源"
		return
	}
	if req.LatestDate == "" {
		br.Msg = "请输入查询日期"
		return
	}

	_, err = time.Parse(utils.FormatDate, req.LatestDate)
	if err != nil {
		br.Msg = "查询日期格式错误"
		return
	}

	condition := ` and source=? and no_update=0 `
	pars := make([]interface{}, 0)
	pars = append(pars, req.Source)

	if req.FrequencyBatch != "" {
		frequency := strings.Split(req.FrequencyBatch, ",")
		condition += ` and frequency in (` + utils.GetOrmInReplace(len(frequency)) + `)`
		for _, v := range frequency {
			pars = append(pars, v)
		}
	}
	updateCondition := condition + ` and latest_date = ?`
	pars = append(pars, req.LatestDate)

	unUpdateCondition := condition + ` and latest_date < ?`

	updateNum, err := models.GetEdbInfoCountByCondition(updateCondition, pars)
	if err != nil {
		br.Msg = "获取数据失败！"
		br.ErrMsg = "获取数据失败，Err:" + err.Error()
		return
	}

	unUpdateNum, err := models.GetEdbInfoCountByCondition(unUpdateCondition, pars)
	if err != nil {
		br.Msg = "获取数据失败！"
		br.ErrMsg = "获取数据失败，Err:" + err.Error()
		return
	}

	resp := models.EdbInfoRefreshCheckResp{
		UpdateNum:   updateNum,
		UnUpdateNum: unUpdateNum,
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = resp
}

// ResetEdbClassify
// @Title 重置指标分类
// @Description 重置指标分类
// @Param	request	body models.ResetEdbClassifyReq true "type json string"
// @Success 200 {object} models.EdbInfo
// @router /classify_reset [post]
func (this *EdbInfoController) ResetEdbClassify() {
	br := new(models.BaseResponse).Init()
	defer func() {
		if br.ErrMsg == "" {
			br.IsSendEmail = false
		}
		this.Data["json"] = br
		this.ServeJSON()
	}()
	var req models.ResetEdbClassifyReq
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}
	req.IndexCode = strings.TrimSpace(req.IndexCode)
	if req.IndexCode == "" {
		br.Msg = "指标编码不可为空"
		return
	}
	req.ClassifyFirst = strings.TrimSpace(req.ClassifyFirst)
	if req.ClassifyFirst == "" {
		br.Msg = "一级分类名称不可为空"
		return
	}
	req.ClassifySecond = strings.TrimSpace(req.ClassifySecond)
	if req.ClassifySecond == "" {
		br.Msg = "二级分类名称不可为空"
		return
	}
	req.ClassifyThird = strings.TrimSpace(req.ClassifyThird)
	if req.ClassifyThird == "" {
		br.Msg = "三级分类名称不可为空"
		return
	}

	// 获取指标信息
	edbInfo, e := models.GetEdbInfoOnlyByEdbCode(req.IndexCode)
	if e != nil {
		if e.Error() == utils.ErrNoRow() {
			br.Msg = "指标不存在"
			return
		}
		br.Msg = "获取指标信息失败"
		br.ErrMsg = "获取指标信息失败, Err: " + e.Error()
		return
	}
	edbInfoType := uint8(edbInfo.EdbInfoType)

	// 新增分类
	firstClassify, e, msg := models.SaveEdbClassify(req.ClassifyFirst, 0, 0, edbInfoType, utils.InitAdminId, utils.InitAdminName)
	if msg != "" {
		br.Msg = "新增一级分类失败"
		br.ErrMsg = "新增一级分类失败, ErrMsg: " + e.Error()
		if e != nil {
			br.ErrMsg = fmt.Sprintf("新增一级分类失败, Err: %s, ErrMsg: %s", e.Error(), msg)
		} else {
			br.IsSendEmail = false
		}
		return
	}
	secondClassify, e, msg := models.SaveEdbClassify(req.ClassifySecond, firstClassify.ClassifyId, 1, edbInfoType, utils.InitAdminId, utils.InitAdminName)
	if msg != "" {
		br.Msg = "新增二级分类失败"
		br.ErrMsg = "新增二级分类失败, ErrMsg: " + e.Error()
		if e != nil {
			br.ErrMsg = fmt.Sprintf("新增二级分类失败, Err: %s, ErrMsg: %s", e.Error(), msg)
		} else {
			br.IsSendEmail = false
		}
		return
	}
	thirdClassify, e, msg := models.SaveEdbClassify(req.ClassifyThird, secondClassify.ClassifyId, 2, edbInfoType, utils.InitAdminId, utils.InitAdminName)
	if msg != "" {
		br.Msg = "新增三级分类失败"
		br.ErrMsg = "新增三级分类失败, ErrMsg: " + e.Error()
		if e != nil {
			br.ErrMsg = fmt.Sprintf("新增三级分类失败, Err: %s, ErrMsg: %s", e.Error(), msg)
		} else {
			br.IsSendEmail = false
		}
		return
	}

	// 更新指标分类
	cols := []string{"ClassifyId", "ModifyTime"}
	edbInfo.ClassifyId = thirdClassify.ClassifyId
	edbInfo.ModifyTime = time.Now().Local()
	if e := edbInfo.Update(cols); e != nil {
		br.Msg = "更新指标失败"
		br.ErrMsg = "更新指标失败, Err: " + e.Error()
		return
	}

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

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