|
|
@ -25,7 +25,11 @@ type PrintTask struct { |
|
|
|
|
|
|
|
// 基于模板将打印任务转换成字节码输出
|
|
|
|
func (t *PrintTask) GenBytesFile(conf *conf.EnvConfig) ([]common.ByteFile, error) { |
|
|
|
TemplatePath := common.EnsureDir(conf.TemplatePath) |
|
|
|
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() |
|
|
@ -51,9 +55,44 @@ func (t *PrintTask) GenBytesFile(conf *conf.EnvConfig) ([]common.ByteFile, error |
|
|
|
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 := common.EnsureDir(conf.TemplatePath) |
|
|
|
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() |
|
|
@ -78,7 +117,11 @@ func (t *PrintTask) GenPrintFile(conf *conf.EnvConfig) error { |
|
|
|
// 输出替换内容行到新建的打印文件
|
|
|
|
fileext := path.Ext(templatefile) |
|
|
|
printfile := t.TaskId + fileext |
|
|
|
inbox := common.EnsureDir(conf.Inbox) |
|
|
|
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() |
|
|
@ -88,7 +131,12 @@ func (t *PrintTask) GenPrintFile(conf *conf.EnvConfig) error { |
|
|
|
} |
|
|
|
w := bufio.NewWriter(fn) |
|
|
|
for _, linetext := range results { |
|
|
|
_, err := w.WriteString(linetext+ "\r\n") |
|
|
|
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 |
|
|
@ -106,7 +154,11 @@ func (t *PrintTask) GenExcelFile(conf *conf.EnvConfig) error { |
|
|
|
xlFile *excelize.File |
|
|
|
err error |
|
|
|
) |
|
|
|
TemplatePath := common.EnsureDir(conf.TemplatePath) |
|
|
|
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 { |
|
|
@ -123,7 +175,11 @@ func (t *PrintTask) GenExcelFile(conf *conf.EnvConfig) error { |
|
|
|
// 输出替换内容行到新建的Excel文件
|
|
|
|
fileext := path.Ext(templatefile) |
|
|
|
printfile := t.TaskId + fileext |
|
|
|
inbox := common.EnsureDir(conf.Inbox) |
|
|
|
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 { |
|
|
|