package services

import (
	"encoding/json"
	"eta/eta_data_analysis/models"
	"eta/eta_data_analysis/utils"
	"fmt"
	"github.com/tealeg/xlsx"
	"os"
)

func JsmHistory(path string) (err error) {
	defer func() {
		if err != nil {
			fmt.Println("RefreshDataFromCoalCoastal  Err:" + err.Error())
			utils.FileLog.Info(fmt.Sprintf("RefreshDataFromCoalCoastal, Err: %s", err))
		}
	}()
	//path := "/home/code/python/coal_mail/emailFile/沿海八省动力煤终端用户供耗存数据更新.xlsx"
	//path := "/Users/xi/Desktop/瑞茂通-中国煤炭市场网数据/442家晋陕蒙、沿海8省、内陆17省历史数据/442家晋陕蒙历史数据.xlsx"
	//path := "D:\\瑞茂通-中国煤炭市场网数据\\442家晋陕蒙、沿海8省、内陆17省历史数据\\442家晋陕蒙历史数据.xlsx"

	var xlFile *xlsx.File
	exist, err := PathExists(path)
	if err != nil {
		fmt.Println(err)
		return
	}
	if exist {
		xlFile, err = xlsx.OpenFile(path)
		if err != nil {
			fmt.Println("OpenFile err:", err)
			return
		}
	} else {
		fmt.Println("Not Exist")
		return
	}

	//sheetData, err := json.Marshal(xlFile)
	//if err != nil {
	//	fmt.Println("json.Marshal err:", err)
	//	return
	//}

	sheetDatas := make([]models.SheetData, 0)
	data := *xlFile.Sheets[0]
	sheetData := models.SheetData{
		Name:     data.Name,
		MaxRow:   data.MaxRow,
		MaxCol:   data.MaxCol,
		Hidden:   data.Hidden,
		Selected: data.Selected,
	}
	rows := make([]models.Row, 0)
	for _, v := range data.Rows {
		cells := make([]models.Cell, 0)
		for _, cell := range v.Cells {
			cells = append(cells, models.Cell{
				Value: cell.String(),
			})
		}
		row := models.Row{
			Cells: cells,
		}
		rows = append(rows, row)
	}
	sheetData.Rows = rows
	sheetDatas = append(sheetDatas, sheetData)

	params := make(map[string]interface{})
	params["SheetData"] = sheetDatas
	result, e := PostEdbLib(params, utils.LIB_ROUTE_COAL_MINE_JSM_HISTORY)
	if e != nil {
		b, _ := json.Marshal(params)
		fmt.Println(e)
		utils.FileLog.Info(fmt.Sprintf("PostEdbLib err: %s, params: %s", e.Error(), string(b)))
		return
	}
	resp := new(models.BaseEdbLibResponse)
	if e := json.Unmarshal(result, &resp); e != nil {
		utils.FileLog.Info(fmt.Sprintf("json.Unmarshal err: %s", e))
		return
	}
	if resp.Ret != 200 {
		utils.FileLog.Info(fmt.Sprintf("Msg: %s, ErrMsg: %s", resp.Msg, resp.ErrMsg))
		return
	}

	return
}

