package models

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

type Printertab struct {
	Finr          int    `xorm:"not null pk comment('车间编号') INT(0)" json:"printertab-finr"`
	Printerid     string `xorm:"not null pk comment('打印机id') VARCHAR(18)" json:"printertab-printerid"`
	Descr         string `xorm:"not null comment('描述') VARCHAR(30)" json:"printertab-descr"`
	Printertype   string `xorm:"not null comment('打印机类型') VARCHAR(1)" json:"printertab-printertype"`
	Printername   string `xorm:"not null comment('打印机名称') VARCHAR(100)" json:"printertab-printername"`
	Ipaddress     string `xorm:"not null comment('IP地址') VARCHAR(15)" json:"printertab-ipaddress"`
	Active        int    `xorm:"not null comment('激活') INT(0)" json:"printertab-active"`
	Printerspec   string `xorm:"not null comment('打印机规格') VARCHAR(30)" json:"printertab-printerspec"`
	Pagesize      string `xorm:"not null comment('页面尺寸') VARCHAR(18)" json:"printertab-pagesize"`
	Port          int    `xorm:"not null comment('端口号') INT(0)" json:"printertab-port"`
	Printinterval int    `xorm:"not null comment('打印机间隔时间(秒)') INT(0)" json:"printertab-printinterval"`
	Lastmodif     string `xorm:"not null comment('上一次更改日期') VARCHAR(14)" json:"printertab-lastmodif"`
	Lastuser      string `xorm:"not null comment('最后编辑人员') VARCHAR(20)" json:"printertab-lastuser"`
	Credatuz      string `xorm:"not null comment('创建时间') VARCHAR(14)" json:"printertab-credatuz"`
}

func (t *Printertab) TableName() string {
	return "printertab"
}

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

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

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

//改
func (t *Printertab) Update() bool {
	e := db.Eloquent.Master()
	_, err := e.ID(core.PK{t.Finr, t.Printerid}).Cols("finr","printerid","descr","printertype","printername","ipaddress","active","printerspec","pagesize","port","printinterval","lastmodif","lastuser","credatuz").Update(t)
	if err != nil {
		return false
	}
	return true
}

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

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

// 获取所有激活的打印机对象列表
func (t *Printertab) GetPrinterList(conf *conf.EnvConfig) ( []Printertab, error ) {

	var (
		printertablist []Printertab
		pt []string
	)
	e := db.Eloquent.Master()
	pt = strings.Split(conf.PrinterType, ",")

	err := e.Where("finr = ? and active = ?", conf.Finr, 1).In("printertype",pt).Find(&printertablist)
	if err != nil{
		log.Printf("failed to query printertab for type: %s due to: %v", pt, err)
		return nil, err
	}
	for i := range printertablist {
		printertablist[i].Clipped()
	}
	return printertablist, nil
}

// 获取打印机ID列表
func (t *Printertab) GetPrinterIdList(conf *conf.EnvConfig) ( []string, error ) {
	var (
		printertablist []Printertab
		prnlist, pt []string
	)
	e := db.Eloquent.Master()
	pt = strings.Split(conf.PrinterType, ",")

	err := e.Where("finr = ? and active = ?", conf.Finr, 1).In("printertype",pt).Find(&printertablist)
	if err != nil{
		log.Printf("failed to query printertab for type: %s due to: %v", pt, err)
		return nil, err
	}
	for i := range printertablist {
		prnlist = append(prnlist, strings.TrimSpace( printertablist[i].Printerid ))
	}
	return prnlist, nil
}

