feat 增加作废明细
This commit is contained in:
@@ -65,4 +65,15 @@ public class BalancesController : ControllerBase
|
||||
{
|
||||
return await _balanceService.GetPageByUserAsync(request);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 作废消费明细
|
||||
/// </summary>
|
||||
/// <param name="request"></param>
|
||||
/// <returns></returns>
|
||||
[HttpPost("obsoluteOutRecord")]
|
||||
public async Task ObsoluteAsync(BalanceRecordObsoluteRequest request)
|
||||
{
|
||||
await _balanceService.ObsoluteAsync(request);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace Dlhis.Canteen.Order.Request;
|
||||
|
||||
public class BalanceRecordObsoluteRequest
|
||||
{
|
||||
public int Id { get; set; }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 事由
|
||||
/// </summary>
|
||||
[Required(ErrorMessage = "事由不能为空")]
|
||||
public string Reason { get; set; }
|
||||
}
|
||||
@@ -11,4 +11,6 @@ public interface IBalanceService : IScopeDependency
|
||||
Task<Pagination<BalanceRecordResponse>> GetPageByDeviceUniqueNoAsync(BalanceRecordPageByDeviceUniqueNoRequest request);
|
||||
|
||||
Task<Pagination<BalanceRecordUserResponse>> GetPageByUserAsync(BalanceRecordUserPageRequest request);
|
||||
|
||||
Task ObsoluteAsync(BalanceRecordObsoluteRequest request);
|
||||
}
|
||||
|
||||
@@ -3,6 +3,8 @@ using Dlhis.Canteen.Order.Response;
|
||||
using Dlhis.Entity.Basic.Entity;
|
||||
using Dlhis.Entity.Canteen.Entities;
|
||||
using Dlhis.Entity.Canteen.Enums;
|
||||
using Kurisu.AspNetCore.UnifyResultAndValidation;
|
||||
using Kurisu.AspNetCore.Utils.Extensions;
|
||||
using Kurisu.Core.DataAccess.Dto;
|
||||
using Kurisu.Core.User.Abstractions;
|
||||
using Kurisu.SqlSugar.Attributes;
|
||||
@@ -16,11 +18,13 @@ public class BalanceService : IBalanceService
|
||||
{
|
||||
private readonly IDbContext _dbContext;
|
||||
private readonly ICurrentUser _currentUser;
|
||||
private readonly ILogger<BalanceService> _logger;
|
||||
|
||||
public BalanceService(IDbContext dbContext, ICurrentUser currentUser)
|
||||
public BalanceService(IDbContext dbContext, ICurrentUser currentUser, ILogger<BalanceService> logger)
|
||||
{
|
||||
_dbContext = dbContext;
|
||||
_currentUser = currentUser;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public async Task<Pagination<BalanceRecordResponse>> GetPageAsync(BalanceRecordPageRequest request)
|
||||
@@ -162,4 +166,61 @@ public class BalanceService : IBalanceService
|
||||
|
||||
return page;
|
||||
}
|
||||
|
||||
[Transactional]
|
||||
public async Task ObsoluteAsync(BalanceRecordObsoluteRequest request)
|
||||
{
|
||||
var balanceRecord = await _dbContext.Queryable<BalanceRecordEntity>().Where(x => x.Id == request.Id).SingleAsync();
|
||||
balanceRecord.ThrowIfNull("记录不存在");
|
||||
|
||||
(balanceRecord.Type != BalanceRecordTypeEnumType.Out).ThrowIfTrue("非消费记录,无法操作");
|
||||
|
||||
balanceRecord.Status = BalanceRecordStatusEnumType.Obsolute;
|
||||
balanceRecord.Remark = request.Reason;
|
||||
|
||||
|
||||
await ContinueUpdateAsync(async () =>
|
||||
{
|
||||
//重算金额
|
||||
var canteenUser = await _dbContext.Queryable<CanteenUserEntity>().Where(x => x.UserNo == balanceRecord.UserNo).SingleAsync();
|
||||
|
||||
//金额 +
|
||||
canteenUser.Balance += Math.Abs(balanceRecord.Amount);
|
||||
|
||||
var effect = await _dbContext.Updateable<CanteenUserEntity>()
|
||||
.SetColumns(x => x.Balance == canteenUser.Balance)
|
||||
.SetColumns(x => x.Version == x.Version + 1)
|
||||
.Where(x => x.UserNo == balanceRecord.UserNo && x.Version == canteenUser.Version)
|
||||
.ExecuteCommandAsync();
|
||||
|
||||
if (effect <= 0) throw new VersionExceptions("更新版本冲突");
|
||||
|
||||
await _dbContext.UpdateAsync(balanceRecord);
|
||||
}, 5);
|
||||
}
|
||||
|
||||
|
||||
//************************************************************ help methods ******************************************************************
|
||||
|
||||
private async Task ContinueUpdateAsync(Func<Task> func, int retry)
|
||||
{
|
||||
for (int i = 0; i < retry; i++)
|
||||
{
|
||||
try
|
||||
{
|
||||
await func();
|
||||
return;
|
||||
}
|
||||
catch (Exception ex) when (ex is VersionExceptions)
|
||||
{
|
||||
_logger.LogWarning(ex, ex.Message);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
throw new UserFriendlyException("操作失败,请重试");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -127,5 +127,11 @@ public class BalanceRecordEntity : SugarBaseEntity, ITenantId
|
||||
[SugarColumn(ColumnDescription = "消费时间")]
|
||||
public DateTime CostDateTime { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 备注
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnDescription = "备注")]
|
||||
public string Remark { get; set; }
|
||||
|
||||
public string TenantId { get; set; }
|
||||
}
|
||||
@@ -25,5 +25,5 @@ public enum BalanceRecordStatusEnumType
|
||||
/// 作废
|
||||
/// </summary>
|
||||
[Description("作废数据")]
|
||||
Absolute = 99
|
||||
Obsolute = 99
|
||||
}
|
||||
Reference in New Issue
Block a user