func CoastalHistory(path string) (err error) {
	defer func() {
		if err != nil {
			fmt.Println("RefreshDataFromCoalCoastal  Err:" + err.Error())
			utils.FileLog.Info(fmt.Sprintf("RefreshDataFromCoalCoastal, Err: %s", err))
		}
	}()
	//path := "/Users/xi/Desktop/瑞茂通-中国煤炭市场网数据/442家晋陕蒙、沿海8省、内陆17省历史数据/CⅢ-8-16 25省市库存和日耗情况（CCTD）.xlsx"
	//path := "D:\\瑞茂通-中国煤炭市场网数据\\442家晋陕蒙、沿海8省、内陆17省历史数据\\CⅢ-8-16 25省市库存和日耗情况（CCTD）.xlsx"

	var xlFile *xlsx.File
	exist, err := PathExists(path)
	if err != nil {
		fmt.Println(err)
		return
	}
	if exist {
		xlFile, err = xlsx.OpenFile(path)
		if err != nil {
			fmt.Println("OpenFile err:", err)
			return
		}
	} else {
		fmt.Println("Not Exist")
		return
	}

	sheetDatas := make([]models.SheetData, 0)
	for i, sheet := range xlFile.Sheets {
		if i < 4 {
			data := sheet
			sheetData := models.SheetData{
				Name:     data.Name,
				MaxRow:   data.MaxRow,
				MaxCol:   data.MaxCol,
				Hidden:   data.Hidden,
				Selected: data.Selected,
			}
			rows := make([]models.Row, 0)
			for _, v := range data.Rows {
				cells := make([]models.Cell, 0)
				for _, cell := range v.Cells {
					cells = append(cells, models.Cell{
						Value: cell.String(),
					})
				}
				row := models.Row{
					Cells: cells,
				}
				rows = append(rows, row)
			}
			sheetData.Rows = rows
			sheetDatas = append(sheetDatas, sheetData)
		}
	}

	params := make(map[string]interface{})
	params["SheetData"] = sheetDatas
	result, e := PostEdbLib(params, utils.LIB_ROUTE_COAL_MINE_COASTAL_HISTORY)
	if e != nil {
		b, _ := json.Marshal(params)
		fmt.Println(e)
		utils.FileLog.Info(fmt.Sprintf("PostEdbLib err: %s, params: %s", e.Error(), string(b)))
		return
	}
	resp := new(models.BaseEdbLibResponse)
	if e := json.Unmarshal(result, &resp); e != nil {
		utils.FileLog.Info(fmt.Sprintf("json.Unmarshal err: %s", e))
		return
	}
	if resp.Ret != 200 {
		utils.FileLog.Info(fmt.Sprintf("Msg: %s, ErrMsg: %s", resp.Msg, resp.ErrMsg))
		return
	}

	return
}

func InlandHistory(path string) (err error) {
	defer func() {
		if err != nil {
			fmt.Println("RefreshDataFromCoalCoastal  Err:" + err.Error())
			utils.FileLog.Info(fmt.Sprintf("RefreshDataFromCoalCoastal, Err: %s", err))
		}
	}()
	//path := "/Users/xi/Desktop/瑞茂通-中国煤炭市场网数据/442家晋陕蒙、沿海8省、内陆17省历史数据/CⅢ-8-16 25省市库存和日耗情况（CCTD）.xlsx"
	//path := "D:\\瑞茂通-中国煤炭市场网数据\\442家晋陕蒙、沿海8省、内陆17省历史数据\\CⅢ-8-16 25省市库存和日耗情况（CCTD）.xlsx"

	var xlFile *xlsx.File
	exist, err := PathExists(path)
	if err != nil {
		fmt.Println(err)
		return
	}
	if exist {
		xlFile, err = xlsx.OpenFile(path)
		if err != nil {
			fmt.Println("OpenFile err:", err)
			return
		}
	} else {
		fmt.Println("Not Exist")
		return
	}

	sheetDatas := make([]models.SheetData, 0)
	for i, sheet := range xlFile.Sheets {
		if i > 3 {
			data := sheet
			sheetData := models.SheetData{
				Name:     data.Name,
				MaxRow:   data.MaxRow,
				MaxCol:   data.MaxCol,
				Hidden:   data.Hidden,
				Selected: data.Selected,
			}
			rows := make([]models.Row, 0)
			for _, v := range data.Rows {
				cells := make([]models.Cell, 0)
				for _, cell := range v.Cells {
					cells = append(cells, models.Cell{
						Value: cell.String(),
					})
				}
				row := models.Row{
					Cells: cells,
				}
				rows = append(rows, row)
			}
			sheetData.Rows = rows
			sheetDatas = append(sheetDatas, sheetData)
		}
	}

	params := make(map[string]interface{})
	params["SheetData"] = sheetDatas
	result, e := PostEdbLib(params, utils.LIB_ROUTE_COAL_MINE_INLAND_HISTORY)
	if e != nil {
		b, _ := json.Marshal(params)
		fmt.Println(e)
		utils.FileLog.Info(fmt.Sprintf("PostEdbLib err: %s, params: %s", e.Error(), string(b)))
		return
	}
	resp := new(models.BaseEdbLibResponse)
	if e := json.Unmarshal(result, &resp); e != nil {
		utils.FileLog.Info(fmt.Sprintf("json.Unmarshal err: %s", e))
		return
	}
	if resp.Ret != 200 {
		utils.FileLog.Info(fmt.Sprintf("Msg: %s, ErrMsg: %s", resp.Msg, resp.ErrMsg))
		return
	}

	return
}

