package chart

import (
	"fmt"
	"hongze/hongze_yb/global"
	chartEdbMappingModel "hongze/hongze_yb/models/tables/chart_edb_mapping"
	chartInfoModel "hongze/hongze_yb/models/tables/chart_info"
	edbDataModel "hongze/hongze_yb/models/tables/edb_data"
	edbInfoModel "hongze/hongze_yb/models/tables/edb_info"
	"hongze/hongze_yb/utils"
	"sort"
	"strconv"
	"strings"
	"time"
)

// ModifyChartInfoAndMapping 修改图表信息
func ModifyChartInfoAndMapping(edbInfoIdStr string, req *chartInfoModel.SaveChartInfoReq, chartType int) (err error) {
	// 开启事务
	tx := global.MYSQL["data"].Begin()
	defer func() {
		if err != nil {
			tx.Rollback()
		} else {
			tx.Commit()
		}
	}()

	// 更新图表信息
	if chartType == 1 {
		sql := ` UPDATE chart_info
			SET
            	edb_info_ids = ?,
				modify_time = NOW(),
            	date_type = ?,
				start_date = ?,
				end_date = ?,
 				left_min = ?,
				left_max = ?,
				right_min = ?,
				right_max = ?
			WHERE chart_info_id = ?`
		err = tx.Exec(sql, edbInfoIdStr, req.DateType, req.StartDate, req.EndDate, req.LeftMin, req.LeftMax, req.RightMin, req.RightMax, req.ChartInfoId).Error
	} else {
		sql := ` UPDATE chart_info
			SET
            	edb_info_ids = ?,
				modify_time = NOW(),
				calendar = ?,
				season_start_date = ?,
				season_end_date = ?,
				left_min = ?,
				left_max = ?,
				right_min = ?,
				right_max = ?
			WHERE chart_info_id = ?`
		err = tx.Exec(sql, edbInfoIdStr, req.Calendar, req.SeasonStartDate, req.SeasonEndDate, req.LeftMin, req.LeftMax, req.RightMin, req.RightMax, req.ChartInfoId).Error
	}
	if err != nil {
		fmt.Println("UPDATE chart_info Err:", err.Error())
		return
	}

	var edbInfoIdArr []string
	mapList := make([]*chartEdbMappingModel.ChartEdbMapping, 0)
	for _, v := range req.ChartEdbInfoList {
		edbInfoIdArr = append(edbInfoIdArr, strconv.Itoa(v.EdbInfoId))
		var count int
		csql := `SELECT COUNT(1) AS count FROM chart_edb_mapping WHERE chart_info_id=? AND edb_info_id=? `
		err = tx.Exec(csql, req.ChartInfoId, v.EdbInfoId).Scan(&count).Error
		if err != nil {
			fmt.Println("QueryRow Err:", err.Error())
			return err
		}
		if count > 0 {
			// 更新指标
			msql := `UPDATE chart_edb_mapping
				SET
                 	modify_time = NOW(),
				 	max_data = ?,
				 	min_data = ?,
				 	is_order = ?,
				 	is_axis = ?,
				 	edb_info_type = ?,
				 	lead_value = ?,
				 	lead_unit = ?,
				 	chart_style = ?,
				 	chart_color = ?,
				 	chart_width = ? 
				WHERE chart_info_id =? AND edb_info_id=? `

			err = tx.Exec(msql, v.MaxData, v.MinData, v.IsOrder, v.IsAxis, v.EdbInfoType, v.LeadValue, v.LeadUnit, v.ChartStyle, v.ChartColor, v.ChartWidth, req.ChartInfoId, v.EdbInfoId).Error
			if err != nil {
				fmt.Println("chart_edb_mapping Err:" + err.Error())
				return err
			}
		} else {
			mapItem := new(chartEdbMappingModel.ChartEdbMapping)
			mapItem.ChartInfoId = req.ChartInfoId
			mapItem.EdbInfoId = v.EdbInfoId
			mapItem.CreateTime = time.Now()
			mapItem.ModifyTime = time.Now()
			timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
			mapItem.UniqueCode = utils.MD5(utils.CHART_PREFIX + "_" + timestamp)
			mapItem.MaxData = v.MaxData
			mapItem.MinData = v.MinData
			mapItem.IsOrder = 0
			if v.IsOrder {
				mapItem.IsOrder = 1
			}
			mapItem.IsAxis = v.IsAxis
			mapItem.EdbInfoType = v.EdbInfoType
			mapItem.LeadValue = v.LeadValue
			mapItem.LeadUnit = v.LeadUnit
			mapItem.ChartStyle = v.ChartStyle
			mapItem.ChartColor = v.ChartColor
			mapItem.ChartWidth = v.ChartWidth
			mapList = append(mapList, mapItem)
		}
	}

	// 批量新增指标
	if len(mapList) > 0 {
		err = tx.Create(mapList).Error
		if err != nil {
			fmt.Println("AddChartEdbMapping Err:" + err.Error())
			return err
		}
	}

	// 清除原图表指标
	if len(edbInfoIdArr) > 0 {
		edbInfoIdStr := strings.Join(edbInfoIdArr, ",")
		if edbInfoIdStr != "" {
			dsql := `DELETE FROM chart_edb_mapping WHERE chart_info_id=? AND edb_info_id NOT IN(` + edbInfoIdStr + `)`
			err = tx.Exec(dsql, req.ChartInfoId).Error
			if err != nil {
				fmt.Println("delete err:" + err.Error())
				return err
			}
		}
	}

	return
}

