package models

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

//日模型
type Daymodel struct {
	Finr       int    `xorm:"pk comment('工厂号') Int" json:"daymodel-finr"`
	Daymodelid int    `xorm:"pk comment('日模型ID') Int" json:"daymodel-daymodelid"`
	Descr      string `xorm:"comment('描述') VARCHAR(30)" json:"daymodel-descr"`
	Abrev      string `xorm:"comment('简写') VARCHAR(8)" json:"daymodel-abrev"`
	Color      string `xorm:"comment('颜色') VARCHAR(8)" json:"daymodel-color"`
	S1Activ    int    `xorm:"comment('激活班次1') INT" json:"daymodel-s1_activ"`
	S2Activ    int    `xorm:"comment('激活班次2') INT" json:"daymodel-s2_activ"`
	S3Activ    int    `xorm:"comment('激活班次3') INT" json:"daymodel-s3_activ"`
	S4Activ    int    `xorm:"comment('激活班次4') INT" json:"daymodel-s4_activ"`
	S1Beg      string `xorm:"comment('班次1开始') VARCHAR(5)" json:"daymodel-s1_beg"`
	S1End      string `xorm:"comment('班次1结束') VARCHAR(5)" json:"daymodel-s1_end"`
	S1Len      int    `xorm:"comment('班次1长度') INT" json:"daymodel-s1_len"`
	S2Beg      string `xorm:"comment('班次2开始') VARCHAR(5)" json:"daymodel-s2_beg"`
	S2End      string `xorm:"comment('班次2结束') VARCHAR(5)" json:"daymodel-s2_end"`
	S2Len      int    `xorm:"comment('班次2长度') VARCHAR(5)" json:"daymodel-s2_len"`
	S3Beg      string `xorm:"comment('班次3开始') VARCHAR(5)" json:"daymodel-s3_beg"`
	S3End      string `xorm:"comment('班次3结束') VARCHAR(5)" json:"daymodel-s3_end"`
	S3Len      int    `xorm:"comment('班次3长度') VARCHAR(5)" json:"daymodel-s3_len"`
	S4Beg      string `xorm:"comment('班次4开始') VARCHAR(5)" json:"daymodel-s4_beg"`
	S4End      string `xorm:"comment('班次4结束') VARCHAR(5)" json:"daymodel-s4_end"`
	S4Len      int    `xorm:"comment('班次4长度') INT" json:"daymodel-s4_len"`
	DmLen      int    `xorm:"comment('Dm长度') INT" json:"daymodel-dm_len"`
	S1B1Beg    string `xorm:"comment('中断1开始') VARCHAR(5)" json:"daymodel-s1_b1_beg"`
	S1B1End    string `xorm:"comment('中断1结束') VARCHAR(5)" json:"daymodel-s1_b1_end"`
	S1B2Beg    string `xorm:"comment('中断2开始') VARCHAR(5)" json:"daymodel-s1_b2_beg"`
	S1B2End    string `xorm:"comment('中断2结束') VARCHAR(5)" json:"daymodel-s1_b2_end"`
	S1B3Beg    string `xorm:"comment('中断3开始') VARCHAR(5)" json:"daymodel-s1_b3_beg"`
	S1B3End    string `xorm:"comment('中断3结束') VARCHAR(5)" json:"daymodel-s1_b3_end"`
	S1B4Beg    string `xorm:"comment('中断4开始') VARCHAR(5)" json:"daymodel-s1_b4_beg"`
	S1B4End    string `xorm:"comment('中断4结束') VARCHAR(5)" json:"daymodel-s1_b4_end"`
	S1B5Beg    string `xorm:"comment('中断5开始') VARCHAR(5)" json:"daymodel-s1_b5_beg"`
	S1B5End    string `xorm:"comment('中断5结束') VARCHAR(5)" json:"daymodel-s1_b5_end"`
	S2B1Beg    string `xorm:"comment('中断1开始') VARCHAR(5)" json:"daymodel-s2_b1_beg"`
	S2B1End    string `xorm:"comment('中断1结束') VARCHAR(5)" json:"daymodel-s2_b1_end"`
	S2B2Beg    string `xorm:"comment('中断1开始') VARCHAR(5)" json:"daymodel-s2_b2_beg"`
	S2B2End    string `xorm:"comment('中断1结束') VARCHAR(5)" json:"daymodel-s2_b2_end"`
	S2B3Beg    string `xorm:"comment('中断1开始') VARCHAR(5)" json:"daymodel-s2_b3_beg"`
	S2B3End    string `xorm:"comment('中断1结束') VARCHAR(5)" json:"daymodel-s2_b3_end"`
	S2B4Beg    string `xorm:"comment('中断1开始') VARCHAR(5)" json:"daymodel-s2_b4_beg"`
	S2B4End    string `xorm:"comment('中断1结束') VARCHAR(5)" json:"daymodel-s2_b4_end"`
	S2B5Beg    string `xorm:"comment('中断1开始') VARCHAR(5)" json:"daymodel-s2_b5_beg"`
	S2B5End    string `xorm:"comment('中断1结束') VARCHAR(5)" json:"daymodel-s2_b5_end"`
	S3B1Beg    string `xorm:"comment('中断1开始') VARCHAR(5)" json:"daymodel-s3_b1_beg"`
	S3B1End    string `xorm:"comment('中断1结束') VARCHAR(5)" json:"daymodel-s3_b1_end"`
	S3B2Beg    string `xorm:"comment('中断1开始') VARCHAR(5)" json:"daymodel-s3_b2_beg"`
	S3B2End    string `xorm:"comment('中断1结束') VARCHAR(5)" json:"daymodel-s3_b2_end"`
	S3B3Beg    string `xorm:"comment('中断1开始') VARCHAR(5)" json:"daymodel-s3_b3_beg"`
	S3B3End    string `xorm:"comment('中断1结束') VARCHAR(5)" json:"daymodel-s3_b3_end"`
	S3B4Beg    string `xorm:"comment('中断1开始') VARCHAR(5)" json:"daymodel-s3_b4_beg"`
	S3B4End    string `xorm:"comment('中断1结束') VARCHAR(5)" json:"daymodel-s3_b4_end"`
	S3B5Beg    string `xorm:"comment('中断1开始') VARCHAR(5)" json:"daymodel-s3_b5_beg"`
	S3B5End    string `xorm:"comment('中断1结束') VARCHAR(5)" json:"daymodel-s3_b5_end"`
	Lastmodif  string `xorm:"comment('最近一次更改时间') VARCHAR(14)" json:"daymodel-lastmodif"`
	Lastuser   string `xorm:"comment('最近一次更改人') VARCHAR(20)" json:"daymodel-lastuser"`
	Credatuz   string `xorm:"comment('创建时间') VARCHAR(14)" json:"daymodel-credatuz"`
}

