You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

220 lines
7.9 KiB
C#

using DS.Module.Core;
using DS.Module.SqlSugar;
using DS.Module.UserModule;
using DS.WMS.Core.TaskPlat.Dtos;
using DS.WMS.Core.TaskPlat.Entity;
using DS.WMS.Core.TaskPlat.Interface;
using Microsoft.AspNetCore.Hosting;
4 months ago
using Microsoft.Extensions.Logging;
using SqlSugar;
using System.Linq.Expressions;
using System.Runtime.InteropServices;
namespace DS.WMS.Core.TaskPlat.Method
{
/// <summary>
/// 任务模块业务类的基类,封装了一些常用的方法
/// </summary>
4 months ago
public class TaskManageBaseService<T> : ITaskManageBaseService
{
protected readonly IUser user;
4 months ago
protected readonly ILogger<T> logger;
protected readonly ISaasDbService saasDbService;
protected readonly IServiceProvider serviceProvider;
protected readonly IWebHostEnvironment environment;
4 months ago
public TaskManageBaseService(IUser user,
4 months ago
ILogger<T> logger,
ISaasDbService saasDbService,
IServiceProvider serviceProvider,
IWebHostEnvironment environment)
{
this.user = user;
4 months ago
this.logger = logger;
this.saasDbService = saasDbService;
this.serviceProvider = serviceProvider;
this.environment = environment;
}
4 months ago
/// <summary>
/// 更新任务主表状态
/// </summary>
/// <param name="taskIds">任务主键数组</param>
/// <param name="columns">需要更新状态的列</param>
public async Task SetTaskStatus(long[] taskIds, params Expression<Func<TaskBaseInfo, bool>>[] columns)
{
SqlSugarScopeProvider tenantDb = saasDbService.GetBizDbScopeById(user.TenantId);
var updateable = tenantDb.Updateable<TaskBaseInfo>();
foreach (var item in columns)
{
updateable.SetColumns(item);
}
updateable.SetColumns(x => x.UpdateBy == long.Parse(user.UserId))
.SetColumns(x => x.UpdateTime == DateTime.Now)
.SetColumns(x => x.UpdateUserName == user.UserName);
await updateable.Where(x => taskIds.Contains(x.Id))
.ExecuteCommandAsync();
// 后面可以记录日志
}
/// <summary>
4 months ago
/// 设置任务处理人
/// </summary>
/// <param name="taskIds">任务主键数组</param>
/// <param name="userInfo">人员信息列表</param>
public async Task SetTaskOwner(long[] taskIds, List<RecvUserInfo> userInfo)
{
SqlSugarScopeProvider tenantDb = saasDbService.GetBizDbScopeById(user.TenantId);
try
{
await tenantDb.Ado.BeginTranAsync();
await tenantDb.Deleteable<TaskBaseAllocation>(x => taskIds.Contains(x.TaskId)).ExecuteCommandAsync();
List<TaskBaseAllocation> allocationList = new();
foreach (var item in taskIds)
{
allocationList.AddRange(userInfo.Select(x => new TaskBaseAllocation
{
TaskId = item,
UserId = x.RecvUserId,
UserName = x.RecvUserName
}));
}
await tenantDb.Insertable(allocationList).ExecuteCommandAsync();
// 后面可以记录日志
await tenantDb.Ado.CommitTranAsync();
}
catch (Exception)
{
await tenantDb.Ado.RollbackTranAsync();
throw;
}
}
#region Tools
/// <summary>
/// 保存文件并返回文件完整路径
/// </summary>
/// <param name="fileBytes">文件二进制流</param>
/// <param name="batchNo">批次号</param>
/// <param name="printFileType">文件类型</param>
/// <param name="attachFileType">附件类型 bcfiles-BC文件 sofile-订舱附件</param>
/// <returns>返回文件完整路径</returns>
protected async Task<string> SaveFile(byte[] fileBytes, string batchNo,
PrintFileTypeEnum printFileType, string attachFileType = "sofiles")
{
var basePath = AppSetting.app(new string[] { "FileSettings", "BasePath" });
var relativePath = AppSetting.app(new string[] { "FileSettings", "RelativePath" });
if (!string.IsNullOrWhiteSpace(attachFileType))
relativePath += $"\\{attachFileType}";
string? dirAbs;
if (string.IsNullOrEmpty(basePath))
{
dirAbs = Path.Combine(environment.WebRootPath ?? "", relativePath);
}
else
{
dirAbs = Path.Combine(basePath, relativePath);
}
if (!Directory.Exists(dirAbs))
Directory.CreateDirectory(dirAbs);
var fileType = string.Empty;
if (printFileType == PrintFileTypeEnum.PDF)
{
fileType = ".pdf";
}
else if (printFileType == PrintFileTypeEnum.XLSX)
{
fileType = ".xlsx";
}
else if (printFileType == PrintFileTypeEnum.DOCX)
{
fileType = ".docx";
}
else if (printFileType == PrintFileTypeEnum.XLS)
{
fileType = ".xls";
}
else if (printFileType == PrintFileTypeEnum.DOC)
{
fileType = ".doc";
}
var id = SnowFlakeSingle.Instance.NextId();
var fileSaveName = $"{id}{fileType}".ToLower();
string fileRelaPath = Path.Combine(relativePath, fileSaveName).ToLower();
string fileAbsPath = Path.Combine(dirAbs, fileSaveName).ToLower();
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
relativePath = relativePath.Replace("\\", "/");
fileRelaPath = fileRelaPath.Replace("\\", "/");
fileAbsPath = fileAbsPath.Replace("\\", "/");
}
//logger.LogInformation("批次={no} 生成文件保存路径完成 路由={filePath} 服务器系统={system}", batchNo, fileRelaPath, RuntimeInformation.OSDescription);
await File.WriteAllBytesAsync(fileAbsPath, fileBytes);
//string bookFilePath;
//if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
//{
// bookFilePath = System.Text.RegularExpressions.Regex.Match(fileAbsPath, relativePath.Replace("/", "\\/") + ".*").Value;
//}
//else
//{
// bookFilePath = System.Text.RegularExpressions.Regex.Match(fileAbsPath, relativePath.Replace("\\", "\\\\") + ".*").Value;
//}
return fileAbsPath;
//return bookFilePath;
}
/// <summary>
/// 获取文件类型
/// </summary>
/// <param name="fileName">文件完成路径</param>
/// <returns>返回文件类型枚举</returns>
protected PrintFileTypeEnum GetFileType(string fileName)
{
PrintFileTypeEnum fileType = PrintFileTypeEnum.PDF;
switch (Path.GetExtension(fileName).ToLower())
{
case ".pdf":
fileType = PrintFileTypeEnum.PDF;
break;
case ".xlsx":
fileType = PrintFileTypeEnum.XLSX;
break;
case ".docx":
fileType = PrintFileTypeEnum.DOCX;
break;
case ".xls":
fileType = PrintFileTypeEnum.XLS;
break;
case ".doc":
fileType = PrintFileTypeEnum.DOC;
break;
}
return fileType;
}
#endregion
}
}