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 HandleMysteelIndex(req *models.HandleMysteelIndexResp) (err error) {
	for _, v := range req.List {
		if v.IndexName == "" || v.IndexCode == "" {
			continue
		}
		err = handleIndex(v)
		if err != nil {
			return
		}
	}

	_ = SetMysteelChemicalEdbInfoUpdateStat(false)
	_ = SetEdbSourceStat(false)

	return
}

func handleIndex(indexItem *models.HandleMysteelIndex) (err error) {
	defer func() {
		if err != nil {
			// 添加刷新失败日志
			dataUpdateResult := 2
			dataUpdateFailedReason := "服务异常"
			edbInfo, e := models.GetEdbInfoByEdbCode(utils.DATA_SOURCE_MYSTEEL_CHEMICAL, indexItem.IndexCode)
			if e == nil {
				//查询指标存在，才添加刷新日志
				_ = AddEdbInfoUpdateLog(edbInfo.EdbInfoId, 2, err.Error(), dataUpdateResult, dataUpdateFailedReason, 1, 0)
			}
		}
	}()

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

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

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

	//判断指标是否存在
	var isAdd int
	item, err := indexObj.GetIndexItem(indexItem.IndexCode)
	if err != nil {
		if err.Error() == utils.ErrNoRow() {
			isAdd = 1
		} else {
			isAdd = -1
			return
		}
	}
	nameChange := false
	if item != nil && item.BaseFromMysteelChemicalIndexId > 0 {
		isAdd = 2
		if item.IndexName != indexItem.IndexName {
			nameChange = true
		}
	} else {
		isAdd = 1
	}

	fmt.Println("isAdd:", isAdd)
	if !strings.Contains(indexItem.Frequency, "度") {
		indexItem.Frequency = indexItem.Frequency + "度"
	}

	if isAdd == 1 {
		indexObj.IndexCode = indexItem.IndexCode
		indexObj.IndexName = indexItem.IndexName
		indexObj.Unit = indexItem.Unit
		indexObj.Source = indexItem.Source
		indexObj.Describe = indexItem.Describe
		indexObj.StartDate, err = utils.DealExcelDate(indexItem.StartDate)
		if err != nil {
			fmt.Println("utils.DealExcelDate err:" + indexItem.StartDate + err.Error())
			return
		}
		indexObj.EndDate, err = utils.DealExcelDate(indexItem.EndDate)
		if err != nil {
			fmt.Println("utils.DealExcelDate err:" + indexItem.EndDate + err.Error())
			return
		}
		indexObj.Frequency = indexItem.Frequency
		//indexObj.CreateTime = time.Now().Local()
		//indexObj.ModifyTime = time.Now().Local()
		err = indexObj.Add()
		if err != nil {
			fmt.Println("add err:" + err.Error())
			return
		}
		indexId = indexObj.BaseFromMysteelChemicalIndexId
	} else if isAdd == 2 {
		indexObj.BaseFromMysteelChemicalIndexId = item.BaseFromMysteelChemicalIndexId
		indexObj.IndexCode = indexItem.IndexCode
		indexObj.IndexName = indexItem.IndexName
		indexObj.Unit = indexItem.Unit
		indexObj.Source = indexItem.Source
		indexObj.Describe = indexItem.Describe
		indexObj.StartDate, err = utils.DealExcelDate(indexItem.StartDate)
		if err != nil {
			fmt.Println("utils.DealExcelDate err:" + indexItem.StartDate + err.Error())
			return
		}
		indexObj.EndDate, err = utils.DealExcelDate(indexItem.EndDate)
		if err != nil {
			fmt.Println("utils.DealExcelDate err:" + indexItem.EndDate + err.Error())
			return
		}
		indexObj.Frequency = indexItem.Frequency
		indexObj.ModifyTime = time.Now()
		indexId = item.BaseFromMysteelChemicalIndexId

		var isStop int
		if strings.Contains(indexItem.IndexName, "停") {
			isStop = 1
		}
		indexObj.IsStop = isStop

		//修改数据
		updateColsArr := make([]string, 0)
		updateColsArr = append(updateColsArr, "index_name")
		updateColsArr = append(updateColsArr, "unit")
		updateColsArr = append(updateColsArr, "source")
		updateColsArr = append(updateColsArr, "frequency")
		updateColsArr = append(updateColsArr, "start_date")
		updateColsArr = append(updateColsArr, "end_date")
		updateColsArr = append(updateColsArr, "describe")
		updateColsArr = append(updateColsArr, "end_date")
		updateColsArr = append(updateColsArr, "is_stop")
		updateColsArr = append(updateColsArr, "modify_time")

		e := indexObj.Update(updateColsArr)
		if e != nil {
			fmt.Println("Index Update Err:" + e.Error())
			return
		}

		dataObj := new(models.BaseFromMysteelChemicalData)

		//获取已存在的所有数据
		exitDataList, err := dataObj.GetIndexDataList(indexItem.IndexCode)
		if err != nil {
			fmt.Println("GetIndexDataList Err:" + err.Error())
			return err
		}
		fmt.Println("exitDataListLen:", len(exitDataList))
		for _, v := range exitDataList {
			dateStr := v.DataTime.Format(utils.FormatDate)
			exitDataMap[dateStr] = v
		}
	}

	dataObj := new(models.BaseFromMysteelChemicalData)
	var hasUpdate bool
	// 遍历excel数据，然后跟现有的数据做校验，不存在则入库
	for date, value := range indexItem.ExcelDataMap {
		dateTime, err := utils.DealExcelDate(date)
		if err != nil {
			fmt.Println("time.ParseInLocation Err:" + err.Error())
			return err
		}
		date = dateTime.Format(utils.FormatDate)
		if findData, ok := exitDataMap[date]; !ok {
			if !strings.Contains(value, "#N/A") {
				dataItem := new(models.BaseFromMysteelChemicalData)
				dataItem.BaseFromMysteelChemicalIndexId = indexId
				dataItem.IndexCode = indexItem.IndexCode
				dataItem.DataTime = dateTime
				dataItem.Value = value
				dataItem.UpdateDate = indexItem.UpdateDate
				dataItem.CreateTime = time.Now()
				dataItem.ModifyTime = time.Now()
				addDataList = append(addDataList, *dataItem)
			}
		} else {
			if findData != nil && findData.Value != value && !strings.Contains(value, "#N/A") { //修改数据
				dataObj.BaseFromMysteelChemicalDataId = findData.BaseFromMysteelChemicalDataId
				dataObj.Value = value
				dataObj.ModifyTime = time.Now()

				updateDataColsArr := make([]string, 0)
				updateDataColsArr = append(updateDataColsArr, "value")
				updateDataColsArr = append(updateDataColsArr, "modify_time")
				dataObj.Update(updateDataColsArr)
				hasUpdate = true
			}
		}
	}

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

	//修改最大最小日期
	mysteelIndexMaxItem, err := dataObj.GetMysteelIndexInfoMaxAndMinInfo(indexItem.IndexCode)
	if err == nil && mysteelIndexMaxItem != nil {
		e := dataObj.ModifyMysteelIndexMaxAndMinInfo(indexItem.IndexCode, mysteelIndexMaxItem)
		if e != nil {
			fmt.Println("ModifyMysteelIndexMaxAndMinInfo Err:" + e.Error())
		}
	}
	// 同步刷新图库钢联的指标
	//go func() {
	var indexErr error
	var lErr error
	defer func() {
		if indexErr != nil {
			tips := fmt.Sprintf("钢联数据刷新-ETA指标刷新异常, 指标编码: %s, err: %s", indexObj.IndexCode, indexErr.Error())
			alarm_msg.SendAlarmMsg(tips, 3)
		}

		if lErr != nil {
			tips := fmt.Sprintf("钢联数据刷新-ETA指标刷新统计异常, 指标编码: %s, err: %s", indexObj.IndexCode, lErr.Error())
			alarm_msg.SendAlarmMsg(tips, 3)
		}
	}()

	edbInfo, e := models.GetEdbInfoByEdbCode(utils.DATA_SOURCE_MYSTEEL_CHEMICAL, indexObj.IndexCode)
	if e != nil && e.Error() != utils.ErrNoRow() {
		indexErr = e
		return
	}

	if edbInfo != nil {
		dataUpdateResult := 2
		dataUpdateFailedReason := "服务异常"
		_, logErrMsg, logErr := logic.RefreshBaseEdbInfo(edbInfo, ``)
		if logErr != nil {
			lErr = AddEdbInfoUpdateLog(edbInfo.EdbInfoId, 2, logErrMsg+logErr.Error(), dataUpdateResult, dataUpdateFailedReason, 1, 0)
			return
		}

		if hasUpdate {
			dataUpdateResult = 1
			dataUpdateFailedReason = ""
		} else {
			dataUpdateFailedReason = "未刷新到数据"
		}

		// 添加刷新成功日志
		lErr = AddEdbInfoUpdateLog(edbInfo.EdbInfoId, 1, "", dataUpdateResult, dataUpdateFailedReason, 1, 0)
		if lErr != nil {
			return
		}
		//如果变更了指标名称，则添加指标信息变更日志
		if nameChange {
			edbInfo.SourceIndexName = indexItem.IndexName
			lErr = edbInfo.Update([]string{"SourceIndexName"})
			if lErr != nil {
				return
			}
			lErr = AddEdbInfoUpdateLog(edbInfo.EdbInfoId, 1, "", 0, "", 0, 1)
		}
	}
	//}()

	return
}