// RefreshChartInfo 刷新图表
func RefreshChartInfo(chartInfoId int) (err error) {
	var errMsg string
	defer func() {
		if err != nil {
			//go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "ChartInfoRefresh:"+errMsg, utils.EmailSendToUsers)
			fmt.Println("ChartInfoRefresh Err:" + errMsg)
		}
	}()

	// 获取需要刷新的指标
	baseEdbInfoArr, calculateInfoArr, err := GetChartInfoRefreshData(chartInfoId)
	if err != nil {
		errMsg = "获取需要刷新的指标失败:Err:" + err.Error()
		return
	}

	newBaseEdbInfoArr := make([]*edbInfoModel.EdbInfo, 0)
	baseMap := make(map[int]*edbInfoModel.EdbInfo)
	for _, bv := range baseEdbInfoArr {
		if _, ok := baseMap[bv.EdbInfoId]; !ok {
			newBaseEdbInfoArr = append(newBaseEdbInfoArr, bv)
		}
		baseMap[bv.EdbInfoId] = bv
	}

	fmt.Println("calculateInfoArr:", len(calculateInfoArr))

	newCalculateInfoArr := make([]*edbInfoModel.EdbInfo, 0)
	calculateMap := make(map[int]*edbInfoModel.EdbInfo)
	var calculateArr []int
	for _, bv := range calculateInfoArr {
		if _, ok := calculateMap[bv.EdbInfoId]; !ok {
			newCalculateInfoArr = append(newCalculateInfoArr, bv)
			calculateArr = append(calculateArr, bv.EdbInfoId)
		}
		calculateMap[bv.EdbInfoId] = bv
	}
	sort.Ints(calculateArr)

	var startDate string
	for _, bv := range newBaseEdbInfoArr {
		source := bv.Source
		edbInfoId := bv.EdbInfoId
		edbCode := bv.EdbCode
		startDate = bv.StartDate.Format(utils.FormatDate)
		sTime, err := time.Parse(utils.FormatDate, bv.EndDate.Format(utils.FormatDate))
		if err != nil {
			return err
		}
		startDate = sTime.Format(utils.FormatDate)

		fmt.Println("source:", source)

		if source == utils.DATA_SOURCE_THS {
			endDate := time.Now().AddDate(utils.DATA_END_DATE_LIMIT, 0, 0).Format(utils.FormatDate)
			err = RefreshEdbDataByThs(edbInfoId, edbCode, startDate, endDate)
			if err != nil {
				errMsg = "RefreshEdbDataByThs Err:" + err.Error()
			}
		} else if source == utils.DATA_SOURCE_WIND {
			endDate := time.Now().Format(utils.FormatDate)
			err = RefreshEdbDataByWind(edbInfoId, edbCode, startDate, endDate)
			if err != nil {
				errMsg = "RefreshEdbDataByWind Err:" + err.Error()
			}
		} else if source == utils.DATA_SOURCE_PB {
			startDate := sTime.Format(utils.FormatDateUnSpace)
			endDate := time.Now().AddDate(utils.DATA_END_DATE_LIMIT, 0, 0).Format(utils.FormatDateUnSpace)
			err = RefreshEdbDataByPb(edbInfoId, edbCode, startDate, endDate)
			if err != nil {
				errMsg = "RefreshEdbDataByPb Err:" + err.Error()
			}
		} else if source == utils.DATA_SOURCE_MANUAL {
			fmt.Println("RefreshAllEdbDataByManual:" + edbCode)
			err = RefreshAllEdbDataByManual(edbInfoId, bv.Source, edbCode)
			if err != nil {
				errMsg = "RefreshEdbDataByManual Err:" + err.Error()
			}
		} else if source == utils.DATA_SOURCE_LZ {
			endDate := time.Now().AddDate(utils.DATA_END_DATE_LIMIT, 0, 0).Format(utils.FormatDate)
			err = RefreshEdbDataByLz(edbInfoId, edbCode, startDate, endDate)
			if err != nil {
				errMsg = "RefreshEdbDataByLz Err:" + err.Error()
			}
		} else if source == utils.DATA_SOURCE_YS {
			endDate := time.Now().AddDate(utils.DATA_END_DATE_LIMIT, 0, 0).Format(utils.FormatDate)
			err = RefreshAllEdbDataByYs(edbInfoId, source, edbCode, startDate, endDate)
			if err != nil {
				errMsg = "RefreshEdbDataByYs Err:" + err.Error()
			}
		} else if source == utils.DATA_SOURCE_GL {
			endDate := time.Now().AddDate(utils.DATA_END_DATE_LIMIT, 0, 0).Format(utils.FormatDate)
			err = RefreshEdbDataByGl(edbInfoId, edbCode, startDate, endDate)
			if err != nil {
				errMsg = "RefreshEdbDataByGl Err:" + err.Error()
			}
		} else if source == utils.DATA_SOURCE_LT {
			endDate := time.Now().Format(utils.FormatDate)
			err = RefreshAllEdbDataByLt(edbInfoId, source, edbCode, startDate, endDate)
			if err != nil {
				errMsg = "RefreshAllEdbDataByLT Err:" + err.Error()
			}
		} else if source == utils.DATA_SOURCE_ZZ {
			endDate := time.Now().AddDate(utils.DATA_END_DATE_LIMIT, 0, 0).Format(utils.FormatDate)
			err = RefreshEdbDataByZz(edbInfoId, edbCode, startDate, endDate)
			if err != nil {
				errMsg = "RefreshAllEdbDataByZz Err:" + err.Error()
			}
		} else if source == utils.DATA_SOURCE_DL {
			endDate := time.Now().AddDate(utils.DATA_END_DATE_LIMIT, 0, 0).Format(utils.FormatDate)
			err = RefreshEdbDataByDl(edbInfoId, edbCode, startDate, endDate)
			if err != nil {
				errMsg = "RefreshAllEdbDataByDl Err:" + err.Error()
			}
		} else if source == utils.DATA_SOURCE_SH {
			endDate := time.Now().AddDate(utils.DATA_END_DATE_LIMIT, 0, 0).Format(utils.FormatDate)
			err = RefreshEdbDataBySh(edbInfoId, edbCode, startDate, endDate)
			if err != nil {
				errMsg = "RefreshAllEdbDataBySh Err:" + err.Error()
			}
		} else if source == utils.DATA_SOURCE_CFFEX {
			endDate := time.Now().AddDate(utils.DATA_END_DATE_LIMIT, 0, 0).Format(utils.FormatDate)
			err = RefreshEdbDataByCffex(edbInfoId, edbCode, startDate, endDate)
			if err != nil {
				errMsg = "RefreshAllEdbDataByCffex Err:" + err.Error()
			}
		} else if source == utils.DATA_SOURCE_SHFE {
			endDate := time.Now().AddDate(utils.DATA_END_DATE_LIMIT, 0, 0).Format(utils.FormatDate)
			err = RefreshEdbDataByShfe(edbInfoId, edbCode, startDate, endDate)
			if err != nil {
				errMsg = "RefreshAllEdbDataByShfe Err:" + err.Error()
			}
		} else if source == utils.DATA_SOURCE_GIE {
			endDate := time.Now().AddDate(utils.DATA_END_DATE_LIMIT, 0, 0).Format(utils.FormatDate)
			err = RefreshEdbDataByGie(edbInfoId, edbCode, startDate, endDate)
			if err != nil {
				errMsg = "RefreshEdbDataByGie Err:" + err.Error()
			}
		}

		maxAndMinItem, err := edbDataModel.GetEdbInfoMaxAndMinInfo(source, edbCode)
		if err != nil {
			return err
		}
		if maxAndMinItem != nil {
			err = edbDataModel.ModifyEdbInfoMaxAndMinInfo(edbInfoId, maxAndMinItem)
			if err != nil {
				return err
			}
		}
	}

	//endDate := "" //计算指标不限制日期
	////刷新相关计算指标
	//for _, v := range calculateArr {
	//	edbInfo := calculateMap[v]
	//	if edbInfo == nil {
	//		return err
	//	}
	//	edbInfoId := edbInfo.EdbInfoId
	//	source := edbInfo.Source
	//	edbCode := edbInfo.EdbCode
	//
	//
	//	/*startDate = bv.StartDate.Format(utils.FormatDate)
	//	sTime, err := time.Parse(utils.FormatDate, bv.EndDate.Format(utils.FormatDate))
	//	if err != nil {
	//		return err
	//	}
	//	startDate = sTime.Format(utils.FormatDate)*/
	//
	//	if source == utils.DATA_SOURCE_CALCULATE {
	//		startDate = edbInfo.StartDate
	//		sTime, err := time.Parse(utils.FormatDate, edbInfo.EndDate)
	//		if err != nil {
	//			return err
	//		}
	//		startDate = sTime.Format(utils.FormatDate)
	//
	//		var edbInfoIdBytes []string
	//		calculateMap, err := edbInfoModel.GetEdbInfoCalculateDetail(edbInfoId, edbInfo.Source)
	//		if err != nil {
	//			errmsg = "GetEdbInfoCalculateDetail Err:" + err.Error()
	//			return err
	//		}
	//		var formulaStr string
	//		edbInfoList := make([]*edbInfoModel.EdbInfo, 0)
	//
	//		for _, v := range calculateMap {
	//			formulaStr += v.FromTag + ","
	//			edbInfoIdBytes = append(edbInfoIdBytes, v.FromTag)
	//			edbInfo, _ := data_manage.GetEdbInfoById(v.FromEdbInfoId)
	//			edbInfoList = append(edbInfoList, edbInfo)
	//		}
	//		err = data_manage.RefreshAllCalculate(edbInfoList, int(edbInfoId), source, edbInfo.EdbCode, edbInfo.CalculateFormula, startDate, endDate, edbInfoIdBytes)
	//		if err != nil {
	//			errmsg = "RefreshCalculate Err:" + err.Error()
	//			return err
	//		}
	//	} else if source == utils.DATA_SOURCE_CALCULATE_LJZZY { //刷新累计值转月值
	//		calculateLjzzy, err := data_manage.GetEdbInfoCalculateLjzzyDetail(edbInfoId)
	//		if err != nil {
	//			errmsg = "GetEdbInfoCalculateLjzzyDetail Err:" + err.Error()
	//			return err
	//		}
	//		fromEdbInfo, err := data_manage.GetEdbInfoById(calculateLjzzy.FromEdbInfoId)
	//		if err != nil {
	//			errmsg = "GetEdbInfoById Err:" + err.Error()
	//			return err
	//		}
	//		startDate = edbInfo.StartDate
	//		endDate = time.Now().Format(utils.FormatDate)
	//		err = data_manage.RefreshAllCalculateLjzzy(edbInfoId, source, fromEdbInfo, calculateLjzzy.EdbCode, startDate, endDate)
	//		if err != nil {
	//			errmsg = "RefreshAllCalculateLjzzy Err:" + err.Error()
	//			return err
	//		}
	//	} else if source == utils.DATA_SOURCE_CALCULATE_TBZ { //刷新同比值
	//		calculateTbz, err := data_manage.GetEdbInfoCalculateTbzDetail(edbInfoId)
	//		if err != nil {
	//			errmsg = "GetEdbInfoCalculateTbzDetail Err:" + err.Error()
	//			return err
	//		}
	//		fromEdbInfo, err := data_manage.GetEdbInfoById(calculateTbz.FromEdbInfoId)
	//		if err != nil {
	//			errmsg = "GetEdbInfoById Err:" + err.Error()
	//			return err
	//		}
	//		startDate = edbInfo.StartDate
	//		endDate = time.Now().Format(utils.FormatDate)
	//		err = data_manage.RefreshAllCalculateTbz(edbInfoId, source, fromEdbInfo, calculateTbz.EdbCode, startDate, endDate)
	//		if err != nil {
	//			errmsg = "RefreshAllCalculateTbz Err:" + err.Error()
	//			return err
	//		}
	//	} else if source == utils.DATA_SOURCE_CALCULATE_TCZ { //同差值
	//		calculateTcz, err := data_manage.GetEdbInfoCalculateTczDetail(edbInfoId)
	//		if err != nil {
	//			errmsg = "GetEdbInfoCalculateTczDetail Err:" + err.Error()
	//			return err
	//		}
	//		fromEdbInfo, err := data_manage.GetEdbInfoById(calculateTcz.FromEdbInfoId)
	//		if err != nil {
	//			errmsg = "GetEdbInfoById Err:" + err.Error()
	//			return err
	//		}
	//		startDate = edbInfo.StartDate
	//		endDate = time.Now().Format(utils.FormatDate)
	//		err = data_manage.RefreshAllCalculateTcz(edbInfoId, source, fromEdbInfo, calculateTcz.EdbCode, startDate, endDate)
	//		if err != nil {
	//			errmsg = "RefreshCalculateTcz Err:" + err.Error()
	//			return err
	//		}
	//	} else if source == utils.DATA_SOURCE_CALCULATE_NSZYDPJJS { //N数值移动平均计算
	//		calculateNszydpjjs, err := data_manage.GetEdbInfoCalculateNszydpjjsDetail(edbInfoId)
	//		if err != nil {
	//			errmsg = "GetEdbInfoCalculateNszydpjjsDetail Err:" + err.Error()
	//			return err
	//		}
	//		fromEdbInfo, err := data_manage.GetEdbInfoById(calculateNszydpjjs.FromEdbInfoId)
	//		if err != nil {
	//			errmsg = "GetEdbInfoById Err:" + err.Error()
	//			return err
	//		}
	//		formulaInt, _ := strconv.Atoi(edbInfo.CalculateFormula)
	//		startDate = edbInfo.StartDate
	//		err = data_manage.RefreshAllCalculateNszydpjjs(edbInfoId, edbInfo.Source, formulaInt, fromEdbInfo, calculateNszydpjjs.EdbCode, startDate)
	//		if err != nil {
	//			errmsg = "RefreshCalculateNszydpjjs Err:" + err.Error()
	//			return err
	//		}
	//	} else if source == utils.DATA_SOURCE_CALCULATE_HBZ { //刷新环比值
	//		calculateTbz, err := data_manage.GetEdbInfoCalculateHbzDetail(edbInfoId)
	//		if err != nil {
	//			errmsg = "GetEdbInfoCalculateHbzDetail Err:" + err.Error()
	//			return err
	//		}
	//		fromEdbInfo, err := data_manage.GetEdbInfoById(calculateTbz.FromEdbInfoId)
	//		if err != nil {
	//			errmsg = "GetEdbInfoById Err:" + err.Error()
	//			return err
	//		}
	//		startDate = edbInfo.StartDate
	//		endDate = time.Now().Format(utils.FormatDate)
	//		formulaInt, _ := strconv.Atoi(edbInfo.CalculateFormula)
	//		err = data_manage.RefreshAllCalculateHbz(edbInfoId, source, fromEdbInfo, calculateTbz.EdbCode, startDate, endDate, formulaInt)
	//		if err != nil {
	//			errmsg = "RefreshAllCalculateHbz Err:" + err.Error()
	//			return err
	//		}
	//	} else if source == utils.DATA_SOURCE_CALCULATE_HCZ { //刷新环差值
	//		calculateTbz, err := data_manage.GetEdbInfoCalculateHczDetail(edbInfoId)
	//		if err != nil {
	//			errmsg = "GetEdbInfoCalculateHczDetail Err:" + err.Error()
	//			return err
	//		}
	//		fromEdbInfo, err := data_manage.GetEdbInfoById(calculateTbz.FromEdbInfoId)
	//		if err != nil {
	//			errmsg = "GetEdbInfoById Err:" + err.Error()
	//			return err
	//		}
	//		startDate = edbInfo.StartDate
	//		endDate = time.Now().Format(utils.FormatDate)
	//		formulaInt, _ := strconv.Atoi(edbInfo.CalculateFormula)
	//		err = data_manage.RefreshAllCalculateHcz(edbInfoId, source, fromEdbInfo, calculateTbz.EdbCode, startDate, endDate, formulaInt)
	//		if err != nil {
	//			errmsg = "RefreshAllCalculateHcz Err:" + err.Error()
	//			return err
	//		}
	//	} else if source == utils.DATA_SOURCE_CALCULATE_BP { //刷新变频
	//		calculateTbz, err := data_manage.GetEdbInfoCalculateBpDetail(edbInfoId)
	//		if err != nil {
	//			errmsg = "GetEdbInfoCalculateTbzDetail Err:" + err.Error()
	//			return err
	//		}
	//		fromEdbInfo, err := data_manage.GetEdbInfoById(calculateTbz.FromEdbInfoId)
	//		if err != nil {
	//			errmsg = "GetEdbInfoById Err:" + err.Error()
	//			return err
	//		}
	//		startDate = edbInfo.StartDate
	//		endDate = time.Now().Format(utils.FormatDate)
	//		err = data_manage.RefreshAllCalculateBp(edbInfoId, source, fromEdbInfo, calculateTbz.EdbCode, startDate, endDate)
	//		if err != nil {
	//			errmsg = "RefreshAllCalculateBp Err:" + err.Error()
	//			return err
	//		}
	//	} else if source == utils.DATA_SOURCE_CALCULATE_ZJPJ { //刷新直接拼接数据
	//		err = data_manage.RefreshAllCalculateZjpj(edbInfo)
	//		if err != nil {
	//			errmsg = "RefreshAllCalculateZjpj Err:" + err.Error()
	//			return err
	//		}
	//	} else if source == utils.DATA_SOURCE_CALCULATE_LJZTBPJ { //刷新累计值同比拼接数据
	//		err = data_manage.RefreshAllCalculateLjztbpj(edbInfo)
	//		if err != nil {
	//			errmsg = "RefreshAllCalculateLjztbpj Err:" + err.Error()
	//			return err
	//		}
	//	}
	//
	//	maxAndMinItem, err := data_manage.GetEdbInfoMaxAndMinInfo(source, edbCode)
	//	if err != nil {
	//		return err
	//	}
	//
	//	if maxAndMinItem != nil {
	//		err = data_manage.ModifyEdbInfoMaxAndMinInfo(edbInfoId, maxAndMinItem)
	//		if err != nil {
	//			return err
	//		}
	//	}
	//	fmt.Println("end calculateArr:", v, time.Now())
	//}

	return
}

