package services

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

// EdbCalculateBatchSave 批量指标运算
func EdbCalculateBatchSave(req models.EdbInfoCalculateBatchSaveReq, lang string) (edbInfo *models.EdbInfo, uniqueCode string, err error, errMsg string) {
	// 校验参数
	req.EdbName = strings.Trim(req.EdbName, " ")
	if req.EdbName == "" {
		err = fmt.Errorf("指标名称不能为空")
		return
	}

	if req.Frequency == "" {
		err = fmt.Errorf("频率不能为空")
		return
	}

	if req.Unit == "" {
		err = fmt.Errorf("单位不能为空")
		return
	}

	if req.ClassifyId <= 0 {
		err = fmt.Errorf("请选择分类")
		return
	}

	// 基础指标id
	fromEdbInfoId := req.FromEdbInfoId

	var formulaInt int
	var nhccDate models.NhccDate // 拟合残差的日期
	switch req.Source {
	case utils.DATA_SOURCE_CALCULATE_NSZYDPJJS, utils.DATA_SOURCE_CALCULATE_HBZ, utils.DATA_SOURCE_CALCULATE_HCZ, utils.DATA_SOURCE_CALCULATE_TIME_SHIFT, utils.DATA_SOURCE_CALCULATE_CJJX:
		if req.Formula == "" {
			err = fmt.Errorf("请填写N值")
			return
		}
		formulaInt, _ = strconv.Atoi(req.Formula)
		if formulaInt <= 0 {
			err = fmt.Errorf(" N值输入错误,请重新输入")
			return
		}
	case utils.DATA_SOURCE_CALCULATE_ZJPJ:
		//直接拼接指标
		//校验时间格式
		_, err = time.ParseInLocation(utils.FormatDate, req.Formula, time.Local)
		if err != nil {
			err = fmt.Errorf("拼接日期有误,请重新输入")
			return
		}
	case utils.DATA_SOURCE_CALCULATE_NHCC: //拟合残差指标
		//指标校验
		if len(req.EdbInfoIdArr) != 2 {
			err = fmt.Errorf("选择的指标异常,请重新选择")
			return
		}
		fromEdbInfoId = req.EdbInfoIdArr[0].EdbInfoId

		//校验时间格式
		//数据格式：2022-11-01,2022-11-10
		timeList := strings.Split(req.Formula, ",")
		if len(timeList) != 2 {
			err = fmt.Errorf("选择时间有误,请重新输入")
			return
		}
		startDate, e := time.ParseInLocation(utils.FormatDate, timeList[0], time.Local)
		if e != nil {
			err = fmt.Errorf("开始日期有误,请重新输入")
			return
		}
		endDate, e := time.ParseInLocation(utils.FormatDate, timeList[1], time.Local)
		if e != nil {
			err = fmt.Errorf("结束日期有误,请重新输入")
			return
		}
		if utils.GetTimeSubDay(startDate, endDate) < 2 {
			err = fmt.Errorf("日期间隔不得少于两天")
			return
		}
		nhccDate.StartDate = startDate
		nhccDate.EndDate = endDate
	case utils.DATA_SOURCE_CALCULATE_JP:
		if req.Formula != "期末值" && req.Formula != "平均值" {
			err = fmt.Errorf("数据取值类型错误:" + req.Formula)
			return
		}
	case utils.DATA_SOURCE_CALCULATE_ZSXY: // 指数修匀
		if req.Formula == "" {
			err = fmt.Errorf("请填写alpha值")
			return
		}
		a, e := strconv.ParseFloat(req.Formula, 64)
		if e != nil {
			err = fmt.Errorf("alpha值输入错误, 请重新输入")
			return
		}
		if a <= 0 || a >= 1 {
			err = fmt.Errorf("alpha值输入错误, 请重新输入")
			return
		}
	}

	notNeedFromEdbSourceList := []int{utils.DATA_SOURCE_CALCULATE_KSZS, utils.DATA_SOURCE_CALCULATE_CORRELATION, utils.DATA_SOURCE_CALCULATE_ZDYFX, utils.DATA_SOURCE_CALCULATE_SUM, utils.DATA_SOURCE_CALCULATE_AVG} // 不需要传入来源指标id的 指标类型
	if fromEdbInfoId <= 0 && !utils.InArrayByInt(notNeedFromEdbSourceList, req.Source) {
		err = fmt.Errorf("请选择指标")
		return
	}
	//加入缓存机制，避免创建同一个名称的指标 start
	redisKey := fmt.Sprint("edb_lib:edb_info:calculate:batch:save:", req.Source, ":", req.EdbName)
	isExist := utils.Rc.IsExist(redisKey)
	if isExist {
		err = fmt.Errorf("指标正在处理,请勿重复提交")
		return
	} else {
		//设置3分钟缓存
		utils.Rc.SetNX(redisKey, 1, time.Second*300)
		defer func() {
			_ = utils.Rc.Delete(redisKey)
		}()
	}
	//加入缓存机制，避免创建同一个名称的指标 end

	var condition string
	var pars []interface{}
	condition += " AND edb_name=? "
	pars = append(pars, req.EdbName)

	count, err := models.GetEdbInfoCountByCondition(condition, pars)
	if err != nil {
		err = fmt.Errorf("判断指标名称是否存在失败")
		errMsg = "判断指标名称是否存在失败,Err:" + err.Error()
		return
	}

	if count > 0 {
		err = fmt.Errorf("指标名称已存在，请重新填写")
		errMsg = "指标名称已存在，请重新填写"
		return
	}

	// 来源指标
	var fromEdbInfo *models.EdbInfo
	if fromEdbInfoId > 0 {
		fromEdbInfo, err = models.GetEdbInfoById(fromEdbInfoId)
		if err != nil {
			errMsg = "获取指标信息失败:Err:" + err.Error()
			err = fmt.Errorf("获取指标信息失败")
			return
		}
	}

	//生成指标编码
	edbCode, err := utils.GenerateEdbCode(1, "")
	if err != nil {
		errMsg = "指标编码生成失败:Err:" + err.Error()
		err = fmt.Errorf("指标生成失败")
		return
	}

	timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
	uniqueCode = utils.MD5(utils.DATA_PREFIX + "_" + timestamp)

	sysUserId := req.AdminId
	sysUserName := req.AdminName

	var sourName string
	var edbInfoId int

	var baseEdbInfoModel models.BaseEdbInfoInterface
	addParam := models.AddCalculateBatchParams{
		Req:             &req,
		FromEdbInfo:     fromEdbInfo,
		EdbCode:         edbCode,
		UniqueCode:      uniqueCode,
		SysUserId:       sysUserId,
		SysUserRealName: sysUserName,
	}

	switch req.Source {
	case utils.DATA_SOURCE_CALCULATE:
		sourName = "计算指标"
		req2 := models.EdbInfoCalculateSaveReq{
			AdminId:          sysUserId,
			AdminName:        sysUserName,
			EdbName:          req.EdbName,
			Frequency:        req.Frequency,
			Unit:             req.Unit,
			ClassifyId:       req.ClassifyId,
			CalculateFormula: req.CalculateFormula,
			//EdbInfoIdArr:     req.EdbInfoIdArr,
			EmptyType:    req.EmptyType,
			MaxEmptyType: req.MaxEmptyType,
			Extra:        req.Extra,
		}

		// 放入A指标
		t := models.EdbInfoFromTag{
			EdbInfoId: req.FromEdbInfoId,
			FromTag:   "A",
		}
		req2.EdbInfoIdArr = append(req2.EdbInfoIdArr, t)
		// 放入B指标
		if len(req.EdbInfoIdArr) > 0 {
			t = models.EdbInfoFromTag{
				EdbInfoId: req.EdbInfoIdArr[0].EdbInfoId,
				FromTag:   "B",
			}
			req2.EdbInfoIdArr = append(req2.EdbInfoIdArr, t)
		}

		edbInfo, _, err, errMsg = EdbCalculateAdd(req2, lang)
	case utils.DATA_SOURCE_CALCULATE_LJZZY:
		sourName = "累计值转月值"
		if fromEdbInfo.Frequency != "月度" {
			err = fmt.Errorf("请选择月度指标")
			return
		}
		edbInfo, err = models.AddCalculateLjzzy(&req, fromEdbInfo, edbCode, uniqueCode, sysUserId, sysUserName)
	case utils.DATA_SOURCE_CALCULATE_TBZ:
		if req.Frequency != fromEdbInfo.Frequency {
			err = fmt.Errorf("当前频度和原指标频度不一致")
			return
		}
		if req.Unit != "无" {
			err = fmt.Errorf("单位只允许为无，禁止选择其他单位")
			return
		}
		sourName = "同比值"
		edbInfo, err = models.AddCalculateTbz(&req, fromEdbInfo, edbCode, uniqueCode, sysUserId, sysUserName)
	case utils.DATA_SOURCE_CALCULATE_TCZ:
		if req.Frequency != fromEdbInfo.Frequency {
			err = fmt.Errorf("当前频度和原指标频度不一致")
			return
		}
		if req.Unit != fromEdbInfo.Unit {
			err = fmt.Errorf("当前单位和原指标单位不一致")
			return
		}
		sourName = "同差值"
		edbInfo, err = models.AddCalculateTcz(&req, fromEdbInfo, edbCode, uniqueCode, sysUserId, sysUserName)
	case utils.DATA_SOURCE_CALCULATE_NSZYDPJJS:
		sourName = "N数值移动平均计算"
		edbInfo, err = models.AddCalculateNszydpjjs(&req, fromEdbInfo, edbCode, uniqueCode, sysUserId, sysUserName, formulaInt)
	case utils.DATA_SOURCE_CALCULATE_HBZ:
		checkCount, tmpErr := models.GetLteZeroEdbDataCount(fromEdbInfo.Source, fromEdbInfo.SubSource, fromEdbInfoId)
		if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
			err = fmt.Errorf("判断环比值是否可计算失败")
			errMsg = "判断环比值是否可计算失败,Err:" + tmpErr.Error()
			return
		}
		if checkCount > 0 {
			err = fmt.Errorf("原始数据中存在0或负数，该指标不能进行环比运算")
			errMsg = "原始数据中出现0和负值时，提示该指标不能进行环比运算"
			return
		}
		sourName = "环比值"
		edbInfo, err = models.AddCalculateHbz(&req, fromEdbInfo, edbCode, uniqueCode, sysUserId, sysUserName, formulaInt)
	case utils.DATA_SOURCE_CALCULATE_HCZ:
		sourName = "环差值"
		edbInfo, err = models.AddCalculateHcz(&req, fromEdbInfo, edbCode, uniqueCode, sysUserId, sysUserName, formulaInt)
	case utils.DATA_SOURCE_CALCULATE_BP:
		sourName = utils.DATA_SOURCE_NAME_CALCULATE_BP
		edbInfo, err = models.AddCalculateBp(&req, fromEdbInfo, edbCode, uniqueCode, sysUserId, sysUserName)
	case utils.DATA_SOURCE_CALCULATE_TIME_SHIFT:
		sourName = "时间移位"
		edbInfo, err = models.AddCalculateTimeShift(&req, fromEdbInfo, edbCode, uniqueCode, sysUserId, sysUserName)
	case utils.DATA_SOURCE_CALCULATE_ZJPJ:
		sourName = "直接拼接"

		if len(req.EdbInfoIdArr) != 1 {
			err = fmt.Errorf("请传入拼接日期之后的指标")
			errMsg = "请传入拼接日期之后的指标"
			return
		}

		secondEdbInfoReq := req.EdbInfoIdArr[0]
		secondEdbInfo, tmpErr := models.GetEdbInfoById(secondEdbInfoReq.EdbInfoId)
		if tmpErr != nil {
			err = fmt.Errorf("获取拼接日期之后的指标信息失败")
			errMsg = "获取拼接日期之后的指标信息失败:Err:" + tmpErr.Error()
			return
		}

		if fromEdbInfo.EdbInfoId == secondEdbInfo.EdbInfoId {
			err = fmt.Errorf("两个指标不允许为同一个")
			errMsg = "两个指标不允许为同一个"
			return
		}
		edbInfo, err = models.AddCalculateZjpj(&req, fromEdbInfo, secondEdbInfo, edbCode, uniqueCode, sysUserId, sysUserName)
	case utils.DATA_SOURCE_CALCULATE_LJZTBPJ:
		sourName = "累计值同比拼接"

		if fromEdbInfo.Frequency != "月度" {
			err = fmt.Errorf("待拼接指标只能筛选月度指标")
			errMsg = "待拼接指标只能筛选月度指标"
			return
		}
		if len(req.EdbInfoIdArr) != 1 {
			err = fmt.Errorf("请传入同比值指标")
			errMsg = "请传入同比值指标"
			return
		}

		secondEdbInfoReq := req.EdbInfoIdArr[0]
		tbzEdbInfo, tmpErr := models.GetEdbInfoById(secondEdbInfoReq.EdbInfoId)
		if tmpErr != nil {
			err = fmt.Errorf("获取同比值指标信息失败")
			errMsg = "获取同比值指标信息失败:Err:" + tmpErr.Error()
			return
		}
		if tbzEdbInfo.Source != utils.DATA_SOURCE_CALCULATE_TBZ {
			err = fmt.Errorf("指标必须是传入同比值指标类型")
			errMsg = "指标必须是传入同比值指标类型"
			return
		}
		if tbzEdbInfo.Frequency != "月度" {
			err = fmt.Errorf("同比值指标只能筛选月度指标")
			errMsg = "同比值指标只能筛选月度指标"
			return
		}

		if fromEdbInfo.EdbInfoId == tbzEdbInfo.EdbInfoId {
			err = fmt.Errorf("两个指标不允许为同一个")
			errMsg = "两个指标不允许为同一个"
			return
		}
		edbInfo, err = models.AddCalculateLjztbpj(&req, fromEdbInfo, tbzEdbInfo, edbCode, uniqueCode, sysUserId, sysUserName)
	case utils.DATA_SOURCE_CALCULATE_CJJX:
		sourName = "超季节性"
		edbInfo, err = models.AddCalculateCjjx(&req, fromEdbInfo, edbCode, uniqueCode, sysUserId, sysUserName, formulaInt)
	case utils.DATA_SOURCE_CALCULATE_NHCC:
		sourName = "拟合残差"

		secondEdbInfoReq := req.EdbInfoIdArr[1]
		secondEdbInfo, tmpErr := models.GetEdbInfoById(secondEdbInfoReq.EdbInfoId)
		if tmpErr != nil {
			err = fmt.Errorf("获取因变量的指标信息失败")
			errMsg = "获取因变量的指标信息失败:Err:" + tmpErr.Error()
			return
		}

		if fromEdbInfo.EdbInfoId == secondEdbInfo.EdbInfoId {
			err = fmt.Errorf("两个指标不允许为同一个")
			errMsg = "两个指标不允许为同一个"
			return
		}
		edbInfo, err, errMsg = models.AddCalculateNhcc(&req, fromEdbInfo, secondEdbInfo, edbCode, uniqueCode, nhccDate, sysUserId, sysUserName)
	case utils.DATA_SOURCE_CALCULATE_JP:
		if !models.CheckFrequency(fromEdbInfo.Frequency, req.Frequency) {
			err = fmt.Errorf("频度异常,不允许低频降频到高频")
			return
		}
		sourName = utils.DATA_SOURCE_NAME_CALCULATE_JP
		edbInfo, err = models.AddCalculateJp(&req, fromEdbInfo, edbCode, uniqueCode, sysUserId, sysUserName)
	case utils.DATA_SOURCE_CALCULATE_NH:
		sourName = utils.DATA_SOURCE_NAME_CALCULATE_NH
		edbInfo, err = models.AddCalculateNh(&req, fromEdbInfo, edbCode, uniqueCode, sysUserId, sysUserName)
	case utils.DATA_SOURCE_CALCULATE_KSZS:
		//关联的指标信息
		if len(req.EdbInfoIdArr) < 2 {
			err = fmt.Errorf("指标数量不能小于2个，请重新选择")
			errMsg = "指标数量不能小于2个，请重新选择"
			//br.IsSendEmail = false
			return
		}
		sourName = utils.DATA_SOURCE_NAME_CALCULATE_KSZS
		edbInfo, err = models.AddCalculateKszs(&req, edbCode, uniqueCode, sysUserId, sysUserName)
	case utils.DATA_SOURCE_CALCULATE_CORRELATION:
		//关联的指标信息
		if len(req.EdbInfoIdArr) < 2 {
			err = fmt.Errorf("指标数量不能小于2个，请重新选择")
			errMsg = "指标数量不能小于2个，请重新选择"
			//br.IsSendEmail = false
			return
		}
		sourName = utils.DATA_SOURCE_NAME_CALCULATE_CORRELATION
		edbInfo, err, errMsg = models.AddCalculateCorrelation(&req, edbCode, uniqueCode, sysUserId, sysUserName)
	case utils.DATA_SOURCE_CALCULATE_RJZ:
		if req.Frequency == "日度" {
			err = fmt.Errorf("日度指标无需进行日均值计算")
			return
		}
		if req.Frequency != fromEdbInfo.Frequency {
			err = fmt.Errorf("当前频度和原指标频度不一致")
			return
		}
		if req.Unit != fromEdbInfo.Unit {
			err = fmt.Errorf("单位只允许为和原指标频度保持一致，禁止选择其他单位")
			return
		}
		sourName = "日均值"
		edbInfo, err = models.AddCalculateRjz(&req, fromEdbInfo, edbCode, uniqueCode, sysUserId, sysUserName)
	default:
		// 获取通用的数据源处理服务
		baseEdbInfoModel = models.GetBaseEdbInfoModel(req.Source)
		// 没有找到的话，那么就直接返回报错吧
		if baseEdbInfoModel == nil {
			err = fmt.Errorf("无效计算方式")
			errMsg = "无效计算方式,source:" + strconv.Itoa(req.Source)
			return
		}
		sourName = baseEdbInfoModel.GetSourceName()
		edbInfo, err, errMsg = baseEdbInfoModel.Add(addParam)
	}

	if err != nil {
		errMsg = "生成" + sourName + "失败 Err:" + err.Error() + errMsg
		err = fmt.Errorf("生成" + sourName + "失败")
		return
	}

	if edbInfo == nil {
		err = fmt.Errorf("生成" + sourName + "失败")
		errMsg = "生成" + sourName + "失败,指标ID错误:" + strconv.Itoa(edbInfoId)
		return
	}
	edbInfoId = edbInfo.EdbInfoId

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

	// 更新指标最大最小值
	err, errMsg = models.UnifiedModifyEdbInfoMaxAndMinInfo(edbInfo)
	if err != nil {
		return
	}

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

