package models

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

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


func (t *Attribute) TableName() string {
	return "attribute"
}

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

//增
func (t *Attribute) Add() error {
	e := db.Eloquent.Master()
	countrole := new(Attribute)
	affw, err := e.Table("attribute").ID(core.PK{t.Finr, t.Attrnr}).Count(countrole)
	if err != nil {
		return err
	}
	if affw > 0 {
		return errors.New("数据已经存在！")
	}
	_, err = e.Table("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([]AttributeValst, 0)
	alldata = t.Valst
	_, err = e.Table("attribute_valst").Insert(&alldata)
	if err != nil {
		return err
	}
	return nil
}

//删
func (t *Attribute) Del() bool {
	e := db.Eloquent.Master()
	_, err := e.Where("finr = ? and attrnr = ?", t.Finr, t.Attrnr).Delete(&Attribute{})
	if err != nil {
		return false
	}
	countrole := new(AttributeValst)
	affw, _ := e.Table("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(&AttributeValst{})
		if err != nil {
			return false
		}
	}

	return true
}

//改
func (t *Attribute) 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("attribute_valst").Where("finr = ? and attrnr = ?", t.Finr, t.Attrnr).Delete(&AttributeValst{})
	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([]AttributeValst, 0)
	alldata = t.Valst
	_, err = e.Table("attribute_valst").Insert(&alldata)
	if err != nil {
		return false
	}
	return true
}

//查
func (t *Attribute) SelectOne() (Attribute, error) {
	e := db.Eloquent.Master()
	var data Attribute
	_, err := e.ID(core.PK{t.Finr, t.Attrnr}).Get(&data)
	if err != nil {
		return data, err
	}
	data.Clipped()
	var datalist []AttributeValst
	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 *Attribute) GetPage(pageSize int, pageIndex int) ([]Attribute, int, error) {
	data := make([]Attribute, 0)
	e := db.Eloquent.Master()
	table := e.Table("attribute").Where("finr = ? ", t.Finr)
	where := "where finr = " + "'" + utils.ValueToString(t.Finr, "") + "'"
	if t.Attrnr > 0 {
		table = table.And("attrnr = ?", t.Attrnr)
		where += " and attrnr = " + "'" + utils.ValueToString(t.Attrnr, "") + "'"
	}
	Offset := (pageIndex - 1) * pageSize
	err := e.SQL("SELECT TOP " + utils.ValueToString(pageSize, "") + " attribute.* FROM attribute " + where + " AND (convert(varchar(10),finr)+convert(varchar(40),attrnr) NOT IN (SELECT TOP " + utils.ValueToString(Offset, "") + " convert(varchar(10),finr)+convert(varchar(40),attrnr) FROM attribute " + where + " ORDER BY credatuz DESC)) ORDER BY credatuz DESC").Find(&data)
	pcount := new(Attribute)
	count, err := table.Count(pcount)
	if err != nil {
		return nil, 0, err
	}
	return data, int(count), nil
}
