package services

import (
	"eta/eta_index_lib/logic"
	"eta/eta_index_lib/models"
	"eta/eta_index_lib/services/alarm_msg"
	"eta/eta_index_lib/utils"
	"fmt"
	"strings"
	"time"
)

// HandleSciIndex 处理卓创（红桃3）的Excel数据
func HandleSciIndex(indexNameList, thirdIndexIdList, frequencyList, unitList []string, dataMap map[string]map[string]string, filePath, terminalCode string) {
	// 卓创（红桃3）指标id列表

	sciIndexModel := new(models.BaseFromSciIndex)
	list, err := sciIndexModel.GetAllIndex()
	if err != nil {
		fmt.Println("获取指标失败:", err)
		return
	}
	allIndexMap := make(map[string]*models.BaseFromSciIndex)
	for _, v := range list {
		allIndexMap[v.IndexCode] = v
	}

	// 需要入库的指标下标
	needAddIndexKeyList := make([]int, 0)
	needAddIndexMap := make(map[string]int, 0)
	for key, v := range thirdIndexIdList {
		if sciIndexInfo, ok := allIndexMap[v]; !ok {
			if _, ok2 := needAddIndexMap[v]; !ok2 {
				needAddIndexKeyList = append(needAddIndexKeyList, key)
				needAddIndexMap[v] = key
			}
		} else {
			updateColList := make([]string, 0)
			if sciIndexInfo.FilePath != filePath {
				sciIndexInfo.FilePath = filePath
				updateColList = append(updateColList, "FilePath")
			}
			if sciIndexInfo.TerminalCode != terminalCode {
				sciIndexInfo.TerminalCode = terminalCode
				updateColList = append(updateColList, "TerminalCode")
			}
			// 更新指标源信息
			if len(updateColList) > 0 {
				sciIndexInfo.Update(updateColList)
			}
		}
	}
	//fmt.Println(needAddIndexKeyList)
	//return
	addSciIndexList := make([]*models.BaseFromSciIndex, 0)
	// 新的指标入库
	for _, key := range needAddIndexKeyList {
		tmpSciIndex := &models.BaseFromSciIndex{
			//BaseFromSciIndexId: 0,
			//ClassifyId:         0,
			IndexCode: thirdIndexIdList[key],
			IndexName: indexNameList[key],
			Frequency: frequencyList[key],
			Unit:      unitList[key],
			//StartDate: time.Time{},
			//EndDate:      time.Time{},
			FilePath:     filePath,
			TerminalCode: terminalCode,
			CreateTime:   time.Now(),
			ModifyTime:   time.Now(),
		}
		addSciIndexList = append(addSciIndexList, tmpSciIndex)
	}
	//fmt.Println(addSciIndexList)
	if len(addSciIndexList) > 0 {
		err = sciIndexModel.BatchAdd(addSciIndexList)
		if err != nil {
			fmt.Println("批量添加指标失败:", err)
			return
		}
		fmt.Println("添加成功")
	}

	// 红桃3实际数据处理
	HandleSciData(dataMap)

}

// HandleData 红桃3实际数据处理
func HandleSciData(dataMap map[string]map[string]string) {
	errMsgList := make([]string, 0)
	defer func() {
		if len(errMsgList) > 0 {
			go alarm_msg.SendAlarmMsg(fmt.Sprint("红桃3实际数据处理失败,err:", strings.Join(errMsgList, "\n")), 3)
		}
	}()
	// 获取所有的指标
	sciIndexModel := new(models.BaseFromSciIndex)
	list, err := sciIndexModel.GetAllIndex()
	if err != nil {
		fmt.Println("获取指标失败:", err)
		return
	}
	allIndexMap := make(map[string]*models.BaseFromSciIndex)
	for _, v := range list {
		allIndexMap[v.IndexCode] = v
	}

	sciIndexDataModel := new(models.BaseFromSciData)
	for indexCode, data := range dataMap {
		indexInfo, ok := allIndexMap[indexCode]
		if !ok {
			fmt.Println("找不到该指标:", indexCode)
			continue
		}

		indexDataList, err := sciIndexDataModel.GetIndexDataList(indexCode)
		if err != nil {
			errMsgList = append(errMsgList, fmt.Sprint("查找卓创基础指标失败,指标编码：", indexCode, ";错误原因：", err.Error()))
			continue
		}
		indexDataExistMap := make(map[string]*models.BaseFromSciData)
		for _, indexData := range indexDataList {
			indexDataExistMap[indexData.DataTime] = indexData
		}
		addSciDataList := make([]*models.BaseFromSciData, 0)

		for currDate, currVal := range data {
			currDataTime, tmpErr := time.ParseInLocation(utils.FormatDate, currDate, time.Local)
			if tmpErr != nil {
				errMsgList = append(errMsgList, fmt.Sprint("时间格式化失败,指标编码：", currDate, ";错误原因：", tmpErr.Error()))
				continue
			}
			timestamp := currDataTime.UnixNano() / 1e6

			sciData, ok := indexDataExistMap[currDate]
			//判断是否存在数据,如果不存在,那么插入数据,存在的话那么修改数据
			if !ok {
				tmpBaseFromSciData := &models.BaseFromSciData{
					//SciDataId:          0,
					BaseFromSciIndexId: indexInfo.BaseFromSciIndexId,
					IndexCode:          indexInfo.IndexCode,
					DataTime:           currDate,
					Value:              currVal,
					DataTimestamp:      timestamp,
					CreateTime:         time.Now(),
					ModifyTime:         time.Now(),
				}
				indexDataExistMap[currDate] = tmpBaseFromSciData
				addSciDataList = append(addSciDataList, tmpBaseFromSciData)
			} else {
				// 更新数据
				existValue := sciData.Value
				if existValue != currVal {
					//fmt.Println("existValue:", existValue, ";====;currVal:", currVal)
					sciData.Value = currVal
					sciData.ModifyTime = time.Now()
					// 如果是已经入库了数据,那么就更新,否则只是单纯更改内存数据,而不去更新数据库
					if sciData.SciDataId > 0 {
						tmpErr = sciData.Update([]string{"Value", "ModifyTime"})
						if tmpErr != nil {
							errMsgList = append(errMsgList, fmt.Sprint("指标数据更新失败,指标编码：", indexCode, "，时间：", currDate, ";错误原因：", tmpErr.Error()))
							continue
						}
					}
				}
			}
		}

		if len(addSciDataList) > 0 {
			err = sciIndexDataModel.BatchAdd(addSciDataList)
			if err != nil {
				errMsgList = append(errMsgList, fmt.Sprint("批量添加数据失败,指标编码：", indexCode, err.Error()))
				continue
			}
		}
		indexInfo.ModifyTime = time.Now()
		indexInfo.Update([]string{"ModifyTime"})

		// 同步刷新ETA图库红桃3的指标
		{
			// 获取指标详情
			edbInfo, err := models.GetEdbInfoByEdbCode(utils.DATA_SOURCE_SCI, indexInfo.IndexCode)
			if err != nil && err.Error() != utils.ErrNoRow() {
				errMsgList = append(errMsgList, fmt.Sprint("刷新ETA指标异常，指标编码：", indexCode, err.Error()))
				continue
			}

			// 已经加入到指标库的话，那么就去更新ETA指标库吧
			if edbInfo != nil {
				go logic.RefreshBaseEdbInfo(edbInfo, ``)
			}
		}
	}
}
