160 lines
5.7 KiB
C#
160 lines
5.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using JNPF.Common.Configuration;
|
|
using JNPF.Common.Extension;
|
|
using JNPF.Common.Helper;
|
|
using JNPF.Common.Models;
|
|
using JNPF.Common.Security;
|
|
using JNPF.Extras.CollectiveOAuth.Enums;
|
|
using JNPF.FriendlyException;
|
|
using Mapster;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using NPOI.HPSF;
|
|
using NPOI.HSSF.UserModel;
|
|
using NPOI.SS.UserModel;
|
|
using NPOI.XSSF.UserModel;
|
|
using SqlSugar;
|
|
using Tnb.BasicData.Entities;
|
|
using Tnb.WarehouseMgr.Entities;
|
|
|
|
namespace Tnb.WarehouseMgr
|
|
{
|
|
public class WmsImportAndExportService
|
|
{
|
|
private readonly ISqlSugarClient _db;
|
|
public WmsImportAndExportService(ISqlSugarRepository<WmsCarryH> repository)
|
|
{
|
|
_db = repository.AsSugarClient();
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 载具导入
|
|
/// </summary>
|
|
/// <param name="file"></param>
|
|
/// <returns></returns>
|
|
public async Task<dynamic> CarryImport(IFormFile file)
|
|
{
|
|
List<Dictionary<string, string>> dics = WmsImport(file);
|
|
List<WmsCarryH> carrys = new List<WmsCarryH>();
|
|
var carryStds = await _db.Queryable<WmsCarrystd>().ToListAsync();
|
|
List<string> locCodes = new();
|
|
WmsCarryH carryH = new WmsCarryH();
|
|
if (carryStds != null)
|
|
{
|
|
string carryStdId = string.Empty;
|
|
foreach (var d in dics)
|
|
{
|
|
carryStdId = carryStds.Find(x => x.carrystd_code == d["carrystd_code"])!.id;
|
|
d.Remove("carrystd_code");
|
|
d.Add("carrystd_id", carryStdId);
|
|
locCodes.Add(d["location_code"]);
|
|
carryH = d.Adapt<WmsCarryH>();
|
|
}
|
|
var locs = await _db.Queryable<BasLocation>().Where(it=>locCodes.Contains(it.location_code)).ToListAsync();
|
|
}
|
|
return Task.FromResult(0);
|
|
}
|
|
|
|
/// <summary>
|
|
/// WMS导入
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
private static List<Dictionary<string, string>> WmsImport(IFormFile file)
|
|
{
|
|
int rowIndex = 1;
|
|
List<Dictionary<string, string>> dics = new List<Dictionary<string, string>>();
|
|
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);
|
|
Dictionary<string, string> dic = new Dictionary<string, string>();
|
|
|
|
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);
|
|
}
|
|
dics.Add(dic);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
|
|
throw;
|
|
}
|
|
|
|
return dics;
|
|
}
|
|
|
|
/// <summary>
|
|
/// WMS导入模板
|
|
/// </summary>
|
|
/// <param name="dics"></param>
|
|
/// <param name="sheetName"></param>
|
|
/// <returns></returns>
|
|
private static dynamic WmsTemplate(List<Dictionary<string, string>> dics, 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++)
|
|
{
|
|
ICell cell1 = nRow.CreateCell(i);
|
|
cell1.SetCellValue(dics[i].Keys.ToString());
|
|
ICell cell2 = dRow.CreateCell(i);
|
|
cell2.SetCellValue(dics[i].Values.ToString());
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|
|
}
|