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"
	"net/url"
	"strconv"
	"strings"
	"time"
)

// PythonController 计算指标
type PythonController struct {
	BaseAuthController
}

// ExcePython
// @Title 执行python代码接口
// @Description 执行python代码接口
// @Success 200 {object} models.ExecPythonEdbReq
// @router /exec [post]
func (this *PythonController) ExcePython() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	//source := utils.DATA_SOURCE_WIND
	var req models.ExecPythonEdbReq
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}

	if req.PythonCode == "" {
		br.Msg = "请填写python代码"
		return
	}
	req.PythonCode, err = url.QueryUnescape(req.PythonCode)
	if err != nil {
		br.Msg = "python代码解析失败"
		return
	}
	edbData, err, errMsg := services.ExecPythonCode("test", req.PythonCode)
	if err != nil {
		br.Msg = "获取数据失败"
		br.ErrMsg = "python代码获取数据失败，err:" + err.Error()
		if errMsg != "" {
			br.Msg = errMsg
		}
		return
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "保存成功"
	br.Data = edbData
	br.IsAddLog = true
}

// Add
// @Title 编辑指标接口
// @Description 编辑指标接口
// @Success 200 {object} models.EditEdbInfoReq
// @router /add [post]
func (this *PythonController) Add() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	//source := utils.DATA_SOURCE_WIND
	var req models.AddPythonEdbReq
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}
	req.EdbName = strings.Trim(req.EdbName, " ")
	if req.EdbName == "" {
		br.Msg = "指标名称不能为空"
		return
	}

	if req.Frequency == "" {
		br.Msg = "频率不能为空"
		return
	}

	if req.Unit == "" {
		br.Msg = "单位不能为空"
		return
	}

	if req.ClassifyId <= 0 {
		br.Msg = "请选择分类"
		return
	}

	if req.PythonCode == "" {
		br.Msg = "请填写python代码"
		return
	}

	//加入缓存机制，避免创建同一个名称的指标 start
	redisKey := fmt.Sprint("edb_info:python:add:", utils.DATA_SOURCE_PYTHON, ":", req.EdbName)
	isExist := utils.Rc.IsExist(redisKey)
	if isExist {
		br.Msg = "指标正在处理,请勿重复提交"
		return
	} else {
		//设置3分钟缓存
		utils.Rc.SetNX(redisKey, 1, time.Second*300)
		defer func() {
			utils.Rc.Delete(redisKey)
		}()
	}

	//获取指标数据
	req.PythonCode, err = url.QueryUnescape(req.PythonCode)
	if err != nil {
		br.Msg = "python代码解析失败"
		return
	}
	edbData, err, errMsg := services.ExecPythonCode(utils.MD5(req.EdbName), req.PythonCode)
	if err != nil {
		br.Msg = "获取数据失败"
		br.ErrMsg = "python代码获取数据失败，err:" + err.Error()
		if errMsg != "" {
			br.ErrMsg = errMsg
		}
		return
	}
	var condition string
	var pars []interface{}
	condition += " AND edb_name=? "
	pars = append(pars, req.EdbName)

	count, err := models.GetEdbInfoCountByCondition(condition, pars)
	if err != nil {
		br.Msg = "判断指标名称是否存在失败"
		br.ErrMsg = "判断指标名称是否存在失败,Err:" + err.Error()
		return
	}
	if count > 0 {
		br.Msg = "指标名称已存在，请重新填写"
		br.ErrMsg = "指标名称已存在，请重新填写"
		return
	}
	//指标code生成
	//生成指标编码
	edbCode, err := utils.GenerateEdbCode(1, "")
	if err != nil {
		br.Msg = "指标生成失败"
		br.ErrMsg = "指标编码生成失败，Err：" + err.Error()
		return
	}

	timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
	uniqueCode := utils.MD5(utils.DATA_PREFIX + "_" + timestamp)
	edbInfo := &models.EdbInfo{
		Source:          utils.DATA_SOURCE_PYTHON,
		SourceName:      "代码运算",
		EdbCode:         edbCode,
		EdbName:         req.EdbName,
		EdbNameSource:   req.EdbName,
		Frequency:       req.Frequency,
		Unit:            req.Unit,
		ClassifyId:      req.ClassifyId,
		SysUserId:       req.AdminId,
		SysUserRealName: req.AdminName,
		CreateTime:      time.Now(),
		ModifyTime:      time.Now(),
		UniqueCode:      uniqueCode,
		EdbType:         2,
	}

	edbInfoId, err := models.AddEdbInfo(edbInfo)
	if err != nil {
		br.Msg = "生成python运算指标失败"
		br.Msg = "生成python运算指标失败,AddEdbInfo Err:" + err.Error()
		return
	}
	edbInfo.EdbInfoId = int(edbInfoId)

	//处理同名指标
	{
		edbNameList, err := models.GetEdbInfoByName(req.EdbName)
		if err != nil {
			br.Msg = "保存失败"
			br.ErrMsg = "获取指标信息失败，Err:" + err.Error()
			return
		}
		if len(edbNameList) >= 2 {
			for _, v := range edbNameList {
				edbName := v.EdbName + "(" + v.SourceName + ")"
				err = models.ModifyEdbInfoNameSource(edbName, v.EdbInfoId)
				if err != nil {
					br.Msg = "保存失败"
					br.ErrMsg = "修改指标名称失败，Err:" + err.Error()
					return
				}
			}
		}
	}

	// 存储python代码
	edbPythonCode := &models.EdbPythonCode{
		EdbInfoId:  int(edbInfoId),
		EdbCode:    edbInfo.EdbCode,
		PythonCode: req.PythonCode,
		ModifyTime: time.Now(),
		CreateTime: time.Now(),
	}
	_, err = models.AddEdbPythonCode(edbPythonCode)
	if err != nil {
		br.Msg = "生成python运算指标失败"
		br.Msg = "生成python运算指标失败,存储python代码失败,AddEdbPythonCode Err:" + err.Error()
		return
	}

	//匹配所有的关联指标
	edbInfoList := models.AnalysisPythonCode(req.PythonCode, req.EdbName)

	//pythonCode
	err = models.AddPythonEdb(edbInfo, edbData, edbInfoList)
	if err != nil {
		br.Msg = "生成python指标失败"
		br.Msg = "生成python指标失败,AddPythonEdb Err:" + err.Error()
		return
	}

	// 更新指标最大最小值
	err, errMsg = models.UnifiedModifyEdbInfoMaxAndMinInfo(edbInfo)
	if err != nil {
		br.Msg = errMsg
		br.ErrMsg = err.Error()
		return
	}
	resp := models.AddEdbInfoResp{
		EdbInfoId:  int(edbInfoId),
		UniqueCode: uniqueCode,
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "保存成功"
	br.Data = resp
	br.IsAddLog = true
}

