129 lines
4.8 KiB
C#
129 lines
4.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
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;
|
|
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 JNPF.Logging;
|
|
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;
|
|
using Tnb.WarehouseMgr.Entities.Enums;
|
|
|
|
namespace Tnb.WarehouseMgr
|
|
{
|
|
/// <summary>
|
|
/// 导入和导出接口服务提供类
|
|
/// </summary>
|
|
public class WmsImportAndExportService : BaseWareHouseService
|
|
{
|
|
private readonly ISqlSugarClient _db;
|
|
private readonly IUserManager _userManager;
|
|
public WmsImportAndExportService(ISqlSugarRepository<WmsCarryH> repository, IUserManager userManager)
|
|
{
|
|
_db = repository.AsSugarClient();
|
|
_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;
|
|
try
|
|
{
|
|
//例1 获取所有表
|
|
|
|
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();
|
|
WmsCarryH carryH = new WmsCarryH();
|
|
if (carryStdDic?.Count > 0)
|
|
{
|
|
string carryStdId = string.Empty;
|
|
foreach (var d in dics)
|
|
{
|
|
var stdCodeKey = d["carrystd_code"].ToString();
|
|
carryStdId = carryStdDic.ContainsKey(stdCodeKey) ? carryStdDic[stdCodeKey]?.ToString() ?? "" : "";
|
|
d.Add("carrystd_id", carryStdId);
|
|
locCodes.Add(d["location_code"]?.ToString() ?? string.Empty);
|
|
carryH = d.Adapt<WmsCarryH>();
|
|
carrys.Add(carryH);
|
|
}
|
|
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)
|
|
{
|
|
var c = carrys.Find(x => x.location_code == loc);
|
|
if (c != null)
|
|
c.location_id = locs[loc].ToString();
|
|
}
|
|
}
|
|
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;
|
|
});
|
|
}
|
|
await _db.Ado.BeginTranAsync();
|
|
row = await _db.Insertable(carrys).ExecuteCommandAsync();
|
|
await _db.Ado.CommitTranAsync();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
await _db.Ado.RollbackTranAsync();
|
|
Log.Error("导入失败",ex)
|
|
throw Oops.Bah("导入失败");
|
|
}
|
|
return row > 0;
|
|
}
|
|
}
|
|
}
|