package implments

import (
	"LAPP_ACURA_MOM_BACKEND/conf"
	dal "LAPP_ACURA_MOM_BACKEND/dao/pln"
	"LAPP_ACURA_MOM_BACKEND/db"
	"LAPP_ACURA_MOM_BACKEND/global"
	"LAPP_ACURA_MOM_BACKEND/infra/logger"
	model "LAPP_ACURA_MOM_BACKEND/models/pln"
	"sort"
	"time"

	"LAPP_ACURA_MOM_BACKEND/utils"
	"fmt"
	"log"
	"testing"
)

func TestCustOrderServiceImplement_ReleaseCustOrder(t *testing.T) {
	var err error
	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 {
		log.Fatal("数据库加载失败", err)
	}
	err = logger.InitConfig()
	if err != nil {
		log.Fatal("日志加载失败", err)
	}
	user := &global.User{
		UserId: "test",
		PlantNr: 100,
	}
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	custOrderDao := dal.NewCustOrderDAO(session, user.PlantNr, user.UserId)
	custOrder, err := custOrderDao.SelectOne("CO-00000592")
	if err != nil {
		log.Fatal("获取custOrder失败, ", err)
	}
	//custSrv := NewCustOrderServiceImplement()
	//err = custSrv.ReleaseCustOrder(user, *custOrder)
	//fmt.Println("err:", err)
	custOrderLi := make([]model.CustOrder, 0, 3)
	custOrderLi = append(custOrderLi, *custOrder)
	custOrder, err = custOrderDao.SelectOne("CO-00000590")
	if err != nil {
		log.Fatal("获取custOrder失败, ", err)
	}
	custOrderLi = append(custOrderLi, *custOrder)
	custOrder, err = custOrderDao.SelectOne("CO-00000589")
	if err != nil {
		log.Fatal("获取custOrder失败, ", err)
	}
	custOrderLi = append(custOrderLi, *custOrder)
	sort.Slice(custOrderLi, func(i, j int) bool {
		itemI := custOrderLi[i]
		itemJ := custOrderLi[j]
		return itemJ.PlanStartTime.Restore().After(itemI.PlanStartTime.Restore())
	})
	fmt.Println(custOrderLi)
}