// Edit
// @Title 编辑指标接口
// @Description 编辑指标接口
// @Success 200 {object} models.EditEdbInfoReq
// @router /edit [post]
func (this *PythonController) Edit() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	//source := utils.DATA_SOURCE_WIND
	var req models.AddPythonEdbReq
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}
	req.EdbName = strings.Trim(req.EdbName, " ")

	if req.EdbInfoId <= 0 {
		br.Msg = "指标id不能为空"
		return
	}
	if req.EdbName == "" {
		br.Msg = "指标名称不能为空"
		return
	}

	if req.Frequency == "" {
		br.Msg = "频率不能为空"
		return
	}

	if req.Unit == "" {
		br.Msg = "单位不能为空"
		return
	}

	if req.ClassifyId <= 0 {
		br.Msg = "请选择分类"
		return
	}

	if req.PythonCode == "" {
		br.Msg = "请填写python代码"
		return
	}

	//加入缓存机制，避免创建同一个名称的指标 start
	redisKey := fmt.Sprint("edb_info:python:edit:", utils.DATA_SOURCE_PYTHON, ":", req.EdbInfoId)
	isExist := utils.Rc.IsExist(redisKey)
	if isExist {
		br.Msg = "指标正在处理,请勿重复提交"
		return
	} else {
		//设置3分钟缓存
		utils.Rc.SetNX(redisKey, 1, time.Second*300)
		defer func() {
			utils.Rc.Delete(redisKey)
		}()
	}

	edbInfo, err := models.GetEdbInfoById(req.EdbInfoId)
	if err != nil {
		br.Msg = "获取指标信息失败"
		br.ErrMsg = "获取指标信息失败，err:" + err.Error()
		return
	}

	//获取指标数据
	req.PythonCode, err = url.QueryUnescape(req.PythonCode)
	if err != nil {
		br.Msg = "python代码解析失败"
		return
	}
	edbData, err, errMsg := services.ExecPythonCode(utils.MD5(req.EdbName), req.PythonCode)
	if err != nil {
		br.Msg = "执行python代码失败"
		br.ErrMsg = "执行python代码失败，err:" + err.Error()
		if errMsg != "" {
			br.ErrMsg = errMsg
		}
		return
	}

	// 判断指标名称是否重复
	{
		var condition string
		var pars []interface{}
		condition += " AND edb_name=? "
		pars = append(pars, req.EdbName)

		condition += " AND edb_info_id<>? "
		pars = append(pars, req.EdbInfoId)

		count, err := models.GetEdbInfoCountByCondition(condition, pars)
		if err != nil {
			br.Msg = "判断指标名称是否存在失败"
			br.ErrMsg = "判断指标名称是否存在失败,Err:" + err.Error()
			return
		}
		if count > 0 {
			br.Msg = "指标名称已存在，请重新填写"
			br.ErrMsg = "指标名称已存在，请重新填写"
			return
		}
	}
	edbInfo.EdbName = utils.TrimStr(req.EdbName)
	edbInfo.EdbNameSource = utils.TrimStr(req.EdbName)
	edbInfo.Unit = req.Unit
	edbInfo.ClassifyId = req.ClassifyId
	edbInfo.Frequency = req.Frequency
	err = edbInfo.Update([]string{"EdbName", "EdbNameSource", "Unit", "ClassifyId", "Frequency"})
	if err != nil {
		br.Msg = "修改python运算指标失败"
		br.Msg = "修改python运算指标失败,ModifyEdbInfo Err:" + err.Error()
		return
	}

	// 修改存储python代码
	edbPythonCode, err := models.GetEdbPythonCodeById(edbInfo.EdbInfoId)
	edbPythonCode.PythonCode = req.PythonCode
	edbPythonCode.ModifyTime = time.Now()
	err = edbPythonCode.Update([]string{"PythonCode", "ModifyTime"})
	if err != nil {
		br.Msg = "修改python运算指标失败"
		br.Msg = "修改python运算指标失败,存储python代码失败,EditEdbPythonCode Err:" + err.Error()
		return
	}

	//匹配所有的关联指标
	edbInfoList := models.AnalysisPythonCode(req.PythonCode, req.EdbName)
	err = models.EditEdbInfoCalculateMapping(edbInfo, edbInfoList)
	if err != nil {
		br.Msg = "修改python运算指标失败"
		br.Msg = "修改python运算指标失败,存储python代码失败,EditEdbPythonCode Err:" + err.Error()
		return
	}
	//刷新数据
	err = models.RefreshAllPythonEdb(edbInfo, edbData)
	if err != nil {
		br.Msg = "刷新python指标失败"
		br.Msg = "刷新python指标失败,EditPythonEdb 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)

	resp := models.AddEdbInfoResp{
		EdbInfoId:  edbInfo.EdbInfoId,
		UniqueCode: edbInfo.UniqueCode,
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "修改成功"
	br.Data = resp
	br.IsAddLog = true
}