// EdbCalculateBatchEdit 批量指标编辑
func EdbCalculateBatchEdit(req models.EdbInfoCalculateBatchEditReq) (edbInfo *models.EdbInfo, uniqueCode string, err error, errMsg string) {
	// 校验参数
	req.EdbName = strings.Trim(req.EdbName, " ")
	if req.EdbName == "" {
		err = fmt.Errorf("指标名称不能为空")
		return
	}

	if req.Frequency == "" {
		err = fmt.Errorf("频率不能为空")
		return
	}

	if req.Unit == "" {
		err = fmt.Errorf("单位不能为空")
		return
	}

	if req.ClassifyId <= 0 {
		err = fmt.Errorf("请选择分类")
		return
	}

	if req.EdbInfoId <= 0 {
		err = fmt.Errorf("请选择指标")
		return
	}

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

	var condition string
	var pars []interface{}
	condition += " AND edb_name=?  AND edb_info_id<>? "
	pars = append(pars, req.EdbName, req.EdbInfoId)

	count, err := models.GetEdbInfoCountByCondition(condition, pars)
	if err != nil {
		err = fmt.Errorf("判断指标名称是否存在失败")
		errMsg = "判断指标名称是否存在失败,Err:" + err.Error()
		return
	}

	if count > 0 {
		err = fmt.Errorf("指标名称已存在，请重新填写")
		errMsg = "指标名称已存在，请重新填写"
		return
	}

	edbInfo, err = models.GetEdbInfoById(req.EdbInfoId)
	if err != nil {
		if err.Error() == utils.ErrNoRow() {
			errMsg = "指标已被删除，请刷新页面:Err:" + err.Error()
			err = fmt.Errorf("指标已被删除，请刷新页面")

			return
		}
		errMsg = "获取指标信息失败:Err:" + err.Error()
		err = fmt.Errorf("获取指标信息失败")

		return
	}
	// 基础指标id
	fromEdbInfoId := req.FromEdbInfoId

	if req.Source <= 0 {
		req.Source = edbInfo.Source
	}
	var formulaInt int
	var nhccDate models.NhccDate // 拟合残差的日期
	// 初期的参数校验
	switch req.Source {
	case utils.DATA_SOURCE_CALCULATE_NSZYDPJJS, utils.DATA_SOURCE_CALCULATE_HBZ, utils.DATA_SOURCE_CALCULATE_HCZ, utils.DATA_SOURCE_CALCULATE_TIME_SHIFT, utils.DATA_SOURCE_CALCULATE_CJJX:
		if req.Formula == "" {
			err = fmt.Errorf("请填写N值")
			return
		}
		formulaInt, _ = strconv.Atoi(req.Formula)
		if formulaInt <= 0 {
			err = fmt.Errorf("N值输入错误,请重新输入")
			return
		}
	case utils.DATA_SOURCE_CALCULATE_ZJPJ:
		//直接拼接指标

		//校验时间格式
		_, err = time.ParseInLocation(utils.FormatDate, req.Formula, time.Local)
		if err != nil {
			err = fmt.Errorf("拼接日期有误,请重新输入")
			return
		}
	case utils.DATA_SOURCE_CALCULATE_NHCC: //拟合残差指标
		//指标校验
		if len(req.EdbInfoIdArr) != 2 {
			err = fmt.Errorf("选择的指标异常,请重新选择")
			return
		}
		fromEdbInfoId = req.EdbInfoIdArr[0].EdbInfoId

		//校验时间格式
		//数据格式：2022-11-01,2022-11-10
		timeList := strings.Split(req.Formula, ",")
		if len(timeList) != 2 {
			err = fmt.Errorf("选择时间有误,请重新输入")
			return
		}
		startDate, e := time.ParseInLocation(utils.FormatDate, timeList[0], time.Local)
		if e != nil {
			err = fmt.Errorf("开始日期有误,请重新输入")
			return
		}
		endDate, e := time.ParseInLocation(utils.FormatDate, timeList[1], time.Local)
		if e != nil {
			err = fmt.Errorf("结束日期有误,请重新输入")
			return
		}
		if utils.GetTimeSubDay(startDate, endDate) < 2 {
			err = fmt.Errorf("日期间隔不得少于两天")
			return
		}
		nhccDate.StartDate = startDate
		nhccDate.EndDate = endDate
	case utils.DATA_SOURCE_CALCULATE_JP:
		if req.Formula != "期末值" && req.Formula != "平均值" {
			err = fmt.Errorf("数据取值类型错误:" + req.Formula)
			return
		}
	case utils.DATA_SOURCE_CALCULATE_ZSXY: // 指数修匀
		if req.Formula == "" {
			err = fmt.Errorf("请填写alpha值")
			return
		}
		a, e := strconv.ParseFloat(req.Formula, 64)
		if e != nil {
			err = fmt.Errorf("alpha值输入错误, 请重新输入")
			return
		}
		if a <= 0 || a >= 1 {
			err = fmt.Errorf("alpha值输入错误, 请重新输入")
			return
		}
	}
	// 来源指标
	var fromEdbInfo *models.EdbInfo
	// 基础指标id
	if fromEdbInfoId > 0 {
		fromEdbInfo, err = models.GetEdbInfoById(fromEdbInfoId)
		if err != nil {
			errMsg = "获取指标信息失败:Err:" + err.Error()
			err = fmt.Errorf("获取指标信息失败")
			return
		}
	}

	var sourName string
	var edbInfoId int
	var baseEdbInfoModel models.BaseEdbInfoInterface
	editParams := models.EditCalculateBatchParams{
		Req:         &req,
		EdbInfo:     edbInfo,
		FromEdbInfo: fromEdbInfo,
	}

	switch req.Source {
	case utils.DATA_SOURCE_CALCULATE_LJZZY:
		sourName = "累计值转月值"
		if fromEdbInfo.Frequency != "月度" {
			err = fmt.Errorf("请选择月度指标")
			return
		}
		err = models.EditCalculateLjzzy(edbInfo, &req, fromEdbInfo)
	case utils.DATA_SOURCE_CALCULATE_TBZ:
		sourName = "同比值"
		err = models.EditCalculateTbz(edbInfo, &req, fromEdbInfo)
	case utils.DATA_SOURCE_CALCULATE_TCZ:
		fmt.Println("start edit", time.Now())
		sourName = "同差值"
		err = models.EditCalculateTcz(edbInfo, &req, fromEdbInfo)
		fmt.Println("end edit", time.Now())
	case utils.DATA_SOURCE_CALCULATE_NSZYDPJJS:
		sourName = "N数值移动平均计算"
		err = models.EditCalculateNszydpjjs(edbInfo, &req, fromEdbInfo, formulaInt, edbInfo.CalculateFormula)
	case utils.DATA_SOURCE_CALCULATE_HBZ:
		checkCount, tmpErr := models.GetLteZeroEdbDataCount(fromEdbInfo.Source, fromEdbInfo.SubSource, req.FromEdbInfoId)
		if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
			errMsg = "判断环比值是否可计算失败,Err:" + tmpErr.Error()
			err = fmt.Errorf("判断环比值是否可计算失败")
			return
		}
		if checkCount > 0 {
			err = fmt.Errorf("原始数据中存在0或负数，该指标不能进行环比运算")
			errMsg = "原始数据中出现0和负值时，提示该指标不能进行环比运算"
			return
		}
		sourName = "环比值"
		err = models.EditCalculateHbz(edbInfo, &req, fromEdbInfo, formulaInt, edbInfo.CalculateFormula)
	case utils.DATA_SOURCE_CALCULATE_HCZ:
		sourName = "环差值"
		err = models.EditCalculateHcz(edbInfo, &req, fromEdbInfo, formulaInt, edbInfo.CalculateFormula)
	case utils.DATA_SOURCE_CALCULATE_BP:
		sourName = utils.DATA_SOURCE_NAME_CALCULATE_BP
		err = models.EditCalculateBp(edbInfo, &req, fromEdbInfo)
	case utils.DATA_SOURCE_CALCULATE_TIME_SHIFT:
		sourName = "时间移位"
		err = models.EditCalculateTimeShift(edbInfo, &req, fromEdbInfo)
	case utils.DATA_SOURCE_CALCULATE_ZJPJ:
		sourName = "直接拼接"

		if len(req.EdbInfoIdArr) != 1 {
			err = fmt.Errorf("请传入拼接日期之后的指标")
			errMsg = "请传入拼接日期之后的指标"
			return
		}

		secondEdbInfoReq := req.EdbInfoIdArr[0]
		secondEdbInfo, tmpErr := models.GetEdbInfoById(secondEdbInfoReq.EdbInfoId)
		if tmpErr != nil {
			errMsg = "获取拼接日期之后的指标信息失败:Err:" + tmpErr.Error()
			err = fmt.Errorf("获取拼接日期之后的指标信息失败")
			return
		}

		if fromEdbInfo.EdbInfoId == secondEdbInfo.EdbInfoId {
			err = fmt.Errorf("两个指标不允许为同一个")
			errMsg = "两个指标不允许为同一个"
			return
		}
		err = models.EditCalculateZjpj(&req, edbInfo, fromEdbInfo, secondEdbInfo)
	case utils.DATA_SOURCE_CALCULATE_LJZTBPJ: //累计值同比拼接
		sourName = "累计值同比拼接"

		if fromEdbInfo.Frequency != "月度" {
			errMsg = "待拼接指标只能筛选月度指标"
			err = fmt.Errorf("待拼接指标只能筛选月度指标")
			return
		}
		if len(req.EdbInfoIdArr) != 1 {
			errMsg = "请传入同比值指标"
			err = fmt.Errorf("请传入同比值指标")
			return
		}

		secondEdbInfoReq := req.EdbInfoIdArr[0]
		tbzEdbInfo, tmpErr := models.GetEdbInfoById(secondEdbInfoReq.EdbInfoId)
		if tmpErr != nil {
			errMsg = "获取同比值指标信息失败:Err:" + tmpErr.Error()
			err = fmt.Errorf("获取同比值指标信息失败")

			return
		}
		if tbzEdbInfo.Source != utils.DATA_SOURCE_CALCULATE_TBZ {
			err = fmt.Errorf("指标必须是传入同比值指标类型")
			errMsg = "指标必须是传入同比值指标类型"
			return
		}
		if tbzEdbInfo.Frequency != "月度" {
			err = fmt.Errorf("同比值指标只能筛选月度指标")
			errMsg = "同比值指标只能筛选月度指标"
			return
		}

		if fromEdbInfo.EdbInfoId == tbzEdbInfo.EdbInfoId {
			err = fmt.Errorf("两个指标不允许为同一个")
			errMsg = "两个指标不允许为同一个"
			return
		}
		err = models.EditCalculateLjztbpj(&req, edbInfo, fromEdbInfo, tbzEdbInfo)
	case utils.DATA_SOURCE_CALCULATE_CJJX:
		sourName = "超季节性"
		err = models.EditCalculateCjjx(&req, edbInfo, fromEdbInfo, formulaInt)
	case utils.DATA_SOURCE_CALCULATE_NHCC:
		sourName = "拟合残差"
		secondEdbInfoReq := req.EdbInfoIdArr[1]
		secondEdbInfo, tmpErr := models.GetEdbInfoById(secondEdbInfoReq.EdbInfoId)
		if tmpErr != nil {
			errMsg = "获取因变量的指标信息失败:Err:" + tmpErr.Error()
			err = fmt.Errorf("获取因变量的指标信息失败")

			return
		}

		if fromEdbInfo.EdbInfoId == secondEdbInfo.EdbInfoId {
			err = fmt.Errorf("两个指标不允许为同一个")
			errMsg = "两个指标不允许为同一个"
			return
		}
		err, errMsg = models.EditCalculateNhcc(&req, edbInfo, fromEdbInfo, secondEdbInfo, nhccDate)
	case utils.DATA_SOURCE_CALCULATE_JP:
		if !models.CheckFrequency(fromEdbInfo.Frequency, req.Frequency) {
			err = fmt.Errorf("频度异常,不允许低频降频到高频")
			return
		}
		sourName = utils.DATA_SOURCE_NAME_CALCULATE_JP
		err = models.EditCalculateJp(edbInfo, &req, fromEdbInfo)
	case utils.DATA_SOURCE_CALCULATE_NH:
		sourName = utils.DATA_SOURCE_NAME_CALCULATE_NH
		err = models.EditCalculateNh(edbInfo, &req, fromEdbInfo)
	case utils.DATA_SOURCE_CALCULATE_KSZS:
		//关联的指标信息
		if len(req.EdbInfoIdArr) < 2 {
			err = fmt.Errorf("指标数量不能小于2个，请重新选择")
			errMsg = "指标数量不能小于2个，请重新选择"
			//br.IsSendEmail = false
			return
		}
		sourName = utils.DATA_SOURCE_NAME_CALCULATE_KSZS
		err = models.EditCalculateKszs(edbInfo, &req)
	case utils.DATA_SOURCE_CALCULATE_CORRELATION:
		//关联的指标信息
		if len(req.EdbInfoIdArr) < 2 {
			err = fmt.Errorf("指标数量不能小于2个，请重新选择")
			errMsg = "指标数量不能小于2个，请重新选择"
			//br.IsSendEmail = false
			return
		}
		sourName = utils.DATA_SOURCE_NAME_CALCULATE_CORRELATION
		err, errMsg = models.EditCalculateCorrelation(edbInfo, &req)
	case utils.DATA_SOURCE_CALCULATE_RJZ:
		if req.Frequency == "日度" {
			err = fmt.Errorf("日度指标无需进行日均值计算")
			return
		}
		if req.Frequency != fromEdbInfo.Frequency {
			err = fmt.Errorf("当前频度和原指标频度不一致")
			return
		}
		if req.Unit != fromEdbInfo.Unit {
			err = fmt.Errorf("单位只允许为和原指标频度保持一致，禁止选择其他单位")
			return
		}
		sourName = "日均值"
		err = models.EditCalculateRjz(edbInfo, &req, fromEdbInfo)
	default:
		// 获取通用的数据源处理服务
		baseEdbInfoModel = models.GetBaseEdbInfoModel(req.Source)
		// 没有找到的话，那么就直接返回报错吧
		if baseEdbInfoModel == nil {
			err = fmt.Errorf("无效计算方式")
			errMsg = "无效计算方式,source:" + strconv.Itoa(req.Source)
			return
		}
		sourName = baseEdbInfoModel.GetSourceName()
		err, errMsg = baseEdbInfoModel.Edit(editParams)
	}
	if err != nil {
		errMsg = "生成" + sourName + "失败 Err:" + err.Error() + errMsg
		err = fmt.Errorf("生成" + sourName + "失败")
		return
	}
	if edbInfo == nil {
		err = fmt.Errorf("生成" + sourName + "失败")
		errMsg = "生成" + sourName + "失败,指标ID错误:" + strconv.Itoa(edbInfoId)
		return
	}
	edbInfoId = edbInfo.EdbInfoId
	uniqueCode = edbInfo.UniqueCode
	//处理同名指标
	{
		edbNameList, tErr := models.GetEdbInfoByName(req.EdbName)
		if tErr != nil {
			err = fmt.Errorf("保存失败")
			errMsg = "获取指标信息失败，Err:" + tErr.Error()
			return
		}
		if len(edbNameList) >= 2 {
			for _, v := range edbNameList {
				edbName := v.EdbName + "(" + v.SourceName + ")"
				err = models.ModifyEdbInfoNameSource(edbName, v.EdbInfoId)
				if err != nil {
					errMsg = "修改指标名称失败，Err:" + err.Error()
					err = fmt.Errorf("保存失败")
					return
				}
			}
		}
	}

	// 更新指标最大最小值
	err, errMsg = models.UnifiedModifyEdbInfoMaxAndMinInfo(edbInfo)
	if err != nil {
		return
	}

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

