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.System.Entity;
using DS.WMS.Core.System.Interface;
using Microsoft.Extensions.DependencyInjection;
using SqlSugar;
using NLog;
using NLog.Web;
using DS.Module.Core.Extensions;
using Logger = NLog.Logger;
using Mapster;
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;
private static readonly Logger _logger = LogManager.GetCurrentClassLogger();
///
///
///
///
public DataCallBackService(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
db = _serviceProvider.GetRequiredService();
user = _serviceProvider.GetRequiredService();
saasService = _serviceProvider.GetRequiredService();
commonService = _serviceProvider.GetRequiredService();
}
///
/// 插入货运动态
///
///
///
public async Task AddBookingStatusLog(List req)
{
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
try
{
if (req.Count == 0)
{
return;
}
else
{
//订舱状态
var dicList = db.Queryable().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 businessId = item.BookingId;
var list = await tenantDb.Queryable().Filter(null, true)
.InnerJoin((d, t) => d.StatusCode == t.Value && d.StatusGroup == "booking_status_group_billtrace" && d.BusinessId == businessId).Select((d, t) => new
{
BusinessId = d.BusinessId,
StatusCode = d.StatusCode,
StatusName = d.StatusName,
StatusTime = d.StatusTime,
Name = t.Name,
Value = t.Value
}).ToListAsync();
var oldLog = await tenantDb.Queryable().Where(x => x.BusinessId == businessId && x.Group == "yunzong").FirstAsync();
//删除日志详情和日志
var oldLogDetails = await tenantDb.Queryable().Where(x => x.PId == oldLog.Id).ToListAsync();
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 {
// 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 SetGoodsStatus("CDFX", item.BookingId);
}
}
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 SetGoodsStatus("BG", item.BookingId);
}
}
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 SetGoodsStatus("ZZFX", item.BookingId);
}
}
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 SetGoodsStatus("MTFX", item.BookingId);
}
}
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().Filter(null, true).Where(x => x.Id == businessId && x.Deleted == false).First();
if (order.ETA != item.OpTime && item.OpTime != null)
{
order.ETA = item.OpTime;
await tenantDb.Updateable(order).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().Filter(null, true).Where(x => x.Id == businessId && x.Deleted == false).First();
if (order.ATA != item.OpTime && item.OpTime != null)
{
order.ATA = item.OpTime;
await tenantDb.Updateable(order).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 SetGoodsStatus("YRG", item.BookingId);
}
}
#endregion
#endregion
}
}
}
catch (Exception ex)
{
_logger.Error(ex.Message);
_logger.Error(ex.StackTrace);
}
}
///
/// 设置货物状态完成
///
///
///
///
public async Task SetGoodsStatus(string code, long bookingId)
{
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
var CreatedUserId = tenantDb.Queryable().Filter(null, true).Where(x => x.Id == bookingId).Select(x => x.CreateBy).First();
if (CreatedUserId != null)
{
var gsCfg = tenantDb.Queryable().First(x => x.SystemCode == code && x.CreateBy == (long)CreatedUserId);
if (gsCfg != null)
{
_logger.Info($"检查{code}货物状态:{bookingId}");
var gs = tenantDb.Queryable().First(x => x.ConfigId == gsCfg.Id && x.BusinessId == bookingId);
if (gs == null)
{
gs = new BookingGoodsStatus()
{
BusinessId = bookingId,
ConfigId = gsCfg.Id,
FinishTime = DateTime.Now,
FinishUserName = user.UserId.IsNull() ? "超级管理员" : user.UserId,
FinishBy = user.UserId.IsNull() ? 0 : long.Parse(user.UserId),
StatusCode = gsCfg.SystemCode,
StatusName = gsCfg.StatusName,
};
await tenantDb.Insertable(gs).ExecuteCommandAsync();
_logger.Info($"发送{code}后自动完成货物状态,Id:{bookingId}");
//更新货物状态
await SetBookingOrderGoodsStatus(bookingId);
}
}
}
}
///
/// 更新主单货物状态
///
///
///
public async Task SetBookingOrderGoodsStatus(long bookingId)
{
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
var order = tenantDb.Queryable().Filter(null, true).First(x => x.Id == bookingId);
var createUserid = order.CreateBy;
//获取当前用户已经录入的货物状态
var list = await tenantDb.Queryable().LeftJoin(tenantDb.Queryable(),
(goods, config) => config.Id == goods.ConfigId).Where((goods, config) => config.CreateBy == createUserid && goods.BusinessId == bookingId).
OrderBy((goods, config) => config.OrderNo).
Select((goods, config) => new
{
ConfigId = config.Id,
SystemCode = config.SystemCode,
StatusName = config.StatusName,
FinishTime = goods.FinishTime,
FinishUser = goods.FinishUserName,
FinishUserId = goods.FinishBy,
IsPublic = goods.IsPublic,
ExtData = goods.ExtData,
Remark = goods.Note,
OrderNo = config.OrderNo
}).ToListAsync();
if (list != null)
{
//回写主单状态
var StatusName = list.Where(x => x.FinishTime.HasValue).OrderByDescending(x => x.OrderNo).Select(x => x.StatusName).FirstOrDefault();
if (StatusName == null)
{
StatusName = "";
}
order.BusinessStatusName = StatusName;
await tenantDb.Updateable(order).EnableDiffLogEvent().ExecuteCommandAsync();
// 记录日志
//var newOrder = order.Adapt();
//newOrder.BSSTATUSNAME = StatusName;
//await SaveLog(newOrder, order, "自动更新货物状态");
}
}
}
}