using DS.Module.SqlSugar;
using DS.Module.UserModule;
using DS.WMS.Core.HangfireJob.Interface;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using DS.Module.Core.Helpers;
using DS.Module.Core;
using DS.WMS.Core.Map.Entity;
using DS.WMS.Core.Op.Entity;
using DS.WMS.Core.Sys.Entity;
using Newtonsoft.Json.Linq;
using DS.WMS.Core.Op.Interface;
using Logger = NLog.Logger;
using NLog;
using DS.Module.Core.Extensions;
namespace DS.WMS.Core.HangfireJob.Method
{
public class OpJobService: IOpJobService
{
private readonly IServiceProvider _serviceProvider;
private readonly ISqlSugarClient db;
private readonly IUser user;
private readonly ISaasDbService saasService;
private readonly ISeaExportCommonService seaComService;
private static readonly Logger _logger = LogManager.GetCurrentClassLogger();
///
///
///
///
public OpJobService(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
db = _serviceProvider.GetRequiredService();
user = _serviceProvider.GetRequiredService();
saasService = _serviceProvider.GetRequiredService();
seaComService = _serviceProvider.GetRequiredService();
}
public async void GetVgmLink(long id)
{
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
var bookingId = id;
var order = tenantDb.Queryable().First(x => x.Id == bookingId);
if (order == null)
{
throw new Exception("海运出口信息不存在!");
}
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(orderUrl.UrlVgm))
//{
// return await Task.FromResult(DataResult.Success(new string[] { orderUrl.UrlVgm, orderUrl.UrlVgmSi }));
//}
//校验船公司
if (string.IsNullOrEmpty(order.Carrier) || order.CarrierId == 0)
{
throw new Exception("船公司未正确填写!");
}
var ctns = tenantDb.Queryable().Where(x => x.BSNO == id.ToString()).ToList();
//判断船公司是否支持
//db.Queryable().Where(x => x.TypeId == 1790194688729419776).Select(x => x.Value).ToList();
var allowCarrier = db.Queryable()
.InnerJoin((a, b) => a.TypeId == b.Id).Where((a, b) => b.Code == "vgm_carrier_list").Select((a, b) => a.Value).ToList();
var carrCode = seaComService.GetClientCode(order.CarrierId, tenantDb);
if (!allowCarrier.Contains(carrCode))
throw new Exception("不支持的船公司!");
//船公司网站账号
var carrWebAccMap = db.Queryable()
.InnerJoin((a, b) => a.TypeId == b.Id).Where((a, b) => b.Code == "carrier_web_account_mapping" && a.Name == carrCode)
.Select((a, b) => new { Value = a.Value, Name = a.Name }).First();
//x.CustomerId == order.CarrierId &&
var webacc = seaComService.GetCodeThirdParty(carrWebAccMap.Value, user.UserId, tenantDb);
if (webacc == null)
throw new Exception("船公司VGM网站账号未配置!");
//场站转换
var yardset = tenantDb.Queryable().Where(x => x.Status == StatusEnum.Enable && x.Module == "BookingVgm" && x.LinkId == order.YardId).First();
if (yardset == null)
{
throw new Exception("场站EDI配置未找到:{0}\", $\"{order.Yard}(VGM)");
}
//校验箱子数据录入
if (ctns.Where(c => string.IsNullOrEmpty(c.CtnAll) || !c.CtnNum.HasValue).Count() > 0)
{
throw new Exception("所有箱型箱量必须录入完整!");
}
//箱型映射
var ctnMapping = tenantDb.Queryable().Where(x => x.Module == "BookingVgm").ToList();
//if (ctnMapping.Count == 0)
//{
// throw Oops.Bah(BookingErrorCode.BOOK122);
//}
var expCode = ctns.Select(x => x.CtnCode).Distinct().Except(ctnMapping.Select(y => y.Code)).ToList();
if (expCode.Count > 0)
{
throw new Exception("箱型EDI配置未找到:{0}\", $\"{string.Join(',', expCode)}(VGM)!");
}
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 destinationCode = seaComService.GetPortCode(order.DestinationId, tenantDb);
var loadPortCode = seaComService.GetPortCode(order.LoadPortId, tenantDb);
var dischargePortCode = seaComService.GetPortCode(order.DischargePortId, tenantDb);
//调用vgm链接服务
var postUrl = db.Queryable().Filter(null, true).Where(x => x.Code == "vgm_link_service" && x.TenantId == 1288018625843826688).First().Value;
var postObj = new
{
SystemCode = "djy_hechuan",
billOrderId = order.Id.ToString(),
sendOrderCode = order.MBLNO,
customerName = $"{tenant.Name}+{userInfo.UserName}", //公司名称+用户姓名
customerId = order.CustomerId.ToString(),
agentName = string.IsNullOrEmpty(order.Forwarder) ? tenant.Name : order.Forwarder,
carrierCode = carrCode,
userName = webacc.AppKey,
userPassword = webacc.AppSecret,
depotCode = yardset.MapCode,
depotName = yardset.MapName,
linkName = userInfo.UserName,
linkMobile = userInfo.Phone,
linkEmail = userInfo.Email,
//userId = userInfo.DjyUserId,
userId = order.SaleId,
signatory = userInfo.UserName,
//returnUrl = dicUrlVgmResp?.Value,
shipName = order.Vessel,
voyNo = order.Voyno,
etdstr = order.ETD.HasValue ? order.ETD.Value.ToString("yyyy-MM-dd") : string.Empty,
potrSend = order.LoadPort,
potrGoal = order.DischargePort,
boxinfoStr = order.CntrTotal.Replace("'", ""),
vgmEndTimeStr = order.CloseVgmDate.HasValue ? order.CloseVgmDate.Value.ToString("yyyy-MM-dd") : string.Empty,
returnOkUrl = "",
//SiReturnUrl = dicUrlSiResp?.Value,
DataInfoJson = new
{
order.Shipper,
order.Consignee,
order.NotifyParty,
order.Marks,
order.Description,
order.MBLFrt,
order.Service,
order.IssueType,
order.TransPort,
order.TransPortCode,
order.Destination,
//order.DestinationId,
destinationCode,
order.KindPkgs,
order.ThirdPayAt,
order.LoadPort,
//order.LoadPortId,
loadPortCode,
order.DischargePort,
//order.DischargePortId
dischargePortCode
}
};
string strPostObj = postObj.ToJsonString();
_logger.Info($"调用VGM链接接口 {postUrl} 传递数据:{strPostObj}");
var result = RequestHelper.Post(strPostObj, postUrl);
_logger.Info($"调用VGM链接接口返回:{result}");
var jobjResp = JObject.Parse(result);
int respCode = jobjResp.GetIntValue("code");
if (respCode != 200)
{
throw new Exception(jobjResp.GetStringValue("message"));
}
//保存url
var memoData = jobjResp.GetJObjectValue("memoData");
orderUrl.UrlVgm = memoData.GetStringValue("vgmUrl");
orderUrl.UrlVgmSi = memoData.GetStringValue("vgmAndSiUrl");
await tenantDb.Updateable(orderUrl).ExecuteCommandAsync();
//货运动态
var bookingStatus = new BookingStatusLog()
{
BusinessId = bookingId,
Status = $"生成VGM链接",
Group = "ship",
OpTime = DateTime.Now,
MBLNO = order.MBLNO
};
await tenantDb.Insertable(bookingStatus).ExecuteCommandAsync();
}
}
}