You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

202 lines
7.8 KiB

2 years ago
2 years ago
6 months ago
6 months ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
4 months ago
2 years ago
2 years ago
  1. using Shentun.Peis.Models;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using Volo.Abp;
  8. using Volo.Abp.Domain.Repositories;
  9. using Volo.Abp.Domain.Services;
  10. using static log4net.Appender.RollingFileAppender;
  11. namespace Shentun.Peis.CardRegisters
  12. {
  13. public class CardRegisterManager : DomainService
  14. {
  15. private readonly IRepository<CardRegister, Guid> _repository;
  16. public CardRegisterManager(IRepository<CardRegister, Guid> repository)
  17. {
  18. _repository = repository;
  19. }
  20. /// <summary>
  21. /// 创建
  22. /// </summary>
  23. /// <param name="entity"></param>
  24. /// <returns></returns>
  25. public async Task<CardRegister> CreateAsync(
  26. CardRegister entity
  27. )
  28. {
  29. Verify(entity);
  30. var queryable = await _repository.GetQueryableAsync();
  31. var findedEntity = queryable.Where(x => x.CardNo == entity.CardNo && x.IsActive == 'Y').FirstOrDefault();
  32. if (findedEntity != null)
  33. {
  34. throw new UserFriendlyException($"卡号已经被{findedEntity.CustomerName}使用");
  35. }
  36. return new CardRegister(
  37. GuidGenerator.Create()
  38. )
  39. {
  40. CardBalance = 0,
  41. IsActive = entity.IsActive,
  42. CardNo = entity.CardNo,
  43. CardPassword = entity.CardPassword,
  44. CardTypeId = entity.CardTypeId,
  45. CustomerName = entity.CustomerName,
  46. Discount = entity.Discount,
  47. ExpiryDate = entity.ExpiryDate,
  48. IdNo = entity.IdNo,
  49. MobileTelephone = entity.MobileTelephone,
  50. MedicalCenterId = entity.MedicalCenterId,
  51. Remark = entity.Remark,
  52. Telephone = entity.Telephone
  53. };
  54. }
  55. /// <summary>
  56. /// 更新
  57. /// </summary>
  58. /// <param name="sourceEntity"></param>
  59. /// <param name="targetEntity"></param>
  60. /// <returns></returns>
  61. public void UpdateAsync(
  62. CardRegister sourceEntity,
  63. CardRegister targetEntity
  64. )
  65. {
  66. DataHelper.CheckEntityIsNull(sourceEntity);
  67. DataHelper.CheckEntityIsNull(targetEntity);
  68. Verify(sourceEntity);
  69. if (sourceEntity.CardNo != targetEntity.CardNo)
  70. {
  71. throw new ArgumentException("卡号不允许修改");
  72. }
  73. if (sourceEntity.CardBalance != 0 && sourceEntity.CardBalance != targetEntity.CardBalance)
  74. {
  75. throw new ArgumentException("卡余额不允许修改");
  76. }
  77. targetEntity.IsActive = sourceEntity.IsActive;
  78. targetEntity.CardPassword = sourceEntity.CardPassword;
  79. // targetEntity.CardTypeId = sourceEntity.CardTypeId;
  80. targetEntity.CustomerName = sourceEntity.CustomerName;
  81. targetEntity.Discount = sourceEntity.Discount;
  82. targetEntity.ExpiryDate = sourceEntity.ExpiryDate;
  83. targetEntity.IdNo = sourceEntity.IdNo;
  84. targetEntity.MobileTelephone = sourceEntity.MobileTelephone;
  85. // targetEntity.MedicalCenterId = sourceEntity.MedicalCenterId;
  86. targetEntity.Remark = sourceEntity.Remark;
  87. targetEntity.Telephone = sourceEntity.Telephone;
  88. }
  89. public async Task UpdateActive(CardRegister entity, char isActive)
  90. {
  91. DataHelper.CheckEntityIsNull(entity);
  92. DataHelper.CheckCharIsYOrN(isActive, "使用标志");
  93. if (isActive == 'N')
  94. {
  95. entity.IsActive = isActive;
  96. }
  97. else
  98. {
  99. //检查卡号是否用过
  100. var queryable = await _repository.GetQueryableAsync();
  101. var findedEntity = queryable.Where(x => x.Id != entity.Id && x.CardNo == entity.CardNo && x.IsActive == 'Y').FirstOrDefault();
  102. if (findedEntity != null)
  103. {
  104. throw new UserFriendlyException($"卡号已经被{findedEntity.CustomerName}使用");
  105. }
  106. }
  107. }
  108. /// <summary>
  109. /// 创建卡充值
  110. /// </summary>
  111. /// <param name="entity"></param>
  112. /// <param name="payModeId"></param>
  113. /// <param name="billFlag"></param>
  114. /// <param name="amount"></param>
  115. /// <returns></returns>
  116. /// <exception cref="ArgumentException"></exception>
  117. public CardBill CreateCardBill(CardRegister entity, string payModeId, char billFlag, decimal amount)
  118. {
  119. DataHelper.CheckEntityIsNull(entity);
  120. if (amount == 0)
  121. {
  122. throw new ArgumentException("金额等于0");
  123. };
  124. if (billFlag == Shentun.Peis.Enums.CardBillFlag.Charge && amount < 0)
  125. {
  126. throw new ArgumentException("充值金额不能小于0");
  127. }
  128. else if (billFlag != Shentun.Peis.Enums.CardBillFlag.Charge && amount > 0)
  129. {
  130. throw new ArgumentException("扣费或退费金额不能大于0");
  131. }
  132. if ((entity.CardBalance + amount) < 0)
  133. {
  134. throw new ArgumentException($"扣费或退费金额不能小于余额{entity.CardBalance}");
  135. }
  136. entity.CardBalance += amount;
  137. var cardBill = new CardBill(GuidGenerator.Create())
  138. {
  139. CardRegisterId = entity.Id,
  140. PayModeId = payModeId,
  141. BillFlag = billFlag,
  142. BillMoney = amount,
  143. };
  144. return cardBill;
  145. }
  146. /// <summary>
  147. /// 增加卡充值,暂时不使用,用来测试聚合根模式
  148. /// </summary>
  149. /// <param name="entity"></param>
  150. /// <param name="payModeId"></param>
  151. /// <param name="billFlag"></param>
  152. /// <param name="amount"></param>
  153. /// <exception cref="ArgumentException"></exception>
  154. public void AddCardBill(CardRegister entity, string payModeId, char billFlag, decimal amount)
  155. {
  156. DataHelper.CheckEntityIsNull(entity);
  157. if (amount == 0)
  158. {
  159. throw new ArgumentException("金额等于0");
  160. };
  161. if (billFlag == Shentun.Peis.Enums.CardBillFlag.Charge && amount < 0)
  162. {
  163. throw new ArgumentException("充值金额不能小于0");
  164. }
  165. else if (billFlag != Shentun.Peis.Enums.CardBillFlag.Charge && amount > 0)
  166. {
  167. throw new ArgumentException("扣费或退费金额不能大于0");
  168. }
  169. if ((entity.CardBalance + amount) < 0)
  170. {
  171. throw new ArgumentException($"扣费或退费金额不能小于余额{entity.CardBalance}");
  172. }
  173. entity.CardBalance += amount;
  174. var cardBill = new CardBill(GuidGenerator.Create())
  175. {
  176. CardRegisterId = entity.Id,
  177. PayModeId = payModeId,
  178. BillFlag = billFlag,
  179. BillMoney = amount,
  180. };
  181. entity.CardBills.Add(cardBill);
  182. }
  183. private void Verify(CardRegister entity)
  184. {
  185. DataHelper.CheckEntityIsNull(entity);
  186. DataHelper.CheckStringIsNull(entity.CardNo, "卡号");
  187. DataHelper.CheckStringIsNull(entity.CustomerName, "领用者");
  188. //DataHelper.CheckStringIsNull(entity.IdNo, "身份证号");
  189. //DataHelper.CheckStringIsNull(entity.IdNo, "手机号");
  190. DataHelper.CheckGuidIsDefaultValue(entity.CardTypeId, "卡类型");
  191. DataHelper.CheckGuidIsDefaultValue(entity.MedicalCenterId, "体检中心");
  192. DataHelper.CheckCharIsYOrN(entity.IsActive, "使用标志");
  193. }
  194. }
  195. }