package main

import (
	"flag"
	"mongodb"
	"os"
	qu "qfw/util"
	sp "spiderutil"
	"time"
	. "util"
)

func init() {
	//config
	qu.ReadConfig("config.json", &Config)
	//mgo
	//Mgo = &mongodb.MongodbSim{
	//	MongodbAddr: qu.ObjToString(config["mongodb"]),
	//	DbName:      qu.ObjToString(config["db"]),
	//	Size:        qu.IntAll(config["size"]),
	//}
	//Mgo.InitPool()
	bidding := Config["bidding"].(map[string]interface{})
	MgoB = &mongodb.MongodbSim{
		MongodbAddr: qu.ObjToString(bidding["mongodb"]),
		DbName:      qu.ObjToString(bidding["db"]),
		Size:        qu.IntAll(bidding["size"]),
		UserName:    qu.ObjToString(bidding["username"]),
		Password:    qu.ObjToString(bidding["password"]),
	}
	MgoB.InitPool()
	//其它参数
	Coll = qu.ObjToString(Config["coll"])
	mail := Config["mail"].(map[string]interface{})
	Tomail = qu.ObjToString(mail["to"])
	Api = qu.ObjToString(mail["api"])
	//udp
	UdpPort = qu.ObjToString(Config["udpport"])
	nextUdp := Config["nextudp"].(map[string]interface{})
	NextAddr = qu.ObjToString(nextUdp["addr"])
	NextPort = qu.IntAll(nextUdp["port"])
	NextStype = qu.ObjToString(nextUdp["stype"])
	InitUdp() //初始化udp
	//初始化oss
	oss := Config["oss"].(map[string]interface{})
	sp.OssInit(
		qu.ObjToString(oss["ossEndpoint"]),
		qu.ObjToString(oss["ossAccessKeyId"]),
		qu.ObjToString(oss["ossAccessKeySecret"]),
		qu.ObjToString(oss["ossBucketName"]),
	)
	//初始化grpc
	OcrServerAddr = qu.ObjToString(Config["ocrserveraddr"])
	InitFileTextGrpcClient()
	go checkMapJob()
}

func main() {
	flag.StringVar(&StartID, "sid", "", "起始id")
	flag.Parse()
	if StartID == "" {
		qu.Debug("起始ID为空")
		os.Exit(-1)
	}
	qu.Debug("起始ID：", StartID)
	go func() {
		for {
			ok := DownloadFile()
			if !ok {
				time.Sleep(1 * time.Minute)
			}
		}
	}()
	//go http.ListenAndServe(UdpPort, nil)
	ch := make(chan bool, 1)
	<-ch
}
