Files
jinko.yikatong/Src/Dlhis.Middle/Services/Implements/OrderService.cs
2024-08-21 16:05:40 +08:00

338 lines
13 KiB
C#

using System.Diagnostics;
using Dlhis.Entity.Canteen.Enums;
using Dlhis.Middle.Entities;
using Kurisu.AspNetCore.Utils;
using Kurisu.AspNetCore.Utils.Extensions;
using Mapster;
using Kurisu.AspNetCore.CustomClass;
using Kurisu.AspNetCore.DataAccess.SqlSugar.Extensions;
using Kurisu.AspNetCore.DataAccess.SqlSugar.Services;
using Kurisu.AspNetCore.UnifyResultAndValidation;
using Dlhis.Middle.Response;
using Dlhis.Middle.Request;
namespace Dlhis.Middle.Services.Implements;
public class OrderService : IOrderService
{
private readonly ICanteenSettingService _canteenSettingService;
private readonly IRemoteOrderService _remoteOrderService;
private readonly ILogger<OrderService> _logger;
private readonly string _factory;
private readonly IDbContext _dbContext;
private Dictionary<CanteenSettingItemEnumType, string> _settings;
public OrderService(ICanteenSettingService canteenSettingService,
IRemoteOrderService remoteOrderService,
IConfiguration configuration,
ILogger<OrderService> logger,
IDbContext dbContext)
{
_canteenSettingService = canteenSettingService;
_remoteOrderService = remoteOrderService;
_logger = logger;
_factory = configuration.GetSection("Factory").Value;
_dbContext = dbContext;
}
public async Task<DefaultApiResult<OrderResultResponse>> AddAsync(OrderAddRequest request)
{
var sw = Stopwatch.StartNew();
try
{
if (await _remoteOrderService.AbleRemoteCallAsync())
{
var remoteResult = await _remoteOrderService.AddAsync(request);
if (remoteResult.Code == ApiStateCode.Success)
{
var d = remoteResult.Data;
await _dbContext.Updateable<LocalGainfoMealEntity>()
.SetColumns(x => x.Balance == d.Balance)
.Where(x => x.UserCode == d.UserCode)
.ExecuteCommandAsync();
}
remoteResult.Msg = "在线消费:" + remoteResult.Msg;
return remoteResult;
}
//根据档案号
var gainfo = await _dbContext.Queryable<LocalGainfoEntity>()
.Where(x => x.Code == request.UserUniqueNo)
.SingleAsync();
var message = "离线消费:";
gainfo.ThrowIfNull("无消费权限");
var isError = false;
try
{
(await _dbContext.Queryable<LocalGainfoMealEntity>().Where(x => x.UserCode == gainfo.Code).AnyAsync()).ThrowIfFalse("余额不足,无法消费");
await ConsumeAsync(request.Adapt<ConsumeRequest>(), gainfo);
}
catch (Exception ex)
{
message += ex.Message;
isError = true;
}
//获取人员余额
var data = await _dbContext.Queryable<LocalGainfoMealEntity>()
.LeftJoin<LocalGainfoEntity>((x, ga) => x.UserCode == ga.Code)
.LeftJoin<LocalDepartmentEntity>((x, ga, department) => ga.DepartmentNo == department.No)
.Where(x => x.UserCode == gainfo.Code)
.Select((x, ga, department) => new OrderResultResponse
{
UserCode = ga.Code,
UserName = ga.Name,
DepartmentNo = ga.DepartmentNo,
DepartmentName = department.Name,
Balance = x.Balance
})
.SingleAsync();
return new DefaultApiResult<OrderResultResponse>
{
Code = isError ? ApiStateCode.Error : ApiStateCode.Success,
Data = data,
Msg = message
};
}
finally
{
_logger.LogInformation("消费请求:{json}. 耗时:{time}", request.ToJson(), sw.ElapsedMilliseconds);
}
}
/// <summary>
/// 消费
/// </summary>
/// <param name="request"></param>
/// <param name="gainfo"></param>
/// <returns></returns>
private async Task ConsumeAsync(ConsumeRequest request, LocalGainfoEntity gainfo)
{
var registry = await _dbContext.Queryable<LocalDeviceRegistryEntity>().Where(x => x.DeviceUniqueNo == request.DeviceUniqueNo).SingleAsync();
registry.ThrowIfNull("设备尚未登记");
var device = await _dbContext.Queryable<LocalDeviceEntity>().Where(x => x.UniqueNo == request.DeviceUniqueNo).SingleAsync();
device.ThrowIfNull("设备尚未登记");
device.Enable.ThrowIfFalse("设备已被停用");
var businessPlaceInfo = await _dbContext.Queryable<LocalBusinessPlaceEntity>()
.LeftJoin<LocalBusinessPlaceShiftRelationshipEntity>((x, sr) => x.Code == sr.PlaceCode)
.Where(x => x.Code == registry.BusinessPlaceCode)
.Select((x, sr) => new
{
x.Code,
sr.ShiftCode,
x.TenantId
})
.FirstAsync();
businessPlaceInfo.ThrowIfNull("设备尚未登记营业点");
//获取当前消费餐次
var shifts = await _dbContext.Queryable<LocalBusinessShiftMealRelationshipEntity>().Where(x => x.ShiftCode == businessPlaceInfo.ShiftCode).ToListAsync();
shifts.ThrowIfEmpty("营业点餐次尚未设置");
var shift = shifts.FirstOrDefault(x => new TimePeriod
{
Start = TimeOnly.FromDateTime(x.Start),
End = TimeOnly.FromDateTime(x.End)
}.IsPresent(request.CostDateTime));
shift.ThrowIfNull("非就餐时间,无法消费");
var costDate = await GetCostDateAsync(shift, request.CostDateTime);
var balanceRecord = new LocalBalanceRecordEntity
{
// RequestJson = request.ToJson(),
// ThirdNo = request.ThirdNo,
Type = BalanceRecordTypeEnumType.Out,
ApplicationType = CanteenApplicationTypeEnumType.Device,
Name = gainfo.Name,
Source = request.Source,
UserNo = gainfo.No,
UserCode = gainfo.Code,
Amount = -Math.Abs(request.Amount),
BusinessPlaceCode = businessPlaceInfo?.Code,
BusinessShiftCode = shift?.ShiftCode,
ShiftType = shift?.ShiftType,
CostDate = costDate.Date,
CostId = gainfo.CostId,
DepartmentNo = gainfo.DepartmentNo,
DeviceUniqueNo = request.DeviceUniqueNo,
No = _factory + "-" + SnowFlakeHelper.Instance.NextId(),
// IsOffline = request.IsOffline,
IsCostCrossTenant = await GetSettingAsync<bool>(businessPlaceInfo!.TenantId, CanteenSettingItemEnumType.AllowCrossTenant),
OriginTenantId = gainfo.Factory,
Werks = gainfo.Werks,
//餐别异常处理
Status = shift == null
? BalanceRecordStatusEnumType.Doubtful
: BalanceRecordStatusEnumType.Valid,
//OrderStatus = CanteenOrderStatusEnumType.Wait,
CostDateTime = request.CostDateTime,
//标识当前消费的基地
TenantId = businessPlaceInfo.TenantId,
};
//校验
await Verify(balanceRecord);
var allowNoBalance = await GetSettingAsync<bool>(balanceRecord.TenantId, CanteenSettingItemEnumType.AllowNoBalance);
await CommonHelper.RetryAsync(async () =>
{
if (!allowNoBalance)
{
var gainfoMeal = await _dbContext.Queryable<LocalGainfoMealEntity>().Where(x => x.UserCode == balanceRecord.UserCode).SingleAsync();
//金额+-
gainfoMeal.Balance -= Math.Abs(balanceRecord.Amount);
(gainfoMeal.Balance < 0).ThrowIfTrue("余额不足,无法消费");
await _dbContext.Updateable<LocalGainfoMealEntity>()
.SetColumns(x => x.Balance == gainfoMeal.Balance)
.SetColumns(x => x.Version == x.Version + 1)
.SetColumns(x => x.ModifiedTime == DateTime.Now)
.Where(x => x.UserCode == balanceRecord.UserCode && x.Version == gainfoMeal.Version) //必须与查询时的version一致
.ExecuteCommandThrowVersionAsync();
}
await _dbContext.InsertAsync(balanceRecord);
}, 5);
}
//******************************************************* help methods ************************************************************/
/// <summary>
/// 获取成本日期
/// </summary>
/// <param name="shift"></param>
/// <param name="costDateTime"></param>
/// <returns></returns>
private async Task<DateTime> GetCostDateAsync(LocalBusinessShiftMealRelationshipEntity shift, DateTime costDateTime)
{
var switchTime = await GetSettingAsync<TimeOnly?>(shift.TenantId, CanteenSettingItemEnumType.BusinessSwitchTime);
var costDate = costDateTime.Date;
if (switchTime.HasValue)
{
var beginOfDay = costDateTime.Date;
var setTime = beginOfDay.Add(switchTime.Value.ToTimeSpan());
if (costDateTime < setTime)
costDate = costDateTime.Date.AddDays(-1);
}
return costDate;
}
/// <summary>
/// 验证参数
/// </summary>
/// <param name="order"></param>
/// <returns></returns>
/// <exception cref="UserFriendlyException"></exception>
private async Task Verify(LocalBalanceRecordEntity order)
{
var beginOfDay = DateTime.Today;
var endOfDay = beginOfDay.AddDays(1).AddSeconds(-1);
//今天所有消费记录
var todayAllBalanceRecords = await _dbContext.Queryable<LocalBalanceRecordEntity>()
.Where(x => x.UserNo == order.UserNo)
.Where(x => x.Type == BalanceRecordTypeEnumType.Out)
.Where(x => x.Status == BalanceRecordStatusEnumType.Valid)
.Where(x => x.CreateTime >= beginOfDay && x.CreateTime <= endOfDay)
.ToListAsync();
//超市营业点
var supermarketCodes = await _dbContext.Queryable<LocalBusinessPlaceEntity>()
.Where(x => x.Catalog == CanteenBusinessPlaceCatalogEnumType.Supermarket)
.Select(x => x.Code)
.ToListAsync();
//一天消费金额
var dayCostMax = await GetSettingAsync<decimal>(order.TenantId, CanteenSettingItemEnumType.DayCostMax);
if (dayCostMax != default)
{
//must has business place code, use .Value is safe
var cost = todayAllBalanceRecords
.Where(x => !supermarketCodes.Contains(x.BusinessPlaceCode.Value)) //filter supermarket
.Sum(x => Math.Abs(x.Amount));
(cost + Math.Abs(order.Amount) > dayCostMax).ThrowIfTrue($"超出当天消费限制{dayCostMax}元,无法消费");
}
//一次消费金额
var onceCostMax = await GetSettingAsync<decimal>(order.TenantId, CanteenSettingItemEnumType.OnceCostMax);
if (onceCostMax != default)
{
(Math.Abs(order.Amount) > onceCostMax).ThrowIfTrue($"单次消费金额不能超出{onceCostMax}元");
}
//超时消费限额
var dayMarketCostMax = await GetSettingAsync<decimal>(order.TenantId, CanteenSettingItemEnumType.DayMarketCostMax);
if (dayMarketCostMax != default)
{
//current businessplace is supermarket
if (order.BusinessPlaceCode.HasValue && supermarketCodes.Contains(order.BusinessPlaceCode.Value))
{
var cost = todayAllBalanceRecords.Where(x => x.BusinessPlaceCode.HasValue)
.Where(x => supermarketCodes.Contains(x.BusinessPlaceCode.Value))
.Sum(x => Math.Abs(x.Amount));
(cost + Math.Abs(order.Amount) > dayMarketCostMax).ThrowIfTrue($"超出当天超市消费限制{dayMarketCostMax},无法消费");
}
}
//重复餐次消费
var allowRepeatInOneShift = await GetSettingAsync(order.TenantId, CanteenSettingItemEnumType.AllowRepeatInOneShift, true);
if (!allowRepeatInOneShift)
{
var costCount = todayAllBalanceRecords.Count(x => x.ShiftType == order.ShiftType);
(costCount > 1).ThrowIfTrue($"{order?.ShiftType?.GetDescription()}不允许多次消费");
}
}
/// <summary>
/// 获取参数配置
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="tenantId"></param>
/// <param name="item"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
private async Task<T> GetSettingAsync<T>(string tenantId, CanteenSettingItemEnumType item, T defaultValue = default)
{
_settings ??= await _canteenSettingService.GetUserSettingAsync(string.Empty, tenantId);
if (_settings.TryGetValue(item, out var v))
{
try
{
return int.TryParse(v, out var n) ? n.Adapt<T>() : v.Adapt<T>();
}
catch (Exception)
{
return defaultValue;
}
}
return defaultValue;
}
}