package services

import (
	"fmt"
	"hongze/hongze_edb_lib/models"
	"hongze/hongze_edb_lib/utils"
	"strings"
	"time"
)

func HandleMysteelIndex(req *models.HandleMysteelIndexResp) {
	for _, v := range req.List {
		if v.IndexName == "" || v.IndexCode == "" {
			continue
		}
		handleIndex(v)
	}
}

func handleIndex(indexItem *models.HandleMysteelIndex) {
	var err error

	//return
	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() == err.Error() {
			isAdd = 1
		} else {
			isAdd = -1
			return
		}
	}
	if item != nil && item.BaseFromMysteelChemicalIndexId > 0 {
		isAdd = 2
	} 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, _ = time.ParseInLocation(utils.FormatDate, indexItem.StartDate, time.Local)
		indexObj.EndDate, _ = time.ParseInLocation(utils.FormatDate, indexItem.EndDate, time.Local)
		indexObj.Frequency = indexItem.Frequency
		err = indexObj.Add()
		if err != nil {
			fmt.Println("add err:" + err.Error())
			return
		}
		indexId = indexObj.BaseFromMysteelChemicalIndexId
	} else if isAdd == 2 {
		indexObj.IndexCode = indexItem.IndexCode
		indexObj.IndexName = indexItem.IndexName
		indexObj.Unit = indexItem.Unit
		indexObj.Source = indexItem.Source
		indexObj.Describe = indexItem.Describe
		indexObj.StartDate, _ = time.ParseInLocation(utils.FormatDate, indexItem.StartDate, time.Local)
		indexObj.EndDate, _ = time.ParseInLocation(utils.FormatDate, indexItem.EndDate, time.Local)
		indexObj.Frequency = indexItem.Frequency
		indexObj.ModifyTime = time.Now()
		indexId = item.BaseFromMysteelChemicalIndexId
		//修改数据
		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, "modify_time")

		indexObj.Update(updateColsArr)

		dataObj := new(models.BaseFromMysteelChemicalData)

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

	dataObj := new(models.BaseFromMysteelChemicalData)
	// 遍历excel数据，然后跟现有的数据做校验，不存在则入库
	for date, value := range indexItem.ExcelDataMap {
		if findData, ok := exitDataMap[date]; !ok {
			dateTime, 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") {
				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)
			}
		}
	}

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