package business

import (
	"fmt"
	"github.com/360EntSecGroup-Skylar/excelize"
	"go.mongodb.org/mongo-driver/bson"
	"strconv"
	"wss-pool/config"
	"wss-pool/internal/data"
	"wss-pool/logging/applogger"
	"wss-pool/pkg/model/stock"
)

// TickerToExcel
//
//	@Description: 导出excel
func TickerToExcel() {
	data.Mgo_init(config.Config.Mongodb)
	filter := bson.M{"ticker": bson.M{"$regex": "USD$"}}
	res := make([]stock.ForexData, 0)
	data.MgoFindRes(data.ForexList, filter, &res)
	applogger.Debug("TickerToExcel to info:", len(res))
	// 创建Excel文件
	file := excelize.NewFile()
	sheetName := "Sheet1"
	// 写入表头
	file.SetCellValue(sheetName, "A1", "code")
	file.SetCellValue(sheetName, "B1", "Name")
	file.SetCellValue(sheetName, "C1", "Country")
	file.SetCellValue(sheetName, "D1", "PrimaryExchange")
	file.SetCellValue(sheetName, "E1", "Symbol")
	file.SetCellValue(sheetName, "F1", "NumericCode")
	// 写入数据
	row := 2 // 从第二行开始写入数据
	for _, val := range res {
		file.SetCellValue(sheetName, "A"+strconv.Itoa(row), val.Ticker)
		file.SetCellValue(sheetName, "B"+strconv.Itoa(row), "")
		file.SetCellValue(sheetName, "C"+strconv.Itoa(row), "")
		file.SetCellValue(sheetName, "D"+strconv.Itoa(row), "")
		file.SetCellValue(sheetName, "E"+strconv.Itoa(row), "")
		file.SetCellValue(sheetName, "F"+strconv.Itoa(row), "")
		row++
	}
	err := file.SaveAs(fmt.Sprintf("./cmd/%s.xlsx", "forex"))
	if err != nil {
		applogger.Error("TickerToExecl to info err:", err)
	}
}