package db
|
|
|
|
import (
|
|
"LAPP_PRN_Service/common"
|
|
conf "LAPP_PRN_Service/config"
|
|
"LAPP_PRN_Service/glog"
|
|
"bufio"
|
|
"github.com/360EntSecGroup-Skylar/excelize/v2"
|
|
"os"
|
|
"path"
|
|
"path/filepath"
|
|
"strings"
|
|
"time"
|
|
"xorm.io/core"
|
|
)
|
|
|
|
/******备注:打印服务数据放到从库***************/
|
|
type PrintTask struct {
|
|
TaskId string
|
|
TaskFile string // 保存打印文件的文件路径
|
|
PrinterId string
|
|
PrintHead PrintHead
|
|
PrintDetailList []PrintDetail
|
|
}
|
|
|
|
// 基于模板将打印任务转换成字节码输出
|
|
func (t *PrintTask) GenBytesFile(conf *conf.EnvConfig) ([]common.ByteFile, error) {
|
|
TemplatePath,err := common.GetCurrentPath(conf.TemplatePath)
|
|
if err != nil {
|
|
glog.Infoln("failed to open template file: due to: ", err)
|
|
return nil, err
|
|
}
|
|
templatefile := filepath.Join(TemplatePath, t.PrintHead.TemplateFile)
|
|
f, err := os.Open(templatefile)
|
|
defer f.Close()
|
|
|
|
if err != nil {
|
|
glog.Infoln("failed to open template file: due to: ", templatefile, err)
|
|
return nil, err
|
|
}
|
|
|
|
var results []common.ByteFile
|
|
scanner := bufio.NewScanner(f)
|
|
for scanner.Scan() {
|
|
linetext := scanner.Text()
|
|
for _, pd := range t.PrintDetailList {
|
|
// -1 替换所有; 1 替换第一个; 5 替换前5个
|
|
linetext = strings.Replace(linetext, pd.VarName, pd.VarValue, -1)
|
|
}
|
|
var r common.ByteFile
|
|
r.ByteLine = []byte(linetext)
|
|
results = append(results, r)
|
|
}
|
|
|
|
return results, nil
|
|
}
|
|
|
|
// 基于模板将打印任务转换成字节码输出
|
|
func (t *PrintTask) GenStrsFile(conf *conf.EnvConfig) (string, error) {
|
|
TemplatePath,err := common.GetCurrentPath(conf.TemplatePath)
|
|
if err != nil {
|
|
glog.Infoln("failed to open template file: due to: ", err)
|
|
return "", err
|
|
}
|
|
templatefile := filepath.Join(TemplatePath, t.PrintHead.TemplateFile)
|
|
|
|
f, err := os.Open(templatefile)
|
|
defer f.Close()
|
|
|
|
if err != nil {
|
|
glog.Infoln("failed to open template file: due to: ", templatefile, err)
|
|
return "", err
|
|
}
|
|
|
|
var results string
|
|
scanner := bufio.NewScanner(f)
|
|
for scanner.Scan() {
|
|
linetext := scanner.Text()
|
|
for _, pd := range t.PrintDetailList {
|
|
// -1 替换所有; 1 替换第一个; 5 替换前5个
|
|
linetext = strings.Replace(linetext, pd.VarName, pd.VarValue, -1)
|
|
}
|
|
results += linetext
|
|
}
|
|
|
|
return results, nil
|
|
}
|
|
|
|
// 基于模板将打印任务转换成打印文件输出
|
|
func (t *PrintTask) GenPrintFile(conf *conf.EnvConfig) error {
|
|
TemplatePath,err := common.GetCurrentPath(conf.TemplatePath)
|
|
if err != nil {
|
|
glog.Infoln("failed to open template file: due to: ", err)
|
|
return err
|
|
}
|
|
templatefile := filepath.Join(TemplatePath, t.PrintHead.TemplateFile)
|
|
f, err := os.Open(templatefile)
|
|
defer f.Close()
|
|
|
|
if err != nil {
|
|
glog.Infoln("GenPrintFile: failed to open template file: due to:", templatefile, err)
|
|
return err
|
|
}
|
|
|
|
// 获取替换文件的内容行
|
|
var results []string
|
|
scanner := bufio.NewScanner(f)
|
|
for scanner.Scan() {
|
|
linetext := scanner.Text()
|
|
for _, pd := range t.PrintDetailList {
|
|
// -1 替换所有; 1 替换第一个; 5 替换前5个
|
|
linetext = strings.Replace(linetext, pd.VarName, pd.VarValue, -1)
|
|
}
|
|
results = append(results, linetext)
|
|
}
|
|
|
|
// 输出替换内容行到新建的打印文件
|
|
fileext := path.Ext(templatefile)
|
|
printfile := t.TaskId + fileext
|
|
inbox,err := common.GetCurrentPath(conf.Inbox)
|
|
if err != nil {
|
|
glog.Infoln("GenPrintFile: failed to create print file for task: due to: ", t.TaskId, err)
|
|
return err
|
|
}
|
|
t.TaskFile = filepath.Join(inbox, printfile)
|
|
fn, err := os.Create(t.TaskFile)
|
|
defer fn.Close()
|
|
if err != nil {
|
|
glog.Infoln("GenPrintFile: failed to create print file for task: due to: ", t.TaskId, err)
|
|
return err
|
|
}
|
|
w := bufio.NewWriter(fn)
|
|
for _, linetext := range results {
|
|
GBKStr,err := common.Utf8ToGbk([]byte(linetext))
|
|
if err != nil {
|
|
glog.InfoExtln("Printer", "Failed to Utf8ToGbk due to: ", err)
|
|
return err
|
|
}
|
|
_, err = w.WriteString(string(GBKStr)+ "\r\n")
|
|
if err != nil {
|
|
glog.Infoln("GenPrintFile: failed to write print file for task: due to: ", t.TaskId, err)
|
|
return err
|
|
}
|
|
}
|
|
w.Flush()
|
|
|
|
return nil
|
|
}
|
|
|
|
// 基于模板将打印任务转换成Excel文件输出
|
|
func (t *PrintTask) GenExcelFile(conf *conf.EnvConfig) error {
|
|
var (
|
|
templatefile string
|
|
xlFile *excelize.File
|
|
err error
|
|
)
|
|
TemplatePath,err := common.GetCurrentPath(conf.TemplatePath)
|
|
if err != nil {
|
|
glog.Infoln("failed to open template file: due to: ", err)
|
|
return err
|
|
}
|
|
templatefile = filepath.Join(TemplatePath, t.PrintHead.TemplateFile)
|
|
xlFile, err = excelize.OpenFile(templatefile)
|
|
if err != nil {
|
|
glog.Infoln("Failed to open the template file: due to: ", templatefile, err)
|
|
return err
|
|
}
|
|
|
|
// 获取替换文件的内容行并替换Excel表格中的内容
|
|
for _, pd := range t.PrintDetailList {
|
|
// 替换单元格对应的内容
|
|
xlFile.SetCellValue("Sheet1", pd.VarPos, pd.VarValue)
|
|
}
|
|
|
|
// 输出替换内容行到新建的Excel文件
|
|
fileext := path.Ext(templatefile)
|
|
printfile := t.TaskId + fileext
|
|
inbox,err := common.GetCurrentPath(conf.Inbox)
|
|
if err != nil {
|
|
glog.Infoln("Failed to open the template file: due to: ", templatefile, err)
|
|
return err
|
|
}
|
|
t.TaskFile = filepath.Join(inbox, printfile)
|
|
err = xlFile.SaveAs(t.TaskFile)
|
|
if err != nil {
|
|
glog.Infoln("GenExcelFile: failed to create excel file for task: due to: ", t.TaskId, err)
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// 获取打印任务
|
|
func (t *PrintTask) GetPrintTaskList(stalist []string, prnlist []string, conf *conf.EnvConfig) ([]PrintTask, error) {
|
|
var (
|
|
tasklist []PrintTask
|
|
printheadlist []PrintHead
|
|
err error
|
|
)
|
|
e := G_DbEngine
|
|
// 获取所有开口的打印任务头
|
|
if len(prnlist) > 0 {
|
|
err = e.Where("PlantNr = ?", conf.PlantNr).In("Status", stalist).In("PrinterId", prnlist).OrderBy("PrintHeadId").Find(&printheadlist)
|
|
} else {
|
|
err = e.Where("PlantNr = ?", conf.PlantNr).In("Status", stalist).OrderBy("PrintHeadId").Find(&printheadlist)
|
|
}
|
|
if err != nil {
|
|
glog.Infoln("failed to query PrintHead due to: ", err)
|
|
return nil, err
|
|
}
|
|
// 遍历打印任务头
|
|
for _, p := range printheadlist {
|
|
var t PrintTask
|
|
t.PrintHead = p
|
|
t.TaskId = t.PrintHead.PrintHeadId
|
|
t.PrinterId = t.PrintHead.PrinterId
|
|
|
|
var pdlist []PrintDetail
|
|
err := e.Where("PlantNr = ? and PrintHeadId = ?", conf.PlantNr, t.PrintHead.PrintHeadId).OrderBy("Pos").Find(&pdlist)
|
|
if err != nil {
|
|
glog.Infoln("failed to query printdetailtab due to: ", err)
|
|
return nil, err
|
|
}
|
|
for _, pd := range pdlist {
|
|
t.PrintDetailList = append(t.PrintDetailList, pd)
|
|
}
|
|
|
|
tasklist = append(tasklist, t)
|
|
}
|
|
|
|
return tasklist, nil
|
|
}
|
|
|
|
// 关闭打印任务
|
|
func (t *PrintTask) ChangePrintTaskStatus(task PrintTask, newStatus string) error {
|
|
e := G_DbEngine
|
|
task.PrintHead.Status = newStatus
|
|
task.PrintHead.LastModify = common.DateTime(time.Now())
|
|
_, err := e.Id(core.PK{task.PrintHead.PlantNr, task.PrintHead.PrintHeadId}).Cols("Status", "LastModify").Update(&task.PrintHead)
|
|
if err != nil {
|
|
glog.Infoln("failed to change PrinterHead status for PrintHeadId:", task.PrintHead.PrintHeadId)
|
|
return err
|
|
}
|
|
return nil
|
|
}
|