package wm

import (
	"LAPP_LF_MOM_BACKEND/grmi"
	"encoding/json"
	"fmt"
	"time"
)

var WM_AUTH_CHECK = WM_AUTH_CHECK_ON  // 权限开关
const (
	// work area 关联类型
	WORK_AREA_TYPE_WARE_HOUSE   = "WARE_HOUSE"
	WORK_AREA_TYPE_STORAGE_AREA = "STORAGE_AREA"
	WORK_AREA_TYPE_STORAGE_BIN  = "STORAGE_BIN"
	WORK_AREA_TYPE_STORAGE_RACK  = "STORAGE_RACK"
	// 盘点单status
	CHECK_STATUS_ON         = 0
	CHECK_STATUS_Accomplish = 1
	// 库位状态
	BIN_STAUTS_CHECK = "CK" // 盘点中
	BIN_STATUS_LOCK  = "LK" // 锁定中
	BIN_STATUS_ON    = "ON" // 开启中

	// 盘点结果
	CHECK_RESULT_MORE = "MORE"
	CHECK_RESULT_EQUAL = "EQUAL"
	CHECK_RESULT_LESS = "LESS"
	// 补库单状态
	REPLENISH_ORDER_STATUS_ON = 0
	REPLENISH_ORDER_STATUS_ACCOMPLISH = 1

	WM_AUTH_CHECK_ON = 0 //开启
	WM_AUTH_CHECK_OFF = 1 //关闭
)



type WorkAreaCheck struct {
	StorageBinIdList []string
	WarehouseIdList []string
	StorageAreaIdList []string
	StorageRackIdList []string
}

func init(){
	//GoodsReceiptHead_StructToJson()
	//Article_StructToJson()
	//GoodsIssueHead_StructToJson()
	//StockTransferHead_StructToJson()
}

//wm用户权限检测
func WM_Check(workAreaCheck WorkAreaCheck,workAreaList []*WorkArea) error{
	fmt.Println(workAreaCheck)
	fmt.Println(workAreaList)

	isPass := false
	for _, entity := range workAreaList{
		switch entity.WorkAreaType {
		case WORK_AREA_TYPE_WARE_HOUSE:
			fmt.Println("WORK_AREA_TYPE_WARE_HOUSE")
			for _, houseId := range workAreaCheck.WarehouseIdList{
				if entity.RelevanceId == houseId{
					isPass = true
				}
			}
		case WORK_AREA_TYPE_STORAGE_AREA:
			fmt.Println("WORK_AREA_TYPE_STORAGE_AREA")
			isPass1, isPass2 := false,false
			for _, houseId := range workAreaCheck.WarehouseIdList{
				if entity.RelevanceId == houseId{
					isPass1 = true
				}
			}
			for _, areaId := range workAreaCheck.StorageAreaIdList{
				if entity.WACtrlPara3 == areaId{
					isPass2 = true
				}
			}
			if isPass2 && isPass1{
				isPass = true
			}
		case WORK_AREA_TYPE_STORAGE_RACK:
			fmt.Println("WORK_AREA_TYPE_STORAGE_RACK")
			isPass1, isPass2 := false,false
			for _, houseId := range workAreaCheck.WarehouseIdList{
				if entity.RelevanceId == houseId{
					isPass1 = true
				}
			}
			for _, areaId := range workAreaCheck.StorageRackIdList{
				if entity.WACtrlPara3 == areaId{
					isPass2 = true
				}
			}
			if isPass2 && isPass1{
				isPass = true
			}
		default:
			return grmi.NewBusinessError("不支持的类型")
		}

	}
	if !isPass{
		return grmi.NewBusinessError("用户权限不足!")
	}
	return nil
}


func Article_StructToJson(){
	Article := Article{
		ArtId:"1",
		Descr1:"1",
		Descr2:"1",
		ArtType :"1",
		ProjectId :"1",
		Uom    :"1",
		Weight   :1,
		WeightUom   :"1",
		Length     :1,
		Width       :1,
		Height     :1,
		SizeUom     :"1",
		Fragile     :false,
		Icon        :"1",
		DisplayColor :"1",
		Document   :"1",
		LotSize      :1,
		ABCClass    :"1",
	}
	ToJson(Article)
}