func (t *Daymodel) TableName() string {
	return "daymodel"
}

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

//增
func (t *Daymodel) Add() error {
	e := db.Eloquent.Master()
	countrole := new(Daymodel)
	affw, err := e.Table("daymodel").ID(core.PK{t.Finr, t.Daymodelid}).Count(countrole)
	if err != nil {
		return err
	}
	if affw > 0 {
		return errors.New("数据已经存在！")
	}
	t.S1Len, _ = utils.Timelen(t.S1End, t.S1Beg)
	t.S2Len, _ = utils.Timelen(t.S2End, t.S2Beg)
	t.S3Len, _ = utils.Timelen(t.S3End, t.S3Beg)

	s1_b1_len, _ := utils.Timelen(t.S1B1End, t.S1B1Beg)
	s1_b2_len, _ := utils.Timelen(t.S1B2End, t.S1B2Beg)
	s1_b3_len, _ := utils.Timelen(t.S1B3End, t.S1B3Beg)
	s1_b4_len, _ := utils.Timelen(t.S1B4End, t.S1B4Beg)
	s1_b5_len, _ := utils.Timelen(t.S1B5End, t.S1B5Beg)

	s2_b1_len, _ := utils.Timelen(t.S2B1End, t.S2B1Beg)
	s2_b2_len, _ := utils.Timelen(t.S2B2End, t.S2B2Beg)
	s2_b3_len, _ := utils.Timelen(t.S2B3End, t.S2B3Beg)
	s2_b4_len, _ := utils.Timelen(t.S2B4End, t.S2B4Beg)
	s2_b5_len, _ := utils.Timelen(t.S2B5End, t.S2B5Beg)

	s3_b1_len, _ := utils.Timelen(t.S3B1End, t.S3B1Beg)
	s3_b2_len, _ := utils.Timelen(t.S3B2End, t.S3B2Beg)
	s3_b3_len, _ := utils.Timelen(t.S3B3End, t.S3B3Beg)
	s3_b4_len, _ := utils.Timelen(t.S3B4End, t.S3B4Beg)
	s3_b5_len, _ := utils.Timelen(t.S3B5End, t.S3B5Beg)

	t.DmLen = (t.S1Len - s1_b1_len - s1_b2_len - s1_b3_len - s1_b4_len - s1_b5_len) + (t.S2Len - s2_b1_len - s2_b2_len - s2_b3_len - s2_b4_len - s2_b5_len) + (t.S3Len - s3_b1_len - s3_b2_len - s3_b3_len - s3_b4_len - s3_b5_len)
	_, err = e.Table("daymodel").Insert(t)
	if err != nil {
		fmt.Printf("err is :%v", err)
		return err
	}
	return nil
}

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

