package fix

import (
	"encoding/json"
	excelModel "eta/eta_index_lib/models/excel"
	"eta/eta_index_lib/utils"
	"fmt"
	"time"
)

// FixTableData ETA1.0.2 自定义分析（生成指标数据修复）
func FixTableData() {
	// 获取一级分类
	classifyList, err := excelModel.GetExcelClassifyByParentId(0, utils.EXCEL_DEFAULT)
	if err != nil && err.Error() != utils.ErrNoRow() {
		fmt.Println("数据修复失败，Err:" + err.Error())
		return
	}
	timeTableMap := make(map[int]int)
	mixTableMap := make(map[int]int)
	for _, v := range classifyList {
		// 时间序列表格
		classify := &excelModel.ExcelClassify{
			//ExcelClassifyId:   0,
			ExcelClassifyName: v.ExcelClassifyName,
			ParentId:          v.ParentId,
			Source:            utils.TIME_TABLE,
			SysUserId:         v.SysUserId,
			SysUserRealName:   v.SysUserRealName,
			Level:             v.Level,
			UniqueCode:        utils.MD5(fmt.Sprint(v.UniqueCode, "_", utils.TIME_TABLE)),
			Sort:              v.Sort,
			CreateTime:        time.Now(),
			ModifyTime:        time.Now(),
		}
		_, err = excelModel.AddExcelClassify(classify)
		timeTableMap[v.ExcelClassifyId] = classify.ExcelClassifyId

		// 混合表格
		classify2 := &excelModel.ExcelClassify{
			//ExcelClassifyId:   0,
			ExcelClassifyName: v.ExcelClassifyName,
			ParentId:          v.ParentId,
			Source:            utils.MIXED_TABLE,
			SysUserId:         v.SysUserId,
			SysUserRealName:   v.SysUserRealName,
			Level:             v.Level,
			UniqueCode:        utils.MD5(fmt.Sprint(v.UniqueCode, "_", utils.MIXED_TABLE)),
			Sort:              v.Sort,
			CreateTime:        time.Now(),
			ModifyTime:        time.Now(),
		}
		_, err = excelModel.AddExcelClassify(classify2)
		mixTableMap[v.ExcelClassifyId] = classify2.ExcelClassifyId
	}

	// 修改时间序列表
	{
		// 获取时间序列表
		timeTableExcelList, err := excelModel.GetNoContentExcelInfoAll(utils.TIME_TABLE, 0)
		if err != nil && err.Error() != utils.ErrNoRow() {
			fmt.Println("获取时间序列表列表失败，Err:" + err.Error())
			return
		}

		for _, v := range timeTableExcelList {
			classifyId, ok := timeTableMap[v.ExcelClassifyId]
			if !ok {
				continue
			}
			excelModel.UpdateExcelInfoClassifyId(classifyId, v.ExcelInfoId)
		}
	}

	// 修改混合序列表
	{
		// 获取时间序列表
		mixTableExcelList, err := excelModel.GetNoContentExcelInfoAll(utils.MIXED_TABLE, 0)
		if err != nil && err.Error() != utils.ErrNoRow() {
			fmt.Println("获取时间序列表列表失败，Err:" + err.Error())
			return
		}

		for _, v := range mixTableExcelList {
			classifyId, ok := mixTableMap[v.ExcelClassifyId]
			if !ok {
				continue
			}
			excelModel.UpdateExcelInfoClassifyId(classifyId, v.ExcelInfoId)
		}
	}

	fmt.Println("完成生成指标数据修复")
}