func GoodsReceiptHead_StructToJson(){
	date := grmi.Date((time.Now()))
	datetime := grmi.DateTime((time.Now()))

	grh := GoodsReceiptHead{
		RefOrderId   :"1",
		GRWarehouseId :"1",
		Remark      :"1",
		DemandDate  :date,
		RegisterDate  :date,
		Status     :"0",
		SupplierId  :"1",
		SupplierName  :"1",
		GRType     :"1",
		GRType1    :"1",
		GRType2    :"1",
		GRReason     :"1",
		OrderedQty  :2,
		DeliveredQty   :2,
		Applicant    :"1",
		Department   :"1",
		ApplyTime    :datetime,
	}
	var grd []GoodsReceiptDetail
	grd1 := GoodsReceiptDetail{
		ArtId:"1",
		ArtName:"1",
		ArtType:"1",
		Remark:"1",
		Auditor:"1",
		AuditTime:datetime,
		AuditStatus:"1",
		BatchId:"1",
		OrderedQty:1,
		OrderType:"1",
		DeliveredQty:1,
		QtyUom:"1",
		UnitPrice:1,
		TotalPrice:1,
		Currency:"1",
		InspectToggle:true,
		InspectPlanId:"1",
		InspectStatus:"1",
	}
	grd2 := GoodsReceiptDetail{
		ArtId:"1",
		ArtName:"1",
		ArtType:"1",
		Remark:"1",
		Auditor:"1",
		AuditTime:datetime,
		AuditStatus:"1",
		BatchId:"1",
		OrderedQty:1,
		OrderType:"1",
		DeliveredQty:1,
		QtyUom:"1",
		UnitPrice:1,
		TotalPrice:1,
		Currency:"1",
		InspectToggle:true,
		InspectPlanId:"1",
		InspectStatus:"1",
	}


	var grl []GoodsReceiptLst
	grl1 := GoodsReceiptLst{
		WarehouseId:"1",
		StorageAreaId:"1",
		StorageBinId:"1",
		StorageRackId:"1",
		Qty:1,
	}
	grl2 := GoodsReceiptLst{
		WarehouseId:"1",
		StorageAreaId:"1",
		StorageBinId:"1",
		StorageRackId:"1",
		Qty:1,
	}

	grl = append(grl, grl1)
	grl = append(grl, grl2)

	grd1.GoodsReceiptLst = grl

	grd = append(grd, grd1)
	grd = append(grd, grd2)

	grh.GoodsReceiptDetail = grd

	ToJson(grh)
}