//改
func (t *Daymodel) Update() bool {
	e := db.Eloquent.Master()

	t.S1Len, _ = utils.Timelen(t.S1End, t.S1Beg)
	t.S2Len, _ = utils.Timelen(t.S2End, t.S2Beg)
	t.S3Len, _ = utils.Timelen(t.S3End, t.S3Beg)

	s1_b1_len, _ := utils.Timelen(t.S1B1End, t.S1B1Beg)
	s1_b2_len, _ := utils.Timelen(t.S1B2End, t.S1B2Beg)
	s1_b3_len, _ := utils.Timelen(t.S1B3End, t.S1B3Beg)
	s1_b4_len, _ := utils.Timelen(t.S1B4End, t.S1B4Beg)
	s1_b5_len, _ := utils.Timelen(t.S1B5End, t.S1B5Beg)

	s2_b1_len, _ := utils.Timelen(t.S2B1End, t.S2B1Beg)
	s2_b2_len, _ := utils.Timelen(t.S2B2End, t.S2B2Beg)
	s2_b3_len, _ := utils.Timelen(t.S2B3End, t.S2B3Beg)
	s2_b4_len, _ := utils.Timelen(t.S2B4End, t.S2B4Beg)
	s2_b5_len, _ := utils.Timelen(t.S2B5End, t.S2B5Beg)

	s3_b1_len, _ := utils.Timelen(t.S3B1End, t.S3B1Beg)
	s3_b2_len, _ := utils.Timelen(t.S3B2End, t.S3B2Beg)
	s3_b3_len, _ := utils.Timelen(t.S3B3End, t.S3B3Beg)
	s3_b4_len, _ := utils.Timelen(t.S3B4End, t.S3B4Beg)
	s3_b5_len, _ := utils.Timelen(t.S3B5End, t.S3B5Beg)

	t.DmLen = (t.S1Len - s1_b1_len - s1_b2_len - s1_b3_len - s1_b4_len - s1_b5_len) + (t.S2Len - s2_b1_len - s2_b2_len - s2_b3_len - s2_b4_len - s2_b5_len) + (t.S3Len - s3_b1_len - s3_b2_len - s3_b3_len - s3_b4_len - s3_b5_len)

	_, err := e.ID(core.PK{t.Finr, t.Daymodelid}).Update(t)
	if err != nil {
		return false
	}
	return true
}

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

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

type DayModelData struct {
	Daymodelid int    `json:"daymodelid"`
	Descr      string `json:"descr"`
	Abrev      string `json:"abrev"`
	Color      string `json:"color"`
}

func (t *Daymodel) SelectArr() ([]DayModelData, error) {
	e := db.Eloquent.Master()
	var data []DayModelData
	err := e.Table("daymodel").Where("finr = ?", t.Finr).Find(&data)
	if err != nil {
		return data, err
	}
	return data, nil
}
