package db
|
|
|
|
import (
|
|
"bufio"
|
|
"github.com/360EntSecGroup-Skylar/excelize/v2"
|
|
"leit.com/leit_seat_aps/common"
|
|
conf "leit.com/leit_seat_aps/config"
|
|
"leit.com/leit_seat_aps/glog"
|
|
"os"
|
|
"path"
|
|
"path/filepath"
|
|
"strings"
|
|
"time"
|
|
"xorm.io/core"
|
|
)
|
|
|
|
/******备注:打印服务数据放到从库***************/
|
|
type PrintTask struct {
|
|
TaskId string
|
|
TaskFile string // 保存打印文件的文件路径
|
|
PrinterId string
|
|
Printhead Printheadtab
|
|
PrintdetailList []Printdetailtab
|
|
}
|
|
|
|
type ByteFile struct {
|
|
ByteLine []byte
|
|
}
|
|
|
|
// 基于模板将打印任务转换成字节码输出
|
|
func (t *PrintTask) GenBytesFile(conf *conf.EnvConfig) ([]ByteFile, error) {
|
|
TemplatePath := common.EnsureDir(conf.TemplatePath)
|
|
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 []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 ByteFile
|
|
r.ByteLine = []byte(linetext)
|
|
results = append(results, r)
|
|
}
|
|
|
|
return results, nil
|
|
}
|
|
|
|
// 基于模板将打印任务转换成打印文件输出
|
|
func (t *PrintTask) GenPrintFile(conf *conf.EnvConfig) error {
|
|
TemplatePath := common.EnsureDir(conf.TemplatePath)
|
|
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.Varpos, -1)
|
|
}
|
|
results = append(results, linetext)
|
|
}
|
|
|
|
// 输出替换内容行到新建的打印文件
|
|
fileext := path.Ext(templatefile)
|
|
printfile := t.TaskId + fileext
|
|
inbox := common.EnsureDir(conf.Inbox)
|
|
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 {
|
|
_, err := w.WriteString(linetext)
|
|
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 := common.EnsureDir(conf.TemplatePath)
|
|
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 {
|
|
// 替换单元格对应的内容
|
|
if pd.Varname == "零件数量" || pd.Varname == "客户排序号" || pd.Varname == "客户订单SEQ号" || pd.Varname == "SEQ号" || pd.Varname == "oemseq" {
|
|
if !common.ValueIsEmpty(pd.Varvalue) {
|
|
xlFile.SetCellValue("Sheet1", pd.Varpos, common.ValueToInt(pd.Varvalue, 0))
|
|
}
|
|
} else if pd.Varname == "RSUM" {
|
|
result, _ := xlFile.CalcCellValue("Sheet1", pd.Varpos)
|
|
xlFile.SetCellValue("Sheet1", pd.Varpos, common.ValueToInt(result, 0))
|
|
} else {
|
|
xlFile.SetCellValue("Sheet1", pd.Varpos, pd.Varvalue)
|
|
}
|
|
}
|
|
|
|
// 输出替换内容行到新建的Excel文件
|
|
fileext := path.Ext(templatefile)
|
|
printfile := t.TaskId + fileext
|
|
inbox := common.EnsureDir(conf.Inbox)
|
|
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 []Printheadtab
|
|
err error
|
|
)
|
|
e := G_DbEngine
|
|
|
|
Msgtypes := strings.Split(conf.Msgtype, ",")
|
|
|
|
if len(Msgtypes) == 0{
|
|
Msgtypes = []string{"BBAASN"}
|
|
}
|
|
// 获取所有开口的打印任务头
|
|
if len(prnlist) > 0 {
|
|
err = e.Where("finr = ?", conf.Finr).In("status", stalist).In("printerid", prnlist).In("msgtype", Msgtypes).OrderBy("printheadid").Find(&printheadlist)
|
|
} else {
|
|
err = e.Where("finr = ?", conf.Finr).In("status", stalist).OrderBy("printheadid").In("msgtype", Msgtypes).Find(&printheadlist)
|
|
}
|
|
if err != nil {
|
|
glog.Infoln("failed to query printheadtab due to: ", err)
|
|
return nil, err
|
|
}
|
|
// 遍历打印任务头
|
|
for _, p := range printheadlist {
|
|
var t PrintTask
|
|
t.Printhead = p
|
|
t.Printhead.Clipped()
|
|
t.TaskId = t.Printhead.Printheadid
|
|
t.PrinterId = t.Printhead.Printerid
|
|
|
|
var pdlist []Printdetailtab
|
|
err := e.Where("finr = ? and printheadid = ?", conf.Finr, 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 {
|
|
pd.Clipped()
|
|
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.Lastmodif = time.Now().Format("20060102150405")
|
|
_, err := e.Id(core.PK{task.Printhead.Finr, task.Printhead.Printheadid}).Cols("status", "lastmodif").Update(&task.Printhead)
|
|
if err != nil {
|
|
glog.Infoln("failed to change printerheadtab status for printheadid:", task.Printhead.Printheadid)
|
|
return err
|
|
}
|
|
return nil
|
|
}
|