fix 餐饮消费控制和设备统计优化
This commit is contained in:
@@ -56,7 +56,7 @@ public class BalancesController : ControllerBase
|
||||
/// <returns></returns>
|
||||
[HttpGet("device/page")]
|
||||
[IgnoreTenant]
|
||||
public async Task<Pagination<BalanceRecordResponse>> GetPageByDeviceUniqueNoGetAsync([FromQuery] BalanceRecordPageByDeviceUniqueNoRequest request)
|
||||
public async Task<Pagination<BalanceRecordDeviceResponse>> GetPageByDeviceUniqueNoGetAsync([FromQuery] BalanceRecordPageByDeviceUniqueNoRequest request)
|
||||
{
|
||||
return await _balanceService.GetPageByDeviceUniqueNoAsync(request);
|
||||
}
|
||||
@@ -68,7 +68,7 @@ public class BalancesController : ControllerBase
|
||||
/// <returns></returns>
|
||||
[HttpPost("device/page")]
|
||||
[IgnoreTenant]
|
||||
public async Task<Pagination<BalanceRecordResponse>> GetPageByDeviceUniqueNoPostAsync(BalanceRecordPageByDeviceUniqueNoRequest request)
|
||||
public async Task<Pagination<BalanceRecordDeviceResponse>> GetPageByDeviceUniqueNoPostAsync(BalanceRecordPageByDeviceUniqueNoRequest request)
|
||||
{
|
||||
return await _balanceService.GetPageByDeviceUniqueNoAsync(request);
|
||||
}
|
||||
|
||||
107
Src/Dlhis.Canteen/Order/Response/BalanceRecordDeviceResponse.cs
Normal file
107
Src/Dlhis.Canteen/Order/Response/BalanceRecordDeviceResponse.cs
Normal file
@@ -0,0 +1,107 @@
|
||||
using Dlhis.Entity.Canteen.Enums;
|
||||
using Kurisu.AspNetCore.Utils.Extensions;
|
||||
|
||||
namespace Dlhis.Canteen.Order.Response;
|
||||
|
||||
public class BalanceRecordDeviceResponse
|
||||
{
|
||||
public long Id { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 流水号
|
||||
/// </summary>
|
||||
public string No { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 档案号/QRCode
|
||||
/// </summary>
|
||||
public string UserNo { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 姓名
|
||||
/// </summary>
|
||||
public string Name { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 学工号
|
||||
/// </summary>
|
||||
public string UserCode { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 部门号
|
||||
/// </summary>
|
||||
public string DepartmentNo { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 部门
|
||||
/// </summary>
|
||||
public string DepartmentName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 所属成本日期
|
||||
/// </summary>
|
||||
public DateTime CostDate { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 来源
|
||||
/// </summary>
|
||||
public BalanceRecordSourceEnumType Source { get; set; }
|
||||
public string SourceDesc => Source.GetDescription();
|
||||
|
||||
/// <summary>
|
||||
/// 收入/支出
|
||||
/// </summary>
|
||||
public BalanceRecordTypeEnumType Type { get; set; }
|
||||
public string TypeDesc => Type.GetDescription();
|
||||
|
||||
/// <summary>
|
||||
/// 金额
|
||||
/// </summary>
|
||||
public decimal Amount { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 营业点
|
||||
/// </summary>
|
||||
public string BusinessPlaceName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 餐次
|
||||
/// </summary>
|
||||
public ShiftEnumType? ShiftType { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 数据状态
|
||||
/// </summary>
|
||||
public BalanceRecordStatusEnumType Status { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 数据状态描述
|
||||
/// </summary>
|
||||
public string StatusDesc => Status.GetDescription();
|
||||
|
||||
/// <summary>
|
||||
/// 记录产生时间
|
||||
/// </summary>
|
||||
public DateTime CostDateTime { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 汇总金额
|
||||
/// </summary>
|
||||
public decimal Sum { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 厂区id
|
||||
/// </summary>
|
||||
public string TenantId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 厂区名称
|
||||
/// </summary>
|
||||
public string TenantName { get; set; }
|
||||
}
|
||||
|
||||
|
||||
public class BalanceRecordDeviceItemResponse
|
||||
{
|
||||
|
||||
}
|
||||
@@ -117,11 +117,6 @@ public class BalanceRecordResponse
|
||||
/// </summary>
|
||||
public DateTime CostDateTime { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 汇总金额
|
||||
/// </summary>
|
||||
public decimal Sum { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 厂区id
|
||||
/// </summary>
|
||||
|
||||
@@ -9,7 +9,7 @@ public interface IBalanceService : IScopeDependency
|
||||
{
|
||||
Task<Pagination<BalanceRecordResponse>> GetPageAsync(BalanceRecordPageRequest request);
|
||||
|
||||
Task<Pagination<BalanceRecordResponse>> GetPageByDeviceUniqueNoAsync(BalanceRecordPageByDeviceUniqueNoRequest request);
|
||||
Task<Pagination<BalanceRecordDeviceResponse>> GetPageByDeviceUniqueNoAsync(BalanceRecordPageByDeviceUniqueNoRequest request);
|
||||
|
||||
Task<BalanceRecordUserResultResponse> GetPageByUserAsync(BalanceRecordUserPageRequest request);
|
||||
|
||||
|
||||
@@ -51,13 +51,13 @@ public class BalanceService : IBalanceService
|
||||
.WhereIF(request.Type.HasValue, record => record.Type == request.Type)
|
||||
.WhereIF(request.Search.IsPresent(), (record, ga) =>
|
||||
ga.Name.Contains(request.Search)
|
||||
|| ga.Code.Contains(request.Search)
|
||||
|| ga.Code == request.Search
|
||||
)
|
||||
.OrderBy(record => record.CreateTime, OrderByType.Desc)
|
||||
.Select((record, ga, department, bplace, bshift) => new BalanceRecordResponse
|
||||
{
|
||||
Id = record.Id,
|
||||
Amount = record.Amount,
|
||||
Amount = SqlFunc.Abs(record.Amount),
|
||||
BusinessPlaceCode = record.BusinessPlaceCode,
|
||||
BusinessShiftCode = record.BusinessShiftCode,
|
||||
CostDate = record.CostDate,
|
||||
@@ -83,11 +83,6 @@ public class BalanceService : IBalanceService
|
||||
})
|
||||
.ToPageListAsync(request);
|
||||
|
||||
page.Data.ForEach(item =>
|
||||
{
|
||||
item.Amount = Math.Abs(item.Amount);
|
||||
});
|
||||
|
||||
try
|
||||
{
|
||||
var tenantResult = await _tenantApi.GetTenantsInternalAsync();
|
||||
@@ -105,6 +100,70 @@ public class BalanceService : IBalanceService
|
||||
|
||||
return page;
|
||||
}
|
||||
|
||||
|
||||
public async Task<Pagination<BalanceRecordDeviceResponse>> GetPageByDeviceUniqueNoAsync(BalanceRecordPageByDeviceUniqueNoRequest request)
|
||||
{
|
||||
var now = request.SearchDate ?? DateTime.Now;
|
||||
var start = request.SearchDate.HasValue ? request.SearchDate.Value.Date : DateTime.Today;
|
||||
var end = start.AddDays(1).AddSeconds(-1);
|
||||
|
||||
var device = await _dbContext.Queryable<DeviceEntity>()
|
||||
.Where(x => x.UniqueNo == request.DeviceUniqueNo || x.Code == Guid.Parse(request.DeviceUniqueNo))
|
||||
.FirstAsync();
|
||||
|
||||
if (device == null)
|
||||
{
|
||||
return new Pagination<BalanceRecordDeviceResponse>()
|
||||
{
|
||||
Data = new List<BalanceRecordDeviceResponse>()
|
||||
};
|
||||
}
|
||||
|
||||
var page = await _dbContext.Queryable<BalanceRecordEntity>()
|
||||
.LeftJoin<GainfoEntity>((record, ga) => record.UserNo == ga.No)
|
||||
.LeftJoin<DepartmentEntity>((record, ga, department) => ga.DepartmentNo == department.No)
|
||||
.LeftJoin<BusinessPlaceEntity>((record, ga, department, bplace) => record.BusinessPlaceCode == bplace.Code)
|
||||
.Where(record => record.CostDate >= start && record.CostDate <= end)
|
||||
.Where(record => record.DeviceUniqueNo == device.UniqueNo || record.DeviceUniqueNo == device.Code.ToString())
|
||||
.Where(record => record.Type == BalanceRecordTypeEnumType.Out && record.Status == BalanceRecordStatusEnumType.Valid)
|
||||
.OrderBy(record => record.CreateTime, OrderByType.Desc)
|
||||
.Select((record, ga, department, bplace) => new BalanceRecordDeviceResponse
|
||||
{
|
||||
Id = record.Id,
|
||||
Amount = SqlFunc.Abs(record.Amount),
|
||||
CostDate = record.CostDate,
|
||||
DepartmentNo = record.DepartmentNo,
|
||||
UserNo = record.UserNo,
|
||||
UserCode = record.UserCode,
|
||||
Type = record.Type,
|
||||
Status = record.Status,
|
||||
Source = record.Source,
|
||||
No = record.No,
|
||||
Name = record.Name,
|
||||
DepartmentName = department.Name,
|
||||
BusinessPlaceName = bplace.Name,
|
||||
ShiftType = record.ShiftType,
|
||||
CostDateTime = record.CostDateTime,
|
||||
TenantId = record.TenantId,
|
||||
})
|
||||
.ToPageListAsync(request);
|
||||
|
||||
//消费总金额
|
||||
var sum = await _dbContext.Queryable<BalanceRecordEntity>()
|
||||
.Where(record => record.CostDate >= start)
|
||||
.Where(record => record.CostDate <= end)
|
||||
.Where(record => record.DeviceUniqueNo == device.UniqueNo || record.DeviceUniqueNo == device.Code.ToString())
|
||||
.Where(record => record.Type == BalanceRecordTypeEnumType.Out && record.Status == BalanceRecordStatusEnumType.Valid)
|
||||
.SumAsync(record => SqlFunc.Abs(SqlFunc.IsNull(record.Amount, 0)));
|
||||
|
||||
page.Data.ForEach(x => x.Sum = sum);
|
||||
|
||||
return page;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 根据成本中心
|
||||
/// </summary>
|
||||
@@ -438,84 +497,6 @@ public class BalanceService : IBalanceService
|
||||
return pagination;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public async Task<Pagination<BalanceRecordResponse>> GetPageByDeviceUniqueNoAsync(BalanceRecordPageByDeviceUniqueNoRequest request)
|
||||
{
|
||||
var now = DateTime.Now;
|
||||
var start = DateTime.Today;
|
||||
var end = new DateTime(now.Year, now.Month, now.Day, 23, 59, 59);
|
||||
|
||||
if (request.SearchDate.HasValue)
|
||||
{
|
||||
now = request.SearchDate.Value;
|
||||
start = new DateTime(now.Year, now.Month, now.Day);
|
||||
end = new DateTime(now.Year, now.Month, now.Day, 23, 59, 59);
|
||||
}
|
||||
|
||||
var device = await _dbContext.Queryable<DeviceEntity>()
|
||||
.Where(x => x.UniqueNo == request.DeviceUniqueNo || x.Code == Guid.Parse(request.DeviceUniqueNo))
|
||||
.FirstAsync();
|
||||
|
||||
if (device == null)
|
||||
{
|
||||
return new Pagination<BalanceRecordResponse>()
|
||||
{
|
||||
Data = new List<BalanceRecordResponse>()
|
||||
};
|
||||
}
|
||||
|
||||
var page = await _dbContext.Queryable<BalanceRecordEntity>()
|
||||
.LeftJoin<GainfoEntity>((record, ga) => record.UserNo == ga.No)
|
||||
.LeftJoin<DepartmentEntity>((record, ga, department) => ga.DepartmentNo == department.No)
|
||||
.LeftJoin<BusinessPlaceEntity>((record, ga, department, bplace) => record.BusinessPlaceCode == bplace.Code)
|
||||
.LeftJoin<BusinessShiftEntity>((record, ga, department, bplace, bshift) => record.BusinessShiftCode == bshift.Code)
|
||||
.LeftJoin<BasicTypeEntity>((record, ga, department, bplace, bshift, type) => record.BusinessShiftCode.ToString() == type.Code)
|
||||
.WhereIF(request.SearchDate.HasValue, record => record.CostDate >= start)
|
||||
.WhereIF(request.SearchDate.HasValue, record => record.CostDate <= end)
|
||||
.Where(record => record.TenantId == device.TenantId && (record.DeviceUniqueNo == device.Code.ToString() || record.DeviceUniqueNo == device.UniqueNo))
|
||||
.Where(record => record.Type == BalanceRecordTypeEnumType.Out && record.Status == BalanceRecordStatusEnumType.Valid)
|
||||
.OrderBy(record => record.CreateTime, OrderByType.Desc)
|
||||
.Select((record, ga, department, bplace, bshift, type) => new BalanceRecordResponse
|
||||
{
|
||||
Id = record.Id,
|
||||
Amount = record.Amount,
|
||||
BusinessPlaceCode = record.BusinessPlaceCode,
|
||||
BusinessShiftCode = record.BusinessShiftCode,
|
||||
CostDate = record.CostDate,
|
||||
CostId = record.CostId,
|
||||
DepartmentNo = record.DepartmentNo,
|
||||
DeviceInfo = record.DeviceUniqueNo,
|
||||
IP = record.IP,
|
||||
UserNo = record.UserNo,
|
||||
UserCode = record.UserCode,
|
||||
Type = record.Type,
|
||||
SubsidiesFromMonth = record.SubsidiesFromMonth,
|
||||
Status = record.Status,
|
||||
Source = record.Source,
|
||||
No = record.No,
|
||||
IsCostCrossTenant = record.IsCostCrossTenant,
|
||||
OriginTenantId = record.OriginTenantId,
|
||||
Name = record.Name,
|
||||
DepartmentName = department.Name,
|
||||
BusinessPlaceName = bplace.Name,
|
||||
BusinessShiftName = type.Display,
|
||||
CostDateTime = record.CostDateTime
|
||||
}).ToPageListAsync(request);
|
||||
|
||||
//消费总金额
|
||||
var sum = await _dbContext.Queryable<BalanceRecordEntity>()
|
||||
.WhereIF(request.SearchDate.HasValue, record => record.CostDate >= start)
|
||||
.WhereIF(request.SearchDate.HasValue, record => record.CostDate <= end)
|
||||
.Where(record => record.TenantId == device.TenantId || record.DeviceUniqueNo == device.Code.ToString() || record.DeviceUniqueNo == device.UniqueNo)
|
||||
.Where(record => record.Type == BalanceRecordTypeEnumType.Out && record.Status == BalanceRecordStatusEnumType.Valid)
|
||||
.SumAsync(record => SqlFunc.Abs(SqlFunc.IsNull(record.Amount, 0)));
|
||||
|
||||
page.Data.ForEach(x => x.Sum = sum);
|
||||
|
||||
return page;
|
||||
}
|
||||
|
||||
public async Task<BalanceRecordUserResultResponse> GetPageByUserAsync(BalanceRecordUserPageRequest request)
|
||||
{
|
||||
var jinkoCode = _currentUser.GetUserClaim("jinko-code") ?? string.Empty;
|
||||
|
||||
@@ -516,65 +516,61 @@ public class OrderService : BaseOrderService, IOrderService
|
||||
}
|
||||
|
||||
var beginOfDay = DateTime.Today;
|
||||
var endOfDay = beginOfDay.AddHours(23).AddMinutes(59).AddSeconds(59);
|
||||
var endOfDay = beginOfDay.AddDays(1).AddSeconds(-1);
|
||||
|
||||
var balanceRecords = await DbContext.Queryable<BalanceRecordEntity>()
|
||||
//今天所有消费记录
|
||||
var todayAllBalanceRecords = await DbContext.Queryable<BalanceRecordEntity>()
|
||||
.Where(x => x.UserNo == order.UserNo)
|
||||
.Where(x => x.Type == BalanceRecordTypeEnumType.Out)
|
||||
.Where(x => x.CreateTime >= beginOfDay && x.CreateTime <= endOfDay) //今天消费记录
|
||||
.Where(x => x.Status == BalanceRecordStatusEnumType.Valid)
|
||||
.Where(x => x.CreateTime >= beginOfDay && x.CreateTime <= endOfDay)
|
||||
.ToListAsync();
|
||||
|
||||
var dayMarketCostMax = await GetSettingAsync<decimal>(CanteenSettingItemEnumType.DayMarketCostMax);
|
||||
if (dayMarketCostMax != default)
|
||||
{
|
||||
if (order.BusinessPlaceCode.HasValue && await DbContext.Queryable<BusinessPlaceEntity>()
|
||||
//超市营业点
|
||||
var supermarketCodes = await DbContext.Queryable<BusinessPlaceEntity>()
|
||||
.Where(x => x.Catalog == CanteenBusinessPlaceCatalogEnumType.Supermarket)
|
||||
.Where(x => x.Code == order.BusinessPlaceCode.Value)
|
||||
.AnyAsync())
|
||||
{
|
||||
var businessPlaceCodes = await DbContext.Queryable<BusinessPlaceEntity>()
|
||||
.Where(x => x.Catalog == CanteenBusinessPlaceCatalogEnumType.Supermarket)
|
||||
.Select(x => x.Code).ToListAsync();
|
||||
|
||||
var cost = balanceRecords.Where(x => x.BusinessPlaceCode.HasValue)
|
||||
.Where(x => businessPlaceCodes.Contains(x.BusinessPlaceCode.Value))
|
||||
.Sum(x => x.Amount);
|
||||
|
||||
if (cost + Math.Abs(order.Amount) > dayMarketCostMax)
|
||||
{
|
||||
throw new UserFriendlyException($"超出当天超市消费限制({dayMarketCostMax}),无法消费");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var allowRepeatInOneShift = await GetSettingAsync(CanteenSettingItemEnumType.AllowRepeatInOneShift, true);
|
||||
if (!allowRepeatInOneShift)
|
||||
{
|
||||
//todo 餐次问题
|
||||
var costCount = balanceRecords.Where(x => x.BusinessShiftCode == order.BusinessShiftCode).Count();
|
||||
if (costCount > 1)
|
||||
{
|
||||
throw new UserFriendlyException($"当前餐次不允许多次消费");
|
||||
}
|
||||
}
|
||||
|
||||
var onceCostMax = await GetSettingAsync<decimal>(CanteenSettingItemEnumType.OnceCostMax);
|
||||
if (onceCostMax != default)
|
||||
{
|
||||
if (Math.Abs(order.Amount) > onceCostMax)
|
||||
{
|
||||
throw new UserFriendlyException($"一次消费金额不能超出{onceCostMax}");
|
||||
}
|
||||
}
|
||||
.Select(x => x.Code)
|
||||
.ToListAsync();
|
||||
|
||||
//一天消费金额
|
||||
var dayCostMax = await GetSettingAsync<decimal>(CanteenSettingItemEnumType.DayCostMax);
|
||||
if (dayCostMax != default)
|
||||
{
|
||||
var cost = balanceRecords.Where(x => x.Type == BalanceRecordTypeEnumType.Out).Sum(x => Math.Abs(x.Amount));
|
||||
if (cost + Math.Abs(order.Amount) > dayCostMax)
|
||||
{
|
||||
throw new UserFriendlyException($"超出当天消费限制({dayCostMax}),无法消费");
|
||||
//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>(CanteenSettingItemEnumType.OnceCostMax);
|
||||
if (onceCostMax != default)
|
||||
{
|
||||
(Math.Abs(order.Amount) > onceCostMax).ThrowIfTrue($"一次消费金额不能超出{onceCostMax}");
|
||||
}
|
||||
|
||||
//超时消费限额
|
||||
var dayMarketCostMax = await GetSettingAsync<decimal>(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(CanteenSettingItemEnumType.AllowRepeatInOneShift, true);
|
||||
if (!allowRepeatInOneShift)
|
||||
{
|
||||
var costCount = todayAllBalanceRecords.Where(x => x.ShiftType == order.ShiftType).Count();
|
||||
(costCount > 1).ThrowIfTrue($"{order?.ShiftType?.GetDescription()}不允许多次消费");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user