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"
)

// HandleBaiinfoIndex 处理百川盈弗的excel数据
func HandleBaiinfoIndex(baseFilePath, terminalCode, renameFilePath, indexName, indexCode string, excelDataMap map[string]string) {
	var err error

	errMsgList := make([]string, 0)
	defer func() {
		if len(errMsgList) > 0 {
			go alarm_msg.SendAlarmMsg(fmt.Sprint("红桃3实际数据处理失败,err:", strings.Join(errMsgList, "\n")), 3)
		}
	}()

	indexObj := new(models.BaseFromBaiinfoIndex)
	var indexId int64

	addDataList := make([]*models.BaseFromBaiinfoData, 0)

	exitDataMap := make(map[string]*models.BaseFromBaiinfoData)

	// 修改指标信息
	if indexName == "" {
		utils.FileLog.Info("未刷新到指标数据:indexName:" + indexName)
		return
	}
	//判断指标是否存在
	var isAdd int
	item, err := indexObj.GetSmmIndexItem(indexCode)
	if err != nil {
		if err.Error() == utils.ErrNoRow() {
			isAdd = 1
		} else {
			isAdd = -1
			fmt.Println("GetSmmIndexItem Err:" + err.Error())
			return
		}
	}
	if item != nil && item.BaseFromBaiinfoIndexId > 0 {
		fmt.Println("item:", item)
		isAdd = 2
	} else {
		isAdd = 1
	}

	if isAdd == 1 {
		indexObj.IndexCode = indexCode
		indexObj.IndexName = indexName
		indexObj.ModifyTime = time.Now()
		indexObj.CreateTime = time.Now()
		indexObj.BaseFileName = baseFilePath
		indexObj.RenameFileName = renameFilePath
		indexObj.TerminalCode = terminalCode

		lastId, err := indexObj.AddBaseFromBaiinfoIndex()
		if err != nil {
			fmt.Println("add err:" + err.Error())
			return
		}
		indexId = lastId
		//修改排序
		go models.ModifyBaiinfoIndexSort(indexId)
	} else if isAdd == 2 {
		if item.TerminalCode == `` && terminalCode != `` {
			item.TerminalCode = terminalCode
			item.Update([]string{"TerminalCode"})
		}

		//获取已存在的所有数据
		exitDataList, err := models.GetBaseFromBaiinfoDataByCode(indexCode)
		if err != nil {
			fmt.Println("GetIndexDataList Err:" + err.Error())
			return
		}
		fmt.Println("exitDataListLen:", len(exitDataList))
		for _, v := range exitDataList {
			dateStr := v.DataTime
			exitDataMap[dateStr] = v
		}
		indexId = item.BaseFromBaiinfoIndexId
	}

	// 遍历excel数据，然后跟现有的数据做校验，不存在则入库
	for date, value := range excelDataMap {
		if findData, ok := exitDataMap[date]; !ok {
			_, err := time.ParseInLocation(utils.FormatDate, date, time.Local)
			if err != nil {
				fmt.Println("time.ParseInLocation Err:" + err.Error())
				return
			}
			if !strings.Contains(value, "#N/A") {
				var saveDataTime time.Time
				if strings.Contains(date, "00:00:00") {
					saveDataTime, err = time.Parse(utils.FormatDateTime, date)
				} else {
					saveDataTime, err = time.Parse(utils.FormatDate, date)
				}
				if err != nil {
					errMsg := "saveDataTime parse err:" + err.Error() + " dataTime:" + date
					fmt.Println(errMsg)
					continue
				}
				timestamp := saveDataTime.UnixNano() / 1e6

				dataItem := new(models.BaseFromBaiinfoData)
				dataItem.BaseFromBaiinfoIndexId = int(indexId)
				dataItem.IndexCode = indexCode
				dataItem.DataTime = date
				dataItem.Value = value
				dataItem.CreateTime = time.Now()
				dataItem.ModifyTime = time.Now()
				dataItem.DataTimestamp = timestamp
				addDataList = append(addDataList, dataItem)
			}
		} else {
			if findData != nil && findData.Value != value && !strings.Contains(value, "#N/A") { //修改数据

			}
		}
	}

	if len(addDataList) > 0 {
		err = models.AddBaseFromBaiinfoData(addDataList)
		if err != nil {
			fmt.Println("dataObj.Add() Err:" + err.Error())
		}

		item, err := models.GetBaiinfoIndexInfoMaxAndMinInfo(indexCode)
		if err == nil && item != nil {
			go models.ModifyBaiinfoIndexMaxAndMinInfo(indexCode, item)
		}
	}

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

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