166 lines
5.6 KiB
C#
166 lines
5.6 KiB
C#
using SJZY.Track.Domain;
|
|
using SJZY.Track.Domain.Aggregates.OrderAggregate;
|
|
using SJZY.Track.Domain.Dtos.Order;
|
|
|
|
namespace SJZY.Track.Repository.Repositories;
|
|
|
|
public class OrderRepository : BasicRepository<OrderEntity>, IOrderRepository, ITransientDependency
|
|
{
|
|
public async Task CreateOrderAsync(OrderEntity order)
|
|
{
|
|
using var tran = TransactionHelper.GetReadCommitted();
|
|
|
|
var orderId = await InsertReturnIdentityAsync(order);
|
|
order.OrderTemplates.ForEach(x => x.OrderId = orderId);
|
|
await Context.Insertable(order.OrderTemplates).ExecuteCommandAsync();
|
|
order.OrderFlights.ForEach(x => x.OrderId = orderId);
|
|
await Context.Insertable(order.OrderFlights).ExecuteCommandAsync();
|
|
|
|
tran.Complete();
|
|
}
|
|
|
|
public Task<int> UpdateBatchIdsAsync(List<int> ids, int newValue)
|
|
{
|
|
var idsString = string.Join(",", ids);
|
|
return Context.Ado.ExecuteCommandAsync(
|
|
$"UPDATE `t_order` SET `DeficiencyTag` = @newValue ,UpdateTime= current_timestamp WHERE `Id` IN ({idsString})",
|
|
new
|
|
{
|
|
newValue
|
|
});
|
|
}
|
|
|
|
public async Task UpdateOrderAsync(OrderEntity order)
|
|
{
|
|
using var tran = TransactionHelper.GetReadCommitted();
|
|
|
|
// 更新订单主表
|
|
await UpdateAsync(order, x => new
|
|
{
|
|
x.CustomId,
|
|
x.SubSystemId
|
|
});
|
|
|
|
// 更新订单模板
|
|
await Context.Deleteable<OrderTemplateEntity>().Where(x => x.OrderId == order.Id).ExecuteCommandAsync();
|
|
order.OrderTemplates.ForEach(x => x.OrderId = order.Id);
|
|
await Context.Insertable(order.OrderTemplates).ExecuteCommandAsync();
|
|
|
|
// 更新订单航班
|
|
await Context.Deleteable<OrderFlightEntity>().Where(x => x.OrderId == order.Id).ExecuteCommandAsync();
|
|
order.OrderFlights.ForEach(x => x.OrderId = order.Id);
|
|
await Context.Insertable(order.OrderFlights).ExecuteCommandAsync();
|
|
|
|
tran.Complete();
|
|
}
|
|
|
|
public async Task DeleteOrderAsync(int orderId)
|
|
{
|
|
using var tran = TransactionHelper.GetReadCommitted();
|
|
|
|
// 软删订单主表
|
|
await SoftDeleteAsync(x => x.Id == orderId);
|
|
|
|
// 软删订单模板
|
|
// 软删订单航班
|
|
// 软删订单关联关系
|
|
|
|
tran.Complete();
|
|
}
|
|
|
|
public async Task<int> PushWarnOrdersAsync(int pageSize, DateTime createTime)
|
|
{
|
|
return await Context.Ado.ExecuteCommandAsync(@"UPDATE `t_track_push_config` `a`
|
|
LEFT JOIN `t_order` `b` ON `a`.`SubSystemId` = `b`.`SubSystemId` AND `a`.`CustomId` = `b`.`CustomId`
|
|
SET `b`.`QuestionType` = CASE
|
|
WHEN `b`.`LatestPushTime` IS NULL THEN 2
|
|
WHEN `b`.`LatestPushTime` < DATE_SUB(NOW(), INTERVAL 12 HOUR) THEN 2
|
|
ELSE 0 END
|
|
WHERE `b`.`IsCompleted` = 0 AND `b`.`QuestionType` != 1 AND `b`.`CreateTime` < @createTime
|
|
AND `a`.`IsDeleted` = 0 AND `b`.`IsDeleted` = 0 limit @pageSize", new
|
|
{
|
|
pageSize,
|
|
createTime
|
|
});
|
|
}
|
|
|
|
/// <summary>
|
|
/// 更新推送预警单
|
|
/// </summary>
|
|
/// <param name="pageSize"></param>
|
|
/// <param name="pageindex"></param>
|
|
/// <returns></returns>
|
|
public async Task<int> PushWarnOrdersAsync(int pageSize, int pageindex)
|
|
{
|
|
var ordersToUpdate = await Context.Queryable<OrderEntity>()
|
|
.Where((b) => !b.IsCompleted
|
|
&& b.QuestionType != 1
|
|
&& !b.IsDeleted)
|
|
.Select(b => new
|
|
{
|
|
b.LatestPushTime,
|
|
b.Id,
|
|
b.QuestionType
|
|
})
|
|
.ToPageListAsync(pageindex, pageSize);
|
|
|
|
var latestPushTimeIds = new List<int>();
|
|
var questionZeroIds = new List<int>();
|
|
foreach (var item in ordersToUpdate)
|
|
{
|
|
var questionType = !item.LatestPushTime.HasValue || item.LatestPushTime < DateTime.Now.AddHours(-12)
|
|
? 2
|
|
: 0;
|
|
if (item.QuestionType == questionType) continue;
|
|
if (questionType == 2) latestPushTimeIds.AddLast(item.Id);
|
|
else questionZeroIds.AddLast(item.Id);
|
|
}
|
|
|
|
if (latestPushTimeIds.Count > 0) UpdateOrderQuestionType(2, latestPushTimeIds);
|
|
if (questionZeroIds.Count > 0) UpdateOrderQuestionType(0, questionZeroIds);
|
|
return ordersToUpdate.Count;
|
|
}
|
|
|
|
private int UpdateOrderQuestionType(int questionType, IEnumerable<int> ids)
|
|
{
|
|
var idsString = string.Join(",", ids);
|
|
return Context.Ado.ExecuteCommand(
|
|
$"UPDATE `t_order` SET `QuestionType` = @questionType ,UpdateTime= current_timestamp WHERE `Id` IN ({idsString})",
|
|
new
|
|
{
|
|
questionType
|
|
});
|
|
}
|
|
|
|
#region 订单关联
|
|
|
|
public async Task<List<LinkedOrderVO>> GetLinkedOrdersAsync(int[] orderIds)
|
|
{
|
|
return await Context.Queryable<OrderLinkEntity>()
|
|
.LeftJoin<OrderEntity>((a, b) => a.LinkOrderId == b.Id)
|
|
.Where(a => orderIds.Contains(a.OrderId))
|
|
.Select((a, b) => new LinkedOrderVO
|
|
{
|
|
OwnerOrderId = a.OrderId,
|
|
OrderId = b.Id,
|
|
OrderNo = b.OrderNo,
|
|
OrderType = b.OrderType,
|
|
SubSystemId = b.SubSystemId,
|
|
CustomId = b.CustomId
|
|
})
|
|
.ToListAsync();
|
|
}
|
|
|
|
public async Task LinkOrderAsync(List<OrderLinkEntity> orderLinkEntities)
|
|
{
|
|
await Context.Insertable(orderLinkEntities).ExecuteCommandAsync();
|
|
}
|
|
|
|
public async Task UnLinkByIdsAsync(List<int> linkIds)
|
|
{
|
|
await Context.Deleteable<OrderLinkEntity>(linkIds).ExecuteCommandAsync();
|
|
}
|
|
|
|
#endregion
|
|
}
|