diff --git a/ds-wms-service/DS.WMS.PrintApi/Controllers/OpenPrintController.cs b/ds-wms-service/DS.WMS.PrintApi/Controllers/OpenPrintController.cs index d9721a96..3aa5653a 100644 --- a/ds-wms-service/DS.WMS.PrintApi/Controllers/OpenPrintController.cs +++ b/ds-wms-service/DS.WMS.PrintApi/Controllers/OpenPrintController.cs @@ -1,7 +1,10 @@ using DS.WMS.PrintApi.Model; using DS.WMS.PrintApi.Service; using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; +using System.IO; +using System; using System.Threading.Tasks; namespace DS.WMS.PrintApi.Controllers @@ -63,5 +66,18 @@ namespace DS.WMS.PrintApi.Controllers var res = await _invokeService.GetOpenSqlPrintInfo(req); return res; } + + /// + /// 下载sql打印源文件 + /// + /// + /// 返回打印frx文件 + [HttpPost] + [Route("DownloadSqlPrintFile")] + [ProducesResponseType(typeof(FileResult), StatusCodes.Status200OK)] + public async Task DownloadSqlPrintFile([FromBody] OpenSqlPrintReq req) + { + return await _invokeService.DownloadSqlPrintFile(req); + } } } diff --git a/ds-wms-service/DS.WMS.PrintApi/Service/IOpenPrintService.cs b/ds-wms-service/DS.WMS.PrintApi/Service/IOpenPrintService.cs index e86e811f..5a0118ca 100644 --- a/ds-wms-service/DS.WMS.PrintApi/Service/IOpenPrintService.cs +++ b/ds-wms-service/DS.WMS.PrintApi/Service/IOpenPrintService.cs @@ -1,4 +1,5 @@ using DS.WMS.PrintApi.Model; +using Microsoft.AspNetCore.Mvc; using System.Threading.Tasks; namespace DS.WMS.PrintApi.Service @@ -27,5 +28,14 @@ namespace DS.WMS.PrintApi.Service /// public Task GetOpenSqlPrintInfo(OpenSqlPrintReq req); + + + /// + /// 下载sql打印frx文件 + /// + /// + /// + + public Task DownloadSqlPrintFile(OpenSqlPrintReq req); } } diff --git a/ds-wms-service/DS.WMS.PrintApi/Service/OpenPrintService.cs b/ds-wms-service/DS.WMS.PrintApi/Service/OpenPrintService.cs index f86d5a0c..859fff70 100644 --- a/ds-wms-service/DS.WMS.PrintApi/Service/OpenPrintService.cs +++ b/ds-wms-service/DS.WMS.PrintApi/Service/OpenPrintService.cs @@ -15,6 +15,7 @@ using Newtonsoft.Json; using Microsoft.Extensions.DependencyInjection; using DS.WMS.PrintApi.Middleware; using Newtonsoft.Json.Schema; +using Microsoft.AspNetCore.Mvc; namespace DS.WMS.PrintApi.Service { @@ -459,5 +460,146 @@ namespace DS.WMS.PrintApi.Service return await Task.FromResult(PrintDataResult.Failed(ex.Message)); } } + + /// + /// 下载sql打印frx文件 + /// + /// + /// + + public async Task DownloadSqlPrintFile(OpenSqlPrintReq req) + { + try + { + + if (req.TenantId == 0 || req.TenantId == null) + { + throw new Exception("缺失关键参数租户id"); + } + var template = await db.Queryable().Filter(null, true).Where(x => x.TenantId == req.TenantId) + .WhereIF(req.TemplateId != 0, x => x.Id == req.TemplateId) + .WhereIF(!string.IsNullOrEmpty(req.TemplateCode), x => x.Id == req.TemplateId) + .FirstAsync(); + if (template == null) + { + throw new Exception("打印模板不存在!"); + } + if (!template.IsUseDataSource) + { + throw new Exception("非Sql打印接口!"); + } + if (string.IsNullOrEmpty(template.SourceSql)) + { + throw new Exception("打印数据源不能为空!"); + } + if (!template.SourceSql.Contains(';')) + { + throw new Exception("数据源必须包含分号!"); + } + if (template.SourceSql.Substring(template.SourceSql.Length - 1, 1) != ";") + { + throw new Exception("数据源最后必须包含分号!"); + } + if (SqlUtil.IsSqlInjection(template.SourceSql)) + { + throw new Exception("sql数据源包含非法字符,请检查!"); + } + if (String.IsNullOrEmpty(template.PrintJsonContent)) + { + throw new Exception("打印模板内容不能为空!"); + } + try + { + var tenantDb = saasService.GetBizDbScopeById(req.TenantId); + + var data = new Dictionary>(); + var sugarParams = new List(); + if (!string.IsNullOrEmpty(req.ParamJsonStr)) + { + var param = JsonHelper.Instance.Deserialize>(req.ParamJsonStr); + foreach (var p in param) + { + sugarParams.Add(new SugarParameter($"@{p.Key}", p.Value)); + } + } + + if (template.SourceSql.Contains(';')) + { + var sqlArr = template.SourceSql.Split(';'); + if (sqlArr.Length > 0) + { + for (int i = 0; i < sqlArr.Length; i++) + { + + if (!string.IsNullOrEmpty(sqlArr[i])) + { + var temp = tenantDb.Ado.SqlQuery(sqlArr[i], sugarParams); + data.Add("sql" + i, temp); + } + } + } + else + { + } + } + + 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 str = new FastReport.Data.JsonConnection.JsonDataSourceConnectionStringBuilder(); + str.Json = JsonConvert.SerializeObject(data); + + if (report.Dictionary.Connections.Count == 0) + { + //重置数据源 + report.Dictionary.Connections.Add(new JsonDataSourceConnection() + { + ConnectionString = $"JSON={JsonConvert.SerializeObject(data)};Encoding=utf-8", + Name = "Connection", + }); + } + else + { + var dataSource = report.Dictionary.Connections[0] as JsonDataSourceConnection; + dataSource.Name = "Connection"; + dataSource.ConnectionString = $"JSON={JsonConvert.SerializeObject(data)};Encoding=utf-8"; + } + + report.Save(printFile); + + + byte[] byteArr = System.IO.File.ReadAllBytes(printFile); + string mimeType = "application/octet-stream"; + return new FileContentResult(byteArr, mimeType) + { + FileDownloadName = printFileName + }; + } + catch (Exception e) + { + throw new Exception(e.ToString()); + } + + } + catch (Exception ex) + { + throw new Exception(ex.ToString()); + } + } } }