package implments

import (
	"LAPP_ACURA_MOM_BACKEND/grmi"
	meta "LAPP_ACURA_MOM_BACKEND/meta/base"
	"fmt"
	_ "github.com/denisenkom/go-mssqldb"
	"github.com/go-xorm/xorm"
	"log"
	"testing"
	"time"
)

func TestSnrDAOImplement_CancelOccupy(t *testing.T) {
	dsn := "server=101.201.121.115;database=LAPP_ACURA_TEST;user id=sa;password=Leit2020;port=1433;encrypt=disable"
	engine, err := xorm.NewEngine("mssql", dsn)
	if err != nil {
		log.Fatal(err)
	}
	engine.ShowSQL(true)
	session := engine.NewSession()
	type fields struct {
		session *xorm.Session
		meta    *grmi.Entity
		plantNr int
		userid  string
	}
	f := fields{
		session: session,
		meta:    &meta.Snr,
		plantNr: 100,
		userid:  "system",
	}
	type args struct {
		snrId   string
		objType string
		objId   string
		qty     int
	}
	tests := []struct {
		name    string
		fields  fields
		args    args
		wantErr bool
	}{
		{
			"condition1",
			f,
			args{
				snrId:   "3218657W1A_SerialOrderSnr",
				objType: "CustOrder",
				objId:   "T1",
				qty:     18,
			},
			false,
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			impl := &SnrDAOImplement{
				session: tt.fields.session,
				meta:    tt.fields.meta,
				plantNr: tt.fields.plantNr,
				userid:  tt.fields.userid,
			}
			if err := impl.CancelOccupy(tt.args.snrId, tt.args.objType, tt.args.objId, tt.args.qty); (err != nil) != tt.wantErr {
				t.Errorf("CancelOccupy() error = %v, wantErr %v", err, tt.wantErr)
			}
		})
	}
}

func TestSnrDAOImplement_GetSnrFromOccupy(t *testing.T) {
	dsn := "server=101.201.121.115;database=LAPP_ACURA_TEST;user id=sa;password=Leit2020;port=1433;encrypt=disable"
	engine, err := xorm.NewEngine("mssql", dsn)
	if err != nil {
		log.Fatal(err)
	}
	engine.ShowSQL(true)
	session := engine.NewSession()
	type fields struct {
		session *xorm.Session
		meta    *grmi.Entity
		plantNr int
		userid  string
	}
	f := fields{
		session: session,
		meta:    &meta.Snr,
		plantNr: 100,
		userid:  "system",
	}
	type args struct {
		snrId   string
		objType string
		objId   string
	}
	tests := []struct {
		name       string
		fields     fields
		args       args
		wantSnrStr string
		wantErr    bool
	}{
		{
			"condition1",
			f,
			args{
				snrId:   "3218657W1A_SerialOrderSnr",
				objType: "CustOrder",
				objId:   "T1",
			},
			"N6N00",
			false,
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			impl := &SnrDAOImplement{
				session: tt.fields.session,
				meta:    tt.fields.meta,
				plantNr: tt.fields.plantNr,
				userid:  tt.fields.userid,
			}
			gotSnrStr, err := impl.GetSnrFromOccupy(tt.args.snrId, tt.args.objType, tt.args.objId)
			fmt.Println("gotSnrStr:", gotSnrStr)
			if (err != nil) != tt.wantErr {
				t.Errorf("GetSnrFromOccupy() error = %v, wantErr %v", err, tt.wantErr)
				return
			}
			if gotSnrStr != tt.wantSnrStr {
				t.Errorf("GetSnrFromOccupy() gotSnrStr = %v, want %v", gotSnrStr, tt.wantSnrStr)
			}
		})
	}
}

func TestSnrDAOImplement_OccupySnrWithTime(t *testing.T) {
	dsn := "server=101.201.121.115;database=LAPP_ACURA_TEST;user id=sa;password=Leit2020;port=1433;encrypt=disable"
	engine, err := xorm.NewEngine("mssql", dsn)
	if err != nil {
		log.Fatal(err)
	}
	engine.ShowSQL(true)
	session := engine.NewSession()
	type fields struct {
		session *xorm.Session
		meta    *grmi.Entity
		plantNr int
		userid  string
	}
	f := fields{
		session: session,
		meta:    &meta.Snr,
		plantNr: 100,
		userid:  "system",
	}
	type args struct {
		snr     string
		t       time.Time
		objType string
		objId   string
		qty     int
	}
	tests := []struct {
		name    string
		fields  fields
		args    args
		wantErr bool
	}{
		{
			"condition1",
			f,
			args{
				snr:     "3218657W1A_SerialOrderSnr",
				t:       time.Now(),
				objType: "CustOrder",
				objId:   "T1",
				qty:     20,
			},
			false,
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			impl := &SnrDAOImplement{
				session: tt.fields.session,
				meta:    tt.fields.meta,
				plantNr: tt.fields.plantNr,
				userid:  tt.fields.userid,
			}
			if err := impl.OccupySnrWithTime(tt.args.snr, tt.args.t, tt.args.objType, tt.args.objId, tt.args.qty); (err != nil) != tt.wantErr {
				t.Errorf("OccupySnrWithTime() error = %v, wantErr %v", err, tt.wantErr)
			}
		})
	}
}
