207 lines
7.6 KiB
C#
207 lines
7.6 KiB
C#
using SJZY.Track.Domain;
|
|
using SJZY.Track.Domain.Aggregates.OrderAggregate;
|
|
using SJZY.Track.Domain.Dtos.Order;
|
|
using SJZY.Track.Domain.Enums;
|
|
using SJZY.Track.Domain.VEntities;
|
|
|
|
namespace SJZY.Track.Repository.Repositories;
|
|
|
|
public class OrderRepository : BasicRepository<OrderEntity>, IOrderRepository, ITransientDependency
|
|
{
|
|
public async Task<UnifyPageResultDto<TrackOrderVO>> GetTrackOrderPagedList(GetPageTrackOrderInputVO input)
|
|
{
|
|
var pageList = await Context.Queryable<OrderEntity>()
|
|
.LeftJoin<VExternalSubjectEntity>((a, b) => a.CustomId == b.Id && b.IsDeleted == false)
|
|
.LeftJoin<VSystemEntity>((a, b, c) => a.SubSystemId == c.Id && c.IsDeleted == false)
|
|
.WhereIF(input.SubSystemId > 0, (a, b, c) => a.SubSystemId == input.SubSystemId)
|
|
.WhereIF(!input.OrderNos.IsNullOrEmpty(), (a, b, c) => input.OrderNos!.Contains(a.OrderNo))
|
|
.WhereIF(input.CreateTimeStart.HasValue && input.CreateTimeEnd.HasValue,
|
|
(a, b, c) => a.CreateTime >= input.CreateTimeStart && a.CreateTime <= input.CreateTimeEnd)
|
|
.WhereIF(input.CreateTimeStart.HasValue && (!input.CreateTimeEnd.HasValue),
|
|
(a, b, c) => a.CreateTime >= input.CreateTimeStart)
|
|
.WhereIF((!input.CreateTimeStart.HasValue) && input.CreateTimeEnd.HasValue,
|
|
(a, b, c) => a.CreateTime <= input.CreateTimeEnd)
|
|
.WhereIF(input.CustomerId > 0, (a, b, c) => a.CustomId == input.CustomerId)
|
|
.WhereIF(input.TrackOrderType > 0, (a, b, c) => a.OrderType == input.TrackOrderType)
|
|
.WhereIF(input.TabType > 0, (a, b, c) => a.QuestionType == input.TabType)
|
|
.WhereIF(input.TabType == (int) TrackProblemType.Warn, (a, b, c) => !a.IsCompleted)
|
|
.OrderByDescending((a, b, c) => a.CreateTime)
|
|
.Select((a, b, c) => new TrackOrderVO
|
|
{
|
|
Id = a.Id,
|
|
OrderNumber = a.OrderNo,
|
|
OrderType = a.OrderType,
|
|
QuestionType = a.QuestionType,
|
|
CustomerShortName = b.Abbreviation,
|
|
CustomerCode = b.Code,
|
|
CustomerId = a.CustomId,
|
|
NewestTrackContent = a.LatestTrackContent,
|
|
NewestTrackEnContent = a.LatestTrackEnContent,
|
|
NewestTrackPlace = a.LatestTrackPlace,
|
|
NewestTrackTime = a.LatestTrackTime,
|
|
IsFinish = a.IsCompleted,
|
|
CreateTime = a.CreateTime,
|
|
SubSystem = c.CnName,
|
|
SubSystemId = c.Id
|
|
})
|
|
.ToPagedListAsync(input.PageIndex, input.PageSize);
|
|
|
|
return pageList;
|
|
}
|
|
|
|
public async Task<int> CreateOrderAsync(OrderEntity order)
|
|
{
|
|
using var tran = TransactionHelper.GetReadCommitted();
|
|
|
|
var orderId = await InsertReturnIdentityAsync(order);
|
|
order.OrderFlights.ForEach(x => x.OrderId = orderId);
|
|
await Context.Insertable(order.OrderFlights).ExecuteCommandAsync();
|
|
order.OrderPullStatus.ForEach(x => x.OrderId = orderId);
|
|
await Context.Insertable(order.OrderPullStatus).ExecuteCommandAsync();
|
|
|
|
tran.Complete();
|
|
|
|
return orderId;
|
|
}
|
|
|
|
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<OrderPullStatusEntity>().Where(x => x.OrderId == order.Id).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();
|
|
}
|
|
|
|
/// <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;
|
|
}
|
|
|
|
public async Task<List<(string code, int id)>> GetSubSystemListByCodesAsync(List<string> codes)
|
|
{
|
|
var res = await Context.Queryable<VSystemEntity>()
|
|
.Where(x => codes.Contains(x.Key))
|
|
.Select(x => new
|
|
{
|
|
x.Key,
|
|
x.Id
|
|
})
|
|
.ToListAsync();
|
|
|
|
return res.Select(x => (x.Key, x.Id)).ToList();
|
|
}
|
|
|
|
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
|
|
}
|