This commit is contained in:
alex
2023-08-07 17:48:24 +08:00

View File

@@ -4,6 +4,7 @@ using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DingTalk.Api.Request;
using JNPF.Common.Configuration;
using JNPF.Common.Contracts;
using JNPF.Common.Core.Manager;
@@ -26,7 +27,10 @@ using Tnb.WarehouseMgr.Entities;
namespace Tnb.WarehouseMgr
{
public class WmsImportAndExportService
/// <summary>
/// 导入和导出接口服务提供类
/// </summary>
public class WmsImportAndExportService : BaseWareHouseService
{
private readonly ISqlSugarClient _db;
private readonly IUserManager _userManager;
@@ -36,12 +40,31 @@ namespace Tnb.WarehouseMgr
_userManager = userManager;
}
/// <summary>
/// 载具导入模板
/// </summary>
/// <param name=""></param>
/// <returns></returns>
[HttpPost]
public async Task<dynamic> CarryImportTemplate()
{
int row = 0;
Dictionary<string,string> dic = new Dictionary<string,string>();
dic.Add("carry_code","载具编号");
dic.Add("carry_name","载具名称" );
dic.Add("carrystd_id","载具规格" );
//headers = _db.DbMaintenance.GetColumnInfosByTableName("wms_carry_h").FindAll(x=>!x.IsNullable).ToDictionary(x => x.DbColumnName ,x => x.ColumnDescription);
FileStreamResult fileStreamResult = WmsTemplate(dic, "载具台账");
return fileStreamResult;
}
/// <summary>
/// 载具导入
/// </summary>
/// <param name="file"></param>
/// <returns></returns>
[HttpPost]
public async Task<dynamic> CarryImport(IFormFile file)
{
int row = 0;
@@ -49,7 +72,7 @@ namespace Tnb.WarehouseMgr
{
//例1 获取所有表
List<Dictionary<string, string>> dics = WmsImport(file);
Dictionary<string, string> dic = WmsImport(file);
List<WmsCarryH> carrys = new List<WmsCarryH>();
var carryStdDic = await _db.Queryable<WmsCarrystd>().ToDictionaryAsync(x => x.carrystd_code, x => x.id);
List<string> locCodes = new();
@@ -57,21 +80,24 @@ namespace Tnb.WarehouseMgr
if (carryStdDic?.Count > 0)
{
string carryStdId = string.Empty;
foreach (var d in dics)
foreach (var d in dic)
{
var stdCodeKey = d["carrystd_code"];
var stdCodeKey = d.Key;
carryStdId = carryStdDic.ContainsKey(stdCodeKey) ? carryStdDic[stdCodeKey]?.ToString() ?? "" : "";
d.Add("carrystd_id", carryStdId);
locCodes.Add(d["location_code"]);
locCodes.Add(d);
carryH = d.Adapt<WmsCarryH>();
carrys.Add(carryH);
}
var locs = await _db.Queryable<BasLocation>().Where(it => locCodes.Contains(it.location_code)).ToDictionaryAsync(x => x.location_code, x => x.id);
foreach (var loc in locCodes)
if (locCodes != null)
{
var c = carrys.Find(x => x.location_code == loc);
if (c != null)
c.location_id = locs[loc].ToString();
var locs = await _db.Queryable<BasLocation>().Where(it => locCodes.Contains(it.location_code)).ToDictionaryAsync(x => x.location_code, x => x.id);
foreach (var loc in locCodes)
{
var c = carrys.Find(x => x.location_code == loc);
if (c != null)
c.location_id = locs[loc].ToString();
}
}
carrys.ForEach(x =>
{
@@ -177,22 +203,26 @@ namespace Tnb.WarehouseMgr
/// <summary>
/// WMS导入模板
/// </summary>
/// <param name="dics"></param>
/// <param name="dic"></param>
/// <param name="sheetName"></param>
/// <returns></returns>
private static dynamic WmsTemplate(List<Dictionary<string, string>> dics, string sheetName)
private static 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);
for (int i = 0; i < dics?.Count; i++)
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(dics[i].Keys.ToString());
cell1.SetCellValue(dic.Values.GetIndex(i));
ICell cell2 = dRow.CreateCell(i);
cell2.SetCellValue(dics[i].Values.ToString());
cell2.SetCellValue(dic.Keys.GetIndex(i));
}
MemoryStream ms = new MemoryStream();