func PathExists(path string) (bool, error) {
	_, err := os.Stat(path)
	if err == nil { //文件或者目录存在
		return true, nil
	}
	if os.IsNotExist(err) {
		return false, nil
	}
	return false, err
}

func Jsm(path string) (err error) {
	defer func() {
		if err != nil {
			fmt.Println("RefreshDataFromCoalCoastal  Err:" + err.Error())
			utils.FileLog.Info(fmt.Sprintf("RefreshDataFromCoalCoastal, Err: %s", err))
		}
	}()
	//path := "/home/code/python/coal_mail/emailFile/沿海八省动力煤终端用户供耗存数据更新.xlsx"
	//path := "/Users/xi/Desktop/瑞茂通-中国煤炭市场网数据/442家晋陕蒙、沿海8省、内陆17省最新数据/442家晋陕蒙煤矿周度产量数据-20231201.xlsx"
	//path := "D:\\瑞茂通-中国煤炭市场网数据\\442家晋陕蒙、沿海8省、内陆17省历史数据\\442家晋陕蒙历史数据.xlsx"

	var xlFile *xlsx.File
	exist, err := PathExists(path)
	if err != nil {
		fmt.Println(err)
		return
	}
	if exist {
		xlFile, err = xlsx.OpenFile(path)
		if err != nil {
			fmt.Println("OpenFile err:", err)
			return
		}
	} else {
		fmt.Println("Not Exist")
		return
	}

	sheetDatas := make([]models.SheetData, 0)
	data := *xlFile.Sheets[0]
	sheetData := models.SheetData{
		Name:     data.Name,
		MaxRow:   data.MaxRow,
		MaxCol:   data.MaxCol,
		Hidden:   data.Hidden,
		Selected: data.Selected,
	}
	rows := make([]models.Row, 0)
	for _, v := range data.Rows {
		cells := make([]models.Cell, 0)
		for _, cell := range v.Cells {
			cells = append(cells, models.Cell{
				Value: cell.String(),
			})
		}
		row := models.Row{
			Cells: cells,
		}
		rows = append(rows, row)
	}
	sheetData.Rows = rows
	sheetDatas = append(sheetDatas, sheetData)

	params := make(map[string]interface{})
	params["SheetData"] = sheetDatas
	result, e := PostEdbLib(params, utils.LIB_ROUTE_COAL_MINE_JSM)
	if e != nil {
		b, _ := json.Marshal(params)
		fmt.Println(e)
		utils.FileLog.Info(fmt.Sprintf("PostEdbLib err: %s, params: %s", e.Error(), string(b)))
		return
	}
	resp := new(models.BaseEdbLibResponse)
	if e := json.Unmarshal(result, &resp); e != nil {
		utils.FileLog.Info(fmt.Sprintf("json.Unmarshal err: %s", e))
		return
	}
	if resp.Ret != 200 {
		utils.FileLog.Info(fmt.Sprintf("Msg: %s, ErrMsg: %s", resp.Msg, resp.ErrMsg))
		return
	}

	return
}

