package models

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

type PmAssettype struct {
	Finr       int    `json:"pm_assettype-finr" xorm:"not null pk INT(4)"`
	Assettype  string `json:"pm_assettype-assettype" xorm:"not null pk VARCHAR(20)"`
	Descr      string `json:"pm_assettype-descr" xorm:"not null VARCHAR(40)"`
	Bitmap     string `json:"pm_assettype-bitmap" xorm:"not null VARCHAR(30)"`
	AstypCust1 string `json:"pm_assettype-astyp_cust1" xorm:"not null VARCHAR(100)"`
	AstypCust2 string `json:"pm_assettype-astyp_cust2" xorm:"not null VARCHAR(100)"`
	AstypCust3 string `json:"pm_assettype-astyp_cust3" xorm:"not null VARCHAR(100)"`
	AstypCust4 string `json:"pm_assettype-astyp_cust4" xorm:"not null VARCHAR(100)"`
	Lastmodif  string `json:"pm_assettype-lastmodif" xorm:"not null VARCHAR(14)"`
	Lastuser   string `json:"pm_assettype-lastuser" xorm:"not null VARCHAR(20)"`
	Credatuz   string `json:"pm_assettype-credatuz" xorm:"not null VARCHAR(14)"`
}

func (t *PmAssettype) TableName() string {
	return " pm_assettype"
}

// 清除string字段的右侧空格
func (t *PmAssettype) Clipped() {
	utils.TrimStruct(t, *t)
}

//增
func (t *PmAssettype) Add() error {
	e := db.Eloquent.Master()
	snr := new(Snrtab)
	snr.Finr = t.Finr
	//生成ID
	Assettypeid, err := snr.GetNextSnr("Assettypeid")
	t.Assettype = Assettypeid
	_, err = e.Table(" pm_assettype").Insert(t)
	if err != nil {
		return err
	}
	return nil
}

//删
func (t *PmAssettype) Del() bool {
	e := db.Eloquent.Master()
	_, err := e.ID(core.PK{t.Finr, t.Assettype}).Delete(&PmAssettype{})
	if err != nil {
		return false
	}
	return true
}

//改
func (t *PmAssettype) Update() bool {
	e := db.Eloquent.Master()
	_, err := e.Cols("descr", "bitmap", "astyp_cust1", "astyp_cust2", "astyp_cust3", "astyp_cust4", "lastmodif", "lastuser").ID(core.PK{t.Finr, t.Assettype}).Update(t)
	if err != nil {
		return false
	}
	return true
}

//查
func (t *PmAssettype) SelectOne() (PmAssettype, error) {
	e := db.Eloquent.Master()
	var data PmAssettype
	_, err := e.ID(core.PK{t.Finr, t.Assettype}).Get(&data)
	if err != nil {
		return data, err
	}
	return data, nil
}

//分页
func (t *PmAssettype) GetPage(descr string, pageSize int, pageIndex int) ([]PmAssettype, int, error) {
	data := make([]PmAssettype, 0)
	e := db.Eloquent.Master()
	table := e.Table(" pm_assettype").Where("finr = ? ", t.Finr)
	parameters := make([]interface{}, 0)
	where := ""
	parameters = append(parameters, t.Finr)
	if descr != "" {
		table = table.And("descr like concat('%',?,'%')", descr)
		where += " and descr like concat('%',?,'%')"
		parameters = append(parameters, descr)
	}
	parameters = append(parameters, (pageIndex-1)*pageSize, pageSize)
	err := e.SQL("SELECT * FROM (SELECT * FROM pm_assettype where finr = ?"+where+") t order by assettype offset ? row fetch next ? row only", parameters...).Find(&data)
	if err != nil {
		return data, 0, err
	}

	count, err := table.Count(new(PmAssettype))
	if err != nil {
		return data, 0, err
	}
	for k, _ := range data {
		data[k].Clipped()
	}
	return data, int(count), nil
}

//全部
func (t *PmAssettype) GetAll() ([]PmAssettype, error) {
	data := make([]PmAssettype, 0)
	e := db.Eloquent.Master()
	err := e.Where("finr = ?", t.Finr).OrderBy("assettype").Find(&data)
	if err != nil {
		return nil, err
	}
	return data, nil
}
