package main

import (
	"LAPP_ACURA_MOM_BACKEND/conf"
	"LAPP_ACURA_MOM_BACKEND/db"
	services "LAPP_ACURA_MOM_BACKEND/services/pln"
	"LAPP_ACURA_MOM_BACKEND/utils"
	"LAPP_ACURA_MOM_BACKEND/web/middleware/glog"
	"flag"
	"fmt"
	"github.com/robfig/cron"
	"log"
	"path/filepath"
	"time"
)

func main() {
	var err error
	//初始化日志开始
	baseDir := ""
	flag.StringVar(&baseDir, "dir", "log", "running in a directory")
	flag.StringVar(&utils.SysPath, "p", "", "root Path")
	flag.Parse()

	defer glog.Flush()
	fmt.Println(baseDir)
	baseDir, err = utils.GetCurrentPath(baseDir)
	if err != nil {
		fmt.Printf("failed to get baseDir: %v\n", err)
		return
	}
	fmt.Println(baseDir)
	pathLogDir := filepath.Join(baseDir, "glog")
	fmt.Println(pathLogDir)
	glog.DefaultInit(true, pathLogDir)

	glog.Infoln("启动日志", "InitDB return success")
	utils.TimezoneLocation, err = time.LoadLocation("Asia/Shanghai")
	if err != nil {
		fmt.Printf("failed to get time location: %v\n", err)
		return
	}
	//加载配置
	path, err := utils.GetCurrentPath("conf/config.yaml")
	if err != nil {
		fmt.Printf("failed to read yaml path: %v", err)
		return
	}

	err = conf.ReadYamlConfig(path)
	if err != nil {
		fmt.Printf("failed to read yaml config due to: %v", err)
		return
	}

	//初始化数据库
	err = db.InitDb()
	if err != nil {
		glog.InfoExtln("数据库加载失败", err)
	}
	c := cron.New()
	svr := services.NewToyotaDeliveryOrderService()
	err = c.AddFunc("*/3 * * * * ?", svr.WriteDeliveryDataToPLCTask)
	if err != nil {
		log.Fatal("启动定时任务失败, 错误：" + err.Error())
	}
	c.Run()
	select {

	}

}
