package data_manage

import (
	"errors"
	"eta/eta_mobile/utils"
	"github.com/beego/beego/v2/client/orm"
	"time"
)

// EditCalculateZjpj 编辑直接拼接数据
func EditCalculateZjpj(req *EdbInfoCalculateBatchEditReq, nowEdbInfo, firstEdbInfo, secondEdbInfo *EdbInfo) (edbInfoId int, err error) {
	edbInfoId = req.EdbInfoId
	o := orm.NewOrmUsingDB("data")
	to, err := o.Begin()
	if err != nil {
		return
	}
	defer func() {
		if err != nil {
			_ = to.Rollback()
		} else {
			_ = to.Commit()
		}
	}()

	//修改指标信息
	sql := ` UPDATE  edb_info SET
			  edb_name =?,
			  edb_name_source=?,
			  frequency = ?,
			  unit = ?,
			  classify_id = ?, 
			  calculate_formula=?,
			  modify_time = NOW()
			WHERE edb_info_id = ? `
	_, err = to.Raw(sql, req.EdbName, req.EdbName, req.Frequency, req.Unit, req.ClassifyId, req.Formula, edbInfoId).Exec()
	if err != nil {
		return
	}

	var existCondition string
	var existPars []interface{}
	existCondition += " AND edb_info_id=? "
	existPars = append(existPars, edbInfoId)

	//查询出所有的关联指标
	existList, err := GetEdbInfoCalculateListByCondition(existCondition, existPars)
	if err != nil {
		err = errors.New("判断指标是否改变失败,Err:" + err.Error())
		return
	}

	var existItemA, existItemB *EdbInfoCalculateMapping
	for _, existItem := range existList {
		if existItem.FromTag == "A" {
			existItemA = existItem
		} else if existItem.FromTag == "B" {
			existItemB = existItem
		}
	}

	addDataList := make([]*EdbDataCalculateZjpj, 0)
	firstDataList := make([]*EdbInfoSearchData, 0)
	secondDataList := make([]*EdbInfoSearchData, 0)

	//如果 之前的拼接日期 与 现在的拼接日期 不一致的话，需要做以下处理
	nowFormulaDate, _ := time.ParseInLocation(utils.FormatDate, nowEdbInfo.CalculateFormula, time.Local)
	reqFormulaDate, _ := time.ParseInLocation(utils.FormatDate, req.Formula, time.Local)

	//如果前后选择的日期不一致，那么需要删除一部分数据
	if nowEdbInfo.CalculateFormula != req.Formula {
		var startDate, endDate time.Time
		//如果当前选择的日期 小于 之前选择的日期
		if reqFormulaDate.Before(nowFormulaDate) {
			startDate = reqFormulaDate
			endDate = nowFormulaDate
		} else { //如果当前选择的日期 大于 之前选择的日期
			startDate = nowFormulaDate
			endDate = reqFormulaDate
		}
		//删除 之前日期 与 当前日期 之间的指标数据
		sql = ` DELETE FROM edb_data_calculate_zjpj WHERE edb_info_id = ? and data_time >= ? and data_time < ?`
		_, err = to.Raw(sql, edbInfoId, startDate, endDate).Exec()
		if err != nil {
			err = errors.New("删除 之前日期 与 当前日期 之间的指标数据失败,Err:" + err.Error())
			return
		}
	}
	//第一个指标数据
	{
		var condition string
		var pars []interface{}
		if existItemA.FromEdbInfoId != firstEdbInfo.EdbInfoId {
			//删除之前的A指标关联关系
			sql = ` DELETE FROM edb_info_calculate_mapping WHERE edb_info_id = ? and from_edb_info_id = ?`
			_, err = to.Raw(sql, edbInfoId, existItemA.FromEdbInfoId).Exec()
			if err != nil {
				err = errors.New("删除拼接日期之前的指标关联关系失败,Err:" + err.Error())
				return
			}

			//删除之前所有的A指标数据
			sql = ` DELETE FROM edb_data_calculate_zjpj WHERE edb_info_id = ? and data_time < ?`
			_, err = to.Raw(sql, edbInfoId, req.Formula).Exec()
			if err != nil {
				err = errors.New("删除拼接日期之前的数据失败,Err:" + err.Error())
				return
			}

			//获取第一个指标的数据列表
			condition += " AND data_time < ? AND edb_info_id=? "
			pars = append(pars, req.Formula, firstEdbInfo.EdbInfoId)

			tmpFirstDataList, tmpErr := GetEdbDataListAll(condition, pars, firstEdbInfo.Source, 0)
			if tmpErr != nil {
				return edbInfoId, tmpErr
			}
			firstDataList = tmpFirstDataList

			//添加新的指标关系
			{
				calculateMappingItem := new(EdbInfoCalculateMapping)
				calculateMappingItem.CreateTime = time.Now()
				calculateMappingItem.ModifyTime = time.Now()
				calculateMappingItem.Sort = 1
				calculateMappingItem.EdbCode = nowEdbInfo.EdbCode
				calculateMappingItem.EdbInfoId = edbInfoId
				calculateMappingItem.FromEdbInfoId = firstEdbInfo.EdbInfoId
				calculateMappingItem.FromEdbCode = firstEdbInfo.EdbCode
				calculateMappingItem.FromEdbName = firstEdbInfo.EdbName
				calculateMappingItem.FromSource = firstEdbInfo.Source
				calculateMappingItem.FromSourceName = firstEdbInfo.SourceName
				calculateMappingItem.FromTag = "A"
				calculateMappingItem.Source = nowEdbInfo.Source
				calculateMappingItem.SourceName = nowEdbInfo.SourceName
				_, err = to.Insert(calculateMappingItem)
				if err != nil {
					return
				}
			}
		} else {
			if req.Formula != nowEdbInfo.CalculateFormula {
				//获取第一个指标的数据列表
				condition += " AND data_time >= ?  AND data_time < ? AND edb_info_id=? "
				pars = append(pars, nowFormulaDate, reqFormulaDate, firstEdbInfo.EdbInfoId)

				tmpFirstDataList, tmpErr := GetEdbDataListAll(condition, pars, firstEdbInfo.Source, 0)
				if tmpErr != nil {
					return edbInfoId, tmpErr
				}
				firstDataList = tmpFirstDataList
			}
		}

		//待插入数据
		for _, v := range firstDataList {
			//时间戳
			currentDate, _ := time.Parse(utils.FormatDate, v.DataTime)
			timestamp := currentDate.UnixNano() / 1e6

			edbDataZjpj := &EdbDataCalculateZjpj{
				EdbInfoId:     edbInfoId,
				EdbCode:       nowEdbInfo.EdbCode,
				DataTime:      v.DataTime,
				Value:         v.Value,
				Status:        1,
				CreateTime:    time.Now(),
				ModifyTime:    time.Now(),
				DataTimestamp: timestamp,
			}
			addDataList = append(addDataList, edbDataZjpj)
		}
	}

	//第二个指标数据
	{
		var condition string
		var pars []interface{}

		if existItemB.FromEdbInfoId != secondEdbInfo.EdbInfoId {
			//删除之前的B指标关联关系
			sql = ` DELETE FROM edb_info_calculate_mapping WHERE edb_info_id = ? and from_edb_info_id = ?`
			_, err = to.Raw(sql, edbInfoId, existItemB.FromEdbInfoId).Exec()
			if err != nil {
				err = errors.New("删除拼接日期之后的指标关联关系失败,Err:" + err.Error())
				return
			}

			//删除历史拼接日期之前所有的B指标数据
			sql = ` DELETE FROM edb_data_calculate_zjpj WHERE edb_info_id = ? and data_time >= ?`
			_, err = to.Raw(sql, edbInfoId, nowEdbInfo.CalculateFormula).Exec()
			if err != nil {
				err = errors.New("删除历史拼接日期之后的数据失败,Err:" + err.Error())
				return
			}

			//第二个指标的数据列表
			condition = " AND data_time >= ?  AND edb_info_id=? "
			pars = append(pars, reqFormulaDate, secondEdbInfo.EdbInfoId)
			tmpSecondDataList, tmpErr := GetEdbDataListAll(condition, pars, secondEdbInfo.Source, 0)
			if tmpErr != nil {
				return edbInfoId, tmpErr
			}
			secondDataList = tmpSecondDataList

			//添加新的指标关系
			{
				calculateMappingItem := new(EdbInfoCalculateMapping)
				calculateMappingItem.CreateTime = time.Now()
				calculateMappingItem.ModifyTime = time.Now()
				calculateMappingItem.Sort = 1
				calculateMappingItem.EdbCode = nowEdbInfo.EdbCode
				calculateMappingItem.EdbInfoId = edbInfoId
				calculateMappingItem.FromEdbInfoId = secondEdbInfo.EdbInfoId
				calculateMappingItem.FromEdbCode = secondEdbInfo.EdbCode
				calculateMappingItem.FromEdbName = secondEdbInfo.EdbName
				calculateMappingItem.FromSource = secondEdbInfo.Source
				calculateMappingItem.FromSourceName = secondEdbInfo.SourceName
				calculateMappingItem.FromTag = "B"
				calculateMappingItem.Source = nowEdbInfo.Source
				calculateMappingItem.SourceName = nowEdbInfo.SourceName
				_, err = to.Insert(calculateMappingItem)
				if err != nil {
					return
				}
			}
		} else {
			if req.Formula != nowEdbInfo.CalculateFormula {
				//获取第二个指标的数据列表
				condition += " AND data_time >= ?  AND data_time < ? AND edb_info_id=? "
				pars = append(pars, reqFormulaDate, nowFormulaDate, secondEdbInfo.EdbInfoId)

				tmpSecondDataList, tmpErr := GetEdbDataListAll(condition, pars, secondEdbInfo.Source, 0)
				if tmpErr != nil {
					return edbInfoId, tmpErr
				}
				secondDataList = tmpSecondDataList
			}
		}

		//待插入数据
		for _, v := range secondDataList {
			//时间戳
			currentDate, _ := time.Parse(utils.FormatDate, v.DataTime)
			timestamp := currentDate.UnixNano() / 1e6

			edbDataZjpj := &EdbDataCalculateZjpj{
				EdbInfoId:     edbInfoId,
				EdbCode:       nowEdbInfo.EdbCode,
				DataTime:      v.DataTime,
				Value:         v.Value,
				Status:        1,
				CreateTime:    time.Now(),
				ModifyTime:    time.Now(),
				DataTimestamp: timestamp,
			}
			addDataList = append(addDataList, edbDataZjpj)
		}
	}

	//数据入库
	tmpAddDataList := make([]*EdbDataCalculateZjpj, 0)
	for _, v := range addDataList {
		tmpAddDataList = append(tmpAddDataList, v)

		if len(tmpAddDataList) >= 200 {
			_, tmpErr := to.InsertMulti(len(tmpAddDataList), tmpAddDataList)
			if tmpErr != nil {
				err = tmpErr
				return
			}
			//重新初始化需要加入的数据切片
			tmpAddDataList = make([]*EdbDataCalculateZjpj, 0)
		}
	}
	//最后如果还有需要新增的数据，那么就统一入库
	if len(tmpAddDataList) > 0 {
		_, tmpErr := to.InsertMulti(len(tmpAddDataList), tmpAddDataList)
		if tmpErr != nil {
			err = tmpErr
			return
		}
	}

	return
}
