导入功能修改
This commit is contained in:
@@ -16,6 +16,7 @@ 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;
|
||||
@@ -92,15 +93,16 @@ namespace Tnb.WarehouseMgr
|
||||
}
|
||||
return Task.FromResult(isMatch);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 解析Excel到内存
|
||||
/// </summary>
|
||||
/// <param name="file"></param>
|
||||
/// <returns></returns>
|
||||
protected Task<Dictionary<string, object>> ImportExcelToMemory(IFormFile file)
|
||||
protected Task<List<Dictionary<string, object>>> ImportExcelToMemory(IFormFile file)
|
||||
{
|
||||
int rowIndex = 1;
|
||||
Dictionary<string, object> dic = new();
|
||||
List<Dictionary<string, object>> dics = new();
|
||||
IWorkbook? workbook = null;
|
||||
try
|
||||
{
|
||||
@@ -127,6 +129,7 @@ namespace Tnb.WarehouseMgr
|
||||
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)
|
||||
@@ -141,19 +144,57 @@ namespace Tnb.WarehouseMgr
|
||||
ICell cell = row.GetCell(columns.IndexOf(col));
|
||||
dic.Add(col, cell.StringCellValue);
|
||||
}
|
||||
dics.Add(dic);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
|
||||
throw;
|
||||
}
|
||||
return Task.FromResult(dic);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 数据导入
|
||||
/// </summary>
|
||||
|
||||
@@ -14,6 +14,7 @@ using JNPF.Common.Models;
|
||||
using JNPF.Common.Security;
|
||||
using JNPF.Extras.CollectiveOAuth.Enums;
|
||||
using JNPF.FriendlyException;
|
||||
using JNPF.Logging;
|
||||
using Mapster;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
@@ -24,6 +25,7 @@ using NPOI.XSSF.UserModel;
|
||||
using SqlSugar;
|
||||
using Tnb.BasicData.Entities;
|
||||
using Tnb.WarehouseMgr.Entities;
|
||||
using Tnb.WarehouseMgr.Entities.Enums;
|
||||
|
||||
namespace Tnb.WarehouseMgr
|
||||
{
|
||||
@@ -49,10 +51,10 @@ namespace Tnb.WarehouseMgr
|
||||
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","载具规格" );
|
||||
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, "载具台账");
|
||||
|
||||
@@ -71,8 +73,8 @@ namespace Tnb.WarehouseMgr
|
||||
try
|
||||
{
|
||||
//例1 获取所有表
|
||||
|
||||
Dictionary<string, string> dic = WmsImport(file);
|
||||
|
||||
List<Dictionary<string, object>> dics = await ImportExcelToMemory(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();
|
||||
@@ -80,16 +82,16 @@ namespace Tnb.WarehouseMgr
|
||||
if (carryStdDic?.Count > 0)
|
||||
{
|
||||
string carryStdId = string.Empty;
|
||||
foreach (var d in dic)
|
||||
foreach (var d in dics)
|
||||
{
|
||||
var stdCodeKey = d.Key;
|
||||
var stdCodeKey = d["carrystd_code"].ToString();
|
||||
carryStdId = carryStdDic.ContainsKey(stdCodeKey) ? carryStdDic[stdCodeKey]?.ToString() ?? "" : "";
|
||||
d.Add("carrystd_id", carryStdId);
|
||||
locCodes.Add(d);
|
||||
locCodes.Add(d["location_code"]?.ToString() ?? string.Empty);
|
||||
carryH = d.Adapt<WmsCarryH>();
|
||||
carrys.Add(carryH);
|
||||
}
|
||||
if (locCodes != null)
|
||||
if (!locCodes.IsNullOrEmpty())
|
||||
{
|
||||
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)
|
||||
@@ -102,137 +104,25 @@ namespace Tnb.WarehouseMgr
|
||||
carrys.ForEach(x =>
|
||||
{
|
||||
x.id = SnowflakeIdHelper.NextId();
|
||||
x.is_lock = 0;
|
||||
x.carry_status = ((int)EnumCarryStatus.空闲).ToString();
|
||||
x.out_status = ((int)EnumOutStatus.正常).ToString();
|
||||
x.is_check = 0;
|
||||
x.create_id = _userManager.UserId;
|
||||
x.create_time = DateTime.Now;
|
||||
});
|
||||
}
|
||||
row = await _db.Storageable(carrys).ExecuteCommandAsync();
|
||||
await _db.Ado.BeginTranAsync();
|
||||
row = await _db.Insertable(carrys).ExecuteCommandAsync();
|
||||
await _db.Ado.CommitTranAsync();
|
||||
}
|
||||
catch (Exception)
|
||||
catch (Exception ex)
|
||||
{
|
||||
return false;
|
||||
throw;
|
||||
await _db.Ado.RollbackTranAsync();
|
||||
Log.Error("导入失败",ex)
|
||||
throw Oops.Bah("导入失败");
|
||||
}
|
||||
return row > 0;
|
||||
}
|
||||
|
||||
public async Task DataImport<T>(IFormFile file,Func<List<T>> getColumns) where T : BaseEntity<string>, new()
|
||||
{
|
||||
var nonBeNullColumnList = new List<string>();
|
||||
var entityInfo = _db.EntityMaintenance.GetEntityInfo<T>();
|
||||
foreach (var column in entityInfo.Columns)
|
||||
{
|
||||
if (!column.IsNullable)
|
||||
{
|
||||
nonBeNullColumnList.Add(column.DbColumnName);
|
||||
}
|
||||
}
|
||||
var importData = WmsImport(file); //ImportExcelToMemory<T> where T:BaseEntity<string>,new()
|
||||
var fillColumns = nonBeNullColumnList.Except(importData.Keys).ToList();
|
||||
if (fillColumns?.Count > 0)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// WMS导入
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
private static Dictionary<string, string> WmsImport(IFormFile file)
|
||||
{
|
||||
int rowIndex = 1;
|
||||
Dictionary<string, string> dic = 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)
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
|
||||
throw;
|
||||
}
|
||||
|
||||
return dic;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// WMS导入模板
|
||||
/// </summary>
|
||||
/// <param name="dic"></param>
|
||||
/// <param name="sheetName"></param>
|
||||
/// <returns></returns>
|
||||
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);
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user