using DS.Module.Core; using DS.Module.RedisModule; using DS.Module.SqlSugar; using DS.Module.UserModule; using DS.WMS.Core.Code.Interface; using DS.WMS.Core.Code.Method; using DS.WMS.Core.Map.Interface; using DS.WMS.Core.Map.Method; using DS.WMS.Core.Sys.Interface; using DS.WMS.Core.Sys.Method; using DS.WMS.Core.TaskPlat.Dtos; using DS.WMS.Core.TaskPlat.Entity; using DS.WMS.Core.TaskPlat.Interface; using Microsoft.Extensions.DependencyInjection; using SqlSugar; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Mapster; using DS.Module.DjyServiceStatus; using Microsoft.AspNetCore.Identity; using DS.WMS.Core.Op.Interface; using Masuit.Tools; using DS.WMS.Core.Op.Dtos; using DS.WMS.Core.Sys.Entity; using HtmlAgilityPack; using Microsoft.AspNetCore.Mvc; using Amazon.Runtime.Internal.Util; using Newtonsoft.Json; using NLog; using DS.Module.Core.Helpers; using System.Text.Json.Nodes; using LanguageExt.Pipes; using DS.WMS.Core.Op.Entity; using Org.BouncyCastle.Asn1.X9; using DS.WMS.Core.Op.EDI; using DS.WMS.Core.Sys.Dtos; using DS.WMS.Core.Code.Dtos; using DS.Module.Core.Data; namespace DS.WMS.Core.TaskPlat.Method { /// /// /// public class TaskTransPlanHasChangeService: ITaskTransPlanHasChangeService { private readonly IServiceProvider _serviceProvider; private readonly ISqlSugarClient db; private readonly IUser user; private readonly ISaasDbService saasService; private readonly ISeaExportService _seaExportService; private readonly IConfigService _configService; private readonly IUserService _userService; private static readonly NLog.Logger Logger = LogManager.GetCurrentClassLogger(); const string EMAIL_API_URL = "email_api_url"; public TaskTransPlanHasChangeService(IServiceProvider serviceProvider) { _serviceProvider = serviceProvider; db = _serviceProvider.GetRequiredService(); user = _serviceProvider.GetRequiredService(); saasService = _serviceProvider.GetRequiredService(); _seaExportService = _serviceProvider.GetRequiredService(); _configService = _serviceProvider.GetRequiredService(); _userService = _serviceProvider.GetRequiredService(); } #region 通过任务主键获取货物运输计划已变更详情 /// /// 通过任务主键获取货物运输计划已变更详情 /// /// 货物运输计划已变更任务主键 /// 返回回执 public async Task> GetInfoByTaskId(long taskPKId) { TaskTransPlanHasChangeShowDto dto = new TaskTransPlanHasChangeShowDto(); var tenantDb = saasService.GetBizDbScopeById(user.TenantId); //任务不考虑OrgId,这里去掉 tenantDb.QueryFilter.Clear(); var queryList = await tenantDb.Queryable() .InnerJoin((a, b) => a.Id == b.TASK_ID) .Where((a, b) => a.Id == taskPKId) .Select((a, b) => new { Base = a, Plan = b }) .ToListAsync(); //任务主键{taskPkId}无法获取业务信息 if (queryList.Count == 0) throw new Exception(string.Format(MultiLanguageConst.GetDescription(nameof(MultiLanguageConst.TaskBaseInfoFromTaskIdNull)), taskPKId)); var planInfo = queryList.FirstOrDefault().Plan; dto.BillNo = planInfo.MBL_NO; dto.BookingNo = planInfo.BOOKING_NO; dto.Carrier = planInfo.CARRIER; dto.OrigBillNo = planInfo.ORIG_MBL_NO; dto.TaskPkId = planInfo.TASK_ID; dto.Id = planInfo.Id; dto.BookingId = planInfo.BOOKING_ID; dto.ChangeReasonNotes = planInfo.CHANGE_REASON; dto.GroupBatchNo = planInfo.GROUP_BATCH_NO; dto.IsLast = planInfo.IS_LAST == 1 ? true : false; var parentIdList = queryList.Select(a => a.Plan.Id).ToList(); var detailList = await tenantDb.Queryable() .Where(a => parentIdList.Contains(a.P_ID)).ToListAsync(); if (detailList.Count > 0) { var ctnList = detailList.Where(a => a.COLUMN_TYPE.Equals("CTN", StringComparison.OrdinalIgnoreCase)).ToList(); dto.ContaNoList = ctnList.Select(a => a.CONTA_NO).ToList(); var fromList = detailList.Where(a => !string.IsNullOrWhiteSpace(a.PLAN_TYPE) && a.PLAN_TYPE.Equals("FROM", StringComparison.OrdinalIgnoreCase)).ToList(); dto.From = new TaskTransportPlanHasChangeDetailShow { portList = new List(), dateList = new List(), vesselList = new List() }; dto.From.portList = fromList.Where(a => !string.IsNullOrWhiteSpace(a.COLUMN_TYPE) && a.COLUMN_TYPE.Equals("PORT", StringComparison.OrdinalIgnoreCase)) .Select(a => { return new TaskTransPlanHasChangePortShow { Indx = a.SORT_NO.HasValue ? a.SORT_NO.Value : 0, PortName = a.PORTLOAD, CountryCode = a.PORTLOAD_COUNTRY, TerminalName = a.PORTLOAD_TERMINAL, IsRemoved = a.IS_REMOVED.HasValue ? a.IS_REMOVED.Value : false }; }).OrderBy(a => a.Indx).ToList(); dto.From.dateList = fromList.Where(a => !string.IsNullOrWhiteSpace(a.COLUMN_TYPE) && a.COLUMN_TYPE.Equals("EATD", StringComparison.OrdinalIgnoreCase)) .Select(a => { return new TaskTransPlanHasChangeDateShow { Indx = a.SORT_NO.HasValue ? a.SORT_NO.Value : 0, DateVal = a.ETD, IsRemoved = a.IS_REMOVED.HasValue ? a.IS_REMOVED.Value : false }; }).OrderBy(a => a.Indx).ToList(); dto.From.vesselList = fromList.Where(a => !string.IsNullOrWhiteSpace(a.COLUMN_TYPE) && a.COLUMN_TYPE.Equals("VESSEL", StringComparison.OrdinalIgnoreCase)) .Select(a => { return new TaskTransPlanHasChangeVesselShow { Indx = a.SORT_NO.HasValue ? a.SORT_NO.Value : 0, Vessel = a.VESSEL, Voyno = a.VOYNO, Flag = a.SHIP_FLAG, IsRemoved = a.IS_REMOVED.HasValue ? a.IS_REMOVED.Value : false }; }).OrderBy(a => a.Indx).ToList(); var toList = detailList.Where(a => !string.IsNullOrWhiteSpace(a.PLAN_TYPE) && a.PLAN_TYPE.Equals("TO", StringComparison.OrdinalIgnoreCase)).ToList(); dto.To = new TaskTransportPlanHasChangeDetailShow { portList = new List(), dateList = new List(), vesselList = new List() }; dto.To.portList = toList.Where(a => !string.IsNullOrWhiteSpace(a.COLUMN_TYPE) && a.COLUMN_TYPE.Equals("PORT", StringComparison.OrdinalIgnoreCase)) .Select(a => { return new TaskTransPlanHasChangePortShow { Indx = a.SORT_NO.HasValue ? a.SORT_NO.Value : 0, PortName = a.PORTDISCHARGE, CountryCode = a.PORTDISCHARGE_COUNTRY, TerminalName = a.PORTDISCHARGE_TERMINAL, IsRemoved = a.IS_REMOVED.HasValue ? a.IS_REMOVED.Value : false }; }).OrderBy(a => a.Indx).ToList(); dto.To.dateList = toList.Where(a => !string.IsNullOrWhiteSpace(a.COLUMN_TYPE) && a.COLUMN_TYPE.Equals("EATD", StringComparison.OrdinalIgnoreCase)) .Select(a => { return new TaskTransPlanHasChangeDateShow { Indx = a.SORT_NO.HasValue ? a.SORT_NO.Value : 0, DateVal = a.ETA, IsRemoved = a.IS_REMOVED.HasValue ? a.IS_REMOVED.Value : false }; }).OrderBy(a => a.Indx).ToList(); dto.To.vesselList = toList.Where(a => !string.IsNullOrWhiteSpace(a.COLUMN_TYPE) && a.COLUMN_TYPE.Equals("VESSEL", StringComparison.OrdinalIgnoreCase)) .Select(a => { return new TaskTransPlanHasChangeVesselShow { Indx = a.SORT_NO.HasValue ? a.SORT_NO.Value : 0, Vessel = a.VESSEL, Voyno = a.VOYNO, Flag = a.SHIP_FLAG, IsRemoved = a.IS_REMOVED.HasValue ? a.IS_REMOVED.Value : false }; }).OrderBy(a => a.Indx).ToList(); } return DataResult.Success(dto); } #endregion #region 检索对应的订舱订单并保存订舱ID /// /// 检索对应的订舱订单并保存订舱ID /// /// 货物运输计划已变更任务主键 /// 返回回执 public async Task> SearchAndMarkBookingOrder(long taskPKId) { SeaExportOrderExtension orderInfo = null; try { var tenantDb = saasService.GetBizDbScopeById(user.TenantId); //任务不考虑OrgId,这里去掉 tenantDb.QueryFilter.Clear(); var queryList = await tenantDb.Queryable() .InnerJoin((a, b) => a.Id == b.TASK_ID) .Where((a, b) => a.Id == taskPKId) .Select((a, b) => new { Base = a, Cut = b }) .ToListAsync(); //任务主键{taskPkId}无法获取业务信息 if (queryList.Count == 0) throw new Exception(string.Format(MultiLanguageConst.GetDescription(nameof(MultiLanguageConst.TaskBaseInfoFromTaskIdNull)), taskPKId)); string mblNo = queryList.FirstOrDefault().Base.MBL_NO; var orderRlt = await _seaExportService.SearchOrderInfo(mblNo); if (!orderRlt.Succeeded) { // 提单号 {0} 检索海运出口订单失败 throw new Exception(string.Format(MultiLanguageConst.GetDescription(nameof(MultiLanguageConst.TaskBaseSearchOrderFailMBLNo)), mblNo)); } orderInfo = orderRlt.Data; foreach (var item in queryList.Select(a => a.Cut).ToList()) { item.BOOKING_ID = orderInfo.currOrder.Id; item.UpdateTime = DateTime.Now; item.UpdateBy = long.Parse(user.UserId); item.UpdateUserName = user.UserName; tenantDb.Updateable(item).UpdateColumns(x => new { x.BOOKING_ID, x.UpdateTime, x.UpdateBy, x.UpdateUserName }).ExecuteCommand(); } } catch (Exception ex) { Logger.Log(NLog.LogLevel.Info, $"taskPKId={taskPKId} 检索货物运输计划已变更订舱记录 处理异常,原因:{ex.Message}"); return DataResult.FailedData(orderInfo, $"检索失败,原因:{ex.Message}", MultiLanguageConst.Operation_Failed); } return DataResult.Success(orderInfo); } #endregion #region 自动转发 /// /// 自动转发 /// /// 货物运输计划已变更任务主键 /// 返回回执 public async Task AutoTranmitToCustomer(long taskPKId) { SeaExportOrderExtension orderInfo = null; try { var queryRlt = await SearchAndMarkBookingOrder(taskPKId); if (!queryRlt.Succeeded) { throw new Exception(queryRlt.Message); } orderInfo = queryRlt.Data; //如果没有配置批量,则按单票发送邮件 var rlt = await SendEmailToCustomer(taskPKId); Logger.Log(NLog.LogLevel.Info, $"taskPKId={taskPKId} 推送邮件完成,结果:{JsonConvert.SerializeObject(rlt)}"); } catch (Exception ex) { Logger.Log(NLog.LogLevel.Info, $"taskPKId={taskPKId} 自动更新订单的截单日期并转发失败异常,原因:{ex.Message}"); return DataResult.Failed($"自动更新订单的截单日期并转发失败,原因:{ex.Message}", MultiLanguageConst.Operation_Failed); } return DataResult.Successed(string.Empty, MultiLanguageConst.OperationSuccess); } #endregion #region 发送邮件通知给客户 /// /// 发送邮件通知给客户 /// /// 货物运输计划已变更主键 /// 返回回执 public async Task SendEmailToCustomer(long taskPKId) { return null; } #endregion } }