using DS.Module.Core;
using DS.Module.Core.Extensions;
using DS.Module.Core.Helpers;
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 DS.WMS.Core.Sys.Entity;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace DS.WMS.Core.Op.Method
{
///
/// 海运出口提箱小票相关接口
///
public partial class SeaExportService
{
#region 小票链接
///
/// 获取提箱小票链接
///
/// 业务Id
///
public async Task> TxxpLink(string id)
{
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
var bookingId = long.Parse(id);
var order = tenantDb.Queryable().First(x => x.Id == bookingId);
if (order == null)
{
return await Task.FromResult(DataResult.Failed("海运出口信息不存在!", MultiLanguageConst.SeaExportNotExist));
}
var orderUrl = tenantDb.Queryable().First(x => x.BusinessId == bookingId);
if (orderUrl == null)
{
orderUrl = new BookingOrderUrl()
{
BusinessId = bookingId
};
await tenantDb.Insertable(orderUrl).ExecuteCommandAsync();
}
//校验船公司
if (string.IsNullOrEmpty(order.Carrier)||order.CarrierId == 0 || order.CarrierId.IsNull())
{
return await Task.FromResult(DataResult.Failed("船公司未正确填写!"));
}
//判断船公司是否支持 1790295446942519296
var allowCarrier = db.Queryable()
.InnerJoin((a, b) => a.TypeId == b.Id).Where((a, b) => b.Code == "txxp_carrier_list").Select((a, b) => a.Value).ToList();
var carrCode = seaComService.GetClientCode(order.CarrierId, tenantDb);
if (!allowCarrier.Contains(carrCode))
return await Task.FromResult(DataResult.Failed("不支持的船公司"));
var carrMap = tenantDb.Queryable().Where(y => y.Code == order.Carrier && y.Module == "BookingTxxp").First();
if (carrMap != null)
{
carrCode = carrMap.MapCode;
}
//根据订舱代理获取账号类型
var ytMapList = db.Queryable()
.InnerJoin((a, b) => a.TypeId == b.Id).Where((a, b) => b.Code == "txxp_yitong_forwarder_map")
.Select((a, b) => new { Value = a.Value, Name = a.Name }).ToList();
var forwarderCode = seaComService.GetClientCode(order.ForwarderId, tenantDb);
var ytmap = ytMapList.FirstOrDefault(x => x.Value == forwarderCode);
if (ytmap == null)
{
return await Task.FromResult(DataResult.Failed($"订舱代理 {order.Forwarder} 对应的网站账号类型未配置"));
}
//亿通账号
var ytAcc = seaComService.GetCodeThirdParty("YitongWeb", user.UserId, tenantDb, order.ForwarderId);
if (ytAcc == null)
return await Task.FromResult(DataResult.Failed("亿通账号未配置"));
//场站转换
var yardsetList = tenantDb.Queryable().Where(x => x.Status == StatusEnum.Enable && x.Module == "BookingTxxp" && x.LinkId == order.YardId && (x.CarrierId == 0 || x.CarrierId == order.CarrierId)).ToList();
if (yardsetList.Count == 0)
{
return await Task.FromResult(DataResult.Failed(String.Format("场站EDI配置未找到:{0}", $"{order.Yard} {order.Carrier}(提箱小票)")));
}
var yardset = yardsetList.FirstOrDefault(x => x.Code == order.Carrier);
if (yardset == null)
{
yardset = yardsetList.FirstOrDefault(x => x.Code == null || x.Code == "");
}
var ctns = tenantDb.Queryable().Where(x => x.BSNO == id).ToList();
//校验箱子数据录入
if (ctns.Where(c => string.IsNullOrEmpty(c.CtnAll) || !c.CtnNum.HasValue).Count() > 0)
{
return await Task.FromResult(DataResult.Failed("所有箱型箱量必须录入完整!"));
}
//箱型映射
var ctnMapping = tenantDb.Queryable().Where(x => x.Module == "BookingTxxp").ToList();
var expCode = ctns.Select(x => x.CtnCode).Distinct().Except(ctnMapping.Select(y => y.Code)).ToList();
if (expCode.Count > 0)
{
var expName = ctns.Where(x => expCode.Contains(x.CtnCode)).Select(x => x.CtnAll).Distinct().ToList();
return await Task.FromResult(DataResult.Failed(String.Format("箱型EDI配置未找到:{0}", $"{string.Join(',', expCode)}(提箱小票)")));
}
var userInfo = await db.Queryable().Filter(null, true).FirstAsync(x => x.Id == long.Parse(user.UserId));
var tenant = db.Queryable().Filter(null, true).First(x => x.Id == long.Parse(user.TenantId));
//调用小票服务
var postUrl = db.Queryable().Filter(null, true).Where(x => x.Code == "txxp_service" && x.TenantId == 1288018625843826688).First().Value;
var postObj = new
{
SystemCode = "djy_hechuan",
CarrierCode = carrCode,
billOrderId = order.Id.ToString(),
sendOrderCode = order.MBLNO,
//userId = userInfo.DjyUserId,
userId = order.SaleId,
customerId = order.CustomerId.ToString(),
// userName = currUser.CODENAME,
//userPassword = currUser.PASSWORD,
operatorName = userInfo.UserName,
depotCode = yardset.MapCode,
depotName = yardset.MapName,
AgentName = tenant.Name,
linkName = userInfo.UserName,
linkMobile = userInfo.Phone,
CustomerName = $"{tenant.Name}+{userInfo.UserName}", //公司名称+用户姓名
memo = string.Empty,
boxInfo = ctns.Select(c =>
{
var mapCtn = ctnMapping.First(x => x.Code == c.CtnCode);
return new
{
boxType = mapCtn == null ? c.CtnAll : mapCtn.MapCode,
boxCount = c.CtnNum.Value
};
}),
sysLoginName = ytAcc.AppKey,
sysPsssword = ytAcc.AppSecret,
systemCode = "Dongsheng8"
};
string strPostObj = postObj.ToJsonString();
_logger.Info($"调用提箱小票接口传递数据:{strPostObj}");
var result = RequestHelper.Post(strPostObj, postUrl);
_logger.Info($"调用提箱小票接口返回:{result}");
var jobjResp = JObject.Parse(result);
int respCode = jobjResp.GetIntValue("code");
if (respCode != 200)
{
return await Task.FromResult(DataResult.Failed(jobjResp.GetStringValue("message")));
}
var addUrlFlag = string.IsNullOrEmpty(orderUrl.UrlTxxp) ? true : false; //新生成还是更新链接
//保存url
//保存url
var txxpUrl = jobjResp.GetStringValue("data");
orderUrl.UrlTxxp = txxpUrl;
await tenantDb.Updateable(orderUrl).ExecuteCommandAsync();
//货运动态
var bookingStatus = new BookingStatusLog()
{
BusinessId = bookingId,
Status = $"{(addUrlFlag ? "生成" : "更新")}提箱小票链接",
Group = "ship",
OpTime = DateTime.Now,
MBLNO = order.MBLNO
};
await tenantDb.Insertable(bookingStatus).ExecuteCommandAsync();
return await Task.FromResult(DataResult.Success(txxpUrl));
}
#endregion
}
}