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

func SmmIndexHandle(baseFilePath, renameFilePath, indexName, indexCode, unit, frequency, source string, excelDataMap map[string]string, terminalCode string) (err error, errMsg string) {
	messages := make([]string, 0)
	defer func() {
		if len(messages) > 0 {
			errMsg = strings.Join(messages, "\n")
			utils.FileLog.Info("SmmIndexHandle ErrMsg: %s", errMsg)
		}
		if err != nil {
			tips := fmt.Sprintf("SMM有色excel数据处理失败, Err: %s, ErrMsg: %s", err.Error(), errMsg)
			utils.FileLog.Info(tips)
			go alarm_msg.SendAlarmMsg(tips, 3)
		}
	}()

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

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

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

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

	if !strings.Contains(frequency, "度") {
		frequency = frequency + "度"
	}

	if isAdd == 1 {
		indexObj.IndexCode = indexCode
		indexObj.IndexName = indexName
		indexObj.Unit = unit
		indexObj.Frequency = frequency
		indexObj.Interface = source
		indexObj.ModifyTime = time.Now()
		indexObj.CreateTime = time.Now()
		indexObj.BaseFileName = baseFilePath
		indexObj.RenameFileName = renameFilePath
		indexObj.TerminalCode = terminalCode
		lastId, e := indexObj.AddBaseFromSmmIndex()
		if e != nil {
			err = fmt.Errorf("AddBaseFromSmmIndex err: %s", e.Error())
			return
		}
		indexId = lastId
		go models.ModifySmmIndexSort(indexId)
	} else if isAdd == 2 {
		//获取已存在的所有数据
		exitDataList, e := models.GetBaseFromSmmDataBySmmCode(indexCode)
		if e != nil {
			err = fmt.Errorf("GetBaseFromSmmDataBySmmCode err: %s", e.Error())
			return
		}
		fmt.Println("exitDataListLen:", len(exitDataList))
		for _, v := range exitDataList {
			//dateStr := v.DataTime.Format(utils.FormatDate)
			exitDataMap[v.DataTime] = v
		}
		indexId = item.BaseFromSmmIndexId
		//修改指标信息
		updateParams := make(map[string]interface{})
		updateParams["index_name"] = indexName
		updateParams["frequency"] = frequency
		updateParams["unit"] = unit
		updateParams["interface"] = source
		updateParams["modify_time"] = time.Now()
		updateParams["terminal_code"] = terminalCode

		if baseFilePath != `` && item.BaseFileName != baseFilePath {
			updateParams["base_file_name"] = baseFilePath
		}
		if renameFilePath != `` && item.RenameFileName != renameFilePath {
			updateParams["rename_file_name"] = renameFilePath
		}
		if terminalCode != `` && item.TerminalCode != terminalCode {
			updateParams["terminal_code"] = terminalCode
		}

		whereParam := make(map[string]interface{})
		whereParam["index_code"] = indexCode

		smmIndexObj := new(models.BaseFromSmmIndex)
		e = smmIndexObj.Update(updateParams, whereParam)
		if e != nil {
			err = fmt.Errorf("smm index update err: %s", e.Error())
			return
		}
		if item.IndexName != indexName {
			baseFromSmmRecord := new(models.BaseFromSmmRecord)
			baseFromSmmRecord.BaseFromSmmIndexId = item.BaseFromSmmIndexId
			baseFromSmmRecord.OldIndexName = item.IndexName
			baseFromSmmRecord.NewIndexName = indexName
			ctime := time.Now()
			baseFromSmmRecord.CreateTime = ctime
			baseFromSmmRecord.Timestamp = ctime.Unix()
			e = models.AddBaseFromSmmRecord(baseFromSmmRecord)
			if e != nil {
				err = fmt.Errorf("add smm record err: %s", e.Error())
				return
			}
		}

	}

	// 遍历excel数据,然后跟现有的数据做校验,不存在则入库
	for date, value := range excelDataMap {
		dateTime, e := time.ParseInLocation(utils.FormatDate, date, time.Local)
		if e != nil {
			messages = append(messages, fmt.Sprintf("data time parse err: %s, date: %s", e.Error(), date))
			continue
		}
		if _, ok := exitDataMap[date]; !ok {
			if !strings.Contains(value, "#N/A") {
				var saveDataTime time.Time
				if strings.Contains(date, "00:00:00") {
					saveDataTime, e = time.Parse(utils.FormatDateTime, date)
					if e != nil {
						messages = append(messages, fmt.Sprintf("save data time parse err: %s, date: %s", e.Error(), date))
						continue
					}
				} else {
					saveDataTime, e = time.Parse(utils.FormatDate, date)
					if e != nil {
						messages = append(messages, fmt.Sprintf("save data time parse err: %s, date: %s", e.Error(), date))
						continue
					}
				}
				if saveDataTime.IsZero() {
					messages = append(messages, fmt.Sprintf("save data time empty"))
					continue
				}
				timestamp := saveDataTime.UnixNano() / 1e6

				dataItem := new(models.BaseFromSmmData)
				dataItem.BaseFromSmmIndexId = int(indexId)
				dataItem.IndexCode = indexCode
				dataItem.DataTime = dateTime.Format(utils.FormatDate)
				dataItem.Value = value
				dataItem.CreateTime = time.Now()
				dataItem.ModifyTime = time.Now()
				dataItem.DataTimestamp = timestamp
				addDataList = append(addDataList, dataItem)
			}
		} else {
			// 更新对应日期数据
			if value == "" || strings.Contains(value, "#N/A") {
				continue
			}
			// 若数值相同则忽略更新
			existData := exitDataMap[date]
			if existData != nil && existData.Value == value {
				continue
			}

			dataItem := new(models.BaseFromSmmData)
			dataItem.IndexCode = indexCode
			dataItem.DataTime = dateTime.Format(utils.FormatDate)
			dataItem.Value = value
			dataItem.ModifyTime = time.Now()
			updateDataList = append(updateDataList, dataItem)
		}
	}

	//fmt.Println("addDataList len: ", len(addDataList))
	if len(addDataList) > 0 {
		e = models.AddBaseFromSmmData(addDataList)
		if e != nil {
			err = fmt.Errorf("AddBaseFromSmmData err: %s", e.Error())
			return
		}
	}

	// 已存在的日期数据更新
	//fmt.Println("updateDataList len: ", len(updateDataList))
	if len(updateDataList) > 0 {
		e = models.MultiUpdateBaseFromSmmDataValue(updateDataList)
		if e != nil {
			err = fmt.Errorf("MultiUpdateBaseFromSmmDataValue err: %s", e.Error())
			return
		}
	}

	itemInfo, err := models.GetSmmIndexInfoMaxAndMinInfo(indexCode)
	if err == nil && itemInfo != nil {
		e = models.ModifySmmIndexMaxAndMinInfo(indexCode, itemInfo)
		if e != nil {
			err = fmt.Errorf("ModifySmmIndexMaxAndMinInfo err: %s", e.Error())
			return
		}
	}

	// 同步刷新ETA图库有色的指标
	{
		// 获取指标详情
		edbInfo, e := models.GetEdbInfoByEdbCode(utils.DATA_SOURCE_YS, indexCode)
		if e != nil && e.Error() != utils.ErrNoRow() {
			messages = append(messages, fmt.Sprintf("刷新ETA指标异常, indexCode: %s, err: %s", indexCode, e.Error()))
		}

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

func GetEdbDataFromSmm(edbCode string) (smmBaseDataAll []models.BaseFromSmmDataList, err error) {
	if utils.SmmDataMethod == "api" {
		return GetSmmNewIndexFromBridge(edbCode)
	}

	return getEdbDataFromSmmData(edbCode)
}

func getEdbDataFromSmmData(edbCode string) (smmBaseDataList []models.BaseFromSmmDataList, err error) {
	var condition string
	var pars []interface{}
	if edbCode != "" {
		condition += " AND index_code=? "
		pars = append(pars, edbCode)
	}
	smmBaseDataAll, err := models.GetBaseFromSmmDataByCondition(condition, pars)
	if err != nil && err.Error() != utils.ErrNoRow() {
		return
	}
	var smmBaseDataListItem models.BaseFromSmmDataList
	smmBaseDataListItem.Data = smmBaseDataAll
	smmBaseDataListItem.IndexCode = edbCode
	smmBaseDataList = append(smmBaseDataList, smmBaseDataListItem)
	return
}