func Coastal(path string) (err error) {
	defer func() {
		if err != nil {
			fmt.Println("RefreshDataFromCoalCoastal  Err:" + err.Error())
			utils.FileLog.Info(fmt.Sprintf("RefreshDataFromCoalCoastal, Err: %s", err))
		}
	}()
	//path := "/Users/xi/Desktop/瑞茂通-中国煤炭市场网数据/442家晋陕蒙、沿海8省、内陆17省最新数据/内陆17省动力煤终端用户供耗存.xlsx"
	//path := "D:\\瑞茂通-中国煤炭市场网数据\\442家晋陕蒙、沿海8省、内陆17省历史数据\\CⅢ-8-16 25省市库存和日耗情况（CCTD）.xlsx"

	var xlFile *xlsx.File
	exist, err := PathExists(path)
	if err != nil {
		fmt.Println(err)
		return
	}
	if exist {
		xlFile, err = xlsx.OpenFile(path)
		if err != nil {
			fmt.Println("OpenFile err:", err)
			return
		}
	} else {
		fmt.Println("Not Exist")
		return
	}

	sheetDatas := make([]models.SheetData, 0)
	for _, sheet := range xlFile.Sheets {
		data := sheet
		sheetData := models.SheetData{
			Name:     data.Name,
			MaxRow:   data.MaxRow,
			MaxCol:   data.MaxCol,
			Hidden:   data.Hidden,
			Selected: data.Selected,
		}
		rows := make([]models.Row, 0)
		for _, v := range data.Rows {
			cells := make([]models.Cell, 0)
			for _, cell := range v.Cells {
				cells = append(cells, models.Cell{
					Value: cell.String(),
				})
			}
			row := models.Row{
				Cells: cells,
			}
			rows = append(rows, row)
		}
		sheetData.Rows = rows
		sheetDatas = append(sheetDatas, sheetData)
	}

	params := make(map[string]interface{})
	params["SheetData"] = sheetDatas
	result, e := PostEdbLib(params, utils.LIB_ROUTE_COAL_MINE_COASTAL)
	if e != nil {
		b, _ := json.Marshal(params)
		fmt.Println(e)
		utils.FileLog.Info(fmt.Sprintf("PostEdbLib err: %s, params: %s", e.Error(), string(b)))
		return
	}
	resp := new(models.BaseEdbLibResponse)
	if e := json.Unmarshal(result, &resp); e != nil {
		utils.FileLog.Info(fmt.Sprintf("json.Unmarshal err: %s", e))
		return
	}
	if resp.Ret != 200 {
		utils.FileLog.Info(fmt.Sprintf("Msg: %s, ErrMsg: %s", resp.Msg, resp.ErrMsg))
		return
	}

	return
}

func Inland(path string) (err error) {
	defer func() {
		if err != nil {
			fmt.Println("RefreshDataFromCoalCoastal  Err:" + err.Error())
			utils.FileLog.Info(fmt.Sprintf("RefreshDataFromCoalCoastal, Err: %s", err))
		}
	}()
	//path := "/Users/xi/Desktop/瑞茂通-中国煤炭市场网数据/442家晋陕蒙、沿海8省、内陆17省最新数据/内陆17省动力煤终端用户供耗存.xlsx"
	//path := "D:\\瑞茂通-中国煤炭市场网数据\\442家晋陕蒙、沿海8省、内陆17省历史数据\\CⅢ-8-16 25省市库存和日耗情况（CCTD）.xlsx"

	var xlFile *xlsx.File
	exist, err := PathExists(path)
	if err != nil {
		fmt.Println(err)
		return
	}
	if exist {
		xlFile, err = xlsx.OpenFile(path)
		if err != nil {
			fmt.Println("OpenFile err:", err)
			return
		}
	} else {
		fmt.Println("Not Exist")
		return
	}

	sheetDatas := make([]models.SheetData, 0)
	for _, sheet := range xlFile.Sheets {
		data := sheet
		sheetData := models.SheetData{
			Name:     data.Name,
			MaxRow:   data.MaxRow,
			MaxCol:   data.MaxCol,
			Hidden:   data.Hidden,
			Selected: data.Selected,
		}
		rows := make([]models.Row, 0)
		for _, v := range data.Rows {
			cells := make([]models.Cell, 0)
			for _, cell := range v.Cells {
				cells = append(cells, models.Cell{
					Value: cell.String(),
				})
			}
			row := models.Row{
				Cells: cells,
			}
			rows = append(rows, row)
		}
		sheetData.Rows = rows
		sheetDatas = append(sheetDatas, sheetData)
	}

	params := make(map[string]interface{})
	params["SheetData"] = sheetDatas
	result, e := PostEdbLib(params, utils.LIB_ROUTE_COAL_MINE_INLAND)
	if e != nil {
		b, _ := json.Marshal(params)
		fmt.Println(e)
		utils.FileLog.Info(fmt.Sprintf("PostEdbLib err: %s, params: %s", e.Error(), string(b)))
		return
	}
	resp := new(models.BaseEdbLibResponse)
	if e := json.Unmarshal(result, &resp); e != nil {
		utils.FileLog.Info(fmt.Sprintf("json.Unmarshal err: %s", e))
		return
	}
	if resp.Ret != 200 {
		utils.FileLog.Info(fmt.Sprintf("Msg: %s, ErrMsg: %s", resp.Msg, resp.ErrMsg))
		return
	}

	return
}
