You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

224 lines
8.9 KiB
C#

using DS.Module.Core;
using DS.WMS.Core.Code.Entity;
using DS.WMS.Core.Map.Entity;
using DS.WMS.Core.Op.Dtos;
using DS.WMS.Core.Op.Entity;
using Newtonsoft.Json;
using System.Text;
using System.Text.RegularExpressions;
namespace DS.WMS.Core.Op.Method
{
/// <summary>
/// 海运出口VOLTA相关接口
/// </summary>
public partial class SeaExportService
{
#region 检查是否VOLTA并返回初始数据
/// <summary>
/// 检查是否VOLTA并返回初始数据
/// </summary>
/// <param name="req">请求参数</param>
/// <returns>返回回执</returns>
public DataResult<VOLTAEDIBaseModel> CheckAndInitVOLTAEdi(BookingOrClosingEDIOrderReq req)
{
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
/*
VOLTA
1
2VOL
3VOLFCSREL
*/
string batchNo = GuidHelper.NewGuidFormatN();
_logger.Info("批次={no}获取请求VOLTA截单详情 {msg}", batchNo, JsonConvert.SerializeObject(req));
if (!req.SendType.Equals("E", StringComparison.OrdinalIgnoreCase))
return DataResult<VOLTAEDIBaseModel>.Failed($"只接受发送类型截单E");
if (req.Id == 0)
return DataResult<VOLTAEDIBaseModel>.Failed($"订单Id不能为空");
var order = tenantDb.Queryable<SeaExport>().First(x => x.Id == req.Id);
if (order == null)
return DataResult<VOLTAEDIBaseModel>.Failed($"获取订单信息失败");
if (order.ParentId == 0)
return DataResult<VOLTAEDIBaseModel>.Failed($"获取当前订单为分单不能生成EDI信息");
_logger.Info("批次={no}提取订单信息完成", batchNo);
var carrCode = seaComService.GetClientCode(order.CarrierId, tenantDb);
if (!carrCode.Equals("VOL", StringComparison.OrdinalIgnoreCase) && !order.Carrier.Equals("FCS", StringComparison.OrdinalIgnoreCase))
{
return DataResult<VOLTAEDIBaseModel>.Failed($"当前订单的船公司不是VOL(沃尔塔航运)不能发送VOLTA截单EDI");
}
if (string.IsNullOrWhiteSpace(order.MBLNO))
return DataResult<VOLTAEDIBaseModel>.Failed($"提单号不能为空");
if (!order.MBLNO.StartsWith("VOL") && !order.MBLNO.StartsWith("FCS") && !order.MBLNO.StartsWith("REL"))
{
return DataResult<VOLTAEDIBaseModel>.Failed($"只支持提单号开头是VOL、FCS、REL发送截单");
}
//箱信息
var ctns = tenantDb.Queryable<OpCtn>().Where(x => x.BSNO == order.Id.ToString()).ToList();
//VOLTAEDIBaseModel voltaModel = order.Adapt<VOLTAEDIBaseModel>();
var voltaModel = new VOLTAEDIBaseModel()
{
BookingId = order.MBLNO,
id = order.Id,
ShpperName = order.ShipperContent,
ConsigneeName = order.ConsigneeContent,
NotifyName = order.NotifyPartyContent,
LoadPort = order.LoadPort,
DischargePort = order.DischargePort,
FinalDestination = order.Destination,
PlaceOfDelivery = order.DeliveryPlace,
CargoDescription = order.Description,
Marks = order.Marks,
Vessel = order.Vessel,
VoyNo = order.Voyno
};
#region 拆分收、发、通(名称、地址拆分)
if (!string.IsNullOrWhiteSpace(voltaModel.ShpperName))
{
string name = voltaModel.ShpperName;
voltaModel.ShpperName = GetShipCneeNotifyDetail(name);
voltaModel.ShpperAddr = GetShipCneeNotifyDetail(name, "addr");
}
if (!string.IsNullOrWhiteSpace(voltaModel.ConsigneeName))
{
string name = voltaModel.ConsigneeName;
voltaModel.ConsigneeName = GetShipCneeNotifyDetail(name);
voltaModel.ConsigneeAddr = GetShipCneeNotifyDetail(name, "addr");
}
if (!string.IsNullOrWhiteSpace(voltaModel.NotifyName))
{
string name = voltaModel.NotifyName;
voltaModel.NotifyName = GetShipCneeNotifyDetail(name);
voltaModel.NotifyAddr = GetShipCneeNotifyDetail(name, "addr");
}
#endregion
if (voltaModel.CargoDescription.EndsWith("\n"))
voltaModel.CargoDescription = voltaModel.CargoDescription.Substring(0, voltaModel.CargoDescription.Length - 1);
if (voltaModel != null)
{
var list = new List<VOLTAEDIContaModel>();
foreach (var item in ctns)
{
list.Add(new VOLTAEDIContaModel()
{
ContaNo = item.CntrNo,
SealNo = item.SealNo,
GWt = item.KGS,
NWt = item.TareWeight,
Qty = item.PKGS,
CBM = item.CBM,
Pkgs = item.KindPkgs
});
}
}
//voltaModel.ContaList = ctns.Adapt<List<VOLTAEDIContaModel>>();
StringBuilder pkgsBuilder = new StringBuilder();
//还需要翻译箱明细的包装
if (voltaModel.ContaList != null && voltaModel.ContaList.Count > 0)
{
List<string> origPkgsList = voltaModel.ContaList.Select(x => x.Pkgs).Distinct().ToList();
//包装基础数据
var basePkgsList = tenantDb.Queryable<CodePackage>().ToList();
var ediPkgsList = tenantDb.Queryable<MappingPackage>().Where(t => t.Module == "BOOK_CLOSING_VOLTA"
&& t.CarrierId.HasValue && t.CarrierId == order.CarrierId)
.ToList();
int startNo = 1;
voltaModel.ContaList.ForEach(x =>
{
x.SNo = startNo;
var curBasePkgs = basePkgsList.FirstOrDefault(p => p.PackageName.Equals(x.Pkgs, StringComparison.OrdinalIgnoreCase));
if (curBasePkgs == null)
pkgsBuilder.Append($"包装{x.Pkgs}的基础数据代码未找到");
if (curBasePkgs != null)
{
var ediPkgs = ediPkgsList.FirstOrDefault(x => x.Code.Equals(curBasePkgs.EdiCode, StringComparison.OrdinalIgnoreCase));
if (ediPkgs == null || string.IsNullOrWhiteSpace(ediPkgs.MapCode))
pkgsBuilder.AppendLine($"包装{x.Pkgs}的EDI代码未找到");
if (ediPkgs != null)
{
x.EdiPkgs = ediPkgs.MapCode?.Trim();
}
}
if (string.IsNullOrWhiteSpace(x.EdiPkgs))
x.EdiPkgs = string.Empty;
startNo++;
});
}
if (pkgsBuilder.Length > 0)
{
_logger.Info("批次={no}获取请求VOLTA截单详情有错误 {msg}", batchNo, JsonConvert.SerializeObject(req));
return DataResult<VOLTAEDIBaseModel>.Failed("获取请求VOLTA截单详情有错误:" + JsonConvert.SerializeObject(req));
}
return DataResult<VOLTAEDIBaseModel>.Success(voltaModel);
}
#endregion
#region 提取收、发、通详情
/// <summary>
/// 提取收、发、通详情
/// </summary>
/// <param name="name">收、发、通信息</param>
/// <param name="location">内容定位name-名称addr-地址)</param>
/// <returns>返回截取字符</returns>
private string GetShipCneeNotifyDetail(string name, string location = "name")
{
string result = string.Empty;
try
{
if (location == "name")
{
result = Regex.Match(name, ".*(?=\\n)").Value;
}
else if (location == "addr")
{
string s = Regex.Match(name, ".*(?=\\n)").Value;
result = name.Replace(s, "");
if (Regex.IsMatch(result, "^\\n"))
{
result = Regex.Replace(result, "^\\n", "");
}
//result = Regex.Match(name, $"(?<={s}).*").Value;
}
}
catch (Exception ex)
{
_logger.Info($"{nameof(GetShipCneeNotifyDetail)} 提取收、发、通详情异常,原因:{ex.Message}");
}
return result;
}
#endregion
}
}