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
{
///
/// 海运出口VOLTA相关接口
///
public partial class SeaExportService
{
#region 检查是否VOLTA并返回初始数据
///
/// 检查是否VOLTA并返回初始数据
///
/// 请求参数
/// 返回回执
public DataResult CheckAndInitVOLTAEdi(BookingOrClosingEDIOrderReq req)
{
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
/*
判断当前单子是否满足执行VOLTA截单流程
1、是截单请求
2、船公司是VOL。
3、提单号开头是(VOL、FCS、REL)
*/
string batchNo = GuidHelper.NewGuidFormatN();
_logger.Info("批次={no}获取请求VOLTA截单详情 {msg}", batchNo, JsonConvert.SerializeObject(req));
if (!req.SendType.Equals("E", StringComparison.OrdinalIgnoreCase))
return DataResult.Failed($"只接受发送类型截单(E)");
if (req.Id == 0)
return DataResult.Failed($"订单Id不能为空");
var order = tenantDb.Queryable().First(x => x.Id == req.Id);
if (order == null)
return DataResult.Failed($"获取订单信息失败");
if (order.ParentId == 0)
return DataResult.Failed($"获取当前订单为分单不能生成EDI信息");
_logger.Info("批次={no}提取订单信息完成", batchNo);
if (!order.Carrier.Equals("VOL", StringComparison.OrdinalIgnoreCase) && !order.Carrier.Equals("FCS", StringComparison.OrdinalIgnoreCase))
{
return DataResult.Failed($"当前订单的船公司不是VOL(沃尔塔航运)不能发送VOLTA截单EDI");
}
if (string.IsNullOrWhiteSpace(order.MBLNO))
return DataResult.Failed($"提单号不能为空");
if (!order.MBLNO.StartsWith("VOL") && !order.MBLNO.StartsWith("FCS") && !order.MBLNO.StartsWith("REL"))
{
return DataResult.Failed($"只支持提单号开头是(VOL、FCS、REL)发送截单");
}
//箱信息
var ctns = tenantDb.Queryable().Where(x => x.BSNO == order.Id.ToString()).ToList();
//VOLTAEDIBaseModel voltaModel = order.Adapt();
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();
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>();
StringBuilder pkgsBuilder = new StringBuilder();
//还需要翻译箱明细的包装
if (voltaModel.ContaList != null && voltaModel.ContaList.Count > 0)
{
List origPkgsList = voltaModel.ContaList.Select(x => x.Pkgs).Distinct().ToList();
//包装基础数据
var basePkgsList = tenantDb.Queryable().ToList();
var ediPkgsList = tenantDb.Queryable().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.Failed("获取请求VOLTA截单详情有错误:" + JsonConvert.SerializeObject(req));
}
return DataResult.Success(voltaModel);
}
#endregion
#region 提取收、发、通详情
///
/// 提取收、发、通详情
///
/// 收、发、通信息
/// 内容定位(name-名称;addr-地址)
/// 返回截取字符
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
}
}