package models

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

type Weekmodel struct {
	Finr        int            `xorm:"pk comment('工厂号') Int" json:"weekmodel-finr"`
	Weekmodelid int            `xorm:"pk comment('周模型ID') Int" json:"weekmodel-weekmodelid"`
	Datefrom    string         `xorm:"comment('工作日历') VARCHAR(14)" json:"weekmodel-datefrom"`
	Descr       string         `xorm:"comment('描述') VARCHAR(30)" json:"weekmodel-descr"`
	Abrev       string         `xorm:"comment('简写') VARCHAR(8)" json:"weekmodel-abrev"`
	Control     int            `xorm:"comment('周模块控制') INT" json:"weekmodel-control"`
	Loopdays    int            `xorm:"comment('循环天数') INT" json:"weekmodel-loopdays"`
	Lastmodif   string         `xorm:"comment('最近一次更改时间') VARCHAR(14)" json:"weekmodel-lastmodif"`
	Lastuser    string         `xorm:"comment('最近一次更改人') VARCHAR(20)" json:"weekmodel-lastuser"`
	Credatuz    string         `xorm:"comment('创建时间') VARCHAR(14)" json:"weekmodel-credatuz"`
	Valst       []Weekmodellst `json:"valst" xorm:"-"`
}

func (t *Weekmodel) TableName() string {
	return "weekmodel"
}

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

//增
func (t *Weekmodel) Add() error {
	e := db.Eloquent.Master()
	countrole := new(Weekmodel)
	affw, err := e.Table("weekmodel").ID(core.PK{t.Finr, t.Weekmodelid}).Count(countrole)
	if err != nil {
		return err
	}
	if affw > 0 {
		return errors.New("数据已经存在！")
	}
	_, err = e.Table("weekmodel").Insert(t)
	if err != nil {
		return err
	}
	//批量插入
	for k, _ := range t.Valst {
		t.Valst[k].Finr = t.Finr
		t.Valst[k].Weekmodelid = t.Weekmodelid
	}
	alldata := make([]Weekmodellst, 0)
	alldata = t.Valst
	_, err = e.Table("weekmodellst").Insert(&alldata)
	if err != nil {
		return err
	}

	return nil
}

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

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

	return true
}

type DayAndWeekmodel struct {
	Weekmodellst `xorm:"extends"`
	Daymodel     `xorm:"extends"`
}

//查
func (t *Weekmodel) SelectOne() (Weekmodel, error) {
	e := db.Eloquent.Master()
	var data Weekmodel
	_, err := e.ID(core.PK{t.Finr, t.Weekmodelid}).Get(&data)
	if err != nil {
		return data, err
	}
	data.Clipped()
	var temlist []Weekmodellst
	var datalist []DayAndWeekmodel
	err = e.Table("weekmodellst").Join("Left", "daymodel", "daymodel.daymodelid = weekmodellst.daymodelid and daymodel.finr = weekmodellst.finr").Where("weekmodellst.finr = ? and weekmodellst.weekmodelid = ?", t.Finr, t.Weekmodelid).Find(&datalist)
	if err != nil {
		return data, err
	}
	for _, v := range datalist {
		dataone := Weekmodellst{}
		dataone = v.Weekmodellst
		dataone.Descr = v.Daymodel.Descr
		temlist = append(temlist, dataone)
	}
	data.Valst = temlist
	return data, nil
}

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