package schedule

import (
	"LAPP_LF_MOM_BACKEND/conf"
	dal "LAPP_LF_MOM_BACKEND/dao/base"
	"LAPP_LF_MOM_BACKEND/db"
	model "LAPP_LF_MOM_BACKEND/models/base"
	"LAPP_LF_MOM_BACKEND/utils"
	"LAPP_LF_MOM_BACKEND/web/middleware/glog"
	"errors"
	"flag"
	"fmt"
	"path/filepath"
	"testing"
)

var result = []string{""}

func TestInit(t *testing.T) {

	var (
		dmsrv    DayModelSrv
		dmtablst []model.DayModel
		err      error
	)

	//初始化日志开始
	baseDir := ""
	flag.StringVar(&baseDir, "dir", "D:\\GoWrok\\src\\leit.com\\LAPP_LF_MOM_BACKEND\\log", "running in a directory")
	flag.Parse()

	defer glog.Flush()
	fmt.Println(baseDir)
	baseDir = utils.EnsureDir(baseDir)
	fmt.Println(baseDir)
	pathLogDir := filepath.Join(baseDir, "glog")
	fmt.Println(pathLogDir)
	glog.DefaultInit(true, pathLogDir)

	glog.Infoln("启动日志", "InitDB return success")
	// 建立数据库连接
	//加载配置
	err = conf.ReadYamlConfig("D:\\GoWrok\\src\\leit.com\\LAPP_LF_MOM_BACKEND\\conf\\config.yaml")
	if err != nil {
		fmt.Printf("failed to read yaml config due to: %v", err)
		return
	}
	//初始化数据库
	err = db.InitDb()
	if err != nil {
		t.Error("数据库加载失败!")
	}
	fmt.Println("数据库加载成功")

	tmsrv := TimeModelSrv{PlantNr: model.PlantNr}
	// 初始化
	tmsrv.DayModelDict 	= make(map[int]DayModelSrv)
	// 加载日模型
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewDayModelDAO(session, model.PlantNr, "scheduler")
	if dmtablst, err = dao.SelectAll(); err != nil {
		err = errors.New(fmt.Sprintf("加载工厂%d的日模型失败%v!", model.PlantNr, err))
		return
	}
	for i := 0; i < len(dmtablst); i++ {
		dmsrv = DayModelSrv{}
		dmsrv.Init(dmtablst[i])
		tmsrv.DayModelDict[dmsrv.DayModelNr] = dmsrv
	}
	t.Logf("dmsrv is :%v",dmsrv)
}
