package line_equation

import (
	"eta/eta_api/global"
	"eta/eta_api/models/data_manage"
	"eta/eta_api/utils"
)

type BatchAddChart struct {
	ChartInfo                       *data_manage.ChartInfo
	EdbMappingList                  []*data_manage.ChartEdbMapping
	MultipleGraphConfigChartMapping *data_manage.MultipleGraphConfigChartMapping
}

// CreateLineEquationChartAndEdb 批量新增拟合方程图表
func CreateLineEquationChartAndEdb(multipleGraphConfig *data_manage.MultipleGraphConfig, batchAddChartList []BatchAddChart) (err error) {
	tx := global.DbMap[utils.DbNameIndex].Begin()
	defer func() {
		if err != nil {
			_ = tx.Rollback()
		} else {
			_ = tx.Commit()
		}
	}()
	// 新增批量配置
	err = tx.Create(multipleGraphConfig).Error
	if err != nil {
		return
	}

	for k, v := range batchAddChartList {
		// 新增图表信息
		chartInfo := v.ChartInfo
		err = tx.Create(chartInfo).Error
		if err != nil {
			return
		}
		batchAddChartList[k].ChartInfo = chartInfo

		// 指标mapping
		edbMappingList := v.EdbMappingList
		if len(edbMappingList) > 0 {
			for i := range edbMappingList {
				edbMappingList[i].ChartInfoId = chartInfo.ChartInfoId
			}
			err = tx.CreateInBatches(edbMappingList, utils.MultiAddNum).Error
			if err != nil {
				return
			}
		}
		batchAddChartList[k].EdbMappingList = edbMappingList

		// 批量配置的关系表
		multipleGraphConfigChartMapping := v.MultipleGraphConfigChartMapping
		multipleGraphConfigChartMapping.ChartInfoId = chartInfo.ChartInfoId
		multipleGraphConfigChartMapping.MultipleGraphConfigId = multipleGraphConfig.MultipleGraphConfigId
		err = tx.Create(v.MultipleGraphConfigChartMapping).Error
		if err != nil {
			return
		}
	}

	return
}

// CreateAndEditLineEquationChartAndEdb 批量新增/编辑拟合方程图表
func CreateAndEditLineEquationChartAndEdb(multipleGraphConfig *data_manage.MultipleGraphConfig, batchAddChartList []BatchAddChart, updateMultipleGraphConfigCols, updateChartCols []string) (err error) {
	tx := global.DbMap[utils.DbNameIndex].Begin()
	defer func() {
		if err != nil {
			_ = tx.Rollback()
		} else {
			_ = tx.Commit()
		}
	}()
	// 编辑批量配置
	err = tx.Select(updateMultipleGraphConfigCols).Updates(multipleGraphConfig).Error
	if err != nil {
		return
	}

	for k, v := range batchAddChartList {
		// 新增图表信息
		chartInfo := v.ChartInfo
		var isDelMapping bool
		{
			if chartInfo.ChartInfoId <= 0 {
				err = tx.Create(chartInfo).Error
				if err != nil {
					return
				}
				chartInfo.ChartInfoId = chartInfo.ChartInfoId
				batchAddChartList[k].ChartInfo = chartInfo
			} else {
				// 更改图表信息
				err = tx.Select(updateChartCols).Updates(chartInfo).Error
				if err != nil {
					return
				}
				isDelMapping = true
			}
		}

		// 图表与指标的关系表
		{
			// 先删除图表关联的指标关系,然后再新增
			if isDelMapping {
				sql := ` DELETE FROM  chart_edb_mapping WHERE chart_info_id=? `
				err = tx.Exec(sql, chartInfo.ChartInfoId).Error
				if err != nil {
					return
				}
			}
			// 指标mapping
			edbMappingList := v.EdbMappingList
			if len(edbMappingList) > 0 {
				for i := range edbMappingList {
					edbMappingList[i].ChartInfoId = chartInfo.ChartInfoId
				}
				err = tx.CreateInBatches(edbMappingList, utils.MultiAddNum).Error
				if err != nil {
					return
				}
			}
			batchAddChartList[k].EdbMappingList = edbMappingList
		}

		// 批量配置的关系表
		{
			multipleGraphConfigChartMapping := v.MultipleGraphConfigChartMapping
			if multipleGraphConfigChartMapping.Id <= 0 {
				multipleGraphConfigChartMapping.ChartInfoId = chartInfo.ChartInfoId
				multipleGraphConfigChartMapping.MultipleGraphConfigId = multipleGraphConfig.MultipleGraphConfigId
				err = tx.Create(v.MultipleGraphConfigChartMapping).Error
				if err != nil {
					return
				}
			} else {
				if multipleGraphConfigChartMapping.ChartInfoId != chartInfo.ChartInfoId {
					multipleGraphConfigChartMapping.ChartInfoId = chartInfo.ChartInfoId
					err = tx.Select("ChartInfoId").Updates(multipleGraphConfigChartMapping).Error
					if err != nil {
						return
					}
				}
			}
			batchAddChartList[k].MultipleGraphConfigChartMapping = multipleGraphConfigChartMapping
		}
	}

	return
}