package schedule

import (
	"LAPP_LF_MOM_BACKEND/conf"
	"LAPP_LF_MOM_BACKEND/db"
	"LAPP_LF_MOM_BACKEND/infra/logger"
	"github.com/stretchr/testify/assert"
	"testing"
)

func TestSchedulerSrv_ReleaseSchedule(t *testing.T) {
	//加载配置
	err := conf.ReadYamlConfig("..\\..\\conf\\config.yaml")
	if err != nil {
		t.Fatal("init config from file failed, error:", err.Error())
	}
	//初始化数据库连接
	err = db.InitTestDb()
	if err != nil {
		t.Fatal("init db failed, error:", err)
	}
	err = logger.InitConfig()
	if err != nil {
		t.Fatal("init logger config failed, error:", err.Error())
	}
	engine := db.Eloquent.Master()
	engine.ShowSQL(true)
	// 初始化数据表
	//_, err = engine.ImportFile("./script/ME_Project.sql")
	//if err != nil {
	//	t.Error("init data table ME_Project failed, error:", err)
	//}
	//_, err = engine.ImportFile("./script/WorkLine.sql")
	//if err != nil {
	//	t.Fatal("init data table WorkLine failed, error:", err)
	//}
	//_, err = engine.ImportFile("./script/OM_SerialOrder.sql")
	//if err != nil {
	//	t.Fatal("init data table OM_SerialOrder failed, error:", err)
	//}
	//_, err = engine.ImportFile("./script/OM_SerialOrderStatus.sql")
	//if err != nil {
	//	t.Fatal("init data table OM_SerialOrderStatus failed, error:", err)
	//}
	//_, err = engine.ImportFile("./script/OM_SerialOrderStatusRecLst.sql")
	//if err != nil {
	//	t.Fatal("init data table OM_SerialOrderStatusRecLst failed, error:", err)
	//}
	//_, err = engine.ImportFile("./script/OM_WorkOrder.sql")
	//if err != nil {
	//	t.Fatal("init data table OM_WorkOrder failed, error:", err)
	//}
	//_, err = engine.ImportFile("./script/OM_WorkOrderStatus.sql")
	//if err != nil {
	//	t.Fatal("init data table OM_WorkOrderStatus failed, error:", err)
	//}
	//_, err = engine.ImportFile("./script/OM_WorkOrderQty.sql")
	//if err != nil {
	//	t.Fatal("init data table OM_WorkOrderQty failed, error:", err)
	//}
	//_, err = engine.ImportFile("./script/OM_WorkOrderStatusRecLst.sql")
	//if err != nil {
	//	t.Fatal("init data table OM_WorkOrderStatusRecLst.sql failed, error:", err)
	//}
	ass := assert.New(t)
	// 指定工厂id
	var plantNr = 100
	// 指定产线类型
	var workLineType = "1"
	// 指定产线id
	var workLineId = "RB"
	schedulerSrv := SchedulerSrv{
		PlantNr: plantNr,
	}
	// 加载项目主数据
	err = schedulerSrv.LoadProjectData()
	if err != nil {
		t.Fatal("load project data failed, error:", err.Error())
	}
	// 项目主数据校验
	ass.NotEqual(schedulerSrv.ProjDict, nil)
	ass.Greater(len(schedulerSrv.ProjDict), 0)
	// 加载产线主数据
	err = schedulerSrv.LoadWorklineData(workLineType)
	if err != nil {
		t.Fatal("load work line data, error:", err.Error())
	}
	// 产线主数据校验
	ass.NotEqual(schedulerSrv.WorklineDict, nil)
	ass.Greater(len(schedulerSrv.WorklineDict), 0)
	// 生产派工
	err = schedulerSrv.ReleaseSchedule(workLineId)
	if err != nil {
		t.Fatal("release schedule failed, error:", err.Error())
	}
	t.Log("release ok")
}