LAPP 打印服务 支持条码打印和表单打印 通过Socket或windows打印方式
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

145 lines
4.7 KiB

3 years ago
3 years ago
3 years ago
  1. package db
  2. import (
  3. "errors"
  4. "fmt"
  5. "LAPP_PRN_Service/common"
  6. conf "LAPP_PRN_Service/config"
  7. "LAPP_PRN_Service/glog"
  8. "strings"
  9. "xorm.io/core"
  10. )
  11. /******************************************************************************
  12. *
  13. * @Struct Name : Printer
  14. *-----------------------------------------------------------------------------
  15. *
  16. * @Description : Printer的实体映射
  17. *
  18. * @Author : lou wenzhi
  19. *
  20. * @Date : 2021-04-13 13:08:46
  21. *
  22. ******************************************************************************/
  23. type Printer struct {
  24. PlantNr int `xorm:"pk int 'PlantNr'" json:"Printer-PlantNr"`
  25. PrinterId string `xorm:"pk varchar(18) 'PrinterId'" json:"Printer-PrinterId"`
  26. Descr string `xorm:"varchar(30) 'Descr' not null" json:"Printer-Descr"`
  27. PrinterType string `xorm:"varchar(1) 'PrinterType' not null" json:"Printer-PrinterType"`
  28. PrinterName string `xorm:"varchar(100) 'PrinterName' not null" json:"Printer-PrinterName"`
  29. IpAddress string `xorm:"varchar(15) 'IpAddress' not null" json:"Printer-IpAddress"`
  30. SerialName string `xorm:"varchar(15) 'SerialName' not null" json:"Printer-SerialName"`
  31. SerialPort int `xorm:"int 'SerialPort' not null" json:"Printer-SerialPort"`
  32. Active int `xorm:"smallint 'Active' not null" json:"Printer-Active"`
  33. PrinterSpec string `xorm:"varchar(30) 'PrinterSpec' not null" json:"Printer-PrinterSpec"`
  34. PageSize string `xorm:"varchar(18) 'PageSize' not null" json:"Printer-PageSize"`
  35. Port int `xorm:"int 'Port' not null" json:"Printer-Port"`
  36. PrintInterval int `xorm:"int 'PrintInterval' not null" json:"Printer-PrintInterval"`
  37. LastModify common.DateTime `xorm:"datetime 'LastModify' not null updated" json:"Printer-LastModify"`
  38. LastUser string `xorm:"nvarchar(20) 'LastUser' not null" json:"Printer-LastUser"`
  39. CreateTime common.DateTime `xorm:"datetime 'CreateTime' not null created" json:"Printer-CreateTime"`
  40. }
  41. func (t *Printer) TableName() string {
  42. return "Printer"
  43. }
  44. //增
  45. func (t *Printer) Add() error {
  46. e := G_DbEngine
  47. countrole := new(Printer)
  48. affw, err := e.Table("Printer").ID(core.PK{t.PlantNr, t.PrinterId}).Count(countrole)
  49. if err != nil {
  50. return err
  51. }
  52. if affw > 0 {
  53. return errors.New("数据已经存在!")
  54. }
  55. _, err = e.Table("Printer").Insert(t)
  56. if err != nil {
  57. fmt.Printf("err is :%v", err)
  58. return err
  59. }
  60. return nil
  61. }
  62. //删
  63. func (t *Printer) Del() bool {
  64. e := G_DbEngine
  65. _, err := e.ID(core.PK{t.PlantNr, t.PrinterId}).Delete(&Printer{})
  66. if err != nil {
  67. return false
  68. }
  69. return true
  70. }
  71. //改
  72. func (t *Printer) Update() bool {
  73. e := G_DbEngine
  74. _, 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)
  75. if err != nil {
  76. return false
  77. }
  78. return true
  79. }
  80. //查
  81. func (t *Printer) SelectOne() (Printer, error) {
  82. e := G_DbEngine
  83. var data Printer
  84. _, err := e.ID(core.PK{t.PlantNr, t.PrinterId}).Get(&data)
  85. if err != nil {
  86. return data, err
  87. }
  88. return data, nil
  89. }
  90. // 获取所有激活的打印机对象列表
  91. func (t *Printer) GetPrinterList(conf *conf.EnvConfig) ([]Printer, error) {
  92. var (
  93. printertablist []Printer
  94. pt []string
  95. printers []string
  96. )
  97. e := G_DbEngine
  98. pt = strings.Split(conf.PrinterType, ",")
  99. printers = strings.Split(conf.Printers, ",")
  100. if len(printers) > 0 {
  101. err := e.Where("PlantNr = ? and Active = ?", conf.PlantNr, 1).In("PrinterId", printers).In("PrinterType", pt).Find(&printertablist)
  102. if err != nil {
  103. glog.Infoln("failed to query Printer for type: %s due to: %v", pt, err)
  104. return nil, err
  105. }
  106. } else {
  107. err := e.Where("PlantNr = ? and Active = ?", conf.PlantNr, 1).In("PrinterType", pt).Find(&printertablist)
  108. if err != nil {
  109. glog.Infoln("failed to query Printer for type: %s due to: %v", pt, err)
  110. return nil, err
  111. }
  112. }
  113. return printertablist, nil
  114. }
  115. // 获取打印机ID列表
  116. func (t *Printer) GetPrinterIdList(conf *conf.EnvConfig) ([]string, error) {
  117. var (
  118. printertablist []Printer
  119. prnlist, printers, pt []string
  120. )
  121. e := G_DbEngine
  122. pt = strings.Split(conf.PrinterType, ",")
  123. printers = strings.Split(conf.Printers, ",")
  124. err := e.Where("PlantNr = ? and Active = ?", conf.PlantNr, 1).In("PrinterId", printers).In("PrinterType", pt).Find(&printertablist)
  125. if err != nil {
  126. glog.Infoln("failed to query printertab for type: due to:", pt, err)
  127. return nil, err
  128. }
  129. for i := range printertablist {
  130. prnlist = append(prnlist, strings.TrimSpace(printertablist[i].PrinterId))
  131. }
  132. return prnlist, nil
  133. }