jianghaiqing 4 months ago
commit e86055b2c3

@ -111,7 +111,7 @@ namespace DS.WMS.Core.Fee.Method
ARFeeStatus = b.ARFeeStatus,
BusinessType = BusinessType.OceanShippingExport,
BusinessStatus = s.BusinessStatusName,
BusinessDate = s.BusinessDate,//业务日期
BusinessDate = s.CreateTime,//业务日期
BLType = s.BLType,
CargoId = s.CargoId,
Carrier = s.Carrier,
@ -249,7 +249,7 @@ namespace DS.WMS.Core.Fee.Method
ARFeeStatus = b.ARFeeStatus,
BusinessType = BusinessType.OceanShippingExport,
BusinessStatus = s.BusinessStatusName,
BusinessDate = s.BusinessDate,//业务日期
BusinessDate = s.CreateTime,//业务日期
BLType = s.BLType,
CargoId = s.CargoId,
Carrier = s.Carrier,
@ -378,7 +378,7 @@ namespace DS.WMS.Core.Fee.Method
Voyage = s.Voyno,
SaleId = s.SaleId,
CustomerId = s.CustomerId,
BusinessDate = s.BusinessDate,
BusinessDate = s.CreateTime,
SourceId = s.SourceId,
AccountDate = s.AccountDate,
OperatorId = s.OperatorId

