|
|
- package db
-
- import (
- "errors"
- "fmt"
- "LAPP_PRN_Service/common"
- conf "LAPP_PRN_Service/config"
- "LAPP_PRN_Service/glog"
- "strings"
- "xorm.io/core"
- )
-
- /******************************************************************************
- *
- * @Struct Name : Printer
- *-----------------------------------------------------------------------------
- *
- * @Description : Printer的实体映射
- *
- * @Author : lou wenzhi
- *
- * @Date : 2021-04-13 13:08:46
- *
- ******************************************************************************/
- type Printer struct {
- PlantNr int `xorm:"pk int 'PlantNr'" json:"Printer-PlantNr"`
- PrinterId string `xorm:"pk varchar(18) 'PrinterId'" json:"Printer-PrinterId"`
- Descr string `xorm:"varchar(30) 'Descr' not null" json:"Printer-Descr"`
- PrinterType string `xorm:"varchar(1) 'PrinterType' not null" json:"Printer-PrinterType"`
- PrinterName string `xorm:"varchar(100) 'PrinterName' not null" json:"Printer-PrinterName"`
- IpAddress string `xorm:"varchar(15) 'IpAddress' not null" json:"Printer-IpAddress"`
- SerialName string `xorm:"varchar(15) 'SerialName' not null" json:"Printer-SerialName"`
- SerialPort int `xorm:"int 'SerialPort' not null" json:"Printer-SerialPort"`
- Active int `xorm:"smallint 'Active' not null" json:"Printer-Active"`
- PrinterSpec string `xorm:"varchar(30) 'PrinterSpec' not null" json:"Printer-PrinterSpec"`
- PageSize string `xorm:"varchar(18) 'PageSize' not null" json:"Printer-PageSize"`
- Port int `xorm:"int 'Port' not null" json:"Printer-Port"`
- PrintInterval int `xorm:"int 'PrintInterval' not null" json:"Printer-PrintInterval"`
- LastModify common.DateTime `xorm:"datetime 'LastModify' not null updated" json:"Printer-LastModify"`
- LastUser string `xorm:"nvarchar(20) 'LastUser' not null" json:"Printer-LastUser"`
- CreateTime common.DateTime `xorm:"datetime 'CreateTime' not null created" json:"Printer-CreateTime"`
- }
-
- func (t *Printer) TableName() string {
- return "Printer"
- }
-
- //增
- func (t *Printer) Add() error {
- e := G_DbEngine
- countrole := new(Printer)
- affw, err := e.Table("Printer").ID(core.PK{t.PlantNr, t.PrinterId}).Count(countrole)
- if err != nil {
- return err
- }
- if affw > 0 {
- return errors.New("数据已经存在!")
- }
- _, err = e.Table("Printer").Insert(t)
- if err != nil {
- fmt.Printf("err is :%v", err)
- return err
- }
- return nil
- }
-
- //删
- func (t *Printer) Del() bool {
- e := G_DbEngine
- _, err := e.ID(core.PK{t.PlantNr, t.PrinterId}).Delete(&Printer{})
- if err != nil {
- return false
- }
- return true
- }
-
- //改
- func (t *Printer) Update() bool {
- e := G_DbEngine
- _, err := e.ID(core.PK{t.PlantNr, t.PrinterId}).Cols("PlantNr", "PrinterId", "Descr", "PrinterType", "PrinterName", "IpAddress", "Active", "PrinterSpec", "PageSize", "Port", "PrintInterval", "LastModify", "LastUser", "CreateTime").Update(t)
- if err != nil {
- return false
- }
- return true
- }
-
- //查
- func (t *Printer) SelectOne() (Printer, error) {
- e := G_DbEngine
- var data Printer
- _, err := e.ID(core.PK{t.PlantNr, t.PrinterId}).Get(&data)
- if err != nil {
- return data, err
- }
- return data, nil
- }
-
- // 获取所有激活的打印机对象列表
- func (t *Printer) GetPrinterList(conf *conf.EnvConfig) ([]Printer, error) {
-
- var (
- printertablist []Printer
- pt []string
- printers []string
- )
- e := G_DbEngine
- pt = strings.Split(conf.PrinterType, ",")
- printers = strings.Split(conf.Printers, ",")
- if len(printers) > 0 {
- err := e.Where("PlantNr = ? and Active = ?", conf.PlantNr, 1).In("PrinterId", printers).In("PrinterType", pt).Find(&printertablist)
- if err != nil {
- glog.Infoln("failed to query Printer for type: %s due to: %v", pt, err)
- return nil, err
- }
- } else {
- err := e.Where("PlantNr = ? and Active = ?", conf.PlantNr, 1).In("PrinterType", pt).Find(&printertablist)
- if err != nil {
- glog.Infoln("failed to query Printer for type: %s due to: %v", pt, err)
- return nil, err
- }
- }
-
- return printertablist, nil
- }
-
- // 获取打印机ID列表
- func (t *Printer) GetPrinterIdList(conf *conf.EnvConfig) ([]string, error) {
- var (
- printertablist []Printer
- prnlist, printers, pt []string
- )
- e := G_DbEngine
- pt = strings.Split(conf.PrinterType, ",")
- printers = strings.Split(conf.Printers, ",")
-
- err := e.Where("PlantNr = ? and Active = ?", conf.PlantNr, 1).In("PrinterId", printers).In("PrinterType", pt).Find(&printertablist)
- if err != nil {
- glog.Infoln("failed to query printertab for type: due to:", pt, err)
- return nil, err
- }
- for i := range printertablist {
- prnlist = append(prnlist, strings.TrimSpace(printertablist[i].PrinterId))
- }
- return prnlist, nil
- }
|