package models

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

type WorklineSpecialday struct {
	Finr       int    `xorm:"pk comment('工厂号') Int" json:"workline_specialday-finr"`
	Worklineid string `xorm:"pk comment('产线ID') VARCHAR(20)" json:"workline_specialday-worklineid"`
	Specialday string `xorm:"pk comment('指定日期') VARCHAR(8)" json:"workline_specialday-specialday"`
	Daymodelid string `xorm:"comment('日模型ID') Int" json:"workline_specialday-daymodelid"`
	Descr      string `xorm:"comment('描述') VARCHAR(30)" json:"workline_specialday-descr"`
	Lastmodif  string `xorm:"comment('最近一次更改时间') VARCHAR(14)" json:"workline_specialday-lastmodif"`
	Lastuser   string `xorm:"comment('最近一次更改人') VARCHAR(20)" json:"workline_specialday-lastuser"`
	Credatuz   string `xorm:"comment('创建时间') VARCHAR(14)" json:"workline_specialday-credatuz"`
}

func (t *WorklineSpecialday) TableName() string {
	return "workline_specialday"
}

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


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

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

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

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

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