using Shentun.Peis.Models; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Volo.Abp; using Volo.Abp.Domain.Repositories; using Volo.Abp.Domain.Services; using static log4net.Appender.RollingFileAppender; namespace Shentun.Peis.CardRegisters { public class CardRegisterManager : DomainService { private readonly IRepository _repository; public CardRegisterManager(IRepository repository) { _repository = repository; } /// /// 创建 /// /// /// public async Task CreateAsync( CardRegister entity ) { Verify(entity); var queryable = await _repository.GetQueryableAsync(); var findedEntity = queryable.Where(x => x.CardNo == entity.CardNo && x.IsActive == 'Y').FirstOrDefault(); if (findedEntity != null) { throw new UserFriendlyException($"卡号已经被{findedEntity.CustomerName}使用"); } return new CardRegister( GuidGenerator.Create() ) { CardBalance = 0, IsActive = entity.IsActive, CardNo = entity.CardNo, CardPassword = entity.CardPassword, CardTypeId = entity.CardTypeId, CustomerName = entity.CustomerName, Discount = entity.Discount, ExpiryDate = entity.ExpiryDate, IdNo = entity.IdNo, MobileTelephone = entity.MobileTelephone, MedicalCenterId = entity.MedicalCenterId, Remark = entity.Remark, Telephone = entity.Telephone }; } /// /// 更新 /// /// /// /// public void UpdateAsync( CardRegister sourceEntity, CardRegister targetEntity ) { DataHelper.CheckEntityIsNull(sourceEntity); DataHelper.CheckEntityIsNull(targetEntity); Verify(sourceEntity); if (sourceEntity.CardNo != targetEntity.CardNo) { throw new ArgumentException("卡号不允许修改"); } if (sourceEntity.CardBalance != 0 && sourceEntity.CardBalance != targetEntity.CardBalance) { throw new ArgumentException("卡余额不允许修改"); } targetEntity.IsActive = sourceEntity.IsActive; targetEntity.CardPassword = sourceEntity.CardPassword; // targetEntity.CardTypeId = sourceEntity.CardTypeId; targetEntity.CustomerName = sourceEntity.CustomerName; targetEntity.Discount = sourceEntity.Discount; targetEntity.ExpiryDate = sourceEntity.ExpiryDate; targetEntity.IdNo = sourceEntity.IdNo; targetEntity.MobileTelephone = sourceEntity.MobileTelephone; // targetEntity.MedicalCenterId = sourceEntity.MedicalCenterId; targetEntity.Remark = sourceEntity.Remark; targetEntity.Telephone = sourceEntity.Telephone; } public async Task UpdateActive(CardRegister entity, char isActive) { DataHelper.CheckEntityIsNull(entity); DataHelper.CheckCharIsYOrN(isActive, "使用标志"); if (isActive == 'N') { entity.IsActive = isActive; } else { //检查卡号是否用过 var queryable = await _repository.GetQueryableAsync(); var findedEntity = queryable.Where(x => x.Id != entity.Id && x.CardNo == entity.CardNo && x.IsActive == 'Y').FirstOrDefault(); if (findedEntity != null) { throw new UserFriendlyException($"卡号已经被{findedEntity.CustomerName}使用"); } } } /// /// 创建卡充值 /// /// /// /// /// /// /// public CardBill CreateCardBill(CardRegister entity, string payModeId, char billFlag, decimal amount) { DataHelper.CheckEntityIsNull(entity); if (amount == 0) { throw new ArgumentException("金额等于0"); }; if (billFlag == Shentun.Peis.Enums.CardBillFlag.Charge && amount < 0) { throw new ArgumentException("充值金额不能小于0"); } else if (billFlag != Shentun.Peis.Enums.CardBillFlag.Charge && amount > 0) { throw new ArgumentException("扣费或退费金额不能大于0"); } if ((entity.CardBalance + amount) < 0) { throw new ArgumentException($"扣费或退费金额不能小于余额{entity.CardBalance}"); } entity.CardBalance += amount; var cardBill = new CardBill(GuidGenerator.Create()) { CardRegisterId = entity.Id, PayModeId = payModeId, BillFlag = billFlag, BillMoney = amount, }; return cardBill; } /// /// 增加卡充值,暂时不使用,用来测试聚合根模式 /// /// /// /// /// /// public void AddCardBill(CardRegister entity, string payModeId, char billFlag, decimal amount) { DataHelper.CheckEntityIsNull(entity); if (amount == 0) { throw new ArgumentException("金额等于0"); }; if (billFlag == Shentun.Peis.Enums.CardBillFlag.Charge && amount < 0) { throw new ArgumentException("充值金额不能小于0"); } else if (billFlag != Shentun.Peis.Enums.CardBillFlag.Charge && amount > 0) { throw new ArgumentException("扣费或退费金额不能大于0"); } if ((entity.CardBalance + amount) < 0) { throw new ArgumentException($"扣费或退费金额不能小于余额{entity.CardBalance}"); } entity.CardBalance += amount; var cardBill = new CardBill(GuidGenerator.Create()) { CardRegisterId = entity.Id, PayModeId = payModeId, BillFlag = billFlag, BillMoney = amount, }; entity.CardBills.Add(cardBill); } private void Verify(CardRegister entity) { DataHelper.CheckEntityIsNull(entity); DataHelper.CheckStringIsNull(entity.CardNo, "卡号"); DataHelper.CheckStringIsNull(entity.CustomerName, "领用者"); //DataHelper.CheckStringIsNull(entity.IdNo, "身份证号"); //DataHelper.CheckStringIsNull(entity.IdNo, "手机号"); DataHelper.CheckGuidIsDefaultValue(entity.CardTypeId, "卡类型"); DataHelper.CheckGuidIsDefaultValue(entity.MedicalCenterId, "体检中心"); DataHelper.CheckCharIsYOrN(entity.IsActive, "使用标志"); } } }