fix 消费时间判断ConsumeIsPresent2Async
This commit is contained in:
@@ -194,6 +194,127 @@ public class OrderService : IOrderService
|
||||
.ExecuteCommandAsync();
|
||||
}
|
||||
|
||||
private async Task ConsumeIsPresent2Async(ConsumeRequest request, GainfoEntity gainfo)
|
||||
{
|
||||
var device = await _dbContext.Queryable<DeviceEntity>().Where(x => x.UniqueNo == request.DeviceUniqueNo).Where(x => x.Enable).FirstAsync();
|
||||
device.ThrowIfNull("设备不存在或者尚未启用");
|
||||
|
||||
var registry = await _dbContext.Queryable<DeviceRegistryEntity>().Where(x => x.DeviceUniqueNo == request.DeviceUniqueNo).FirstAsync();
|
||||
registry.ThrowIfNull("设备尚未注册");
|
||||
|
||||
var businessPlace = await _dbContext.Queryable<CanteenBusinessPlaceEntity>().Where(x => x.Code == registry.BusinessPlaceCode).FirstAsync();
|
||||
businessPlace.ThrowIfNull("营业点尚未设置");
|
||||
|
||||
// 获取当前消费餐次
|
||||
var shifts = await _dbContext.Queryable<CanteenBusinessShiftEntity>().Where(x => x.BusinessPlaceCode == businessPlace.Code).ToListAsync();
|
||||
|
||||
// 使用 LINQ 查询找到满足条件的餐次
|
||||
var shift = shifts.FirstOrDefault(x => IsPresent2(new TimePeriod { Start = x.Start, End = x.End }, request.CostDateTime));
|
||||
|
||||
var settings = await _canteenSettingService.GetUserSettingAsync(request.UserUniqueNo);
|
||||
var switchTime = GetSetting<TimeOnly?>(settings, CanteenSettingItemEnumType.BusinessSwitchTime);
|
||||
|
||||
var shiftTimePeriod = new TimePeriod
|
||||
{
|
||||
Start = shift?.Start,
|
||||
End = shift?.End
|
||||
};
|
||||
|
||||
// 计算成本日期 todo 优化
|
||||
var costDate = DateTime.Now;
|
||||
|
||||
if (shift == null)
|
||||
{
|
||||
costDate = DateTime.Now;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (switchTime.HasValue)
|
||||
{
|
||||
var beginOfDay = DateTime.Today;
|
||||
var setTime = beginOfDay.Add(switchTime.Value.ToTimeSpan());
|
||||
|
||||
if (DateTime.Now < setTime)
|
||||
costDate = DateTime.Now.AddDays(-1);
|
||||
}
|
||||
}
|
||||
|
||||
var order = new CanteenOrderEntity
|
||||
{
|
||||
RequestJson = request.ToJson(),
|
||||
ThirdNo = request.ThirdNo,
|
||||
Type = BalanceRecordTypeEnumType.Out,
|
||||
Name = gainfo.Name,
|
||||
Source = request.Source,
|
||||
UserNo = gainfo.No,
|
||||
UserCode = gainfo.Code,
|
||||
Amount = request.Amount,
|
||||
BusinessPlaceCode = businessPlace.Code,
|
||||
BusinessShiftCode = shift?.Code,
|
||||
CostDate = costDate.Date,
|
||||
CostId = gainfo.CostId,
|
||||
DepartmentNo = gainfo.DepartmentNo,
|
||||
DeviceInfo = request.DeviceUniqueNo,
|
||||
|
||||
No = "O" + SnowFlakeHelper.Instance.NextId(),
|
||||
SubsidiesFromMonth = request.CostDateTime.ToString("yyyyMM"),
|
||||
IsOffline = request.IsOffline,
|
||||
IsCostCrossTenant = GetSetting<bool>(settings, CanteenSettingItemEnumType.AllowCrossTenant),
|
||||
OriginTenantId = gainfo.Werks,
|
||||
|
||||
Status = shift == null ? BalanceRecordStatusEnumType.Doubtful : BalanceRecordStatusEnumType.Valid,
|
||||
OrderStatus = CanteenOrderStatusEnumType.Wait,
|
||||
CostDateTime = request.CostDateTime,
|
||||
TenantId = businessPlace.TenantId,
|
||||
};
|
||||
|
||||
var orderId = await _dbContext.InsertReturnIdentityAsync(order);
|
||||
|
||||
try
|
||||
{
|
||||
await GenerateConsumeRecordAsync(orderId, settings, shifts, request.CostDateTime);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
await _dbContext.Updateable<CanteenOrderEntity>()
|
||||
.SetColumns(x => x.OrderStatus == CanteenOrderStatusEnumType.Failure)
|
||||
.SetColumns(x => x.Result == ex.Message)
|
||||
.SetColumns(x => x.ModifiedTime == DateTime.Now)
|
||||
.Where(x => x.Id == orderId)
|
||||
.ExecuteCommandAsync();
|
||||
|
||||
throw;
|
||||
}
|
||||
|
||||
await _dbContext.Updateable<CanteenOrderEntity>()
|
||||
.SetColumns(x => x.OrderStatus == CanteenOrderStatusEnumType.Successful)
|
||||
.SetColumns(x => x.ModifiedTime == DateTime.Now)
|
||||
.Where(x => x.Id == orderId)
|
||||
.ExecuteCommandAsync();
|
||||
}
|
||||
public static bool IsPresent2(TimePeriod period, DateTime dateTime)
|
||||
{
|
||||
var start = DateTime.Now;
|
||||
var end = DateTime.Now;
|
||||
|
||||
if (period.IsCrossDay())
|
||||
{
|
||||
// 第二天
|
||||
if (dateTime.Hour >= 0)
|
||||
{
|
||||
start = DateTime.Today.AddDays(-1).Add(period.Start.Value.ToTimeSpan());
|
||||
end = DateTime.Today.Add(period.End.Value.ToTimeSpan());
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
start = DateTime.Today.Add(period.Start.Value.ToTimeSpan());
|
||||
end = DateTime.Today.Add(period.End.Value.ToTimeSpan());
|
||||
}
|
||||
|
||||
return dateTime >= start && dateTime <= end;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 充值
|
||||
/// </summary>
|
||||
@@ -464,4 +585,5 @@ public class OrderService : IOrderService
|
||||
|
||||
}, 5);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user