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.

558 lines
30 KiB
C#

using Amazon.Runtime.Internal.Util;
using DS.Module.SqlSugar;
using DS.Module.UserModule;
using DS.WMS.Core.Op.Dtos;
using DS.WMS.Core.Op.Entity;
using DS.WMS.Core.Op.Interface;
using DS.WMS.Core.Sys.Entity;
using DS.WMS.Core.Sys.Interface;
using Microsoft.Extensions.DependencyInjection;
using SqlSugar;
using NLog;
using NLog.Web;
using DS.Module.Core.Extensions;
using Logger = NLog.Logger;
using Mapster;
4 months ago
using Microsoft.AspNetCore.Identity;
using DS.Module.Core.Constants;
4 months ago
using Microsoft.AspNetCore.Http;
using Newtonsoft.Json;
namespace DS.WMS.Core.Op.Method
{
public class DataCallBackService : IDataCallBackService
{
private readonly IServiceProvider _serviceProvider;
private readonly ISqlSugarClient db;
private readonly IUser user;
private readonly ISaasDbService saasService;
private readonly ICommonService commonService;
6 months ago
private readonly ISeaExportCommonService seaComService;
4 months ago
private readonly IHttpContextAccessor accessor;
private static readonly Logger _logger = LogManager.GetCurrentClassLogger();
/// <summary>
///
/// </summary>
/// <param name="serviceProvider"></param>
public DataCallBackService(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
db = _serviceProvider.GetRequiredService<ISqlSugarClient>();
user = _serviceProvider.GetRequiredService<IUser>();
saasService = _serviceProvider.GetRequiredService<ISaasDbService>();
commonService = _serviceProvider.GetRequiredService<ICommonService>();
6 months ago
seaComService = _serviceProvider.GetRequiredService<ISeaExportCommonService>();
4 months ago
accessor = _serviceProvider.GetRequiredService<IHttpContextAccessor>();
}
/// <summary>
/// 插入货运动态
/// </summary>
/// <param name="req"></param>
/// <returns></returns>
public async Task AddBookingStatusLog(List<BookingStatusLogReq> req)
{
try
{
// 当BusinessId不为空时将BusinessId的值转为订舱主键否则仍然使用BookingId作为订舱记录主键
if (!string.IsNullOrEmpty(req[0].BusinessId))
{
if (long.TryParse(req[0].BusinessId, out long itemBusinessId))
{
req[0].BookingId = itemBusinessId;
}
else
{
_logger.Error($"在将item.BusinessId转为BookingId时发生异常货运动态插入失败item.BusinessId的值为{req[0].BusinessId}");
return;
}
}
_logger.Info($"货运动态回调:"+ JsonConvert.SerializeObject(req));
//订舱状态
var dicList = db.Queryable<SysDictData>().Where(x => x.TypeId == 1788810332945387520).ToList();
foreach (var item in req)
{
// 当BusinessId不为空时将BusinessId的值转为订舱主键否则仍然使用BookingId作为订舱记录主键
if (!string.IsNullOrEmpty(item.BusinessId))
{
if (long.TryParse(item.BusinessId, out long itemBusinessId))
{
item.BookingId = itemBusinessId;
}
else
{
_logger.Error($"在将item.BusinessId转为BookingId时发生异常货运动态插入失败item.BusinessId的值为{item.BusinessId}");
continue;
}
}
var tenantId = accessor.HttpContext.Request.Headers["PushBackKey"].FirstOrDefault();
var tenantDb = saasService.GetBizDbScopeById(tenantId);
var businessId = item.BookingId;
//TODO
var list = await tenantDb.Queryable<BookingStatus>().Filter(null, true)
.InnerJoin<SysDictData>((d, t) => d.StatusCode == t.Value && t.TypeId == 1788810332945387520 && d.StatusGroup == "booking_status_group_billtrace"
&& d.BusinessId == businessId, "shippingweb8_dev.sys_dict_data")
.Select((d, t) => new
{
BusinessId = d.BusinessId,
StatusCode = d.StatusCode,
StatusName = d.StatusName,
StatusTime = d.StatusTime,
Name = t.Name,
Value = t.Value
}).ToListAsync();
// var list = await tenantDb.Queryable<BookingStatus>().Filter(null, true).Where(x=>x.StatusGroup == "booking_status_group_billtrace" && x.BusinessId == businessId).Select(d=> new
//{
// BusinessId = d.BusinessId,
// StatusCode = d.StatusCode,
// StatusName = d.StatusName,
// StatusTime = d.StatusTime
//}).ToListAsync();
var oldLog = await tenantDb.Queryable<BookingStatusLog>().Where(x => x.BusinessId == businessId && x.Group == "yunzong").FirstAsync();
if (oldLog.IsNotNull()) {
//删除日志详情和日志
var oldLogDetails = await tenantDb.Queryable<BookingStatusLogDetail>().Where(x => x.PId == oldLog.Id).ToListAsync();
if (oldLogDetails.Count > 0)
{
await tenantDb.Deleteable(oldLogDetails).ExecuteCommandAsync();
}
await tenantDb.Deleteable(oldLog).ExecuteCommandAsync();
}
#region 日志
//新增数据
var statusLog = new BookingStatusLog()
{
BusinessId = businessId,
Group = "yunzong",
Status = item.Status,
OpTime = item.OpTime,
MBLNO = item.MBLNO
};
await tenantDb.Insertable(statusLog).ExecuteCommandAsync();
if (item.detail != null && item.detail.Count > 0)
{
foreach (var dt in item.detail)
{
var logDetail = new BookingStatusLogDetail()
{
PId = statusLog.Id,
Status = dt.Status,
CNTRNO = dt.CNTRNO,
OpTime = dt.OPTime
};
await tenantDb.Insertable(logDetail).ExecuteCommandAsync();
}
}
if (item.Status == "提箱")
{
//自动引入场站箱型箱量数据
//await _bookingorderservice.AutoYardData(item.BookingId);
//#region 推送已提箱状态
////2023-12-28 JHQ 增加提箱推送状态
//DateTime opTime = DateTime.MinValue;
//if (item.detail != null && item.detail.Count > 0 && item.detail.Any(x => x.OPTime.HasValue))
// opTime = item.detail.Where(x => x.OPTime.HasValue).Max(x => x.OPTime.Value);
//var pushModel = new ModifyServiceProjectStatusDto
//{
// BookingId = businessId,
// SourceType = TrackingSourceTypeEnum.AUTO,
// StatusCodes = new List<ModifyServiceProjectStatusDetailDto> {
// new ModifyServiceProjectStatusDetailDto { StatusCode = "YTX" } }
//};
//if (opTime != DateTime.MinValue)
//{
// pushModel.StatusCodes[0].SetActDate = opTime;
//}
//var saveStatusRlt = await _bookingValueAddedService.SaveServiceStatus(pushModel);
//_logger.Info("请求JSON={json} 异步推送服务状态完成,结果={rlt}", JSON.Serialize(pushModel), JSON.Serialize(saveStatusRlt));
//#endregion
}
#endregion
#region 订舱状态
if (item.Status == "舱单")
{
if (list.Where(x => x.Value == "status_cangdan").FirstOrDefault() == null)
{
var bookingStatus = new BookingStatus()
{
BusinessId = businessId,
StatusCode = "status_cangdan",
StatusName = dicList.Where(x => x.Value == "status_cangdan").Select(x => x.Name).First(),
StatusTime = item.OpTime,
StatusGroup = "booking_status_group_billtrace"
};
await tenantDb.Insertable(bookingStatus).ExecuteCommandAsync();
//2023-12-28 JHQ 增加舱单放行推送状态
await seaComService.SetGoodsStatus("CDFX", item.BookingId, tenantDb);
}
}
if (item.Status == "海关放行")
{
if (list.Where(x => x.Value == "status_haifang").FirstOrDefault() == null)
{
var bookingStatus = new BookingStatus()
{
BusinessId = businessId,
StatusCode = "status_haifang",
StatusName = dicList.Where(x => x.Value == "status_haifang").Select(x => x.Name).First(),
StatusTime = item.OpTime,
StatusGroup = "booking_status_group_billtrace"
};
await tenantDb.Insertable(bookingStatus).ExecuteCommandAsync();
await seaComService.SetGoodsStatus("BG", item.BookingId, tenantDb);
}
}
if (item.Status == "装载")
{
if (list.Where(x => x.Value == "status_zhuangzai").FirstOrDefault() == null)
{
var bookingStatus = new BookingStatus()
{
BusinessId = businessId,
StatusCode = "status_zhuangzai",
StatusName = dicList.Where(x => x.Value == "status_zhuangzai").Select(x => x.Name).First(),
StatusTime = item.OpTime,
StatusGroup = "booking_status_group_billtrace"
};
await tenantDb.Insertable(bookingStatus).ExecuteCommandAsync();
await seaComService.SetGoodsStatus("ZZFX", item.BookingId, tenantDb);
}
}
if (item.Status == "码放")
{
if (list.Where(x => x.Value == "status_mafang").FirstOrDefault() == null)
{
var bookingStatus = new BookingStatus()
{
BusinessId = businessId,
StatusCode = "status_mafang",
StatusName = dicList.Where(x => x.Value == "status_mafang").Select(x => x.Name).First(),
StatusTime = item.OpTime,
StatusGroup = "booking_status_group_billtrace"
};
await tenantDb.Insertable(bookingStatus).ExecuteCommandAsync();
await seaComService.SetGoodsStatus("MTFX", item.BookingId, tenantDb);
}
}
if (item.Status == "装船")
{
if (list.Where(x => x.Value == "status_zhuangchuan").FirstOrDefault() == null)
{
var bookingStatus = new BookingStatus()
{
BusinessId = businessId,
StatusCode = "status_zhuangchuan",
StatusName = dicList.Where(x => x.Value == "status_zhuangchuan").Select(x => x.Name).First(),
StatusTime = item.OpTime,
StatusGroup = "booking_status_group_billtrace"
};
await tenantDb.Insertable(bookingStatus).ExecuteCommandAsync();
}
}
if (item.Status == "ATD")
{
if (list.Where(x => x.Value == "status_atd").FirstOrDefault() == null)
{
var bookingStatus = new BookingStatus()
{
BusinessId = businessId,
StatusCode = "status_atd",
StatusName = dicList.Where(x => x.Value == "status_atd").Select(x => x.Name).First(),
StatusTime = item.OpTime,
StatusGroup = "booking_status_group_billtrace"
};
await tenantDb.Insertable(bookingStatus).ExecuteCommandAsync();
}
}
if (item.Status == "MDGETA")
{
if (list.Where(x => x.Value == "status_mdgeta").FirstOrDefault() == null)
{
var bookingStatus = new BookingStatus()
{
BusinessId = businessId,
StatusCode = "status_mdgeta",
StatusName = dicList.Where(x => x.Value == "status_mdgeta").Select(x => x.Name).First(),
StatusTime = item.OpTime,
StatusGroup = "booking_status_group_billtrace"
};
await tenantDb.Insertable(bookingStatus).ExecuteCommandAsync();
}
var order = tenantDb.Queryable<SeaExport>().Filter(null, true).Where(x => x.Id == businessId && x.Deleted == false).First();
if (order.ETA != item.OpTime && item.OpTime != null)
{
order.ETA = item.OpTime;
order.Note = "运踪回调更新ETA";
await tenantDb.Updateable(order).UpdateColumns(x => new
{
x.ETA,
x.Note
}).EnableDiffLogEvent().ExecuteCommandAsync();
}
}
if (item.Status == "MDGATA")
{
if (list.Where(x => x.Value == "status_mdgata").FirstOrDefault() == null)
{
var bookingStatus = new BookingStatus()
{
BusinessId = businessId,
StatusCode = "status_mdgata",
StatusName = dicList.Where(x => x.Value == "status_mdgata").Select(x => x.Name).First(),
StatusTime = item.OpTime,
StatusGroup = "booking_status_group_billtrace"
};
await tenantDb.Insertable(bookingStatus).ExecuteCommandAsync();
}
var order = tenantDb.Queryable<SeaExport>().Filter(null, true).Where(x => x.Id == businessId && x.Deleted == false).First();
if (order.ATA != item.OpTime && item.OpTime != null)
{
order.ATA = item.OpTime;
order.Note = "运踪回调更新ATA";
await tenantDb.Updateable(order).UpdateColumns(x => new
{
x.ATA,
x.Note
}).EnableDiffLogEvent().ExecuteCommandAsync();
}
}
#region 入港时间
if (item.Status == "入港")
{
if (list.Where(x => x.Value == "status_rugang").FirstOrDefault() == null)
{
var bookingStatus = new BookingStatus()
{
BusinessId = businessId,
StatusCode = "status_rugang",
StatusName = dicList.Where(x => x.Value == "status_rugang").Select(x => x.Name).First(),
StatusTime = item.OpTime,
StatusGroup = "booking_status_group_billtrace"
};
await tenantDb.Insertable(bookingStatus).ExecuteCommandAsync();
await seaComService.SetGoodsStatus("YRG", item.BookingId, tenantDb);
}
}
#endregion
#endregion
#region 船舶动态ATD ETD ATA ETA MDGETA MDGATA
if (item.Status == "ATD" || item.Status == "ETD" || item.Status == "ATA" || item.Status == "ETA" || item.Status == "MDGETA" || item.Status == "MDGATA")
{
var config = await db.Queryable<SysConfig>().FirstAsync(x => x.Code == TenantParamCode.ENABLE_STATUS_TO_SAILING_DATE);
4 months ago
if (config.IsNotNull() && config.Value == "YES")
4 months ago
{
var order = await tenantDb.Queryable<SeaExport>().Where(x => x.Id == businessId && x.MBLNO == item.MBLNO).FirstAsync();
var oldOrder = order.Adapt<SeaExport>();
if (item.Status == "ATD")
4 months ago
{
//2023-8-31排查单号为177GZHZHQ5711V的数据原来录入的船已开船运踪港前不再查询而因订阅港后数据会继续回推数据所以导致ATD被改
//因此暂时取消运踪回推时写入ATD还是以船期表查询为准
// 2024-4-8 因为港捷需要,重新开启,并通过租户参数判断
//变更ATD
4 months ago
if (order != null && item.OpTime != null && order.ATD != item.OpTime)
{
order.ATD = item.OpTime;
//order.Note = "运踪回调更新ATD";
await tenantDb.Updateable(order).UpdateColumns(x => new
4 months ago
{
x.ATD
}).EnableDiffLogEvent().ExecuteCommandAsync();
// 记录日志
await seaComService.SaveSeaExportLogAsync(new SeaExportSaveLog()
{
OperateType = "Update",
OldOrder = oldOrder,
NewOrder = order,
SourceCode = "TraceUpdteATD",
SourceName = "运踪回调更新ATD",
}, tenantDb);
//sendDsList.Add(item.BookingId);
//await _bookingorderservice.SaveLog(newOrder, oldOrder, "运踪更新船期");
4 months ago
}
/*
* 2023824ATD
*
*
*/
////批量变更相同船期
//if (!string.IsNullOrEmpty(o.VESSEL) && !string.IsNullOrEmpty(o.VOYNO) && !string.IsNullOrEmpty(o.CARRIERID))
//{
// var order = await _rep.AsQueryable().Filter(null, true).Where(x => x.VOYNO == o.VOYNO && x.VESSEL == o.VESSEL && x.CARRIERID == o.CARRIERID && x.TenantId == o.TenantId && x.IsDeleted == false && x.Id != item.BookingId).ToListAsync();
// foreach (var it in order)
// {
// var _oldorder = it;
// var _oldatd = it.ATD;
// if (_oldatd != item.OpTime)
// {
// it.ATD = item.OpTime;
// await _rep.UpdateAsync(it);
// await _bookingorderservice.SaveLog(it, _oldorder);
// await _bookingorderservice.SendBookingOrder(new long[] { it.Id });
// }
// }
//}
}
else if (item.Status == "ETD")
{
if (order != null && item.OpTime != null && order.YgtETD != item.OpTime)
{
order.YgtETD = item.OpTime;
//order.Note = "运踪回调更新YgtETD";
await tenantDb.Updateable(order).UpdateColumns(x => new
{
x.YgtETD,x.Note
}).EnableDiffLogEvent().ExecuteCommandAsync();
// 记录日志
await seaComService.SaveSeaExportLogAsync(new SeaExportSaveLog()
{
OperateType = "Update",
OldOrder = oldOrder,
NewOrder = order,
SourceCode = "TraceUpdteYgtETD",
SourceName = "运踪回调更新YgtETD",
}, tenantDb);
//await _bookingorderservice.SaveLog(newOrder, oldOrder, "运踪更新船期");
}
}
else if (item.Status == "ATA")
{
if (order != null && item.OpTime != null && order.StartATA != item.OpTime)
{
order.StartATA = item.OpTime;
//order.Note = "运踪回调更新StartATA";
await tenantDb.Updateable(order).UpdateColumns(x => new
{
x.StartATA
}).EnableDiffLogEvent().ExecuteCommandAsync();
// 记录日志
await seaComService.SaveSeaExportLogAsync(new SeaExportSaveLog()
{
OperateType = "Update",
OldOrder = oldOrder,
NewOrder = order,
SourceCode = "TraceUpdteStartATA",
SourceName = "运踪回调更新StartATA",
}, tenantDb);
//await _bookingorderservice.SaveLog(newOrder, oldOrder, "运踪更新船期");
}
}
else if (item.Status == "ETA")
{
if (order != null && item.OpTime != null && order.StartETA != item.OpTime)
{
order.StartETA = item.OpTime;
//order.Note = "运踪回调更新StartETA";
await tenantDb.Updateable(order).UpdateColumns(x => new
{
x.StartETA
}).EnableDiffLogEvent().ExecuteCommandAsync();
// 记录日志
await seaComService.SaveSeaExportLogAsync(new SeaExportSaveLog()
{
OperateType = "Update",
OldOrder = oldOrder,
NewOrder = order,
SourceCode = "TraceUpdteStartETA",
SourceName = "运踪回调更新StartETA",
}, tenantDb);
//await _bookingorderservice.SaveLog(newOrder, oldOrder, "运踪更新船期");
}
}
else if (item.Status == "MDGETA")
{
if (order != null && item.OpTime != null && order.ETA != item.OpTime)
4 months ago
{
order.ETA = item.OpTime;
//order.Note = "运踪回调更新ETA";
await tenantDb.Updateable(order).UpdateColumns(x => new
4 months ago
{
x.ETA
}).EnableDiffLogEvent().ExecuteCommandAsync();
// 记录日志
await seaComService.SaveSeaExportLogAsync(new SeaExportSaveLog()
{
OperateType = "Update",
OldOrder = oldOrder,
NewOrder = order,
SourceCode = "TraceUpdteMDGETA",
SourceName = "运踪回调更新ETA",
}, tenantDb);
//await _bookingorderservice.SaveLog(newOrder, oldOrder, "运踪更新船期");
4 months ago
}
}
else if (item.Status == "MDGATA")
{
if (order != null && item.OpTime != null && order.ATA != item.OpTime)
{
order.ATA = item.OpTime;
//order.Note = "运踪回调更新ATA";
await tenantDb.Updateable(order).UpdateColumns(x => new
4 months ago
{
x.ATA
}).EnableDiffLogEvent().ExecuteCommandAsync();
// 记录日志
await seaComService.SaveSeaExportLogAsync(new SeaExportSaveLog()
{
OperateType = "Update",
OldOrder = oldOrder,
NewOrder = order,
SourceCode = "TraceUpdteMDGATA",
SourceName = "运踪更新船期ATA",
}, tenantDb);
//await _bookingorderservice.SaveLog(newOrder, oldOrder, "运踪更新船期");
4 months ago
}
4 months ago
}
}
}
#endregion
}
}
catch (Exception ex)
{
_logger.Error(ex.Message);
_logger.Error(ex.StackTrace);
}
}
}
}