10 changed files with 333 additions and 5 deletions
-
26src/Shentun.Peis.Application.Contracts/CardRegisters/CreateCardBillDto.cs
-
22src/Shentun.Peis.Application/CardRegisters/CardRegisterAppService.cs
-
20src/Shentun.Peis.Domain.Shared/Enums/CardBillFlag.cs
-
1src/Shentun.Peis.Domain/CardBills/CardBill.cs
-
63src/Shentun.Peis.Domain/CardRegisters/CardRegisterManager.cs
-
2src/Shentun.Peis.Domain/Sexs/Sex.cs
-
13src/Shentun.Peis.EntityFrameworkCore/EntityFrameworkCore/PeisDbContext.cs
-
54src/Shentun.Peis.EntityFrameworkCore/IncludeDetails/IncludeDetailsExtr.cs
-
135test/Shentun.Peis.Domain.Tests/CardRegisterManagerTest.cs
-
2test/Shentun.Peis.Domain.Tests/GuideTypeManagerTest.cs
@ -0,0 +1,26 @@ |
|||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Text; |
||||
|
|
||||
|
namespace Shentun.Peis.CardRegisters |
||||
|
{ |
||||
|
public class CreateCardBillDto |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// 卡登记编号
|
||||
|
/// </summary>
|
||||
|
public Guid CardRegisterId { get; set; } |
||||
|
/// <summary>
|
||||
|
/// 支付方式
|
||||
|
/// </summary>
|
||||
|
public string PayModeId { get; set; } |
||||
|
/// <summary>
|
||||
|
/// 记账金额
|
||||
|
/// </summary>
|
||||
|
public decimal BillMoney { get; set; } |
||||
|
/// <summary>
|
||||
|
/// 记账标志 0、扣费 1、退费 2、充值
|
||||
|
/// </summary>
|
||||
|
public char BillFlag { get; set; } |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,20 @@ |
|||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.ComponentModel; |
||||
|
using System.Text; |
||||
|
|
||||
|
namespace Shentun.Peis.Enums |
||||
|
{ |
||||
|
public static class CardBillFlag |
||||
|
{ |
||||
|
|
||||
|
[Description("充值")] |
||||
|
public const char Charge = '0'; |
||||
|
|
||||
|
[Description("扣费")] |
||||
|
public const char Deduction = '1'; |
||||
|
|
||||
|
[Description("退费")] |
||||
|
public const char Refund = '2'; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,54 @@ |
|||||
|
using Microsoft.EntityFrameworkCore; |
||||
|
using Shentun.Peis.EntityFrameworkCore; |
||||
|
using Shentun.Peis.Models; |
||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Linq; |
||||
|
using System.Text; |
||||
|
using System.Threading.Tasks; |
||||
|
using Volo.Abp.Domain.Repositories; |
||||
|
using Volo.Abp.Domain.Repositories.EntityFrameworkCore; |
||||
|
using Volo.Abp.EntityFrameworkCore; |
||||
|
using Volo.Abp.Identity; |
||||
|
using Volo.Abp.Identity.EntityFrameworkCore; |
||||
|
|
||||
|
namespace Shentun.Peis.IncludeDetails |
||||
|
{ |
||||
|
internal static class IncludeDetailsExtr |
||||
|
{ |
||||
|
public static async Task<IQueryable<CardRegister>> IncludeDetails(this Task<IQueryable<CardRegister>> queryable, |
||||
|
bool include = true) |
||||
|
{ |
||||
|
if (!include) |
||||
|
{ |
||||
|
return queryable.Result; |
||||
|
} |
||||
|
|
||||
|
return queryable.Result |
||||
|
.Include(x => x.CardBills); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public interface ICardRegisterRepository : IBasicRepository<CardRegister, Guid>, IBasicRepository<CardRegister>, |
||||
|
IReadOnlyRepository<CardRegister>, IRepository, IReadOnlyBasicRepository<CardRegister, Guid> |
||||
|
{ |
||||
|
} |
||||
|
|
||||
|
|
||||
|
public class EfCoreCardRegisterRepository |
||||
|
: EfCoreRepository<PeisDbContext, CardRegister, Guid>, ICardRegisterRepository |
||||
|
{ |
||||
|
public EfCoreCardRegisterRepository( |
||||
|
IDbContextProvider<PeisDbContext> dbContextProvider) |
||||
|
: base(dbContextProvider) |
||||
|
{ |
||||
|
} |
||||
|
|
||||
|
public async override Task<IQueryable<CardRegister>> WithDetailsAsync() |
||||
|
{ |
||||
|
var query = await GetQueryableAsync().IncludeDetails(); |
||||
|
return query; // Uses the extension method defined above
|
||||
|
} |
||||
|
} |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,135 @@ |
|||||
|
using NSubstitute; |
||||
|
using NSubstitute.Exceptions; |
||||
|
using Shentun.Peis.CardRegisters; |
||||
|
using Shentun.Peis.Enums; |
||||
|
using Shentun.Peis.GuidTypes; |
||||
|
using Shentun.Peis.IncludeDetails; |
||||
|
using Shentun.Peis.Models; |
||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Linq; |
||||
|
using System.Text; |
||||
|
using System.Threading.Tasks; |
||||
|
using TencentCloud.Cpdp.V20190820.Models; |
||||
|
using Volo.Abp.Domain.Repositories; |
||||
|
using Volo.Abp.Uow; |
||||
|
using Xunit; |
||||
|
using Xunit.Abstractions; |
||||
|
|
||||
|
namespace Shentun.Peis |
||||
|
{ |
||||
|
public class CardRegisterManagerTest : PeisDomainTestBase |
||||
|
{ |
||||
|
private readonly IRepository<CardRegister, Guid> _repository; |
||||
|
private readonly IRepository<CardBill, Guid> _cardBillRepository; |
||||
|
private readonly ICardRegisterRepository _cardRegisterRepository; |
||||
|
private readonly CardRegisterManager _manager; |
||||
|
private readonly ITestOutputHelper _output; |
||||
|
private readonly IUnitOfWorkManager _unitOfWorkManager; |
||||
|
public CardRegisterManagerTest(ITestOutputHelper output) |
||||
|
{ |
||||
|
_output = output; |
||||
|
_repository = GetRequiredService<IRepository<CardRegister, Guid>>(); |
||||
|
_cardRegisterRepository = GetRequiredService<ICardRegisterRepository>(); |
||||
|
_cardBillRepository = GetRequiredService<IRepository<CardBill, Guid>>(); |
||||
|
_manager = GetRequiredService<CardRegisterManager>(); |
||||
|
|
||||
|
_unitOfWorkManager = GetRequiredService<IUnitOfWorkManager>(); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
[UnitOfWork] |
||||
|
public async void AddCardBillTest() |
||||
|
{ |
||||
|
var id = new Guid("3a0d9464-e69e-67d5-c292-b2e11fb334b1"); |
||||
|
using (IUnitOfWork unitOfWork = _unitOfWorkManager.Begin()) |
||||
|
{ |
||||
|
var cardRegister = await _cardRegisterRepository.GetAsync(id, includeDetails: true); |
||||
|
//var cardRegister = _cardRegisterRepository.WithDetails().Where(o => o.Id == id).FirstOrDefault();
|
||||
|
//var cardRegister = await _cardRegisterRepository.GetAsync(id, includeDetails: true);
|
||||
|
//var cardRegister = await _cardRegisterRepository.Get(new Guid("3a0d9464-e69e-67d5-c292-b2e11fb334b1"), includeDetails: true);
|
||||
|
//await _repository.EnsureCollectionLoadedAsync(cardRegister, x => x.CardBills);
|
||||
|
_output.WriteLine(cardRegister.CardBalance.ToString()); |
||||
|
_output.WriteLine(cardRegister.CardBills.Count.ToString()); |
||||
|
|
||||
|
var cardBill = _manager.CreateCardBill(cardRegister, "01", CardBillFlag.Charge, Convert.ToDecimal(10)); |
||||
|
try |
||||
|
{ |
||||
|
await _repository.UpdateAsync(cardRegister); |
||||
|
await _cardBillRepository.InsertAsync(cardBill); |
||||
|
await unitOfWork.CompleteAsync(); |
||||
|
} |
||||
|
catch (Exception ex) |
||||
|
{ |
||||
|
await unitOfWork.RollbackAsync(); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
|
||||
|
_output.WriteLine(cardRegister.CardBalance.ToString()); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async void AddCardBillNoUnitOfWorkTest() |
||||
|
{ |
||||
|
var id = new Guid("3a0d9464-e69e-67d5-c292-b2e11fb334b1"); |
||||
|
|
||||
|
var cardRegister = await _cardRegisterRepository.GetAsync(id, includeDetails: true); |
||||
|
//var cardRegister = _cardRegisterRepository.WithDetails().Where(o => o.Id == id).FirstOrDefault();
|
||||
|
//var cardRegister = await _cardRegisterRepository.GetAsync(id, includeDetails: true);
|
||||
|
//var cardRegister = await _cardRegisterRepository.Get(new Guid("3a0d9464-e69e-67d5-c292-b2e11fb334b1"), includeDetails: true);
|
||||
|
//await _repository.EnsureCollectionLoadedAsync(cardRegister, x => x.CardBills);
|
||||
|
_output.WriteLine(cardRegister.CardBalance.ToString()); |
||||
|
_output.WriteLine(cardRegister.CardBills.Count.ToString()); |
||||
|
|
||||
|
var cardBill = _manager.CreateCardBill(cardRegister, "01", CardBillFlag.Refund, Convert.ToDecimal(-1000)); |
||||
|
await _repository.UpdateAsync(cardRegister); |
||||
|
await _cardBillRepository.InsertAsync(cardBill); |
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
} |
||||
|
/// <summary>
|
||||
|
/// 要支持级联更新,必须使用工作单元UnitOfWork
|
||||
|
/// </summary>
|
||||
|
[Fact] |
||||
|
//[UnitOfWork]
|
||||
|
public async void AddCardBillByGetIdTest() |
||||
|
{ |
||||
|
var id = new Guid("3a0d9464-e69e-67d5-c292-b2e11fb334b1"); |
||||
|
|
||||
|
|
||||
|
|
||||
|
using (IUnitOfWork unitOfWork = _unitOfWorkManager.Begin()) |
||||
|
{ |
||||
|
var cardRegister = await _cardRegisterRepository.GetAsync(id, includeDetails: true); |
||||
|
_output.WriteLine(cardRegister.CardBalance.ToString()); |
||||
|
_output.WriteLine(cardRegister.CardBills.Count.ToString()); |
||||
|
|
||||
|
_manager.AddCardBill(cardRegister, "01", CardBillFlag.Charge, Convert.ToDecimal(100)); |
||||
|
|
||||
|
try |
||||
|
{ |
||||
|
await _repository.UpdateAsync(cardRegister); |
||||
|
await unitOfWork.CompleteAsync(); |
||||
|
} |
||||
|
catch (Exception ex) |
||||
|
{ |
||||
|
await unitOfWork.RollbackAsync(); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
|
||||
|
_output.WriteLine(cardRegister.CardBalance.ToString()); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
|
||||
|
} |
||||
|
|
||||
|
} |
||||
|
} |
||||
Write
Preview
Loading…
Cancel
Save
Reference in new issue