// GetChartInfoRefreshData 获取需要刷新的指标
func GetChartInfoRefreshData(chartInfoId int) (baseEdbInfoArr, calculateInfoArr []*edbInfoModel.EdbInfo, err error) {
	sql := ` SELECT b.* FROM chart_edb_mapping AS a
		   INNER JOIN edb_info AS b ON a.edb_info_id=b.edb_info_id 
		   WHERE a.chart_info_id=? `
	edbInfoList := make([]*edbInfoModel.EdbInfo, 0)
	err = global.MYSQL["data"].Exec(sql, chartInfoId).Error
	if err != nil {
		return
	}

	for _, v := range edbInfoList {
		fmt.Println(v.EdbInfoId, v.EdbType)
		if v.EdbType == 1 {
			baseEdbInfoArr = append(baseEdbInfoArr, v)
		} else {
			calculateInfoArr = append(calculateInfoArr, v)
			newBaseEdbInfoArr, newCalculateInfoArr, err := GetChartRefreshEdbInfo(v.EdbInfoId, v.Source, 0)
			if err != nil {
				return baseEdbInfoArr, calculateInfoArr, err
			}
			baseEdbInfoArr = append(baseEdbInfoArr, newBaseEdbInfoArr...)
			calculateInfoArr = append(calculateInfoArr, newCalculateInfoArr...)
		}
	}

	return
}

