package models

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

type Printdetailtab struct {
	Finr        int    `xorm:"pk comment('车间编号')" json:"printdetailtab-finr"`
	Printheadid string `xorm:"pk comment('打印信息头id')" json:"printdetailtab-printheadid"`
	Pos         int    `xorm:"pk comment('位置')" json:"printdetailtab-pos"`
	Varname     string `xorm:"comment('变量名')" json:"printdetailtab-varname"`
	Varvalue    string `xorm:"comment('变量值')" json:"printdetailtab-varvalue"`
	Vartype     string `xorm:"comment('变量类型')" json:"printdetailtab-vartype"`
	Varpos      string `xorm:"comment('变量位置')" json:"printdetailtab-varpos"`
	Lastmodif   string `xorm:"comment('上一次更改日期')" json:"printdetailtab-lastmodif"`
	Lastuser    string `xorm:"comment('最近编辑人员')" json:"printdetailtab-lastuser"`
	Credatuz    string `xorm:"comment('创建时间')" json:"printdetailtab-credatuz"`
}

func (t *Printdetailtab) TableName() string {
	return "printdetailtab"
}

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

//增
func (t *Printdetailtab) Add() error {
	e := db.Eloquent.Master()
	countrole := new(Printdetailtab)
	affw, err := e.Table("printdetailtab").ID(core.PK{t.Finr, t.Printheadid, t.Pos}).Count(countrole)
	if err != nil {
		return err
	}
	if affw > 0 {
		return errors.New("数据已经存在！")
	}
	_, err = e.Table("printdetailtab").Insert(t)
	if err != nil {
		return err
	}
	return nil
}

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

//改
func (t *Printdetailtab) Update() bool {
	e := db.Eloquent.Master()
	_, err := e.ID(core.PK{t.Finr, t.Printheadid, t.Pos}).Update(t)
	if err != nil {
		return false
	}
	return true
}

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

//分页
func (t *Printdetailtab) GetPage(pageSize int, pageIndex int) ([]Printdetailtab, int, error) {
	data := make([]Printdetailtab, 0)
	e := db.Eloquent.Master()
	table := e.Table("printdetailtab").Where("finr = ? ", t.Finr)
	where := "where finr = " + "'" + utils.ValueToString(t.Finr, "") + "'"
	if !utils.ValueIsEmpty(t.Printheadid) {
		table = table.And("printheadid = ?", t.Printheadid)
		where += " and printheadid = " + "'" + t.Printheadid + "'"
	}
	Offset := (pageIndex - 1) * pageSize
	err := e.SQL("SELECT TOP " + utils.ValueToString(pageSize, "") + " printdetailtab.* FROM printdetailtab " + where + " AND (convert(varchar(10),finr)+convert(varchar(40),printheadid)+convert(varchar(20),pos) NOT IN (SELECT TOP " + utils.ValueToString(Offset, "") + " convert(varchar(10),finr)+convert(varchar(40),printheadid)+convert(varchar(20),pos) FROM printdetailtab " + where + " ORDER BY credatuz DESC)) ORDER BY credatuz DESC").Find(&data)
	pcount := new(Printdetailtab)
	count, err := table.Count(pcount)
	if err != nil {
		return data, 0, err
	}
	for k, _ := range data {
		data[k].Clipped()
	}
	return data, int(count), nil
}