package models

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

type PmAttribute struct {
	Finr         int                `json:"pm_attribute-finr" xorm:"not null pk INT(4)"`
	Attrnr       int                `json:"pm_attribute-attrnr" xorm:"not null pk INT(4)"`
	Attrname     string             `json:"pm_attribute-attrname" xorm:"not null VARCHAR(30)"`
	Descr        string             `json:"pm_attribute-descr" xorm:"not null VARCHAR(40)"`
	Shortcode    string             `json:"pm_attribute-shortcode" xorm:"not null VARCHAR(16)"`
	Attrdatatype string             `json:"pm_attribute-attrdatatype" xorm:"not null VARCHAR(1)"`
	Attrvaltype  string             `json:"pm_attribute-attrvaltype" xorm:"not null VARCHAR(1)"`
	Validval     string             `json:"pm_attribute-validval" xorm:"not null VARCHAR(40)"`
	Valpicture   string             `json:"pm_attribute-valpicture" xorm:"not null VARCHAR(40)"`
	Picture1     string             `json:"pm_attribute-picture1" xorm:"not null VARCHAR(40)"`
	Picture2     string             `json:"pm_attribute-picture2" xorm:"not null VARCHAR(40)"`
	Isattrgrp    int                `json:"pm_attribute-isattrgrp" xorm:"not null INT(4)"`
	Lastmodif    string             `json:"pm_attribute-lastmodif" xorm:"not null VARCHAR(14)"`
	Lastuser     string             `json:"pm_attribute-lastuser" xorm:"not null VARCHAR(20)"`
	Credatuz     string             `json:"pm_attribute-credatuz" xorm:"not null VARCHAR(14)"`
	Valst        []PmAttributeValst `json:"valst" xorm:"-"`
	Parameter    string             `json:"pm_attribute-parameter" xorm:"-"`
}

func (t *PmAttribute) TableName() string {
	return "pm_attribute"
}

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

//增
func (t *PmAttribute) Add() error {
	e := db.Eloquent.Master()
	snr := new(Snrtab)
	snr.Finr = t.Finr
	//生成ID
	Attrnr, err := snr.GetNextSnr("Attrnr")
	t.Attrnr = utils.ValueToInt(Attrnr,0)
	_, err = e.Table("pm_attribute").Insert(t)
	if err != nil {
		return err
	}
	//批量插入
	for k, _ := range t.Valst {
		t.Valst[k].Finr = t.Finr
		t.Valst[k].Attrnr = t.Attrnr
		t.Valst[k].Credatuz = t.Credatuz
		t.Valst[k].Lastuser = t.Lastuser
		t.Valst[k].Lastmodif = t.Lastmodif
	}
	alldata := make([]PmAttributeValst, 0)
	alldata = t.Valst
	_, err = e.Table("pm_attribute_valst").Insert(&alldata)
	if err != nil {
		return err
	}
	return nil
}

//删
func (t *PmAttribute) Del() bool {
	e := db.Eloquent.Master()
	_, err := e.Where("finr = ? and attrnr = ?", t.Finr, t.Attrnr).Delete(&PmAttribute{})
	if err != nil {
		return false
	}
	countrole := new(PmAttributeValst)
	affw, _ := e.Table("pm_attribute_valst").Where("finr = ? and attrnr = ?", t.Finr, t.Attrnr).Count(countrole)
	if affw > 0 {
		_, err = e.Where("finr = ? and attrnr = ?", t.Finr, t.Attrnr).Delete(&PmAttributeValst{})
		if err != nil {
			return false
		}
	}

	return true
}

//改
func (t *PmAttribute) Update() bool {
	e := db.Eloquent.Master()
	_, err := e.ID(core.PK{t.Finr, t.Attrnr}).Update(t)
	if err != nil {
		return false
	}
	//批量删除
	_, err = e.Table("pm_attribute_valst").Where("finr = ? and attrnr = ?", t.Finr, t.Attrnr).Delete(&PmAttributeValst{})
	if err != nil {
		return false
	}
	//批量插入
	for k, _ := range t.Valst {
		t.Valst[k].Finr = t.Finr
		t.Valst[k].Attrnr = t.Attrnr
		t.Valst[k].Credatuz = t.Credatuz
		t.Valst[k].Lastuser = t.Lastuser
		t.Valst[k].Lastmodif = t.Lastmodif
	}
	//批量插入
	alldata := make([]PmAttributeValst, 0)
	alldata = t.Valst
	_, err = e.Table("pm_attribute_valst").Insert(&alldata)
	if err != nil {
		return false
	}
	return true
}

//查
func (t *PmAttribute) SelectOne() (PmAttribute, error) {
	e := db.Eloquent.Master()
	var data PmAttribute
	_, err := e.ID(core.PK{t.Finr, t.Attrnr}).Get(&data)
	if err != nil {
		return data, err
	}
	var datalist []PmAttributeValst
	err = e.Where("finr = ? and attrnr = ?", t.Finr, t.Attrnr).Find(&datalist)
	if err != nil {
		return data, err
	}
	data.Valst = datalist
	return data, nil
}

//分页
func (t *PmAttribute) GetPage(descr string, pageSize int, pageIndex int) ([]PmAttribute, int, error) {
	data := make([]PmAttribute, 0)
	e := db.Eloquent.Master()
	table := e.Table("pm_attribute").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_attribute where finr = ?"+where+") t order by attrnr offset ? row fetch next ? row only", parameters...).Find(&data)
	if err != nil {
		return data, 0, err
	}

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