// GetChartRefreshEdbInfo
func GetChartRefreshEdbInfo(edbInfoId, source, n int) (baseEdbInfoArr, calculateInfoArr []*edbInfoModel.EdbInfo, err error) {
	calculateList, err := GetEdbInfoCalculateMap(edbInfoId, source)
	if err != nil && err != utils.ErrNoRow {
		return
	}
	n++
	for _, item := range calculateList {
		fmt.Println(item.EdbInfoId)
		if item.EdbType == 1 {
			baseEdbInfoArr = append(baseEdbInfoArr, item)
		} else {
			calculateInfoArr = append(calculateInfoArr, item)
			if n > 10 {
				return
			}
			newBaseEdbInfoArr, newCalculateInfoArr, _ := GetChartRefreshEdbInfo(item.EdbInfoId, item.Source, n)
			baseEdbInfoArr = append(baseEdbInfoArr, newBaseEdbInfoArr...)
			calculateInfoArr = append(calculateInfoArr, newCalculateInfoArr...)
		}
	}

	return
}

func GetEdbInfoCalculateMap(edbInfoId, source int) (list []*edbInfoModel.EdbInfo, err error) {
	sql := ` SELECT b.* FROM edb_info_calculate_mapping AS a
			INNER JOIN edb_info AS b ON a.from_edb_info_id=b.edb_info_id
			WHERE a.edb_info_id=? ORDER BY sort ASC `
	err = global.MYSQL["data"].Exec(sql, edbInfoId).Error

	return
}