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

package me

import (
	"LAPP_LF_MOM_BACKEND/db"
	"LAPP_LF_MOM_BACKEND/grmi"
	"LAPP_LF_MOM_BACKEND/utils"
	"xorm.io/core"
)

/******************************************************************************
 *
 * @Struct Name              : BomLst
 *-----------------------------------------------------------------------------
 *
 * @Description              : BomLst的实体映射
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-08-09 14:13:50
 *
 ******************************************************************************/
type BomLst struct {
	PlantNr       int           `xorm:"pk int 'PlantNr'" json:"ME_BomLst-PlantNr"`
	BomId         string        `xorm:"pk nvarchar(20) 'BomId'" json:"ME_BomLst-BomId"`
	FmatNr        string        `xorm:"pk nvarchar(20) 'FmatNr'" json:"ME_BomLst-FmatNr"`
	CmatNr        string        `xorm:"pk nvarchar(20) 'CmatNr'" json:"ME_BomLst-CmatNr"`
	FmatName      string        `xorm:"nvarchar(40) 'FmatName' not null" json:"ME_BomLst-FmatName"`
	FmatSpec      string        `xorm:"nvarchar(40) 'FmatSpec' not null" json:"ME_BomLst-FmatSpec"`
	FmatQty       float64       `xorm:"decimal 'FmatQty' not null" json:"ME_BomLst-FmatQty"`
	FmatUom       string        `xorm:"nvarchar(10) 'FmatUom' not null" json:"ME_BomLst-FmatUom"`
	CmatName      string        `xorm:"nvarchar(40) 'CmatName' not null" json:"ME_BomLst-CmatName"`
	CmatQty       float64       `xorm:"decimal 'CmatQty' not null" json:"ME_BomLst-CmatQty"`
	CmatUom       string        `xorm:"nvarchar(10) 'CmatUom' not null" json:"ME_BomLst-CmatUom"`
	CmatType      string        `xorm:"nvarchar(20) 'CmatType' not null" json:"ME_BomLst-CmatType"`
	Position      int           `xorm:"int 'Position' not null" json:"ME_BomLst-Position"`
	LeadTime      float64       `xorm:"decimal 'LeadTime' not null" json:"ME_BomLst-LeadTime"`
	TimeUom       string        `xorm:"nvarchar(10) 'TimeUom' not null" json:"ME_BomLst-TimeUom"`
	LossRate      float64       `xorm:"decimal 'LossRate' not null" json:"ME_BomLst-LossRate"`
	BackFlushItem int           `xorm:"smallint 'BackFlushItem' not null" json:"ME_BomLst-BackFlushItem"`
	CriticalItem  int           `xorm:"smallint 'CriticalItem' not null" json:"ME_BomLst-CriticalItem"`
	ValidFrom     string        `xorm:"nvarchar(14) 'ValidFrom'" json:"ME_BomLst-ValidFrom"`
	ValidTo       string        `xorm:"nvarchar(14) 'ValidTo'" json:"ME_BomLst-ValidTo"`
	BomlstCval1   string        `xorm:"nvarchar(40) 'BomlstCval1' not null" json:"ME_BomLst-BomlstCval1"`
	BomlstCval2   string        `xorm:"nvarchar(40) 'BomlstCval2' not null" json:"ME_BomLst-BomlstCval2"`
	BomlstIval1   int           `xorm:"int 'BomlstIval1' not null" json:"ME_BomLst-BomlstIval1"`
	BomlstIval2   int           `xorm:"int 'BomlstIval2' not null" json:"ME_BomLst-BomlstIval2"`
	LevelNr       int           `xorm:"int 'LevelNr'" json:"ME_BomLst-LevelNr"`
	LastModify    grmi.DateTime `xorm:"datetime 'LastModify' not null updated" json:"ME_BomLst-LastModify"`
	LastUser      string        `xorm:"nvarchar(20) 'LastUser' not null" json:"ME_BomLst-LastUser"`
	CreateTime    grmi.DateTime `xorm:"datetime 'CreateTime' not null created" json:"ME_BomLst-CreateTime"`
	ItemLi        []BomLst      `xorm:"-" json:"ME_BomLst-ItemLi"`
}

/******************************************************************************
 *
 * @Function Name            : GetKey
 *-----------------------------------------------------------------------------
 *
 * @Description              : 获取实体的主键
 *
 * @Return Value             : 实体的主键
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-08-09 14:13:50
 *
 ******************************************************************************/
func (self *BomLst) GetKey() core.PK {
	return core.PK{self.PlantNr, self.BomId, self.FmatNr, self.CmatNr}
}

func (t *BomLst) TableName() string {
	return "ME_BomLst"
}

func (t *BomLst) GetPage() (Menus []BomLst, err error) {
	e := db.Eloquent.Master()
	table := e.Table(t.TableName()).Where("PlantNr = ?", t.PlantNr)
	if t.BomId != "" {
		table = table.And("BomId = ?", t.BomId)
	}
	if err = table.Asc("FmatNr").Find(&Menus); err != nil {
		return
	}
	return
}

//递归查询侧边栏
func (t *BomLst) SetMenu(FmatNr string) (m []BomLst, err error) {
	menulist, err := t.GetPage()
	m = make([]BomLst, 0)
	for i := 0; i < len(menulist); i++ {
		//menulist[i].Clipped()
		if menulist[i].FmatNr != FmatNr {
			continue
		}
		menusInfo := DiguiMeBomlst(&menulist, menulist[i])
		m = append(m, menusInfo)
	}
	return
}

//递归查询
func DiguiMeBomlst(me_bomlst *[]BomLst, me BomLst) BomLst {
	list := *me_bomlst

	min := make([]BomLst, 0)
	for j := 0; j < len(list); j++ {
		//list[j].Clipped()
		if me.CmatNr != list[j].FmatNr {
			continue
		}
		mi := BomLst{}
		mi.PlantNr = list[j].PlantNr
		mi.BomId = list[j].BomId
		mi.FmatNr = list[j].FmatNr
		mi.CmatNr = list[j].CmatNr
		mi.FmatName = list[j].FmatName
		mi.FmatSpec = list[j].FmatSpec
		mi.FmatQty = list[j].FmatQty
		mi.FmatUom = list[j].FmatUom
		mi.LevelNr = list[j].LevelNr
		mi.CmatName = list[j].CmatName
		mi.CmatQty = list[j].CmatQty
		mi.CmatUom = list[j].CmatUom
		mi.CmatType = list[j].CmatType
		mi.Position = list[j].Position
		mi.LeadTime = list[j].LeadTime
		mi.TimeUom = list[j].TimeUom
		mi.LossRate = list[j].LossRate
		mi.BackFlushItem = list[j].BackFlushItem
		mi.CriticalItem = list[j].CriticalItem
		mi.ValidFrom = list[j].ValidFrom
		mi.ValidTo = list[j].ValidTo
		mi.BomlstCval1 = list[j].BomlstCval1
		mi.BomlstCval2 = list[j].BomlstCval2
		mi.BomlstIval1 = list[j].BomlstIval1
		mi.BomlstIval2 = list[j].BomlstIval2
		mi.ItemLi = []BomLst{}
		ms := DiguiMeBomlst(me_bomlst, mi)
		utils.TrimStruct(&ms, ms)
		min = append(min, ms)
	}
	me.ItemLi = min
	return me
}