package models

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

type Workcalendar struct {
	Finr           int               `xorm:"pk comment('工厂号') Int" json:"workcalendar-finr"`
	Workcalendarid int               `xorm:"pk comment('日模型ID') Int" json:"workcalendar-workcalendarid"`
	Descr          string            `xorm:"comment('描述') VARCHAR(30)" json:"workcalendar-descr"`
	Lastmodif      string            `xorm:"comment('最近一次更改时间') VARCHAR(14)" json:"workcalendar-lastmodif"`
	Lastuser       string            `xorm:"comment('最近一次更改人') VARCHAR(20)" json:"workcalendar-lastuser"`
	Credatuz       string            `xorm:"comment('创建时间') VARCHAR(14)" json:"workcalendar-credatuz"`
	Valst          []Workcalendarlst `json:"valst" xorm:"-"`
}

func (t *Workcalendar) TableName() string {
	return "workcalendar"
}

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

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

	//批量插入
	for k, _ := range t.Valst {
		t.Valst[k].Finr = t.Finr
		t.Valst[k].Workcalendarid = t.Workcalendarid
	}
	alldata := make([]Workcalendarlst, 0)
	alldata = t.Valst
	_, err = e.Table("workcalendarlst").Insert(&alldata)
	if err != nil {
		return err
	}

	return nil
}

//删
func (t *Workcalendar) Del() bool {
	e := db.Eloquent.Master()
	_, err := e.ID(core.PK{t.Finr, t.Workcalendarid}).Delete(&Workcalendar{})
	if err != nil {
		return false
	}
	count := new(Workcalendarlst)
	affw, _ := e.Table("workcalendarlst").Where("finr = ? and workcalendarid = ?", t.Finr, t.Workcalendarid).Count(count)
	if affw > 0 {
		_, err = e.Where("finr = ? and workcalendarid = ?", t.Finr, t.Workcalendarid).Delete(&Workcalendarlst{})
		if err != nil {
			return false
		}
	}
	return true
}

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

type DayAndWorkmodel struct {
	Workcalendarlst `xorm:"extends"`
	Daymodel        `xorm:"extends"`
}

//查
func (t *Workcalendar) SelectOne() (Workcalendar, error) {
	e := db.Eloquent.Master()
	var data Workcalendar
	_, err := e.ID(core.PK{t.Finr, t.Workcalendarid}).Get(&data)
	if err != nil {
		return data, err
	}
	var datalist []DayAndWorkmodel
	var temlist []Workcalendarlst
	err = e.Table("workcalendarlst").Join("Left", "daymodel", "daymodel.daymodelid = workcalendarlst.daymodelid and daymodel.finr = workcalendarlst.finr").Where("workcalendarlst.finr = ? and workcalendarlst.workcalendarid = ?", t.Finr, t.Workcalendarid).Find(&datalist)
	if err != nil {
		return data, err
	}
	for _, v := range datalist {
		dataone := Workcalendarlst{}
		dataone = v.Workcalendarlst
		dataone.Descr = v.Daymodel.Descr
		dataone.Color = v.Daymodel.Color
		dataone.Abrev = v.Daymodel.Abrev
		temlist = append(temlist,dataone)
	}
	data.Valst = temlist
	return data, nil
}

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