using DS.Module.Core; using DS.Module.Core.Data; using DS.Module.Core.Extensions; using DS.Module.Core.Helpers; using DS.Module.DjyServiceStatus; using DS.WMS.Core.Code.Entity; using DS.WMS.Core.Info.Entity; using DS.WMS.Core.Map.Entity; using DS.WMS.Core.Op.Dtos; using DS.WMS.Core.Op.EDI; using DS.WMS.Core.Op.Entity; using DS.WMS.Core.Sys.Entity; using Mapster; using Newtonsoft.Json; using Newtonsoft.Json.Linq; namespace DS.WMS.Core.Op.Method { /// /// 海运出口电放接口 /// public partial class SeaExportService { #region 电放 /// /// 船司电放提交 /// /// 订舱主键数组 /// 每单的处理提交结果 public async Task>> SubmitTelex(IdModel req) { var tenantDb = saasService.GetBizDbScopeById(user.TenantId); var orderList = await tenantDb.Queryable().Where(x => req.Ids.Contains(x.Id)).ToListAsync(); if (orderList.Count == 0) { return await Task.FromResult(DataResult>.Failed($"电放失败,未找到相应订舱")); } var url = db.Queryable().Filter(null, true).Where(x => x.Code == "TelexServiceUrl" && x.TenantId == 1288018625843826688).First()?.Value; var key = db.Queryable().Filter(null, true).Where(x => x.Code == "TelexServiceKey" && x.TenantId == 1288018625843826688).First()?.Value; var secret = db.Queryable().Filter(null, true).Where(x => x.Code == "TelexServiceSecret" && x.TenantId == 1288018625843826688).First()?.Value; if (string.IsNullOrWhiteSpace(url)) { return await Task.FromResult(DataResult>.Failed($"电放失败,大简云电放服务接口地址未配置,请联系管理员")); } if (string.IsNullOrWhiteSpace(key)) { return await Task.FromResult(DataResult>.Failed($"电放失败,大简云电放服务接口Key未配置,请联系管理员")); } if (string.IsNullOrWhiteSpace(secret)) { return await Task.FromResult(DataResult>.Failed($"电放失败,大简云电放服务接口Secret未配置,请联系管理员")); } var webAccount = tenantDb.Queryable().Where(x => x.AccountType == "MSKWeb" && x.CreateBy == long.Parse(user.UserId)).First(); if (webAccount == null) { return await Task.FromResult(DataResult>.Failed($"电放失败,未配置网站账户,类型:MSKWeb")); } var mappingCarrier = tenantDb.Queryable().Where(v => v.Status == StatusEnum.Enable).ToList(); var mappingIssueType = tenantDb.Queryable().Where(v => v.Status == StatusEnum.Enable).ToList(); List result = new(); List<(long Id, string MBLNO, string CarrierId)> pushList = new(); foreach (var order in orderList) { try { var carrierCode = seaComService.GetCarrierCode(order.YardId, tenantDb); if (string.IsNullOrWhiteSpace(order.MBLNO)) { result.Add(new SubmitTelexResultDto(order.MBLNO, false, "提单号为空")); continue; } if (string.IsNullOrWhiteSpace(order.IssueType)) { result.Add(new SubmitTelexResultDto(order.MBLNO, false, "签单方式为空")); continue; } if (string.IsNullOrWhiteSpace(order.Carrier)) { result.Add(new SubmitTelexResultDto(order.MBLNO, false, "船公司为空")); continue; } // 判断船公司是否符合条件 string carrierId = mappingCarrier.FirstOrDefault(x => x.Module == "TelexCheck" && x.LinkId == order.CarrierId)?.MapCode; carrierId = (carrierId ?? carrierCode).ToUpper(); if (carrierId != "MSK") { result.Add(new SubmitTelexResultDto(order.MBLNO, false, $"[{order.Carrier}]船公司此功能待开发")); continue; } // 判断签单方式是否符合条件 string issueType = mappingIssueType.FirstOrDefault(x => x.Module == "TelexCheck" && x.Code == order.IssueType)?.MapCode; issueType = issueType ?? order.IssueType; if (!issueType.Equals("telex", StringComparison.OrdinalIgnoreCase)) { result.Add(new SubmitTelexResultDto(order.MBLNO, false, $"签单方式非电放")); continue; } pushList.Add((order.Id, order.MBLNO, carrierId)); } catch (Exception ex) { _logger.Error("请求电放条件判断的过程中发生未知异常,mblno={mblno}, ex={ex}", order.MBLNO, ex); result.Add(new SubmitTelexResultDto(order.MBLNO, false, $"提交错误:{ex.Message}")); continue; } } if (pushList.Count > 0) { return await Task.FromResult(DataResult>.FailedData(result)); //return result; } if (req.Ids.Length > 1) { var usr = db.Queryable().Filter(null, true).First(u => u.Id == long.Parse(user.UserId)); url += "/v1/tasks"; var mblnos = string.Concat(',', pushList.Select(x => x.MBLNO)); try { // 提交电放 var data = pushList.Select(x => new { web_code = x.CarrierId, web_user = webAccount.AppKey, web_psw = webAccount.AppSecret, bno = x.MBLNO, email = usr.Email }); var body = new { user_key = key, user_secret = secret, data = data }; _logger.Info("请求电放提交接口开始,mblnos={mblnos}, url={url}, body={body}", mblnos, url, body); //var rtn = await url.OnClientCreating(client => //{ // client.Timeout = TimeSpan.FromMinutes(3); // 设置超时时间 3分钟 //}).SetBody(body).PostAsStringAsync(); var rtn = RequestHelper.Post(JsonConvert.SerializeObject(body), url, 180000); _logger.Info("请求电放提交接口结束,rtn={rtn}", rtn); var jsonRtn = JObject.Parse(rtn); if (jsonRtn.GetIntValue("code") == 200) { _logger.Info("请求电放提交接口成功,mblnos={mblnos}", mblnos); foreach (var item in pushList) { // 推送 电放已安排 状态 var pushModel = new EmbedServiceProjectStatusDto { businessId = item.Id.ToString(), SourceType = 0, StatusCodes = new List { new EmbedServiceProjectStatusDetailDto { StatusCode = "DFYAP" } } }; //var saveStatusRlt = await bookingValueAddedService.SaveServiceStatus(pushModel); var saveStatusRlt = await _djyServiceStatusService.SaveServiceStatus(pushModel); _logger.Info("请求电放提交接口成功后推送 电放已安排 状态,mblno={mblno},JSON={json} ,结果={rlt}", item.MBLNO, JsonConvert.SerializeObject(pushModel), JsonConvert.SerializeObject(saveStatusRlt)); result.Add(new SubmitTelexResultDto(item.MBLNO, true, null)); } return await Task.FromResult(DataResult>.Success(result)); } else { var msg = jsonRtn.GetStringValue("msg")?.Replace("爬虫", ""); _logger.Info("请求电放提交接口失败,mblnos={mblnos}, 电放服务返回:{msg}", mblnos, msg); result.AddRange(pushList.Select(x => new SubmitTelexResultDto(x.MBLNO, false, $"电放服务返回:{msg}"))); } } catch (Exception ex) { _logger.Error("请求电放服务的过程中发生未知异常,mblnos={mblnos}, ex={ex}", mblnos, ex); result.AddRange(pushList.Select(x => new SubmitTelexResultDto(x.MBLNO, false, $"错误:{ex.Message}"))); } } else { url += "/v1/submit"; var order = pushList[0]; try { // 提交电放 var body = new { user_key = key, user_secret = secret, web_user = webAccount.AppKey, web_psw = webAccount.AppSecret, web_code = order.CarrierId, bno = order.MBLNO }; _logger.Info("请求电放提交接口开始,mblno={mblno}, url={url}, body={body}", order.MBLNO, url, body); //var rtn = await url.SetBody(body).PostAsStringAsync(); var rtn = RequestHelper.Post(JsonConvert.SerializeObject(body), url, 180000); _logger.Info("请求电放提交接口结束,mblno={mblno}, rtn={rtn}", order.MBLNO, rtn); var jsonRtn = JObject.Parse(rtn); if (jsonRtn.GetIntValue("code") == 200) { _logger.Info("请求电放提交接口成功,mblno={mblno}", order.MBLNO); // 推送 电放已安排 状态 var pushModel = new EmbedServiceProjectStatusDto { businessId = order.Id.ToString(), SourceType = 0, StatusCodes = new List { new EmbedServiceProjectStatusDetailDto { StatusCode = "DFYAP" } } }; var saveStatusRlt = await _djyServiceStatusService.SaveServiceStatus(pushModel); //if (saveStatusRlt.success) //{ // seaComService.SetBookingGoodsStatus(bookingOrder.Id, true, "手动更新货物状态"); //} _logger.Info("请求电放提交接口成功后推送 电放已安排 状态,JSON={json} ,结果={rlt}", JsonConvert.SerializeObject(pushModel), JsonConvert.SerializeObject(saveStatusRlt)); result.Add(new SubmitTelexResultDto(order.MBLNO, true, null)); return await Task.FromResult(DataResult>.Success(result)); } else { var msg = jsonRtn.GetStringValue("msg")?.Replace("爬虫", ""); _logger.Info("请求电放提交接口失败,mblno={mblno}, 电放服务返回:{msg}", order.MBLNO, msg); result.Add(new SubmitTelexResultDto(order.MBLNO, false, $"电放服务返回:{msg}")); } } catch (Exception ex) { _logger.Error("请求电放服务的过程中发生未知异常,mblno={mblno}, ex={ex}", order.MBLNO, ex); result.Add(new SubmitTelexResultDto(order.MBLNO, false, $"错误:{ex.Message}")); } } if (result.Count > 0) { return await Task.FromResult(DataResult>.FailedData(result)); } else { return await Task.FromResult(DataResult>.SuccessMsg("电放成功!")); } } #endregion } }