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.

191 lines
7.3 KiB

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