@ -58,11 +58,7 @@ public class SeaExportReq
///// 费用状态
///// </summary>
//public int FeeStatus { get; set; }
/// <summary>
/// 录入日期
/// </summary>
public DateTime BusinessDate { get; set; } = DateTime.Now;
/// <summary>
/// 财务日期
@ -1126,9 +1122,7 @@ public class SeaExportReqValidator : AbstractValidator<SeaExportReq>
/// 构造函数
/// </summary>
public SeaExportReqValidator()
{
this.RuleFor(o => o.BusinessDate)
.NotEmpty().WithName("录入日期");
{ ;
this.RuleFor(o => o.AccountDate)
.NotEmpty().WithName("财务日期");
this.RuleFor(o => o.BLType)

@ -112,10 +112,7 @@ public class SeaExportRes
/// 应收对账状态描述
/// </summary>
public string ARCheckStatusName => ARCheckStatus?.GetDescription();
/// <summary>
/// 录入日期
/// </summary>
public DateTime BusinessDate { get; set; } = DateTime.Now;
/// <summary>
/// 财务日期

@ -36,12 +36,7 @@ public class SeaExport : BaseOrgModel<long>
/// Desc:是否费用锁定
/// </summary>
[SugarColumn(ColumnDescription = "是否费用锁定", DefaultValue = "0")]
public bool? IsFeeLocking { get; set; } = false;
/// <summary>
/// 录入日期
/// </summary>
[SqlSugar.SugarColumn(ColumnDescription = "录入日期", IsNullable = true)]
public DateTime? BusinessDate { get; set; } = DateTime.Now;
public bool? IsFeeLocking { get; set; } = false;
/// <summary>
/// 财务日期

@ -76,10 +76,7 @@ namespace DS.WMS.Core.Op.View
/// 应收对账状态
/// </summary>
public int ARCheckStatus { get; set; } = 0;
/// <summary>
/// 录入日期
/// </summary>
public DateTime BusinessDate { get; set; } = DateTime.Now;
/// <summary>
/// 财务日期

@ -115,7 +115,7 @@ namespace DS.WMS.Core.Settlement.Method
CreateBy = f.CreateBy,
AccountDate = s.AccountDate,
BusinessDate = s.BusinessDate,//业务日期
BusinessDate = s.CreateTime,//业务日期
ClientName = s.CustomerName,//委托单位
CustomerNo = s.CustomerNo,
CustomNo = s.CustomNo,

@ -1082,7 +1082,7 @@ public class CommonService : ICommonService
.WhereIF(!string.IsNullOrEmpty(queryKey), x => x.PortName.Contains(queryKey) || x.CnName.Contains(queryKey))
.Select(x => new CodePortSelectRes()
{
PinYinCode = x.CnName + "(" + x.PortName + ")",
PinYinCode = !String.IsNullOrEmpty(x.EdiCode) ? x.EdiCode + "/" + x.PortName : x.PortName,
},true)
.Take(20)
.WithCache(SqlSugarCacheConst.Port)

@ -15,6 +15,7 @@
<PackageReference Include="FastReport.Compat" Version="2024.1.0" />
<PackageReference Include="FastReport.Core3" Version="2020.3.4" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.1.32" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="NLog" Version="5.2.7" />
<PackageReference Include="SqlSugarCore" Version="5.1.4.166" />

@ -0,0 +1,26 @@
using SqlSugar;
namespace DS.WMS.PrintApi.Middleware
{
public interface ISaasDbService
{
/// <summary>
/// 获取业务库
/// </summary>
/// <returns></returns>
public ISqlSugarClient GetMasterDb();
/// <summary>
/// 主库根据Id获取业务库Scope
/// </summary>
/// <param name="configId"></param>
/// <returns></returns>
public SqlSugarScopeProvider GetBizDbScopeById(object configId);
/// <summary>
/// 获取业务库Scope
/// </summary>
/// <returns></returns>
public SqlSugarScopeProvider GetMasterDbScope();
}
}

@ -0,0 +1,23 @@
using Microsoft.Extensions.DependencyInjection;
using System;
namespace DS.WMS.PrintApi.Middleware
{
/// <summary>
/// 注入Saas分库服务
/// </summary>
public static class SaasDbInstall
{
/// <summary>
///
/// </summary>
/// <param name="services"></param>
/// <exception cref="ArgumentNullException"></exception>
public static void AddSaasDbInstall(this IServiceCollection services)
{
if (services == null) throw new ArgumentNullException(nameof(services));
services.AddScoped<ISaasDbService, SaasDbService>();
}
}
}

@ -0,0 +1,113 @@
using DS.WMS.PrintApi.Model;
using Microsoft.Extensions.DependencyInjection;
using NLog;
using SqlSugar;
using System;
namespace DS.WMS.PrintApi.Middleware
{
public class SaasDbService: ISaasDbService
{
private readonly IServiceProvider _serviceProvider;
private readonly SqlSugarScope db;
private readonly ISaasDbService saasService;
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
/// <summary>
/// 构造函数
/// </summary>
/// <param name="serviceProvider"></param>
public SaasDbService(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
db = (SqlSugarScope)_serviceProvider.GetRequiredService<ISqlSugarClient>();
}
/// <summary>
/// 获取主库信息
/// </summary>
/// <returns></returns>
public ISqlSugarClient GetMasterDb()
{
return db.GetConnection("1288018625843826688");
}
/// <summary>
/// 主库根据Id获取业务库Scope
/// </summary>
/// <param name="configId"></param>
/// <returns></returns>
public SqlSugarScopeProvider GetBizDbScopeById(object configId)
{
if (!db.IsAnyConnection(configId))
{
var connInfo = GetMasterDb().Queryable<SysTenantLink>().Filter(null,true).First(x => x.TenantId == Convert.ToInt64(configId));
//用非默认ConfigId进行测试
//添加业务库只在当前上下文有效原理SqlSugarScope模式入门文档去看
db.AddConnection(new ConnectionConfig()
{
ConfigId = configId,
ConnectionString = connInfo.Connection,
DbType = connInfo.DbType,
IsAutoCloseConnection = true
});
var dbProvider = db.GetConnectionScope(configId);
dbProvider.Ado.CommandTimeOut = 30;
dbProvider.Aop.OnLogExecuting = (sql, pars) =>
{
//执行前事件
//Logger.Log(LogLevel.Info,
// DateTime.Now.ToString() + "\r\n" +
// UtilMethods.GetSqlString(c.DbType, sql, pars));
string sqlStr = sql;
foreach (var item in pars)
{
if (item.Value != null && item.Value != DBNull.Value)
{
string? strValue = string.Empty;
if (item.DbType == System.Data.DbType.String)
{
strValue = "'" + (string)item.Value + "'";
}
else if (item.DbType == System.Data.DbType.Boolean)
{
strValue = (bool)item.Value ? "1" : "0";
}
else
{
strValue = item.Value.ToString();
}
sqlStr = sqlStr.Replace(item.ParameterName, strValue);
}
}
Console.WriteLine("执行的SQL" + Environment.NewLine + sqlStr);
};
//数据处理事件
dbProvider.Aop.DataExecuting = (oldValue, entityInfo) =>
{
};
dbProvider.Aop.OnDiffLogEvent = it =>
{
//排除日志库操作
if (Convert.ToInt64(configId) != 1288018625843826680)
{
}
};
}
return db.GetConnectionScope(configId);
}
public SqlSugarScopeProvider GetMasterDbScope()
{
return db.GetConnectionScope("1288018625843826688");
}
}
}

@ -0,0 +1,66 @@
using DS.WMS.PrintApi.Utils;
using Microsoft.Extensions.DependencyInjection;
using NLog;
using SqlSugar;
using System;
using System.Collections.Generic;
using DbType = SqlSugar.DbType;
namespace DS.WMS.PrintApi.Middleware
{
/// <summary>
/// SqlSugar 启动服务
/// </summary>
public static class SqlSugarSetup
{
public static readonly Logger Logger = LogManager.GetCurrentClassLogger();
/// <summary>
/// 默认的Sql Server的链接字符串
/// </summary>
private static string ProTag = AppSetting.Configuration["ConnectionStrings:Pro"];
private static string DataType = AppSetting.Configuration["ConnectionStrings:DbType"];
/// <summary>
/// 将模块服务添加到依赖注入服务容器中
/// </summary>
/// <param name="services">依赖注入服务容器</param>
/// <returns></returns>
public static IServiceCollection AddSqlSugarSetup(this IServiceCollection services)
{
if (services == null) throw new ArgumentNullException(nameof(services));
SqlSugarScope sqlSugar = new SqlSugarScope(new List<ConnectionConfig>()
{
new ConnectionConfig()
{
ConfigId = "1288018625843826688", DbType = DataType.ToEnum<DbType>(), ConnectionString = ProTag,
InitKeyType = InitKeyType.Attribute, IsAutoCloseConnection = true
},
//new ConnectionConfig(){ ConfigId="2", DbType=DbType.SqlServer, ConnectionString=DevTag,InitKeyType=InitKeyType.Attribute,IsAutoCloseConnection=true,AopEvents=aop}
});
//db.ChangeDatabase(configId);
//记录数据库配置
//Console.WriteLine("数据库配置:"+JsonConvert.SerializeObject(db.CurrentConnectionConfig));
//Logger.Log(LogLevel.Trace, JsonConvert.SerializeObject(db.CurrentConnectionConfig));
sqlSugar.Ado.CommandTimeOut = 30;
sqlSugar.Aop.OnLogExecuting = (sql, pars) =>
{
Logger.Log(LogLevel.Info,
DateTime.Now.ToString() + "\r\n" +
UtilMethods.GetSqlString(DataType.ToEnum<DbType>(), sql, pars));
};
sqlSugar.Aop.OnError = (exp) => //执行SQL 错误事件
{
// Console.WriteLine(DateTime.Now.ToString() + "\r\n" + exp.Sql + "\r\n" + exp.Parametres);
Logger.Error(DateTime.Now.ToString() + "\r\n" + exp.Sql + "\r\n" + exp.Parametres);
//Logger.Log(LogLevel.Error, DateTime.Now.ToString() + "\r\n" + exp.Sql + "\r\n" + exp.Parametres);
//exp.sql exp.parameters 可以拿到参数和错误Sql
};
services.AddSingleton<ISqlSugarClient>(sqlSugar); //这边是SqlSugarScope用AddSingleton
return services;
}
}
}

@ -0,0 +1,31 @@
using SqlSugar;
namespace DS.WMS.PrintApi.Model
{
/// <summary>
/// 租户数据库链接
/// </summary>
[SqlSugar.SugarTable("sys_tenant_dblink")]
public class SysTenantLink
{
/// <summary>
/// ID
/// </summary>
[SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
public int Id { get; set; }
/// <summary>
/// 租户id
/// </summary>
public long TenantId { get; set; }
/// <summary>
/// 数据库类型
/// </summary>
[SugarColumn(ColumnDescription = "数据库类型")]
public SqlSugar.DbType DbType { get; set; } = SqlSugar.DbType.MySql;
/// <summary>
/// 数据库链接字符串
/// </summary>
public string Connection { get; set; }
}
}

@ -21,7 +21,7 @@
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "swagger/index.html",
"applicationUrl": "http://localhost:5000",
"applicationUrl": "http://localhost:2999",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}

@ -12,93 +12,107 @@ using SqlSugar;
using static System.Net.Mime.MediaTypeNames;
using System.Collections.Generic;
using Newtonsoft.Json;
using Microsoft.Extensions.DependencyInjection;
using DS.WMS.PrintApi.Middleware;
namespace DS.WMS.PrintApi.Service
{
public class OpenPrintService : IOpenPrintService
{
private readonly IServiceProvider _serviceProvider;
private readonly ISqlSugarClient db;
private readonly ISaasDbService saasService;
/// <summary>
///
/// </summary>
/// <param name="serviceProvider"></param>
public OpenPrintService(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
db = _serviceProvider.GetRequiredService<ISqlSugarClient>();
saasService = _serviceProvider.GetRequiredService<ISaasDbService>();
}
public async Task<PrintDataResult> GetOpenJsonPrintInfo(OpenJsonPrintReq req)
{
try
{
using (var db = SqlSugarUtil.GetInstance())
var template = db.Queryable<SysPrintTemplate>().Filter(null, true).Where(x => x.Id == req.TemplateId).First();
if (template == null)
{
var template = db.Queryable<SysPrintTemplate>().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))
{
return await Task.FromResult(PrintDataResult.Failed("打印模板不存在!"));
Byte[] info = new UTF8Encoding(true).GetBytes(template.PrintJsonContent);
fs.Write(info, 0, info.Length);
}
if (template.IsUseDataSource)
//生成报表
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")
{
return await Task.FromResult(PrintDataResult.Failed("非Json打印接口!"));
printName = $"{fileName}.pdf";
saveFile = Path.Combine(savePath, printName);
PDFExport pdfExport = new PDFExport();
pdfExport.Export(report, saveFile);
}
if (String.IsNullOrEmpty(template.PrintJsonContent))
else if (req.PrintType == "2")
{
return await Task.FromResult(PrintDataResult.Failed("打印模板内容不能为空!"));
printName = $"{fileName}.xlsx";
saveFile = Path.Combine(savePath, printName);
Excel2007Export excelExport = new Excel2007Export();
excelExport.Export(report, saveFile);
}
try
else if (req.PrintType == "3")
{
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));
printName = $"{fileName}.doc";
saveFile = Path.Combine(savePath, printName);
Word2007Export wordExport = new Word2007Export();
wordExport.Export(report, saveFile);
}
catch (Exception e)
else
{
return await Task.FromResult(PrintDataResult.Failed(e.Message));
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)
{
@ -111,98 +125,96 @@ namespace DS.WMS.PrintApi.Service
{
try
{
using (var db = SqlSugarUtil.GetInstance())
var template = db.Queryable<SysPrintTemplate>().Filter(null, true).Where(x => x.TemplateCode == req.Code && x.TenantId == req.TenantId).First();
if (template == null)
{
var template = db.Queryable<SysPrintTemplate>().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("打印模板编码:[" + 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))
{
return await Task.FromResult(PrintDataResult.Failed("打印模板内容不能为空!"));
Byte[] info = new UTF8Encoding(true).GetBytes(template.PrintJsonContent);
fs.Write(info, 0, info.Length);
}
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);
//生成报表
FastReport.Report report = new FastReport.Report();
report.Load(printFile);
//写入CRX文件
using (FileStream fs = new FileStream(printFile, FileMode.Create))
{
Byte[] info = new UTF8Encoding(true).GetBytes(template.PrintJsonContent);
fs.Write(info, 0, info.Length);
}
var dataSource = report.Dictionary.Connections[0] as JsonDataSourceConnection;
var str = new FastReport.Data.JsonConnection.JsonDataSourceConnectionStringBuilder();
str.Json = req.JsonDataStr;
dataSource.ConnectionString = str.ConnectionString;
//生成报表
FastReport.Report report = new FastReport.Report();
report.Load(printFile);
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);
var dataSource = report.Dictionary.Connections[0] as JsonDataSourceConnection;
var str = new FastReport.Data.JsonConnection.JsonDataSourceConnectionStringBuilder();
str.Json = req.JsonDataStr;
dataSource.ConnectionString = str.ConnectionString;
#region 处理托书挂载附件
report.Prepare();
var printName = string.Empty;
var saveFile = string.Empty;
//var saveFile = Path.Combine(savePath, printName);
if (req.PrintType == "1")
if (template.TemplateCode == "bill_of_lading")
{
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))
{
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();
await file.CopyToAsync(fileStream);
}
#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));
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);
}
catch (Exception e)
else
{
return await Task.FromResult(PrintDataResult.Failed(e.Message));
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)
{
@ -220,139 +232,138 @@ namespace DS.WMS.PrintApi.Service
{
try
{
using (var db = SqlSugarUtil.GetInstance())
if (req.TenantId == 0 || req.TenantId == null)
{
if (req.TenantId == 0 || req.TenantId == null)
{
return await Task.FromResult(PrintDataResult.Failed("缺失关键参数租户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)
{
return await Task.FromResult(PrintDataResult.Failed("打印模板不存在!"));
}
if (!template.IsUseDataSource)
{
return await Task.FromResult(PrintDataResult.Failed("非Sql打印接口!"));
}
if (string.IsNullOrEmpty(template.SourceSql))
{
return await Task.FromResult(PrintDataResult.Failed("打印数据源不能为空!"));
}
if (!template.SourceSql.Contains(';'))
{
return await Task.FromResult(PrintDataResult.Failed("数据源必须包含分号!"));
}
if (template.SourceSql.Substring(template.SourceSql.Length - 1, 1) != ";")
{
return await Task.FromResult(PrintDataResult.Failed("数据源最后必须包含分号!"));
}
if (SqlUtil.IsSqlInjection(template.SourceSql))
{
return await Task.FromResult(PrintDataResult.Failed("sql数据源包含非法字符,请检查!"));
}
if (String.IsNullOrEmpty(template.PrintJsonContent))
return await Task.FromResult(PrintDataResult.Failed("缺失关键参数租户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)
{
return await Task.FromResult(PrintDataResult.Failed("打印模板不存在!"));
}
if (!template.IsUseDataSource)
{
return await Task.FromResult(PrintDataResult.Failed("非Sql打印接口!"));
}
if (string.IsNullOrEmpty(template.SourceSql))
{
return await Task.FromResult(PrintDataResult.Failed("打印数据源不能为空!"));
}
if (!template.SourceSql.Contains(';'))
{
return await Task.FromResult(PrintDataResult.Failed("数据源必须包含分号!"));
}
if (template.SourceSql.Substring(template.SourceSql.Length - 1, 1) != ";")
{
return await Task.FromResult(PrintDataResult.Failed("数据源最后必须包含分号!"));
}
if (SqlUtil.IsSqlInjection(template.SourceSql))
{
return await Task.FromResult(PrintDataResult.Failed("sql数据源包含非法字符,请检查!"));
}
if (String.IsNullOrEmpty(template.PrintJsonContent))
{
return await Task.FromResult(PrintDataResult.Failed("打印模板内容不能为空!"));
}
try
{
var tenantDb = saasService.GetBizDbScopeById(req.TenantId);
var data = new Dictionary<string, List<dynamic>>();
var sugarParams = new List<SugarParameter>();
if (!string.IsNullOrEmpty(req.ParamJsonStr))
{
return await Task.FromResult(PrintDataResult.Failed("打印模板内容不能为空!"));
var param = JsonHelper.Instance.Deserialize<Dictionary<string, string>>(req.ParamJsonStr);
foreach (var p in param)
{
sugarParams.Add(new SugarParameter($"@{p.Key}", p.Value));
}
}
try
{
var tenantDb = db.GetConnection(req.TenantId);
var data = new Dictionary<string, List<dynamic>>();
var sugarParams = new List<SugarParameter>();
if (!string.IsNullOrEmpty(req.ParamJsonStr))
if (template.SourceSql.Contains(';'))
{
var sqlArr = template.SourceSql.Split(';');
if (sqlArr.Length > 0)
{
var param = JsonHelper.Instance.Deserialize<Dictionary<string, string>>(req.ParamJsonStr);
foreach (var p in param)
for (int i = 0; i < sqlArr.Length; i++)
{
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]))
{
if (!string.IsNullOrEmpty(sqlArr[i]))
{
var temp = tenantDb.Ado.SqlQuery<dynamic>(sqlArr[i], sugarParams);
data.Add("sql" + i, temp);
}
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))
else
{
Byte[] info = new UTF8Encoding(true).GetBytes(template.PrintJsonContent);
fs.Write(info, 0, info.Length);
}
}
//生成报表
FastReport.Report report = new FastReport.Report();
report.Load(printFile);
var basePath = String.Empty;
var savePath = "wwwroot/PrintTempFile";
var dataSource = report.Dictionary.Connections[0] as JsonDataSourceConnection;
var str = new FastReport.Data.JsonConnection.JsonDataSourceConnectionStringBuilder();
str.Json = JsonConvert.SerializeObject(data);
dataSource.ConnectionString = str.ConnectionString;
var fileName = DateTime.Now.Ticks + "-" + NumUtil.GetRandomString(3);
var printFileName = $"{fileName}.frx";
var printFile = Path.Combine(savePath, printFileName);
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("非法打印格式!"));
}
//写入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 = JsonConvert.SerializeObject(data);
dataSource.ConnectionString = str.ConnectionString;
return await Task.FromResult(PrintDataResult.OK(printName));
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);
}
catch (Exception e)
else
{
return await Task.FromResult(PrintDataResult.Failed(e.Message));
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)
{

@ -1,3 +1,4 @@
using DS.WMS.PrintApi.Middleware;
using DS.WMS.PrintApi.Service;
using DS.WMS.PrintApi.Utils;
using Microsoft.AspNetCore.Builder;
@ -25,7 +26,10 @@ namespace DS.WMS.PrintApi
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
//注入IWMSCommonService 公共服务
services.AddSqlSugarSetup();
//services.AddSaasDbInstall();
services.AddSingleton<ISaasDbService, SaasDbService>();
services.AddSingleton<IOpenPrintService, OpenPrintService>();
services.AddControllers(
@ -58,6 +62,7 @@ namespace DS.WMS.PrintApi
services.AddSwaggerSetup();
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.

Loading…
Cancel
Save