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; using DS.WMS.Core.HangfireJob.Dtos; using DS.Module.Core.Data; 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 IJobCommonService jobComService; private static readonly Logger _logger = LogManager.GetCurrentClassLogger(); /// /// /// /// public OpJobService(IServiceProvider serviceProvider) { _serviceProvider = serviceProvider; db = _serviceProvider.GetRequiredService(); user = _serviceProvider.GetRequiredService(); saasService = _serviceProvider.GetRequiredService(); jobComService = _serviceProvider.GetRequiredService(); } public async void VgmLinkJob(BaseJobReq req) { var tenantDb = saasService.GetBizJobDbScopeById(req); tenantDb.QueryFilter.Clear(); var bookingId = req.Id; var order = tenantDb.Queryable().First(x => x.Id == bookingId); if (order == null) { _logger.Error("海运出口信息不存在!"); 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) { _logger.Error("船公司未正确填写!"); throw new Exception("船公司未正确填写!"); } var ctns = tenantDb.Queryable().Where(x => x.BSNO == bookingId.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 = jobComService.GetCarrierCode(order.CarrierId, tenantDb); if (!allowCarrier.Contains(carrCode)) { _logger.Error("不支持的船公司!"); 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 = jobComService.GetCodeThirdParty(carrWebAccMap.Value, user.UserId, tenantDb); if (webacc == null) { _logger.Error("船公司VGM网站账号未配置!"); 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) { _logger.Error("场站EDI配置未找到:{0}\", $\"{order.Yard}(VGM)!"); throw new Exception("场站EDI配置未找到:{0}\", $\"{order.Yard}(VGM)"); } //校验箱子数据录入 if (ctns.Where(c => string.IsNullOrEmpty(c.CtnAll) || !c.CtnNum.HasValue).Count() > 0) { _logger.Error("所有箱型箱量必须录入完整!"); 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) { _logger.Error("箱型EDI配置未找到:{0}\", $\"{string.Join(',', expCode)}(VGM)!"); throw new Exception("箱型EDI配置未找到:{0}\", $\"{string.Join(',', expCode)}(VGM)!"); } var userInfo = await db.Queryable().Filter(null, true).FirstAsync(x => x.Id == req.UserId); var tenant = db.Queryable().Filter(null, true).First(x => x.Id == req.TenantId); var destinationCode = jobComService.GetPortCode(order.DestinationId, tenantDb); var loadPortCode = jobComService.GetPortCode(order.LoadPortId, tenantDb); var dischargePortCode = jobComService.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) { _logger.Error(jobjResp.GetStringValue("message")); 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(); } public async void TxxpLinkJob(BaseJobReq req) { var tenantDb = saasService.GetBizJobDbScopeById(req); tenantDb.QueryFilter.Clear(); var bookingId = req.Id; var order = tenantDb.Queryable().First(x => x.Id == bookingId); 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) { _logger.Error("船公司未正确填写!"); throw new Exception("船公司未正确填写!"); } var ctns = tenantDb.Queryable().Where(x => x.BSNO == bookingId.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 = jobComService.GetCarrierCode(order.CarrierId, tenantDb); if (!allowCarrier.Contains(carrCode)) { _logger.Error("不支持的船公司!" + carrCode); throw new Exception("不支持的船公司!" + carrCode); } var carrMap = tenantDb.Queryable().Where(y => y.Code == carrCode && 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 = jobComService.GetClientCode(order.ForwarderId, tenantDb); var ytmap = ytMapList.FirstOrDefault(x => x.Value == forwarderCode); if (ytmap == null) { _logger.Error($"订舱代理 {order.Forwarder} 对应的网站账号类型未配置"); throw new Exception($"订舱代理 {order.Forwarder} 对应的网站账号类型未配置"); } //亿通账号 var ytAcc = jobComService.GetCodeThirdParty("YitongWeb", user.UserId, tenantDb, order.ForwarderId); if (ytAcc == null) { _logger.Error("亿通账号未配置"); throw new Exception("亿通账号未配置"); } //场站转换 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) { _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) { _logger.Error("所有箱型箱量必须录入完整!"); throw new Exception("所有箱型箱量必须录入完整!"); } //箱型映射 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(); _logger.Error("箱型EDI配置未找到:{0}\", $\"{string.Join(',', expCode)}(提箱小票)!"); throw new Exception("箱型EDI配置未找到:{0}\", $\"{string.Join(',', expCode)}(提箱小票)!"); } var userInfo = await db.Queryable().Filter(null, true).FirstAsync(x => x.Id == req.UserId); var tenant = db.Queryable().Filter(null, true).First(x => x.Id == req.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) { _logger.Error(jobjResp.GetStringValue("message")); throw new Exception(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(); } } }