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.

418 lines
19 KiB
C#

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;
4 months ago
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;
using DS.WMS.Core.HangfireJob.Dtos;
using DS.Module.Core.Data;
namespace DS.WMS.Core.HangfireJob.Method
{
public class OpJobService: IOpJobService
4 months ago
{
private readonly IServiceProvider _serviceProvider;
private readonly ISqlSugarClient db;
private readonly IUser user;
private readonly ISaasDbService saasService;
private readonly IJobCommonService jobComService;
4 months ago
private static readonly Logger _logger = LogManager.GetCurrentClassLogger();
/// <summary>
///
/// </summary>
/// <param name="serviceProvider"></param>
public OpJobService(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
db = _serviceProvider.GetRequiredService<ISqlSugarClient>();
user = _serviceProvider.GetRequiredService<IUser>();
saasService = _serviceProvider.GetRequiredService<ISaasDbService>();
jobComService = _serviceProvider.GetRequiredService<IJobCommonService>();
4 months ago
}
public void VgmLinkJob(BaseJobReq req)
4 months ago
{
var tenantDb = saasService.GetBizJobDbScopeById(req);
tenantDb.QueryFilter.Clear<IOrgId>();
4 months ago
var bookingId = req.Id;
4 months ago
var order = tenantDb.Queryable<SeaExport>().First(x => x.Id == bookingId);
if (order == null)
{
2 months ago
//_logger.Error("海运出口信息不存在!");
4 months ago
throw new Exception("海运出口信息不存在!");
}
4 months ago
//if (!string.IsNullOrEmpty(orderUrl.UrlVgm))
//{
// return await Task.FromResult(DataResult<string[]>.Success(new string[] { orderUrl.UrlVgm, orderUrl.UrlVgmSi }));
//}
//校验船公司
if (string.IsNullOrEmpty(order.Carrier) || order.CarrierId == 0)
{
2 months ago
//_logger.Error("船公司未正确填写!");
4 months ago
throw new Exception("船公司未正确填写!");
}
var ctns = tenantDb.Queryable<OpCtn>().Where(x => x.BSNO == bookingId.ToString()).ToList();
4 months ago
//判断船公司是否支持
//db.Queryable<SysDictData>().Where(x => x.TypeId == 1790194688729419776).Select(x => x.Value).ToList();
var allowCarrier = db.Queryable<SysDictData>()
.InnerJoin<SysDictType>((a, b) => a.TypeId == b.Id).Where((a, b) => b.Code == "vgm_carrier_list").Select((a, b) => a.Value).ToList();
var carrCode = jobComService.GetCarrierCode(order.CarrierId, tenantDb);
4 months ago
if (!allowCarrier.Contains(carrCode))
{
2 months ago
//_logger.Error("不支持的船公司!");
4 months ago
throw new Exception("不支持的船公司!");
}
4 months ago
//船公司网站账号
var carrWebAccMap = db.Queryable<SysDictData>()
.InnerJoin<SysDictType>((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 = jobComService.GetCodeThirdParty(carrWebAccMap.Value, user.UserId, tenantDb);
4 months ago
if (webacc == null)
{
2 months ago
//_logger.Error("船公司VGM网站账号未配置!");
4 months ago
throw new Exception("船公司VGM网站账号未配置!");
}
4 months ago
//场站转换
var yardset = tenantDb.Queryable<MappingYard>().Where(x => x.Status == StatusEnum.Enable && x.Module == "BookingVgm" && x.LinkId == order.YardId).First();
if (yardset == null)
{
2 months ago
//_logger.Error("场站EDI配置未找到{0}\", $\"{order.Yard}(VGM)!");
4 months ago
throw new Exception("场站EDI配置未找到{0}\", $\"{order.Yard}(VGM)");
}
//校验箱子数据录入
if (ctns.Where(c => string.IsNullOrEmpty(c.CtnAll) || !c.CtnNum.HasValue).Count() > 0)
{
2 months ago
//_logger.Error("所有箱型箱量必须录入完整!");
4 months ago
throw new Exception("所有箱型箱量必须录入完整!");
}
//箱型映射
var ctnMapping = tenantDb.Queryable<MappingCtn>().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)
{
2 months ago
//_logger.Error("箱型EDI配置未找到{0}\", $\"{string.Join(',', expCode)}(VGM)!");
4 months ago
throw new Exception("箱型EDI配置未找到{0}\", $\"{string.Join(',', expCode)}(VGM)!");
}
var userInfo = db.Queryable<SysUser>().Filter(null, true).First(x => x.Id == req.UserId);
var tenant = db.Queryable<SysTenant>().Filter(null, true).First(x => x.Id == req.TenantId);
4 months ago
var destinationCode = jobComService.GetPortCode(order.DestinationId, tenantDb);
var loadPortCode = jobComService.GetPortCode(order.LoadPortId, tenantDb);
var dischargePortCode = jobComService.GetPortCode(order.DischargePortId, tenantDb);
4 months ago
//调用vgm链接服务
var postUrl = db.Queryable<SysConfig>().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)
{
_logger.Error(jobjResp.GetStringValue("message"));
4 months ago
throw new Exception(jobjResp.GetStringValue("message"));
}
try
{
tenantDb.Ado.BeginTran();
var orderUrl = tenantDb.Queryable<BookingOrderUrl>().First(x => x.BusinessId == bookingId);
if (orderUrl == null)
{
orderUrl = new BookingOrderUrl()
{
BusinessId = bookingId
};
tenantDb.Insertable(orderUrl).ExecuteCommand();
}
//保存url
var memoData = jobjResp.GetJObjectValue("memoData");
orderUrl.UrlVgm = memoData.GetStringValue("vgmUrl");
orderUrl.UrlVgmSi = memoData.GetStringValue("vgmAndSiUrl");
tenantDb.Updateable(orderUrl).ExecuteCommand();
4 months ago
//货运动态
var bookingStatus = new BookingStatusLog()
{
BusinessId = bookingId,
Status = $"生成VGM链接",
Group = "ship",
OpTime = DateTime.Now,
MBLNO = order.MBLNO
};
tenantDb.Insertable(bookingStatus).ExecuteCommand();
tenantDb.Ado.CommitTran();
}
catch (Exception ex)
4 months ago
{
tenantDb.Ado.RollbackTran();
throw new Exception(ex.ToString());
}
}
public void TxxpLinkJob(BaseJobReq req)
{
var tenantDb = saasService.GetBizJobDbScopeById(req);
tenantDb.QueryFilter.Clear<IOrgId>();
var bookingId = req.Id;
var order = tenantDb.Queryable<SeaExport>().First(x => x.Id == bookingId);
//校验船公司
if (string.IsNullOrEmpty(order.Carrier) || order.CarrierId == 0)
{
2 months ago
//_logger.Error("船公司未正确填写!");
throw new Exception("船公司未正确填写!");
}
var ctns = tenantDb.Queryable<OpCtn>().Where(x => x.BSNO == bookingId.ToString()).ToList();
//判断船公司是否支持
//db.Queryable<SysDictData>().Where(x => x.TypeId == 1790194688729419776).Select(x => x.Value).ToList();
var allowCarrier = db.Queryable<SysDictData>()
.InnerJoin<SysDictType>((a, b) => a.TypeId == b.Id).Where((a, b) => b.Code == "vgm_carrier_list").Select((a, b) => a.Value).ToList();
var carrCode = jobComService.GetCarrierCode(order.CarrierId, tenantDb);
if (!allowCarrier.Contains(carrCode)) {
2 months ago
//_logger.Error("不支持的船公司!" + carrCode);
throw new Exception("不支持的船公司!" + carrCode);
}
var carrMap = tenantDb.Queryable<MappingCarrier>().Where(y => y.Code == carrCode && y.Module == "BookingTxxp").First();
if (carrMap != null)
{
carrCode = carrMap.MapCode;
}
//根据订舱代理获取账号类型
var ytMapList = db.Queryable<SysDictData>()
.InnerJoin<SysDictType>((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 = jobComService.GetClientCode(order.ForwarderId, tenantDb);
var ytmap = ytMapList.FirstOrDefault(x => x.Value == forwarderCode);
if (ytmap == null)
{
2 months ago
//_logger.Error($"订舱代理 {order.Forwarder} 对应的网站账号类型未配置");
throw new Exception($"订舱代理 {order.Forwarder} 对应的网站账号类型未配置");
}
//亿通账号
var ytAcc = jobComService.GetCodeThirdParty("YitongWeb", user.UserId, tenantDb, order.ForwarderId);
if (ytAcc == null)
{
2 months ago
//_logger.Error("亿通账号未配置");
throw new Exception("亿通账号未配置");
}
//场站转换
var yardsetList = tenantDb.Queryable<MappingYard>().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)
{
2 months ago
//_logger.Error(String.Format("场站EDI配置未找到{0}", $"{order.Yard} {order.Carrier}(提箱小票)"));
throw new Exception(String.Format("场站EDI配置未找到{0}", $"{order.Yard} {order.Carrier}(提箱小票)"));
}
var yardset = yardsetList.FirstOrDefault(x => x.Code == carrCode);
if (yardset == null)
{
yardset = yardsetList.FirstOrDefault(x => x.Code == null || x.Code == "");
}
//校验箱子数据录入
if (ctns.Where(c => string.IsNullOrEmpty(c.CtnAll) || !c.CtnNum.HasValue).Count() > 0)
{
2 months ago
//_logger.Error("所有箱型箱量必须录入完整!");
throw new Exception("所有箱型箱量必须录入完整!");
}
//箱型映射
var ctnMapping = tenantDb.Queryable<MappingCtn>().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();
2 months ago
//_logger.Error("箱型EDI配置未找到{0}\", $\"{string.Join(',', expCode)}(提箱小票)!");
throw new Exception("箱型EDI配置未找到{0}\", $\"{string.Join(',', expCode)}(提箱小票)!");
}
var userInfo = db.Queryable<SysUser>().Filter(null, true).First(x => x.Id == req.UserId);
var tenant = db.Queryable<SysTenant>().Filter(null, true).First(x => x.Id == req.TenantId);
//调用小票服务
var postUrl = db.Queryable<SysConfig>().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)
{
_logger.Error(jobjResp.GetStringValue("message"));
throw new Exception(jobjResp.GetStringValue("message"));
}
try
{
tenantDb.Ado.BeginTran();
var orderUrl = tenantDb.Queryable<BookingOrderUrl>().First(x => x.BusinessId == bookingId);
if (orderUrl == null)
{
orderUrl = new BookingOrderUrl()
{
BusinessId = bookingId
};
tenantDb.Insertable(orderUrl).ExecuteCommand();
}
var addUrlFlag = string.IsNullOrEmpty(orderUrl.UrlTxxp) ? true : false; //新生成还是更新链接
//保存url
//保存url
var txxpUrl = jobjResp.GetStringValue("data");
orderUrl.UrlTxxp = txxpUrl;
tenantDb.Updateable(orderUrl).ExecuteCommand();
//货运动态
var bookingStatus = new BookingStatusLog()
{
BusinessId = bookingId,
Status = $"{(addUrlFlag ? "" : "")}提箱小票链接",
Group = "ship",
OpTime = DateTime.Now,
MBLNO = order.MBLNO
};
tenantDb.Insertable(bookingStatus).ExecuteCommand();
tenantDb.Ado.CommitTran();
}
catch (Exception ex)
{
tenantDb.Ado.RollbackTran();
throw new Exception(ex.ToString());
}
}
}
}