using DS.WMS.PrintApi.Model; using DS.WMS.PrintApi.Utils; using FastReport.Data.JsonConnection; using FastReport.Export.OoXML; using FastReport.Export.Pdf; using System.IO; using System.Text; using System; using System.Threading.Tasks; namespace DS.WMS.PrintApi.Service { public class OpenPrintService : IOpenPrintService { public async Task GetOpenJsonPrintInfo(OpenJsonPrintReq req) { try { using (var db = SqlSugarUtil.GetInstance()) { var template = db.Queryable().Filter(null,true).Where(x => x.Id == req.TemplateId).First(); if (template == null) { return await Task.FromResult(PrintDataResult.Failed("打印模板不存在!")); } if (template.IsUseDataSource) { return await Task.FromResult(PrintDataResult.Failed("非Json打印接口!")); } if (String.IsNullOrEmpty(template.PrintJsonContent)) { return await Task.FromResult(PrintDataResult.Failed("打印模板内容不能为空!")); } try { var basePath = String.Empty; var savePath = "wwwroot/PrintTempFile"; var fileName = DateTime.Now.Ticks + "-" + NumUtil.GetRandomString(3); var printFileName = $"{fileName}.frx"; var printFile = Path.Combine(savePath, printFileName); //写入CRX文件 using (FileStream fs = new FileStream(printFile, FileMode.Create)) { Byte[] info = new UTF8Encoding(true).GetBytes(template.PrintJsonContent); fs.Write(info, 0, info.Length); } //生成报表 FastReport.Report report = new FastReport.Report(); report.Load(printFile); var dataSource = report.Dictionary.Connections[0] as JsonDataSourceConnection; var str = new FastReport.Data.JsonConnection.JsonDataSourceConnectionStringBuilder(); str.Json = req.JsonDataStr; dataSource.ConnectionString = str.ConnectionString; report.Prepare(); var printName = string.Empty; var saveFile = string.Empty; //var saveFile = Path.Combine(savePath, printName); if (req.PrintType == "1") { printName = $"{fileName}.pdf"; saveFile = Path.Combine(savePath, printName); PDFExport pdfExport = new PDFExport(); pdfExport.Export(report, saveFile); } else if (req.PrintType == "2") { printName = $"{fileName}.xlsx"; saveFile = Path.Combine(savePath, printName); Excel2007Export excelExport = new Excel2007Export(); excelExport.Export(report, saveFile); } else if (req.PrintType == "3") { printName = $"{fileName}.doc"; saveFile = Path.Combine(savePath, printName); Word2007Export wordExport = new Word2007Export(); wordExport.Export(report, saveFile); } else { return await Task.FromResult(PrintDataResult.Failed("非法打印格式!")); } return await Task.FromResult(PrintDataResult.OK(printName)); } catch (Exception e) { return await Task.FromResult(PrintDataResult.Failed(e.Message)); } } } catch (Exception ex) { //Logger.Warn(ex, "方法: GetOpenJsonPrintInfo"); return await Task.FromResult(PrintDataResult.Failed(ex.Message)); } } public async Task GetOpenJsonPrintInfoByTemplateCode(OpenJsonPrintByCodeReq req) { try { using (var db = SqlSugarUtil.GetInstance()) { var template = db.Queryable().Filter(null, true).Where(x => x.TemplateCode == req.Code && x.TenantId == req.TenantId).First(); if (template == null) { return await Task.FromResult(PrintDataResult.Failed("打印模板编码:["+ req.Code+"]的打印格式不存在!")); } if (template.IsUseDataSource) { return await Task.FromResult(PrintDataResult.Failed("非Json打印接口!")); } if (String.IsNullOrEmpty(template.PrintJsonContent)) { return await Task.FromResult(PrintDataResult.Failed("打印模板内容不能为空!")); } try { var basePath = String.Empty; var savePath = "wwwroot/PrintTempFile"; var fileName = DateTime.Now.Ticks + "-" + NumUtil.GetRandomString(3); var printFileName = $"{fileName}.frx"; var printFile = Path.Combine(savePath, printFileName); //写入CRX文件 using (FileStream fs = new FileStream(printFile, FileMode.Create)) { Byte[] info = new UTF8Encoding(true).GetBytes(template.PrintJsonContent); fs.Write(info, 0, info.Length); } //生成报表 FastReport.Report report = new FastReport.Report(); report.Load(printFile); var dataSource = report.Dictionary.Connections[0] as JsonDataSourceConnection; var str = new FastReport.Data.JsonConnection.JsonDataSourceConnectionStringBuilder(); str.Json = req.JsonDataStr; dataSource.ConnectionString = str.ConnectionString; report.Prepare(); var printName = string.Empty; var saveFile = string.Empty; //var saveFile = Path.Combine(savePath, printName); if (req.PrintType == "1") { printName = $"{fileName}.pdf"; saveFile = Path.Combine(savePath, printName); PDFExport pdfExport = new PDFExport(); pdfExport.Export(report, saveFile); #region 处理托书挂载附件 if (template.TemplateCode == "bill_of_lading") { System.IO.FileStream file = new System.IO.FileStream(saveFile, FileMode.Open, FileAccess.Read); var movePath =Path.Combine(AppSetting.Configuration["FileSettings:MovePath"], printName); using (var fileStream = File.Create(movePath)) { await file.CopyToAsync(fileStream); } file.Close(); } #endregion } else if (req.PrintType == "2") { printName = $"{fileName}.xlsx"; saveFile = Path.Combine(savePath, printName); Excel2007Export excelExport = new Excel2007Export(); excelExport.Export(report, saveFile); } else if (req.PrintType == "3") { printName = $"{fileName}.doc"; saveFile = Path.Combine(savePath, printName); Word2007Export wordExport = new Word2007Export(); wordExport.Export(report, saveFile); } else { return await Task.FromResult(PrintDataResult.Failed("非法打印格式!")); } return await Task.FromResult(PrintDataResult.OK(printName)); } catch (Exception e) { return await Task.FromResult(PrintDataResult.Failed(e.Message)); } } } catch (Exception ex) { //Logger.Warn(ex, "方法: GetOpenJsonPrintInfo"); return await Task.FromResult(PrintDataResult.Failed(ex.Message)); } } } }