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.

235 lines
8.6 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. using Microsoft.Extensions.Configuration.UserSecrets;
  2. using Shentun.Utilities;
  3. using Shentun.Utilities.Enums;
  4. using Shentun.WebPeis.Models;
  5. using Shentun.WebPeis.SysParmValues;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Security.Cryptography;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using Volo.Abp;
  13. using Volo.Abp.Domain.Entities;
  14. using Volo.Abp.Domain.Repositories;
  15. using Volo.Abp.Domain.Services;
  16. using Volo.Abp.Identity;
  17. namespace Shentun.WebPeis.Persons
  18. {
  19. public class PersonManager : DomainService
  20. {
  21. private readonly IRepository<Person> _repository;
  22. private readonly IRepository<IdentityUser> _identityUserRepository;
  23. private readonly IdentityUserManager _identityUserManager;
  24. private readonly IRepository<Nation> _nationRepository;
  25. private readonly SysParmValueManager _sysParmValueManager;
  26. private readonly IRepository<PrimarykeyBuilder> _primarykeyBuilderRepository;
  27. public PersonManager(IRepository<Person> repository,
  28. IRepository<IdentityUser> identityUserRepository,
  29. IdentityUserManager identityUserManager,
  30. IRepository<Nation> nationRepository,
  31. SysParmValueManager sysParmValueManager,
  32. IRepository<PrimarykeyBuilder> primarykeyBuilderRepository)
  33. {
  34. _repository = repository;
  35. _identityUserRepository = identityUserRepository;
  36. _identityUserManager = identityUserManager;
  37. _nationRepository = nationRepository;
  38. _sysParmValueManager = sysParmValueManager;
  39. _primarykeyBuilderRepository = primarykeyBuilderRepository;
  40. }
  41. public async Task<UserWithPerson> CreateAsync(
  42. Person entity,
  43. string name,
  44. string? email,
  45. string phone
  46. )
  47. {
  48. await Verify(entity);
  49. Check.NotNullOrWhiteSpace(name, "姓名");
  50. Check.NotNullOrWhiteSpace(phone, "手机");
  51. if (!string.IsNullOrWhiteSpace(entity.IdNo))
  52. {
  53. var existPerson = await _repository.FirstOrDefaultAsync(o => o.IdNo == entity.IdNo);
  54. if (existPerson != null)
  55. {
  56. throw new UserFriendlyException("该身份证号已经在人员信息中注册");
  57. }
  58. entity.BirthDate = ConvertExtr.ToBirthDateByIdNo(entity.IdNo);
  59. }
  60. var users = (from identityUser in await _identityUserRepository.GetQueryableAsync()
  61. join person in await _repository.GetQueryableAsync()
  62. on identityUser.Id equals person.PersonId
  63. where identityUser.PhoneNumber == phone
  64. select
  65. identityUser
  66. ).ToList();
  67. if (users.Any())
  68. {
  69. throw new UserFriendlyException("该手机号已经在人员信息中注册");
  70. }
  71. var wechatPerson = await _repository.FindAsync(o => o.WechatOpenId == entity.WechatOpenId
  72. && !string.IsNullOrWhiteSpace(entity.WechatOpenId));
  73. if (wechatPerson != null)
  74. {
  75. throw new UserFriendlyException("该微信号已经注册");
  76. }
  77. entity.PersonNo = await CreatePersonNoAsync(entity.MedicalCenterId);
  78. if(string.IsNullOrWhiteSpace(email))
  79. {
  80. email = entity.PersonNo + "@qq.com";
  81. }
  82. var user = new IdentityUser(GuidGenerator.Create(), entity.PersonNo, email)
  83. {
  84. Name = name,
  85. };
  86. user.SetPhoneNumber(phone, false);
  87. // await _identityUserManager.CreateAsync(user);
  88. if (!string.IsNullOrWhiteSpace(entity.IdNo))
  89. {
  90. var existPatient = await _repository.FirstOrDefaultAsync(o => o.IdNo == entity.IdNo);
  91. if (existPatient != null)
  92. {
  93. throw new UserFriendlyException("该身份证号已经在人员信息中注册");
  94. }
  95. entity.BirthDate = ConvertExtr.ToBirthDateByIdNo(entity.IdNo);
  96. }
  97. entity.PersonId = user.Id;
  98. entity.SimpleCode = LanguageConverter.GetPYSimpleCode(name);
  99. var userWithPerson = new UserWithPerson()
  100. {
  101. User = user,
  102. Person = entity,
  103. };
  104. return userWithPerson;
  105. }
  106. private async Task Verify(Person entity)
  107. {
  108. Check.NotNull(entity, "entity");
  109. Check.NotNullOrWhiteSpace(entity.IdNo, "身份证号");
  110. Check.NotNullOrWhiteSpace(entity.NationId, "民族");
  111. CheckExter.CheckSex(entity.SexId);
  112. CheckExter.CheckMaritalStatus(entity.MaritalStatusId);
  113. if(string.IsNullOrWhiteSpace(entity.IdTypeId))
  114. {
  115. entity.IdTypeId = "01";
  116. }
  117. if (string.IsNullOrWhiteSpace(entity.CountryCode))
  118. {
  119. entity.CountryCode = "86";
  120. }
  121. if (entity.IsAllowBind == null)
  122. {
  123. entity.IsAllowBind = 'Y';
  124. }
  125. if (entity.MedicalCenterId == null || entity.MedicalCenterId == Guid.Empty)
  126. {
  127. throw new UserFriendlyException("体检中心不能为空");
  128. }
  129. if (!string.IsNullOrEmpty(entity.IdNo))
  130. {
  131. entity.IdNo = entity.IdNo.Trim();
  132. if (entity.IdNo.Length != 18)
  133. {
  134. throw new UserFriendlyException("身份证长度必须为18位");
  135. }
  136. var sexByIdNo = ConvertExtr.ToSexByIdNo(entity.IdNo).ToCharArray();
  137. if (entity.SexId == default(char))
  138. {
  139. entity.SexId = sexByIdNo[0];
  140. }
  141. else
  142. {
  143. if (sexByIdNo[0] != entity.SexId)
  144. {
  145. throw new UserFriendlyException("身份证号解析出的性别与填入的性别不一致");
  146. }
  147. }
  148. }
  149. if (!string.IsNullOrWhiteSpace(entity.NationId))
  150. {
  151. if ((await _nationRepository.GetQueryableAsync()).
  152. Where(o => o.NationId == entity.NationId).Count() == 0)
  153. {
  154. throw new UserFriendlyException("民族ID不存在");
  155. }
  156. }
  157. }
  158. private async Task<string> CreatePersonNoAsync(Guid medicalCenterId)
  159. {
  160. var person_id_rule_prefix = ""; //前缀
  161. var person_id_rule_tail_len = ""; //尾号长度
  162. person_id_rule_tail_len = await _sysParmValueManager.GetSysParmValueAsync(medicalCenterId, "person_id_rule_tail_len");
  163. person_id_rule_prefix = await _sysParmValueManager.GetSysParmValueAsync(medicalCenterId, "person_id_rule_prefix");
  164. if (string.IsNullOrWhiteSpace(person_id_rule_tail_len))
  165. {
  166. throw new UserFriendlyException("人员号尾号长度不能为空");
  167. }
  168. int tailLen;
  169. if (!int.TryParse(person_id_rule_tail_len, out tailLen))
  170. {
  171. throw new UserFriendlyException("人员号尾号长度必须是数字");
  172. };
  173. if (tailLen < 5)
  174. {
  175. throw new UserFriendlyException("人员号尾号长度必须大于5");
  176. }
  177. string maxnum = "1"; //未补位的档案号
  178. var primarykeyBuilderEnt = await _primarykeyBuilderRepository
  179. .FirstOrDefaultAsync(f => f.PrimarykeyBuilderId == "person_no");
  180. if (primarykeyBuilderEnt != null)
  181. {
  182. //获取最新的,加1,并更新数据
  183. maxnum = (Convert.ToInt32(primarykeyBuilderEnt.SerialNo) + 1).ToString();
  184. primarykeyBuilderEnt.SerialNo = maxnum;
  185. await _primarykeyBuilderRepository.UpdateAsync(primarykeyBuilderEnt);
  186. }
  187. else
  188. {
  189. //生成首位,并添加
  190. primarykeyBuilderEnt = new PrimarykeyBuilder
  191. {
  192. PrimarykeyBuilderId = "person_no",
  193. DateString = "",
  194. SerialNo = maxnum
  195. };
  196. await _primarykeyBuilderRepository.InsertAsync(primarykeyBuilderEnt, true);
  197. }
  198. return person_id_rule_prefix + maxnum.PadLeft(Convert.ToInt32(person_id_rule_tail_len), '0');
  199. }
  200. }
  201. }