// Copyright (c) Shenyang Leading Edge Intelligent Technology Co., Ltd. All rights reserved.

package pm

import (
	"LAPP_ACURA_MOM_BACKEND/grmi"
	"xorm.io/core"
)

/******************************************************************************
 *
 * @Struct Name              : Location
 *-----------------------------------------------------------------------------
 *
 * @Description              : Location的实体映射
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-10-08 13:20:42
 *
 ******************************************************************************/
type Location struct {
	PlantNr    int           `xorm:"pk int 'PlantNr'" json:"PM_Location-PlantNr"`
	LocaNr     int           `xorm:"pk int 'LocaNr'" json:"PM_Location-LocaNr"`
	LocaName   string        `xorm:"varchar(40) 'LocaName' not null" json:"PM_Location-LocaName"`
	LocaPara1  string        `xorm:"varchar(100) 'LocaPara1' not null" json:"PM_Location-LocaPara1"`
	LocaPara2  string        `xorm:"varchar(100) 'LocaPara2' not null" json:"PM_Location-LocaPara2"`
	LocaPara3  string        `xorm:"varchar(100) 'LocaPara3' not null" json:"PM_Location-LocaPara3"`
	LocaPara4  string        `xorm:"varchar(100) 'LocaPara4' not null" json:"PM_Location-LocaPara4"`
	LastModify grmi.DateTime `xorm:"datetime 'LastModify' not null updated" json:"PM_Location-LastModify"`
	LastUser   string        `xorm:"nvarchar(20) 'LastUser' not null" json:"PM_Location-LastUser"`
	CreateTime grmi.DateTime `xorm:"datetime 'CreateTime' not null created" json:"PM_Location-CreateTime"`
	Valst     []Asset `json:"valst" xorm:"-"`
	UnValst   []Asset `json:"unvalst" xorm:"-"`
	Children  []Asset `json:"children" xorm:"-"`
}

/******************************************************************************
 *
 * @Function Name            : GetKey
 *-----------------------------------------------------------------------------
 *
 * @Description              : 获取实体的主键
 *
 * @Return Value             : 实体的主键
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-10-08 13:20:42
 *
 ******************************************************************************/
func (self *Location) GetKey() core.PK {
	return core.PK{self.PlantNr, self.LocaNr}
}

type TreePmLocation struct {
	Locaid   int       `json:"pm_asset-locaid" xorm:"not null pk INT(8)"`
	Locaname string    `json:"pm_asset-assetname" xorm:"not null VARCHAR(40)"`
	Children []Asset `json:"children" xorm:"-"`
}

type LocationAsset struct {
	Asset    `xorm:"extends"`
	Location `xorm:"extends"`
}

func (self *Location)DiguiLocation(locationlist *[]LocationAsset, location TreePmLocation) TreePmLocation {
	list := *locationlist

	min := make([]Asset, 0)
	for j := 0; j < len(list); j++ {
		if location.Locaid != list[j].Location.LocaNr {
			continue
		}
		mi := Asset{}
		mi.LocaNr = list[j].Asset.LocaNr
		mi.PlantNr = list[j].Asset.PlantNr
		mi.Descr = list[j].Asset.Descr
		mi.AssetNr = list[j].Asset.AssetNr
		mi.AssetName = list[j].Asset.Descr
		mi.AssetStatus = list[j].Asset.AssetStatus
		mi.AssetType = list[j].Asset.AssetType
		mi.WeekModelId = list[j].Asset.WeekModelId
		mi.Operator = list[j].Asset.Operator
		mi.CostCenterId = list[j].Asset.CostCenterId
		mi.Maker = list[j].Asset.Maker
		mi.Year = list[j].Asset.Year
		min = append(min, mi)
	}
	location.Children = min
	return location
}

