package services

import (
	"encoding/json"
	"errors"
	"eta/eta_task/models/data_manage"
	"eta/eta_task/utils"
	"fmt"
	"time"
)

// ICPI消费指数指标数据同步
func SyncBaseFromIcpi() (err error) {
	var startDate string
	maxDate, err := data_manage.GetBaseFromIcpiIndexMaxDate()
	if err != nil || maxDate.IsZero() {
		startDate = time.Now().AddDate(0, 0, -1).Format(utils.FormatDate) + " 00:00:00"
	} else {
		startDate = maxDate.AddDate(0, 0, 1).Format(utils.FormatDate) + " 00:00:00"
	}

	method := `index/list`
	data := make(map[string]interface{})
	data["Source"] = utils.DATA_SOURCE_ICPI
	data["StartDate"] = startDate
	//data["EndDate"] = endDate
	result, err := HttpPost("SyncRankingFromIcpi", method, data)
	utils.FileLog.Info(result)
	fmt.Println(result)

	respObj := new(data_manage.IcpiIndexResp)
	err = json.Unmarshal([]byte(result), &respObj)
	if err != nil {
		utils.FileLog.Info("err:", err.Error())
		fmt.Println("err:", err.Error())
		return err
	}
	//获取所有指标信息  某一天的
	/*allIndex, err := data_manage.GetBaseFromIcpiIndexAll(startDate)
	if err != nil {
		return
	}

	existIndexMap := make(map[int]*data_manage.BaseFromIcpiIndex)
	for _, v := range allIndex {
		existIndexMap[v.BaseFromIcpiIndexId] = v
	}*/

	icpiObj := new(data_manage.BaseFromIcpiIndex)

	for _, zv := range respObj.Data {
		newID, err := icpiObj.InsertOrUpdateBaseFromIcpiIndex(zv)
		if err != nil {
			fmt.Println("InsertOrUpdateBaseFromIcpiIndex error:", err)
		}
		fmt.Println("InsertOrUpdateBaseFromIcpiIndex new indexID:", newID)
	}
	return err
}

// ICPI消费指数-分类
func SyncBaseFromIcpiClassify() (err error) {
	data := make(map[string]interface{})
	data["Source"] = utils.DATA_SOURCE_ICPI
	method := `classify/list`
	result, err := HttpPost("SyncBaseFromIcpiClassify", method, data)
	if err != nil {
		fmt.Println("SyncBaseFromIcpiClassify HttpPost Err:", err.Error())
	}
	utils.FileLog.Info(result)
	fmt.Println("SyncBaseFromIcpiClassify result:", result)

	respObj := new(data_manage.IcpiClassifyResp)
	err = json.Unmarshal([]byte(result), &respObj)
	if err != nil {
		return err
	}
	//获取所有分类
	/*	allClassify, err := data_manage.GetBaseFromIcpiClassifyAll()
		if err != nil {
			return
		}

		existIndexMap := make(map[int]*data_manage.BaseFromIcpiClassify)
		for _, v := range allClassify {
			existIndexMap[v.BaseFromIcpiClassifyId] = v
		}*/

	for _, item := range respObj.Data {
		//if _, ok := existIndexMap[item.BaseFromIcpiClassifyId]; !ok {
		newID, err := data_manage.InsertOrUpdateBaseFromIcpiClassify(item)
		if err != nil {
			fmt.Println("InsertOrUpdateBaseFromIcpiClassify error:", err)
		}
		fmt.Println("InsertOrUpdateBaseFromIcpiClassify new indexID:", newID)
		//}
	}
	return err
}

// ICPI消费指数-数据
func SyncBaseFromIcpiData() (err error) {
	startDate := time.Now().Format(utils.FormatDate) + " 00:00:00"
	//var startDate string
	maxDate, err := data_manage.GetBaseFromIcpiMaxDate()
	if err != nil || maxDate.IsZero() {
		startDate = time.Now().AddDate(0, 0, -1).Format(utils.FormatDate) + " 00:00:00"
	} else {
		startDate = maxDate.AddDate(0, 0, 1).Format(utils.FormatDate) + " 00:00:00"
	}

	method := `index/data/list_page`

	existDataMap := make(map[string]*data_manage.BaseFromIcpiData)
	allData, err1 := data_manage.GetAllBaseFromIcpiDataList(startDate)
	if err1 != nil {
		fmt.Println("get GetAllBaseFromIcpiDataList err:" + err1.Error())
		return
	}
	for _, dv := range allData {
		tmpKey := dv.IndexCode + "_" + dv.DataTime
		existDataMap[tmpKey] = dv
	}

	//获取所有指标信息  某一天的
	maxPage := 1

	for currPage := 0; currPage < maxPage; currPage++ {
		data := make(map[string]interface{})
		data["Source"] = utils.DATA_SOURCE_ICPI
		data["StartDate"] = startDate
		data["CurrPage"] = currPage
		data["PageSize"] = 500 //

		var result string
		result, err = HttpPost("SyncBaseFromIcpiData", method, data)
		utils.FileLog.Info(result)
		fmt.Println(result)

		respObj := new(data_manage.BaseFromIcpiDataResp)
		err = json.Unmarshal([]byte(result), &respObj)
		if err != nil {
			fmt.Println("json.Unmarshal err:" + err.Error())
			return err
		}
		if respObj.Ret != 200 {
			err = errors.New(respObj.ErrMsg)
			return
		}
		// 总页码数
		maxPage = respObj.Data.Paging.Pages

		addDataList := make([]*data_manage.BaseFromIcpiData, 0)
		if respObj.Data.List != nil && len(respObj.Data.List) > 0 {
			for _, dv := range respObj.Data.List {
				tmpKey := dv.IndexCode + "_" + dv.DataTime
				if _, ok := existDataMap[tmpKey]; !ok {
					addDataList = append(addDataList, dv)
					existDataMap[tmpKey] = dv
				}
			}
		}

		// 最后如果还有数据未插入,那么继续插入吧
		if len(addDataList) > 0 {
			_, err = data_manage.MultiAddBaseFromIcpiDataIndex(addDataList)
			if err != nil {
				fmt.Println("MultiAddBaseFromIcpiDataIndex error:", err)
			}
		}
	}

	return err
}