打印模块 下载sql打印源文件接口

usertest
cjy 3 months ago
parent 36474256b6
commit 45781779fd

@ -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;
}
/// <summary>
/// 下载sql打印源文件
/// </summary>
/// <param name="req"></param>
/// <returns>返回打印frx文件</returns>
[HttpPost]
[Route("DownloadSqlPrintFile")]
[ProducesResponseType(typeof(FileResult), StatusCodes.Status200OK)]
public async Task<IActionResult> DownloadSqlPrintFile([FromBody] OpenSqlPrintReq req)
{
return await _invokeService.DownloadSqlPrintFile(req);
}
}
}

@ -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
/// <returns></returns>
public Task<PrintDataResult> GetOpenSqlPrintInfo(OpenSqlPrintReq req);
/// <summary>
/// 下载sql打印frx文件
/// </summary>
/// <param name="req"></param>
/// <returns></returns>
public Task<IActionResult> DownloadSqlPrintFile(OpenSqlPrintReq req);
}
}

@ -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));
}
}
/// <summary>
/// 下载sql打印frx文件
/// </summary>
/// <param name="req"></param>
/// <returns></returns>
public async Task<IActionResult> DownloadSqlPrintFile(OpenSqlPrintReq req)
{
try
{
if (req.TenantId == 0 || req.TenantId == null)
{
throw new Exception("缺失关键参数租户id");
}
var template = await db.Queryable<SysPrintTemplate>().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<string, List<dynamic>>();
var sugarParams = new List<SugarParameter>();
if (!string.IsNullOrEmpty(req.ParamJsonStr))
{
var param = JsonHelper.Instance.Deserialize<Dictionary<string, string>>(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<dynamic>(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());
}
}
}
}

Loading…
Cancel
Save