Files
tnb.server/WarehouseMgr/Tnb.WarehouseMgr/BaseWareHouseService.cs
FanLian 3deea63f3f 1
2023-08-07 17:49:21 +08:00

285 lines
9.8 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Security.Claims;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using Aspose.Cells.Drawing;
using JNPF;
using JNPF.Common.Contracts;
using JNPF.Common.Core.Manager;
using JNPF.Common.Enums;
using JNPF.Common.Extension;
using JNPF.DataEncryption;
using JNPF.DependencyInjection;
using JNPF.DynamicApiController;
using JNPF.Extras.CollectiveOAuth.Enums;
using JNPF.FriendlyException;
using JNPF.Systems.Interfaces.System;
using JNPF.VisualDev;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using SqlSugar;
using Tnb.BasicData.Entities;
using Tnb.WarehouseMgr.Entities;
using Tnb.WarehouseMgr.Entities.Attributes;
using Tnb.WarehouseMgr.Entities.Consts;
using Tnb.WarehouseMgr.Entities.Dto;
using Tnb.WarehouseMgr.Entities.Dto.Outputs;
using Tnb.WarehouseMgr.Entities.Entity;
using Tnb.WarehouseMgr.Interfaces;
namespace Tnb.WarehouseMgr
{
[ApiDescriptionSettings(Tag = ModuleConsts.Tag, Area = ModuleConsts.Area, Order = 700)]
[Route("api/[area]/[controller]/[action]")]
public class BaseWareHouseService : IOverideVisualDevService, IDynamicApiController, ITransient
{
private static Dictionary<string, IWHStorageService> _stroageMap = new(StringComparer.OrdinalIgnoreCase);
public OverideVisualDevFunc OverideFuncs { get; } = new OverideVisualDevFunc();
static BaseWareHouseService()
{
var serviceTypes = App.EffectiveTypes.Where(u => u.IsClass && !u.IsInterface && !u.IsAbstract && typeof(IWHStorageService).IsAssignableFrom(u)).ToList();
foreach (var serviceType in serviceTypes)
{
var callerName = serviceType.GetCustomAttribute<CallerAttribute>()?.Name ?? string.Empty;
if (!callerName.IsNullOrEmpty())
{
var obj = Activator.CreateInstance(serviceType) as IWHStorageService;
if (obj == null) continue;
_stroageMap[callerName] = obj;
}
}
}
protected Task<ClaimsPrincipal> GetUserIdentity(string aToken)
{
var claims = JWTEncryption.ReadJwtToken(aToken)?.Claims;
ClaimsIdentity toKen = new ClaimsIdentity();
foreach (Claim item in claims)
{
toKen.AddClaim(item);
}
var curUser = new ClaimsPrincipal(toKen);
return Task.FromResult(curUser);
}
/// <summary>
/// 判断最终目标库位是否可以放置当前载具
/// </summary>
/// <param name="carry">当前载具</param>
/// <param name="locDest">目标库位</param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
[NonAction]
protected Task<bool> IsCarryAndLocationMatchByCarryStd(WmsCarryH carry, BasLocation locDest)
{
bool isMatch = false;
if (carry == null) throw new ArgumentNullException(nameof(carry));
if (locDest == null) throw new ArgumentNullException(nameof(locDest));
if (!carry.carrystd_id.IsNullOrEmpty() && !locDest.carrystd_id.IsNullOrEmpty())
{
var jsonArr = JArray.Parse(locDest.carrystd_id);
var locCarryStdArr = jsonArr.Select(x => x.ToObject<string>()).ToArray();
isMatch = locCarryStdArr.Contains(carry.carrystd_id);
}
return Task.FromResult(isMatch);
}
/// <summary>
/// 解析Excel到内存
/// </summary>
/// <param name="file"></param>
/// <returns></returns>
protected Task<List<Dictionary<string, object>>> ImportExcelToMemory(IFormFile file)
{
int rowIndex = 1;
List<Dictionary<string, object>> dics = new();
IWorkbook? workbook = null;
try
{
using (Stream stream = file.OpenReadStream())
{
// 2007版本
if (file.Name.IndexOf(".xlsx") > 0)
workbook = new XSSFWorkbook(stream);
else if (file.Name.IndexOf(".xls") > 0)
workbook = new HSSFWorkbook(stream);
ISheet? sheet = workbook?.GetSheetAt(0);
if (workbook == null || sheet == null)
throw Oops.Bah("无导入数据");
if (sheet?.LastRowNum <= 1)
throw Oops.Bah("无导入数据");
ICellStyle style = workbook.CreateCellStyle();
IFont font = workbook.CreateFont();
font.Color = IndexedColors.Red.Index; // 将字体颜色设置为红色
style.SetFont(font);
var nameRow = sheet?.GetRow(0);
if (nameRow != null)
{
Dictionary<string, object> dic = new Dictionary<string, object>();
List<string> columns = new List<string>();
columns = nameRow.Cells.Select(x => x.StringCellValue).ToList();
if (columns?.Count > 0)
{
for (rowIndex = 1; rowIndex <= sheet?.LastRowNum; rowIndex++)
{
var row = sheet?.GetRow(rowIndex);
if (row != null)
{
foreach (var col in columns)
{
ICell cell = row.GetCell(columns.IndexOf(col));
dic.Add(col, cell.StringCellValue);
}
dics.Add(dic);
}
}
}
}
}
}
catch (Exception)
{
throw;
}
return Task.FromResult(dics);
}
/// <summary>
/// WMS导入模板
/// </summary>
/// <param name="dic"></param>
/// <param name="sheetName"></param>
/// <returns></returns>
protected dynamic WmsTemplate(Dictionary<string, string> dic, string sheetName)
{
XSSFWorkbook workbook = new XSSFWorkbook();
ISheet sheet = workbook.CreateSheet(sheetName);
IRow nRow = sheet.CreateRow(0);
IRow dRow = sheet.CreateRow(1);
dRow.Hidden = true;
dic.Remove("id");
dic.Remove("create_id");
dic.Remove("create_time");
//sheet.SetColumnHidden(1, true);
for (int i = 0; i < dic?.Count; i++)
{
ICell cell1 = nRow.CreateCell(i);
cell1.SetCellValue(dic.Values.GetIndex(i));
ICell cell2 = dRow.CreateCell(i);
cell2.SetCellValue(dic.Keys.GetIndex(i));
}
MemoryStream ms = new MemoryStream();
workbook.Write(ms);
MemoryStream ms2 = new MemoryStream(ms.ToArray());
ms2.Position = 0;
FileStreamResult fileStreamResult = new FileStreamResult(ms2, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") { FileDownloadName = "template.xlsx" };
return fileStreamResult;
}
[NonAction]
protected async Task DoUpdate(WareHouseUpInput input)
{
if (_stroageMap.ContainsKey(input.loginType))
{
await _stroageMap[input.loginType].Do(input);
}
}
[NonAction]
public virtual Task ModifyAsync(WareHouseUpInput input)
{
return Task.CompletedTask;
}
/// <summary>
/// Api响应结果
/// </summary>
/// <returns></returns>
[NonAction]
protected Task<Result> ToApiResult()
{
Result result = new();
return Task.FromResult(result);
}
/// <summary>
/// Api响应结果
/// </summary>
/// <returns></returns>
[NonAction]
protected Task<Result> ToApiResult(HttpStatusCode statusCode, object data)
{
Result result = new()
{
code = statusCode,
data = data
};
return Task.FromResult(result);
}
/// <summary>
/// Api响应结果
/// </summary>
/// <returns></returns>
[NonAction]
protected Task<Result> ToApiResult(object data)
{
Result result = new()
{
data = data
};
return Task.FromResult(result);
}
/// <summary>
/// Api响应结果
/// </summary>
/// <returns></returns>
[NonAction]
protected Task<Result> ToApiResult(HttpStatusCode statusCode, string msg)
{
Result result = new()
{
code = statusCode,
msg = msg
};
return Task.FromResult(result);
}
/// <summary>
/// Api响应结果
/// </summary>
/// <returns></returns>
[NonAction]
protected Task<Result> ToApiResult(HttpStatusCode statusCode, string msg, object data)
{
Result result = new()
{
code = statusCode,
msg = msg,
data = data
};
return Task.FromResult(result);
}
}
}