package line_equation

import (
	"github.com/beego/beego/v2/client/orm"
	"hongze/hz_crm_api/models/data_manage"
)

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) {
	o := orm.NewOrmUsingDB("data")
	tx, err := o.Begin()
	if err != nil {
		return
	}
	defer func() {
		if err != nil {
			_ = tx.Rollback()
		} else {
			_ = tx.Commit()
		}
	}()
	// 新增批量配置
	newMultipleGraphConfigId, err := tx.Insert(multipleGraphConfig)
	if err != nil {
		return
	}
	multipleGraphConfig.MultipleGraphConfigId = int(newMultipleGraphConfigId)

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

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

		// 批量配置的关系表
		multipleGraphConfigChartMapping := v.MultipleGraphConfigChartMapping
		multipleGraphConfigChartMapping.ChartInfoId = chartInfo.ChartInfoId
		multipleGraphConfigChartMapping.MultipleGraphConfigId = multipleGraphConfig.MultipleGraphConfigId
		newMultipleGraphConfigChartMappingId, tmpErr := tx.Insert(v.MultipleGraphConfigChartMapping)
		if tmpErr != nil {
			err = tmpErr
			return
		}
		multipleGraphConfigChartMapping.Id = int(newMultipleGraphConfigChartMappingId)
	}

	return
}

// CreateAndEditLineEquationChartAndEdb 批量新增/编辑拟合方程图表
func CreateAndEditLineEquationChartAndEdb(multipleGraphConfig *data_manage.MultipleGraphConfig, batchAddChartList []BatchAddChart, updateMultipleGraphConfigCols, updateChartCols []string) (err error) {
	o := orm.NewOrmUsingDB("data")
	tx, err := o.Begin()
	if err != nil {
		return
	}
	defer func() {
		if err != nil {
			_ = tx.Rollback()
		} else {
			_ = tx.Commit()
		}
	}()
	// 编辑批量配置
	_, err = tx.Update(multipleGraphConfig, updateMultipleGraphConfigCols...)
	if err != nil {
		return
	}

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

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

		// 批量配置的关系表
		{
			multipleGraphConfigChartMapping := v.MultipleGraphConfigChartMapping
			if multipleGraphConfigChartMapping.Id <= 0 {
				multipleGraphConfigChartMapping.ChartInfoId = chartInfo.ChartInfoId
				multipleGraphConfigChartMapping.MultipleGraphConfigId = multipleGraphConfig.MultipleGraphConfigId
				newMultipleGraphConfigChartMappingId, tmpErr := tx.Insert(v.MultipleGraphConfigChartMapping)
				if tmpErr != nil {
					err = tmpErr
					return
				}
				multipleGraphConfigChartMapping.Id = int(newMultipleGraphConfigChartMappingId)
			} else {
				if multipleGraphConfigChartMapping.ChartInfoId != chartInfo.ChartInfoId {
					multipleGraphConfigChartMapping.ChartInfoId = chartInfo.ChartInfoId
					_, err = tx.Update(multipleGraphConfigChartMapping, "ChartInfoId")
					if err != nil {
						return
					}
				}
			}
			batchAddChartList[k].MultipleGraphConfigChartMapping = multipleGraphConfigChartMapping
		}
	}

	return
}