func StockTransferHead_StructToJson(){
	date := grmi.Date((time.Now()))
	datetime := grmi.DateTime((time.Now()))

	grh := StockTransferHead{
		RefOrderId:"1",
		WarehouseId:"1",
		TargetWarehouseId :"1",
		Remark :"1",
		DemandDate :date,
		RegisterDate  :date,
		Status  :"1",
		STType  :"1",
		STType1 :"1",
		STType2 :"1",
		STReason  :"1",
		OrderedQty  :1,
		DeliveredQty :1,
		Applicant   :"1",
		Department  :"1",
		ApplyTime   :datetime,
	}
	var grd []StockTransferDetail
	grd1 := StockTransferDetail{
		ArtId         :"1",
		ArtName           :"1",
		ArtType             :"1",
		Remark            :"1",
		Auditor           :"1",
		AuditTime          :datetime,
		AuditStatus      :"1",
		BatchId          :"1",
		OrderedQty        : 1,
		OrderType      :"1",
		DeliveredQty   :  1,
		QtyUom         :"1",
		UnitPrice       :  1,
		TotalPrice          :  1,
		Currency         :"1",
		InspectToggle      :true,
		InspectPlanId      :"1",
		InspectStatus      :"1",
		StorageBinId       :"1",
		StorageAreaId       :"1",
		WarehouseId       :"1",
		TargetStorageBinId  :"2",
		TargetStorageAreaId :"2",
		TargetWarehouseId  : "2",
		StorageRackId     : "1",
		TargetStorageRackId      : "2",
	}
	//grd2 := StockTransferDetail{
	//	ArtId         :"1",
	//	ArtName           :"1",
	//	ArtType             :"1",
	//	Remark            :"1",
	//	Auditor           :"1",
	//	AuditTime          :datetime,
	//	AuditStatus      :"1",
	//	BatchId          :"1",
	//	OrderedQty        : 1,
	//	OrderType      :"1",
	//	DeliveredQty   :  1,
	//	QtyUom         :"1",
	//	UnitPrice       :  1,
	//	TotalPrice          :  1,
	//	Currency         :"1",
	//	InspectToggle      :true,
	//	InspectPlanId      :"1",
	//	InspectStatus      :"1",
	//	StorageBinId       :"1",
	//	StorageAreaId       :"1",
	//	WarehouseId       :"1",
	//	TargetStorageBinId  :"2",
	//	TargetStorageAreaId :"2",
	//	TargetWarehouseId  : "2",
	//	StorageRackId     : "1",
	//	TargetStorageRackId      : "2",
	//}

	grd = append(grd, grd1)
	//grd = append(grd, grd2)

	grh.StockTransferDetail = grd

	ToJson(grh)
}

func GoodsIssueHead_StructToJson(){
	date := grmi.Date((time.Now()))
	datetime := grmi.DateTime((time.Now()))

	grh := GoodsIssueHead{
		GoodsIssueId:"1",
		RefOrderId :"1",
		CustOrderId :"1",
		Destination:"1",
		Remark :"1",
		DeliveryDate :date,
		Status   :"1",
		GIType   :"1",
		GIType1 :"1",
		GIType2   :"1",
		GIReason  :"1",
		Priority  :1,
		CostCenterId :"1",
		BookingKey  :"1",
	}
	var grd []GoodsIssueDetail
	grd1 := GoodsIssueDetail{
		GoodsIssueId  :"1",
		GIPos       :1,
		Remark      :"1",
		ArtId        :"1",
		ArtName       :"1",
		ArtType      :"1",
		RequiredQty   :1,
		RequestType   :"1",
		ReservedQty  :1,
		ActualQty     :1,
		OrderQty     :1,
		DeliveryDate  :date,
		InspectToggle:true,
		InspectPlanId:"1",
		InspectStatus:"1",
		Auditor      :"1",
		AuditTime     :datetime,
		AuditStatus  :"1",
		BatchId     :"1",
	}
	grd2 := GoodsIssueDetail{
		GoodsIssueId  :"1",
		GIPos       :1,
		Remark      :"1",
		ArtId        :"1",
		ArtName       :"1",
		ArtType      :"1",
		RequiredQty   :1,
		RequestType   :"1",
		ReservedQty  :1,
		ActualQty     :1,
		OrderQty     :1,
		DeliveryDate  :date,
		InspectToggle:true,
		InspectPlanId:"1",
		InspectStatus:"1",
		Auditor      :"1",
		AuditTime     :datetime,
		AuditStatus  :"1",
		BatchId     :"1",
	}


	var grl []GoodsIssueLst
	grl1 := GoodsIssueLst{
		WarehouseId:"1",
		StorageAreaId:"1",
		StorageBinId:"1",
		StorageRackId:"1",
		Qty:1,
	}
	grl2 := GoodsIssueLst{
		WarehouseId:"1",
		StorageAreaId:"1",
		StorageBinId:"1",
		StorageRackId:"1",
		Qty:1,
	}

	grl = append(grl, grl1)
	grl = append(grl, grl2)

	grd1.GoodsIssueLst = grl

	grd = append(grd, grd1)
	grd = append(grd, grd2)

	grh.GoodsIssueDetail = grd

	ToJson(grh)
}

func ToJson(j interface{}) {
	jsonBytes, err := json.Marshal(j)
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(string(jsonBytes))
}