// FixTableDataMapping ETA1.0.2 自定义分析（修复excel与指标的关系）
func FixTableDataMapping() {

	// 修改时间序列表
	{
		// 获取时间序列表
		timeTableExcelList, err := excelModel.GetAllExcelInfoBySource(utils.TIME_TABLE)
		if err != nil && err.Error() != utils.ErrNoRow() {
			fmt.Println("获取时间序列表列表失败，Err:" + err.Error())
			return
		}

		for _, v := range timeTableExcelList {

			var tableData excelModel.TableDataReq
			err = json.Unmarshal([]byte(v.Content), &tableData)
			if err != nil {
				fmt.Println(v.ExcelInfoId, "json转结构体失败，Err:"+err.Error())
				continue
			}
			if len(tableData.EdbInfoIdList) > 0 {
				excelEdbMappingList := make([]*excelModel.ExcelEdbMapping, 0)
				for _, edbInfoId := range tableData.EdbInfoIdList {
					excelEdbMappingList = append(excelEdbMappingList, &excelModel.ExcelEdbMapping{
						//ExcelEdbMappingId: 0,
						ExcelInfoId: v.ExcelInfoId,
						Source:      v.Source,
						EdbInfoId:   edbInfoId,
						CreateTime:  time.Now(),
						ModifyTime:  time.Now(),
					})
				}
				err = excelModel.AddExcelEdbMappingMulti(excelEdbMappingList)
				if err != nil {
					fmt.Println(v.ExcelInfoId, "自定义表格关系保存失败,Err:"+err.Error())
					continue
				}
			}

		}
	}

	// 修改混合序列表
	{
		// 获取时间序列表
		mixTableExcelList, err := excelModel.GetAllExcelInfoBySource(utils.MIXED_TABLE)
		if err != nil && err.Error() != utils.ErrNoRow() {
			fmt.Println("获取时间序列表列表失败，Err:" + err.Error())
			return
		}

		for _, excelInfo := range mixTableExcelList {
			var result excelModel.MixedTableReq
			err = json.Unmarshal([]byte(excelInfo.Content), &result)
			if err != nil {
				fmt.Println(excelInfo.ExcelInfoId, "修改混合序列表,json转结构体失败，Err:"+err.Error())
				continue
			}
			newResult, tmpErr, _ := GetMixedTableCellData(result.CellRelation, result.Data)
			if tmpErr != nil {
				fmt.Println(excelInfo.ExcelInfoId, "获取最新的数据失败，Err:"+err.Error())
				continue
			}
			edbInfoIdList := make([]int, 0)
			edbInfoIdMap := make(map[int]int)
			for _, tmpV := range newResult {
				for _, v := range tmpV {
					if v.EdbInfoId > 0 {
						if _, ok := edbInfoIdMap[v.EdbInfoId]; !ok {
							edbInfoIdMap[v.EdbInfoId] = v.EdbInfoId
							edbInfoIdList = append(edbInfoIdList, v.EdbInfoId)
						}
					}
				}
			}

			if len(edbInfoIdList) > 0 {
				excelEdbMappingList := make([]*excelModel.ExcelEdbMapping, 0)
				for _, edbInfoId := range edbInfoIdList {
					excelEdbMappingList = append(excelEdbMappingList, &excelModel.ExcelEdbMapping{
						//ExcelEdbMappingId: 0,
						ExcelInfoId: excelInfo.ExcelInfoId,
						Source:      excelInfo.Source,
						EdbInfoId:   edbInfoId,
						CreateTime:  time.Now(),
						ModifyTime:  time.Now(),
					})
				}
				err = excelModel.AddExcelEdbMappingMulti(excelEdbMappingList)
				if err != nil {
					fmt.Println(excelInfo.ExcelInfoId, "混合表格关系保存失败,Err:"+err.Error())
					continue
				}
			}

		}

	}

	fmt.Println("完成excel与指标的关系修复")
}

// CellRelationConf
// @Description: 单元格的关系配置结构体
type CellRelationConf struct {
	CellRelation
	//Type         int          `json:"type" description:"数据类型，跟MixedTableCellDataReq的DataType保持一致"`
	//Key          string       `json:"key" description:"单元格的唯一标识"`
	RelationDate CellRelation `json:"relation_date"`
	RelationEdb  CellRelation `json:"relation_edb"`
}

// CellRelation
// @Description: 单元格的关系结构体
type CellRelation struct {
	Type int    `json:"type" description:"数据类型，跟MixedTableCellDataReq的DataType保持一致"`
	Key  string `json:"key" description:"单元格的唯一标识"`
}

// GetMixedTableCellData 获取混合表格数据
func GetMixedTableCellData(cellRelationConf string, config [][]excelModel.MixedTableCellDataReq) (newMixedTableCellDataList [][]excelModel.MixedTableCellDataReq, err error, errMsg string) {
	// 单元格关系配置x信息
	cellRelationConfMap := make(map[string]CellRelationConf)
	cellRelationConfList := make([]CellRelationConf, 0)
	if cellRelationConf != `` {
		err = json.Unmarshal([]byte(cellRelationConf), &cellRelationConfList)
		if err != nil {
			return
		}

		for _, v := range cellRelationConfList {
			cellRelationConfMap[v.Key] = v
		}
	}

	// 找出所有的关联指标id
	config, _, _, err, errMsg = handleConfig(config)
	if err != nil {
		return
	}

	newMixedTableCellDataList = config

	return
}

// 单元格的数据类型
const (
	DateDT          = iota + 1 //日期
	EdbDT                      // 指标类型
	CustomTextDT               // 自定义文本
	InsertDataDT               // 插值
	PopInsertDataDT            // 弹框插值
)

func handleConfig(configList [][]excelModel.MixedTableCellDataReq) (newConfig [][]excelModel.MixedTableCellDataReq, edbInfoIdList []int, dataEdbInfoIdList []int, err error, errMsg string) {
	edbInfoIdList = make([]int, 0)
	dataEdbInfoIdList = make([]int, 0)

	for ck, rowList := range configList {
		for _, cell := range rowList {
			switch cell.DataType {
			case EdbDT: // 指标信息
				edbInfoIdList = append(edbInfoIdList, cell.EdbInfoId)
			case InsertDataDT, PopInsertDataDT: // 插值、弹框插值
				dataEdbInfoIdList = append(dataEdbInfoIdList, cell.EdbInfoId)
			}
		}
		configList[ck] = rowList
	}

	newConfig = configList

	return
}