// EdbCalculateAdd 新增指标运算
func EdbCalculateAdd(req models.EdbInfoCalculateSaveReq, lang string) (edbInfo *models.EdbInfo, uniqueCode string, err error, errMsg string) {
	calculateFormula := req.CalculateFormula
	calculateFormula = strings.Replace(calculateFormula, "（", "(", -1)
	calculateFormula = strings.Replace(calculateFormula, "）", ")", -1)
	calculateFormula = strings.Replace(calculateFormula, "，", ",", -1)
	calculateFormula = strings.Replace(calculateFormula, "。", ".", -1)
	calculateFormula = strings.Replace(calculateFormula, "%", "*0.01", -1)
	req.CalculateFormula = calculateFormula

	//判断是否重复指标
	edbInfoMap := make(map[int]string)
	//移除研究员选择指标中的未使用的指标
	{
		//转大写的计算公式
		upperCalculateFormulaStr := strings.ToUpper(req.CalculateFormula)
		//用到的指标
		newEdbInfoIdArr := make([]models.EdbInfoFromTag, 0)
		for _, tmpEdbInfo := range req.EdbInfoIdArr {
			_, ok := edbInfoMap[tmpEdbInfo.EdbInfoId]
			if ok {
				err = fmt.Errorf("选择指标失败，请勿选择重复指标！")
				return
			}
			edbInfoMap[tmpEdbInfo.EdbInfoId] = tmpEdbInfo.FromTag

			upperFromTag := strings.ToUpper(tmpEdbInfo.FromTag)
			if strings.Contains(upperCalculateFormulaStr, upperFromTag) {
				newEdbInfoIdArr = append(newEdbInfoIdArr, tmpEdbInfo)
			}
		}
		req.EdbInfoIdArr = newEdbInfoIdArr
	}

	// 校验指标名称是否存在
	existEdbName, err := logic.CheckExistByEdbNameAndEdbInfoId(0, 0, req.EdbName, lang)
	if err != nil {
		errMsg = "判断指标名称是否存在失败,Err:" + err.Error()
		err = fmt.Errorf("判断指标名称是否存在失败")
		return
	}

	if existEdbName {
		errMsg = "指标名称已存在，请重新填写"
		err = fmt.Errorf(errMsg)
		//br.IsSendEmail = false
		return
	}

	//检验公式
	var formulaStr string
	var edbInfoIdBytes []string
	for _, v := range req.EdbInfoIdArr {
		formulaStr += v.FromTag + ","
		edbInfoIdBytes = append(edbInfoIdBytes, v.FromTag)
	}
	formulaSlice, err := utils.CheckFormulaJson(req.CalculateFormula)
	if err != nil {
		err = fmt.Errorf("公式格式错误，请重新填写")
		return
	}
	for _, formula := range formulaSlice {
		formulaMap, e := utils.CheckFormula(formula)
		if e != nil {
			err = fmt.Errorf("公式错误，请重新填写")
			return
		}
		for _, v := range formulaMap {
			if !strings.Contains(formulaStr, v) {
				err = fmt.Errorf("公式错误，请重新填写")
				return
			}
		}
	}

	//关联的指标信息
	edbInfoList := make([]*models.EdbInfo, 0)
	//关联的指标数据表
	calculateMappingList := make([]*models.EdbInfoCalculateMapping, 0)
	for k, v := range req.EdbInfoIdArr {
		fromEdbInfo, e := models.GetEdbInfoById(v.EdbInfoId)
		if e != nil {
			if e.Error() == utils.ErrNoRow() {
				err = fmt.Errorf("生成计算指标失败")
				errMsg = "指标 " + strconv.Itoa(v.EdbInfoId) + " 不存在"
				return
			}
			err = fmt.Errorf("生成计算指标失败")
			errMsg = "获取指标失败:Err:" + e.Error()
			return
		}

		edbInfoList = append(edbInfoList, fromEdbInfo)
		//关联关系表
		{
			calculateMappingItem := &models.EdbInfoCalculateMapping{
				EdbInfoCalculateMappingId: 0,
				EdbInfoId:                 0,
				Source:                    utils.DATA_SOURCE_CALCULATE,
				SourceName:                "指标运算",
				EdbCode:                   "",
				FromEdbInfoId:             fromEdbInfo.EdbInfoId,
				FromEdbCode:               fromEdbInfo.EdbCode,
				FromEdbName:               fromEdbInfo.EdbName,
				FromSource:                fromEdbInfo.Source,
				FromSourceName:            fromEdbInfo.SourceName,
				FromTag:                   v.FromTag,
				Sort:                      k + 1,
				CreateTime:                time.Now(),
				ModifyTime:                time.Now(),
			}
			//calculateMappingItem.EdbCode = edbCode
			//calculateMappingItem.EdbInfoId = int(edbInfoId)
			calculateMappingList = append(calculateMappingList, calculateMappingItem)
		}
	}

	for _, v := range formulaSlice {
		formulaMap, e := utils.CheckFormula(v)
		if e != nil {
			err = fmt.Errorf("公式错误，请重新填写")
			return
		}
		//预先计算，判断公式是否正常
		ok, _ := models.CheckFormula2(edbInfoList, formulaMap, v, edbInfoIdBytes)
		if !ok {
			err = fmt.Errorf("生成计算指标失败，请使用正确的计算公式")
			return
		}
	}
	//生成指标编码
	edbCode, err := utils.GenerateEdbCode(1, "")
	if err != nil {
		errMsg = "生成计算指标失败,AddEdbInfo Err:" + err.Error()
		err = fmt.Errorf("指标编码生成失败，Err：" + err.Error())
		return
	}
	timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
	uniqueCode = utils.MD5(utils.DATA_PREFIX + "_" + timestamp)

	// 新增计算指标
	edbInfo, err = models.AddCalculateInfo(req, calculateMappingList, edbInfoList, edbCode, uniqueCode, edbInfoIdBytes)
	if err != nil {
		errMsg = "生成计算指标失败,AddEdbInfo Err:" + err.Error()
		err = fmt.Errorf("生成计算指标失败")
		return
	}

	return
}
