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.

1430 lines
61 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
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
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
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
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
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
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
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. using Microsoft.EntityFrameworkCore;
  2. using NPOI.POIFS.Crypt.Dsig;
  3. using Shentun.Peis.CustomerOrgRegisters;
  4. using Shentun.Peis.CustomerOrgs;
  5. using Shentun.Peis.Enums;
  6. using Shentun.Peis.HelperDto;
  7. using Shentun.Peis.Models;
  8. using Shentun.Peis.OrganizationUnits;
  9. using Shentun.Peis.Patients;
  10. using Shentun.Peis.RegisterAsbitems;
  11. using Shentun.Peis.RegisterCheckCriticalValues;
  12. using Shentun.Peis.RegisterCheckItems;
  13. using Shentun.Peis.RegisterCheckPictures;
  14. using Shentun.Peis.RegisterChecks;
  15. using Shentun.Peis.RegisterCheckSuggestions;
  16. using Shentun.Peis.RegisterCheckSummarys;
  17. using Shentun.Peis.SysParmValues;
  18. using Shentun.Utilities;
  19. using System;
  20. using System.Collections.Generic;
  21. using System.ComponentModel.DataAnnotations;
  22. using System.Linq;
  23. using System.Text;
  24. using System.Threading.Tasks;
  25. using Volo.Abp;
  26. using Volo.Abp.Domain.Repositories;
  27. using Volo.Abp.Domain.Services;
  28. using Volo.Abp.Guids;
  29. using Volo.Abp.Users;
  30. namespace Shentun.Peis.PatientRegisters
  31. {
  32. /// <summary>
  33. /// 人员体检登记
  34. /// </summary>
  35. public class PatientRegisterManager : DomainService
  36. {
  37. private readonly IRepository<PatientRegister, Guid> _repository;
  38. private readonly IRepository<CustomerOrg, Guid> _customerOrgRepository;
  39. private readonly IRepository<CustomerOrgRegister, Guid> _customerOrgRegisterRepository;
  40. private readonly IRepository<CustomerOrgGroup, Guid> _customerOrgGroupRepository;
  41. private readonly IRepository<MedicalType, Guid> _medicalTypeRepository;
  42. private readonly IRepository<PersonnelType, Guid> _personnelTypeRepository;
  43. private readonly IRepository<MedicalPackage, Guid> _medicalPackageRepository;
  44. private readonly IRepository<MedicalConclusion, Guid> _medicalConclusionRepository;
  45. private readonly IRepository<SexHormoneTerm, Guid> _sexHormoneTermRepository;
  46. private readonly IRepository<PrimarykeyBuilder> _primarykeyBuilderRepository;
  47. private readonly IRepository<Patient, Guid> _patientRepository;
  48. private readonly IRepository<RegisterCheckAsbitem, Guid> _registerAsbitemRepository;
  49. private readonly IRepository<RegisterCheck, Guid> _registerCheckRepository;
  50. private readonly IRepository<RegisterCheckCriticalValue, Guid> _registerCheckCriticalValueRepository;
  51. private readonly IRepository<RegisterCheckItem> _registerCheckItemRepository;
  52. private readonly CurrentUser _currentUser;
  53. private readonly PeisOrganizationUnitManager _peisOrganizationUnitManager;
  54. private readonly PatientManager _patientManager;
  55. private readonly RegisterCheckAsbitemManager _registerAsbitemManager;
  56. private readonly CustomerOrgManager _customerOrgManager;
  57. private readonly CustomerOrgRegisterManager _customerOrgRegisterManager;
  58. private readonly RegisterCheckSuggestionManager _registerCheckSuggestionManager;
  59. private readonly RegisterCheckSummaryManager _registerCheckSummaryManager;
  60. private readonly RegisterCheckPictureManager _registerCheckPictureManager;
  61. private readonly RegisterCheckItemManager _registerCheckItemManager;
  62. private readonly RegisterCheckCriticalValueManager _registerCheckCriticalValueManager;
  63. private readonly RegisterCheckManager _registerCheckManager;
  64. private readonly IRepository<SysParmValue> _sysParmValueRepository;
  65. private readonly IGuidGenerator _guidGenerator;
  66. private readonly SysParmValueManager _sysParmValueManager;
  67. public PatientRegisterManager(
  68. IRepository<PatientRegister, Guid> repository,
  69. IRepository<CustomerOrgRegister, Guid> customerOrgRegisterRepository,
  70. IRepository<CustomerOrgGroup, Guid> customerOrgGroupRepository,
  71. IRepository<MedicalType, Guid> medicalTypeRepository,
  72. IRepository<PersonnelType, Guid> personnelTypeRepository,
  73. IRepository<MedicalPackage, Guid> medicalPackageRepository,
  74. IRepository<MedicalConclusion, Guid> medicalConclusionRepository,
  75. IRepository<SexHormoneTerm, Guid> sexHormoneTermRepository,
  76. IRepository<SysParmValue> sysParmValueRepository,
  77. IRepository<CustomerOrg, Guid> customerOrgRepository,
  78. IRepository<PrimarykeyBuilder> primarykeyBuilderRepository,
  79. IRepository<Patient, Guid> patientRepository,
  80. IRepository<RegisterCheckAsbitem, Guid> registerAsbitemRepository,
  81. IRepository<RegisterCheck, Guid> registerCheckRepository,
  82. IRepository<RegisterCheckCriticalValue, Guid> registerCheckCriticalValueRepository,
  83. IRepository<RegisterCheckItem> registerCheckItemRepository,
  84. CurrentUser currentUser,
  85. PeisOrganizationUnitManager peisOrganizationUnitManager,
  86. PatientManager patientManager,
  87. RegisterCheckAsbitemManager registerAsbitemManager,
  88. CustomerOrgManager customerOrgManager,
  89. CustomerOrgRegisterManager customerOrgRegisterManager,
  90. RegisterCheckSuggestionManager registerCheckSuggestionManager,
  91. RegisterCheckSummaryManager registerCheckSummaryManager,
  92. RegisterCheckPictureManager registerCheckPictureManager,
  93. RegisterCheckItemManager registerCheckItemManager,
  94. RegisterCheckCriticalValueManager registerCheckCriticalValueManager,
  95. RegisterCheckManager registerCheckManager,
  96. IGuidGenerator guidGenerator,
  97. SysParmValueManager sysParmValueManager)
  98. {
  99. _repository = repository;
  100. this._customerOrgRepository = customerOrgRepository;
  101. _customerOrgRegisterRepository = customerOrgRegisterRepository;
  102. _customerOrgGroupRepository = customerOrgGroupRepository;
  103. _medicalTypeRepository = medicalTypeRepository;
  104. _personnelTypeRepository = personnelTypeRepository;
  105. _medicalPackageRepository = medicalPackageRepository;
  106. _medicalConclusionRepository = medicalConclusionRepository;
  107. _sexHormoneTermRepository = sexHormoneTermRepository;
  108. this._primarykeyBuilderRepository = primarykeyBuilderRepository;
  109. this._patientRepository = patientRepository;
  110. this._registerAsbitemRepository = registerAsbitemRepository;
  111. this._registerCheckRepository = registerCheckRepository;
  112. this._registerCheckCriticalValueRepository = registerCheckCriticalValueRepository;
  113. this._registerCheckItemRepository = registerCheckItemRepository;
  114. this._currentUser = currentUser;
  115. _peisOrganizationUnitManager = peisOrganizationUnitManager;
  116. this._patientManager = patientManager;
  117. this._registerAsbitemManager = registerAsbitemManager;
  118. this._customerOrgManager = customerOrgManager;
  119. this._customerOrgRegisterManager = customerOrgRegisterManager;
  120. this._registerCheckSuggestionManager = registerCheckSuggestionManager;
  121. this._registerCheckSummaryManager = registerCheckSummaryManager;
  122. this._registerCheckPictureManager = registerCheckPictureManager;
  123. this._registerCheckItemManager = registerCheckItemManager;
  124. this._registerCheckCriticalValueManager = registerCheckCriticalValueManager;
  125. this._registerCheckManager = registerCheckManager;
  126. this._sysParmValueRepository = sysParmValueRepository;
  127. _guidGenerator = guidGenerator;
  128. _sysParmValueManager = sysParmValueManager;
  129. }
  130. /// <summary>
  131. /// 创建
  132. /// </summary>
  133. /// <param name="entity"></param>
  134. /// <param name="patientEntity"></param>
  135. /// <param name="IsMaxMedicalTimes">是否使用最后一次体检次数</param>
  136. /// <returns></returns>
  137. public async Task<PatientRegister> CreateAsync(
  138. PatientRegister entity,
  139. Patient patientEntity,
  140. char IsMaxMedicalTimes
  141. )
  142. {
  143. if (entity.PatientId == Guid.Empty)
  144. {
  145. //未选档号号,创建档案号
  146. var ent = new Patient
  147. {
  148. Address = patientEntity.Address,
  149. BirthDate = entity.BirthDate,
  150. BirthPlaceId = patientEntity.BirthPlaceId,
  151. DisplayName = entity.PatientName,
  152. Email = patientEntity.Email,
  153. IdNo = patientEntity.IdNo,
  154. MaritalStatusId = entity.MaritalStatusId,
  155. MobileTelephone = patientEntity.MobileTelephone,
  156. NationId = patientEntity.NationId,
  157. PatientPassword = "",
  158. PostalCode = patientEntity.PostalCode,
  159. SexId = entity.SexId,
  160. Telephone = patientEntity.Telephone
  161. };
  162. var patientEnt = await _patientManager.CreateAsync(entity.MedicalCenterId, ent);
  163. if (patientEnt != null)
  164. {
  165. entity.PatientId = patientEnt.Id;
  166. }
  167. }
  168. else
  169. {
  170. //查档案信息
  171. var oldent = await _patientRepository.GetAsync(entity.PatientId);
  172. if (oldent != null)
  173. {
  174. oldent.Address = patientEntity.Address;
  175. oldent.BirthDate = entity.BirthDate;
  176. oldent.BirthPlaceId = patientEntity.BirthPlaceId;
  177. oldent.DisplayName = entity.PatientName;
  178. oldent.Email = patientEntity.Email;
  179. oldent.IdNo = patientEntity.IdNo;
  180. oldent.MaritalStatusId = entity.MaritalStatusId;
  181. oldent.MobileTelephone = patientEntity.MobileTelephone;
  182. oldent.NationId = patientEntity.NationId;
  183. //oldent.PatientPassword = "",
  184. oldent.PostalCode = patientEntity.PostalCode;
  185. oldent.SexId = entity.SexId;
  186. oldent.Telephone = patientEntity.Telephone;
  187. }
  188. await _patientRepository.UpdateAsync(oldent); //修改档案
  189. }
  190. //暂时如此使用,这段代码以后要废除
  191. Guid CustomerOrgRegisterId = new Guid(); //最新单位次数ID
  192. if (IsMaxMedicalTimes == 'Y')
  193. {
  194. Guid CustomerOrgParentId = EntityHelper.GetParentNoSql(await _customerOrgRepository.GetListAsync(), entity.CustomerOrgId); //一级单位ID
  195. if (CustomerOrgParentId != Guid.Empty)
  196. {
  197. var customerOrgRegisterEnt = await _customerOrgRegisterManager.GetMaxAsync(CustomerOrgParentId);
  198. if (customerOrgRegisterEnt != null)
  199. CustomerOrgRegisterId = customerOrgRegisterEnt.Id;
  200. }
  201. }
  202. else
  203. {
  204. CustomerOrgRegisterId = entity.CustomerOrgRegisterId;
  205. }
  206. var patientRegisterEnt = new PatientRegister(_guidGenerator.Create())
  207. {
  208. Age = entity.Age,
  209. AuditDate = entity.AuditDate,
  210. AuditDoctorId = entity.AuditDoctorId,
  211. BirthDate = entity.BirthDate,
  212. CompleteFlag = entity.CompleteFlag,
  213. CustomerOrgGroupId = entity.CustomerOrgGroupId,
  214. CustomerOrgId = entity.CustomerOrgId,
  215. CustomerOrgRegisterId = CustomerOrgRegisterId,
  216. GuidePrintTimes = entity.GuidePrintTimes,
  217. InterposeMeasure = entity.InterposeMeasure,
  218. IsAudit = entity.IsAudit,
  219. IsLock = entity.IsLock,
  220. IsMedicalStart = entity.IsMedicalStart,
  221. IsNameHide = entity.IsNameHide,
  222. IsPhoneFollow = entity.IsPhoneFollow,
  223. IsRecoverGuide = entity.IsRecoverGuide,
  224. IsUpload = entity.IsUpload,
  225. IsVip = entity.IsVip,
  226. JobCardNo = entity.JobCardNo,
  227. JobPost = entity.JobPost,
  228. JobTitle = entity.JobTitle,
  229. MaritalStatusId = entity.MaritalStatusId,
  230. MedicalCardNo = entity.MedicalCardNo,
  231. MedicalConclusionId = entity.MedicalConclusionId,
  232. MedicalPackageId = entity.MedicalPackageId,
  233. MedicalStartDate = entity.MedicalStartDate,
  234. MedicalTimes = entity.PatientId == Guid.Empty ? (short)1 : await GetPatientCount(entity.PatientId),
  235. MedicalTypeId = entity.MedicalTypeId,
  236. MedicalCenterId = entity.MedicalCenterId,
  237. PatientId = entity.PatientId,
  238. PatientName = entity.PatientName,
  239. PatientRegisterNo = await CreatePatientRegisterNo(entity.MedicalCenterId),
  240. PersonnelTypeId = entity.PersonnelTypeId,
  241. Photo = entity.Photo,
  242. Remark = entity.Remark,
  243. ReportPrintTimes = entity.ReportPrintTimes,
  244. Salesman = entity.Salesman,
  245. SexHormoneTermId = entity.SexHormoneTermId,
  246. SexId = entity.SexId,
  247. SummaryDate = entity.SummaryDate,
  248. SummaryDoctorId = entity.SummaryDoctorId,
  249. ThirdInfo = entity.ThirdInfo
  250. };
  251. //return patientRegisterEnt;
  252. return await _repository.InsertAsync(patientRegisterEnt, true);
  253. }
  254. public async Task<PatientRegister> CreateAsync(CreatePatientRegisterArg entity)
  255. {
  256. await VerifyCreate(entity);
  257. if (entity.IsVip == default(char))
  258. {
  259. entity.IsVip = 'N';
  260. }
  261. DataHelper.CheckCharIsYOrN(entity.IsVip, "VIP标志");
  262. DataHelper.CheckCharIsYOrN(entity.IsNameHide, "姓名隐藏");
  263. if (entity.CompleteFlag == default(char))
  264. {
  265. entity.CompleteFlag = PatientRegisterCompleteFlag.PreRegistration;
  266. }
  267. if (entity.CompleteFlag != PatientRegisterCompleteFlag.PreRegistration &&
  268. entity.CompleteFlag != PatientRegisterCompleteFlag.Registration)
  269. {
  270. throw new ArgumentException("完成标志错误");
  271. }
  272. var patientRegisterNo = await CreatePatientRegisterNo(entity.MedicalCenterId);
  273. if (entity.MedicalTimes == 0)
  274. {
  275. entity.MedicalTimes = await GetPatientCount(entity.PatientId);
  276. }
  277. var patientRegisterEnt = new PatientRegister(_guidGenerator.Create())
  278. {
  279. Age = entity.Age,
  280. AuditDate = null,
  281. AuditDoctorId = null,
  282. BirthDate = entity.BirthDate,
  283. CompleteFlag = entity.CompleteFlag,
  284. CustomerOrgGroupId = entity.CustomerOrgGroupId,
  285. CustomerOrgId = entity.CustomerOrgId,
  286. CustomerOrgRegisterId = entity.CustomerOrgRegisterId,
  287. GuidePrintTimes = 0,
  288. InterposeMeasure = null,
  289. IsAudit = 'N',
  290. IsLock = entity.IsLock,
  291. IsMedicalStart = 'N',
  292. IsNameHide = entity.IsNameHide,
  293. IsPhoneFollow = 'N',
  294. IsRecoverGuide = 'N',
  295. IsUpload = 'N',
  296. IsVip = entity.IsVip,
  297. JobCardNo = entity.JobCardNo,
  298. JobPost = entity.JobPost,
  299. JobTitle = entity.JobTitle,
  300. MaritalStatusId = entity.MaritalStatusId,
  301. MedicalCardNo = entity.MedicalCardNo,
  302. MedicalConclusionId = null,
  303. MedicalPackageId = entity.MedicalPackageId,
  304. MedicalStartDate = DateTime.Now,
  305. MedicalTimes = entity.MedicalTimes,
  306. MedicalTypeId = entity.MedicalTypeId,
  307. MedicalCenterId = entity.MedicalCenterId,
  308. PatientId = entity.PatientId,
  309. PatientName = entity.PatientName,
  310. PatientRegisterNo = patientRegisterNo,
  311. PersonnelTypeId = entity.PersonnelTypeId,
  312. Remark = entity.Remark,
  313. ReportPrintTimes = 0,
  314. Salesman = entity.Salesman,
  315. SexHormoneTermId = entity.SexHormoneTermId,
  316. SexId = entity.SexId,
  317. SummaryDate = null,
  318. SummaryDoctorId = null,
  319. ThirdInfo = entity.ThirdInfo
  320. };
  321. if (patientRegisterEnt.CustomerOrgGroupId != Guid.Empty && patientRegisterEnt.CustomerOrgGroupId != null)
  322. {
  323. await SetCustomerOrgGroupId(patientRegisterEnt, (Guid)patientRegisterEnt.CustomerOrgGroupId);
  324. }
  325. return patientRegisterEnt;
  326. }
  327. private async Task VerifyCreate(CreatePatientRegisterArg entity)
  328. {
  329. DataHelper.CheckEntityIsNull(entity);
  330. DataHelper.CheckGuidIsDefaultValue(entity.PatientId, "病人ID");
  331. DataHelper.CheckStringIsNull(entity.PatientName, "姓名");
  332. DataHelper.CheckGuidIsDefaultValue(entity.MedicalCenterId, "单位ID");
  333. DataHelper.CheckGuidIsDefaultValue(entity.CustomerOrgId, "客户单位ID");
  334. DataHelper.CheckSex(entity.SexId);
  335. DataHelper.CheckMaritalStatus(entity.MaritalStatusId);
  336. if (entity.MedicalTimes < 0)
  337. {
  338. throw new UserFriendlyException("体检次数必须大于等于0");
  339. }
  340. if (entity.Age <= 0)
  341. {
  342. throw new UserFriendlyException("年龄不能小于等于0");
  343. }
  344. if (entity.CustomerOrgGroupId != Guid.Empty && entity.CustomerOrgGroupId != null)
  345. {
  346. if ((await _customerOrgGroupRepository.GetQueryableAsync()).
  347. Where(o => o.CustomerOrgRegisterId == entity.CustomerOrgRegisterId &&
  348. o.Id == entity.CustomerOrgGroupId).Count() == 0)
  349. {
  350. throw new UserFriendlyException("单位分组ID不存在");
  351. }
  352. }
  353. if (entity.MedicalPackageId != Guid.Empty && entity.MedicalPackageId != null)
  354. {
  355. if (await _medicalPackageRepository.GetQueryableAsync().Result.
  356. Where(o => o.Id == entity.MedicalPackageId).CountAsync() == 0)
  357. {
  358. throw new UserFriendlyException("套餐ID不存在");
  359. }
  360. }
  361. if (entity.MedicalTypeId != Guid.Empty && entity.MedicalTypeId != null)
  362. {
  363. if (await _medicalTypeRepository.GetQueryableAsync().Result.
  364. Where(o => o.Id == entity.MedicalTypeId).CountAsync() == 0)
  365. {
  366. throw new UserFriendlyException("体检类别ID不存在");
  367. }
  368. }
  369. if (entity.PersonnelTypeId != Guid.Empty && entity.PersonnelTypeId != null)
  370. {
  371. if (await _personnelTypeRepository.GetQueryableAsync().Result.
  372. Where(o => o.Id == entity.PersonnelTypeId).CountAsync() == 0)
  373. {
  374. throw new UserFriendlyException("人员类别ID不存在");
  375. }
  376. }
  377. if (entity.SexHormoneTermId != Guid.Empty && entity.SexHormoneTermId != null)
  378. {
  379. if (await _sexHormoneTermRepository.GetQueryableAsync().Result.
  380. Where(o => o.Id == entity.SexHormoneTermId).CountAsync() == 0)
  381. {
  382. throw new UserFriendlyException("性激素期限ID不存在");
  383. }
  384. }
  385. if (!await _peisOrganizationUnitManager.IsPeis(entity.MedicalCenterId))
  386. {
  387. throw new UserFriendlyException("组织单位ID不是体检中心");
  388. }
  389. var customerOrg = await _customerOrgRepository.FindAsync(o => o.Id == entity.CustomerOrgId);
  390. if (customerOrg == null)
  391. {
  392. throw new UserFriendlyException("客户单位ID不存在");
  393. }
  394. if (customerOrg.Id == GuidFlag.PersonCustomerOrgId)
  395. {
  396. if (entity.CustomerOrgGroupId != Guid.Empty && entity.CustomerOrgGroupId != null)
  397. {
  398. throw new UserFriendlyException("个人体检不能选单位分组");
  399. }
  400. }
  401. else
  402. {
  403. if (entity.MedicalPackageId != Guid.Empty && entity.MedicalPackageId != null)
  404. {
  405. throw new UserFriendlyException("单位体检不能选套餐");
  406. }
  407. }
  408. var customerOrgRegister = await _customerOrgRegisterRepository.FindAsync(o => o.Id == entity.CustomerOrgRegisterId);
  409. if (customerOrgRegister == null)
  410. {
  411. throw new UserFriendlyException("客户登记单位次数不存在");
  412. }
  413. if (customerOrgRegister.CustomerOrgId != customerOrg.Id)
  414. {
  415. var topCustomerOrg = await _customerOrgManager.GetTopCustomerOrgAsync(customerOrg.Id);
  416. var topCustomerOrgRegisterOrg = await _customerOrgManager.GetTopCustomerOrgAsync(customerOrgRegister.CustomerOrgId);
  417. if (topCustomerOrg.Id != topCustomerOrgRegisterOrg.Id)
  418. {
  419. throw new UserFriendlyException("登记单位和登记单位体检次数不一致");
  420. }
  421. }
  422. if ((entity.Age == 0 || entity.Age == null))
  423. {
  424. if (entity.BirthDate != null && entity.BirthDate > new DateTime(1900, 1, 1))
  425. {
  426. entity.Age = (short)ConvertExtr.ToAge(((DateTime)entity.BirthDate));
  427. };
  428. }
  429. if (entity.BirthDate == null || entity.BirthDate <= new DateTime(1900, 1, 1))
  430. {
  431. if (entity.Age != null && entity.Age > 0)
  432. {
  433. entity.BirthDate = ConvertExtr.ToBirthDateByAge((short)entity.Age);
  434. }
  435. }
  436. }
  437. /// <summary>
  438. /// 更新
  439. /// </summary>
  440. /// <param name="sourceEntity">Dto实体映射数据</param>
  441. /// <param name="targetEntity">原始数据</param>
  442. /// <returns></returns>
  443. public async Task UpdateAsync(
  444. UpdatePatientRegisterArg sourceEntity,
  445. PatientRegister targetEntity
  446. )
  447. {
  448. DataHelper.CheckStringIsNull(sourceEntity.PatientName, "姓名");
  449. DataHelper.CheckGuidIsDefaultValue(sourceEntity.MedicalCenterId, "单位ID");
  450. DataHelper.CheckGuidIsDefaultValue(sourceEntity.CustomerOrgId, "客户单位ID");
  451. DataHelper.CheckSex(sourceEntity.SexId);
  452. DataHelper.CheckMaritalStatus(sourceEntity.MaritalStatusId);
  453. DataHelper.CheckCharIsYOrN(sourceEntity.IsVip, "VIP标志");
  454. DataHelper.CheckCharIsYOrN(sourceEntity.IsNameHide, "姓名隐藏");
  455. if (sourceEntity.CompleteFlag != PatientRegisterCompleteFlag.PreRegistration &&
  456. sourceEntity.CompleteFlag != PatientRegisterCompleteFlag.Registration &&
  457. sourceEntity.CompleteFlag != PatientRegisterCompleteFlag.PartCheck)
  458. {
  459. throw new ArgumentException("完成标志错误");
  460. }
  461. //if (sourceEntity.MedicalTimes < 1)
  462. //{
  463. // throw new UserFriendlyException("体检次数必须大于0");
  464. //}
  465. //if (sourceEntity.MedicalTimes != targetEntity.MedicalTimes)
  466. //{
  467. // var cnt = (await _repository.GetQueryableAsync()).Where(o => o.Id != targetEntity.Id &&
  468. // o.PatientId == sourceEntity.PatientId &&
  469. // o.MedicalTimes == sourceEntity.MedicalTimes).Count();
  470. // if (cnt > 0)
  471. // {
  472. // throw new UserFriendlyException("体检次数已经使用过");
  473. // }
  474. //}
  475. if (sourceEntity.Age <= 0)
  476. {
  477. throw new UserFriendlyException("年龄不能小于等于0");
  478. }
  479. if (sourceEntity.CustomerOrgGroupId != targetEntity.CustomerOrgGroupId &&
  480. sourceEntity.CustomerOrgGroupId != Guid.Empty && sourceEntity.CustomerOrgGroupId != null)
  481. {
  482. if ((await _customerOrgGroupRepository.GetQueryableAsync()).
  483. Where(o => o.CustomerOrgRegisterId == sourceEntity.CustomerOrgRegisterId &&
  484. o.Id == sourceEntity.CustomerOrgGroupId).Count() == 0)
  485. {
  486. throw new UserFriendlyException("单位分组ID不存在");
  487. }
  488. }
  489. if (sourceEntity.MedicalPackageId != targetEntity.MedicalPackageId && sourceEntity.MedicalPackageId != Guid.Empty && sourceEntity.MedicalPackageId != null)
  490. {
  491. if (await _medicalPackageRepository.GetQueryableAsync().Result.
  492. Where(o => o.Id == sourceEntity.MedicalPackageId).CountAsync() == 0)
  493. {
  494. throw new UserFriendlyException("套餐ID不存在");
  495. }
  496. }
  497. if (sourceEntity.MedicalTypeId != targetEntity.MedicalTypeId && sourceEntity.MedicalTypeId != Guid.Empty && sourceEntity.MedicalTypeId != null)
  498. {
  499. if (await _medicalTypeRepository.GetQueryableAsync().Result.
  500. Where(o => o.Id == sourceEntity.MedicalTypeId).CountAsync() == 0)
  501. {
  502. throw new UserFriendlyException("体检类别ID不存在");
  503. }
  504. }
  505. if (sourceEntity.PersonnelTypeId != targetEntity.PersonnelTypeId && sourceEntity.PersonnelTypeId != Guid.Empty && sourceEntity.PersonnelTypeId != null)
  506. {
  507. if (await _personnelTypeRepository.GetQueryableAsync().Result.
  508. Where(o => o.Id == sourceEntity.PersonnelTypeId).CountAsync() == 0)
  509. {
  510. throw new UserFriendlyException("人员类别ID不存在");
  511. }
  512. }
  513. //if (sourceEntity.MedicalConclusionId != targetEntity.MedicalConclusionId && sourceEntity.MedicalConclusionId != Guid.Empty && sourceEntity.MedicalConclusionId != null)
  514. //{
  515. // if (await _medicalConclusionRepository.GetQueryableAsync().Result.
  516. // Where(o => o.Id == sourceEntity.MedicalConclusionId).CountAsync() == 0)
  517. // {
  518. // throw new UserFriendlyException("体检结论ID不存在");
  519. // }
  520. //}
  521. if (sourceEntity.SexHormoneTermId != targetEntity.SexHormoneTermId && sourceEntity.SexHormoneTermId != Guid.Empty && sourceEntity.SexHormoneTermId != null)
  522. {
  523. if (await _sexHormoneTermRepository.GetQueryableAsync().Result.
  524. Where(o => o.Id == sourceEntity.SexHormoneTermId).CountAsync() == 0)
  525. {
  526. throw new UserFriendlyException("性激素期限ID不存在");
  527. }
  528. }
  529. if (sourceEntity.MedicalCenterId != targetEntity.MedicalCenterId && !await _peisOrganizationUnitManager.IsPeis(sourceEntity.MedicalCenterId))
  530. {
  531. throw new UserFriendlyException("组织单位ID不是体检中心");
  532. }
  533. CustomerOrg customerOrg = null;
  534. if (sourceEntity.CustomerOrgId != targetEntity.CustomerOrgId)
  535. {
  536. customerOrg = await _customerOrgRepository.FindAsync(o => o.Id == sourceEntity.CustomerOrgId);
  537. if (customerOrg == null)
  538. {
  539. throw new UserFriendlyException("客户单位ID不存在");
  540. }
  541. }
  542. CustomerOrgRegister customerOrgRegister = null;
  543. if (sourceEntity.CustomerOrgRegisterId != targetEntity.CustomerOrgRegisterId)
  544. {
  545. customerOrgRegister = await _customerOrgRegisterRepository.FindAsync(o => o.Id == sourceEntity.CustomerOrgRegisterId);
  546. if (customerOrgRegister == null)
  547. {
  548. throw new UserFriendlyException("客户登记单位次数不存在");
  549. }
  550. }
  551. if (sourceEntity.CustomerOrgId != targetEntity.CustomerOrgId || sourceEntity.CustomerOrgRegisterId != targetEntity.CustomerOrgRegisterId)
  552. {
  553. if (customerOrg == null)
  554. {
  555. customerOrg = await _customerOrgRepository.FindAsync(o => o.Id == sourceEntity.CustomerOrgId);
  556. }
  557. if (customerOrg == null)
  558. {
  559. throw new UserFriendlyException("客户单位ID不存在");
  560. }
  561. if (customerOrgRegister == null)
  562. {
  563. customerOrgRegister = await _customerOrgRegisterRepository.FindAsync(o => o.Id == sourceEntity.CustomerOrgRegisterId);
  564. }
  565. if (customerOrgRegister == null)
  566. {
  567. throw new UserFriendlyException("客户登记单位次数不存在");
  568. }
  569. if (customerOrgRegister.CustomerOrgId != customerOrg.Id)
  570. {
  571. var topCustomerOrg = await _customerOrgManager.GetTopCustomerOrgAsync(customerOrg.Id);
  572. var topCustomerOrgRegisterOrg = await _customerOrgManager.GetTopCustomerOrgAsync(customerOrgRegister.CustomerOrgId);
  573. if (topCustomerOrg.Id != topCustomerOrgRegisterOrg.Id)
  574. {
  575. throw new UserFriendlyException("登记单位和登记单位体检次数不一致");
  576. }
  577. }
  578. }
  579. if (sourceEntity.CustomerOrgId == GuidFlag.PersonCustomerOrgId)
  580. {
  581. if (sourceEntity.CustomerOrgGroupId != Guid.Empty && sourceEntity.CustomerOrgGroupId != null)
  582. {
  583. throw new UserFriendlyException("个人体检不能选单位分组");
  584. }
  585. }
  586. else
  587. {
  588. if (sourceEntity.MedicalPackageId != Guid.Empty && sourceEntity.MedicalPackageId != null)
  589. {
  590. throw new UserFriendlyException("单位体检不能选套餐");
  591. }
  592. }
  593. if ((sourceEntity.Age == 0 || sourceEntity.Age == null))
  594. {
  595. if (sourceEntity.BirthDate != null && sourceEntity.BirthDate > new DateTime(1900, 1, 1))
  596. {
  597. sourceEntity.Age = (short)ConvertExtr.ToAge(((DateTime)sourceEntity.BirthDate));
  598. };
  599. }
  600. if (sourceEntity.BirthDate == null || sourceEntity.BirthDate <= new DateTime(1900, 1, 1))
  601. {
  602. if (sourceEntity.Age != null && sourceEntity.Age > 0)
  603. {
  604. sourceEntity.BirthDate = ConvertExtr.ToBirthDateByAge((short)sourceEntity.Age);
  605. }
  606. }
  607. if (targetEntity.PatientName != sourceEntity.PatientName && targetEntity.SexId != sourceEntity.SexId)
  608. {
  609. throw new UserFriendlyException("人员信息姓名和性别和原来的信息都不一致,禁止修改");
  610. }
  611. //预登记改正式登记 修改体检日期跟状态
  612. if (targetEntity.CompleteFlag == PatientRegisterCompleteFlag.PreRegistration
  613. && sourceEntity.CompleteFlag == PatientRegisterCompleteFlag.Registration)
  614. {
  615. targetEntity.MedicalStartDate = DateTime.Now;
  616. targetEntity.IsMedicalStart = 'Y';
  617. }
  618. //targetEntity.AuditDate = sourceEntity.AuditDate;
  619. //targetEntity.AuditDoctor = sourceEntity.AuditDoctor;
  620. targetEntity.BirthDate = sourceEntity.BirthDate;
  621. targetEntity.Age = sourceEntity.Age;
  622. targetEntity.CompleteFlag = sourceEntity.CompleteFlag;
  623. targetEntity.CustomerOrgGroupId = sourceEntity.CustomerOrgGroupId;
  624. targetEntity.CustomerOrgId = sourceEntity.CustomerOrgId;
  625. targetEntity.CustomerOrgRegisterId = sourceEntity.CustomerOrgRegisterId;
  626. //targetEntity.GuidePrintTimes = sourceEntity.GuidePrintTimes;
  627. //targetEntity.InterposeMeasure = sourceEntity.InterposeMeasure;
  628. //targetEntity.IsAudit = sourceEntity.IsAudit;
  629. targetEntity.IsLock = sourceEntity.IsLock;
  630. //targetEntity.IsMedicalStart = sourceEntity.IsMedicalStart;
  631. targetEntity.IsNameHide = sourceEntity.IsNameHide;
  632. //targetEntity.IsPhoneFollow = sourceEntity.IsPhoneFollow;
  633. //targetEntity.IsRecoverGuide = sourceEntity.IsRecoverGuide;
  634. //targetEntity.IsUpload = sourceEntity.IsUpload;
  635. targetEntity.IsVip = sourceEntity.IsVip;
  636. targetEntity.JobCardNo = sourceEntity.JobCardNo;
  637. targetEntity.JobPost = sourceEntity.JobPost;
  638. targetEntity.JobTitle = sourceEntity.JobTitle;
  639. targetEntity.MaritalStatusId = sourceEntity.MaritalStatusId;
  640. targetEntity.MedicalCardNo = sourceEntity.MedicalCardNo;
  641. //targetEntity.MedicalConclusionId = sourceEntity.MedicalConclusionId;
  642. targetEntity.MedicalPackageId = sourceEntity.MedicalPackageId;
  643. //targetEntity.MedicalStartDate = sourceEntity.MedicalStartDate;
  644. //targetEntity.MedicalTimes = sourceEntity.MedicalTimes;
  645. targetEntity.MedicalTypeId = sourceEntity.MedicalTypeId;
  646. targetEntity.MedicalCenterId = sourceEntity.MedicalCenterId;
  647. //targetEntity.PatientId = sourceEntity.PatientId;
  648. targetEntity.PatientName = sourceEntity.PatientName;
  649. //targetEntity.PatientRegisterNo = sourceEntity.PatientRegisterNo;
  650. targetEntity.PersonnelTypeId = sourceEntity.PersonnelTypeId;
  651. targetEntity.Remark = sourceEntity.Remark;
  652. //targetEntity.ReportPrintTimes = sourceEntity.ReportPrintTimes;
  653. targetEntity.Salesman = sourceEntity.Salesman;
  654. targetEntity.SexHormoneTermId = sourceEntity.SexHormoneTermId;
  655. targetEntity.SexId = sourceEntity.SexId;
  656. //targetEntity.SummaryDate = sourceEntity.SummaryDate;
  657. //targetEntity.SummaryDoctor = sourceEntity.SummaryDoctor;
  658. targetEntity.ThirdInfo = sourceEntity.ThirdInfo;
  659. if (sourceEntity.CustomerOrgGroupId != Guid.Empty && sourceEntity.CustomerOrgGroupId != null)
  660. {
  661. await SetCustomerOrgGroupId(targetEntity, (Guid)sourceEntity.CustomerOrgGroupId);
  662. }
  663. //targetEntity.MedicalTimes = sourceEntity.MedicalTimes;
  664. //targetEntity.PatientId = sourceEntity.PatientId;
  665. //targetEntity.PatientRegisterNo = sourceEntity.PatientRegisterNo;
  666. //if (isUpdateCheck)
  667. //{
  668. // //有更新性别或者年龄,需要更新check表数据
  669. // await _registerAsbitemManager.UpdateCheckReferenceRange(targetEntity.Id);
  670. //}
  671. }
  672. public async Task SetCustomerOrgGroupId(PatientRegister patientRegister, Guid customerOrgGroupId)
  673. {
  674. var customerOrgGroup = await _customerOrgGroupRepository.GetAsync(customerOrgGroupId);
  675. if ((customerOrgGroup.ForSexId == ForSexFlag.Male && patientRegister.SexId != SexFlag.Male && patientRegister.SexId != SexFlag.UnKnown)
  676. || (customerOrgGroup.ForSexId == ForSexFlag.Female && patientRegister.SexId != SexFlag.Female && patientRegister.SexId != SexFlag.UnKnown))
  677. {
  678. throw new UserFriendlyException("分组和人员性别不一致");
  679. }
  680. if ((customerOrgGroup.MaritalStatusId == MaritalStatusFlag.UnMarried && patientRegister.MaritalStatusId != MaritalStatusFlag.UnMarried && patientRegister.MaritalStatusId != MaritalStatusFlag.UnKnown)
  681. || (customerOrgGroup.MaritalStatusId == MaritalStatusFlag.Married && patientRegister.MaritalStatusId == MaritalStatusFlag.UnMarried))
  682. {
  683. throw new UserFriendlyException("分组和人员婚姻状况不一致");
  684. }
  685. if (patientRegister.Age < customerOrgGroup.AgeLowerLimit)
  686. {
  687. throw new UserFriendlyException("年龄不能小于分组年龄下限");
  688. }
  689. if (patientRegister.Age > customerOrgGroup.AgeUpperLimit)
  690. {
  691. throw new UserFriendlyException("年龄不能大于于分组年龄上限");
  692. }
  693. patientRegister.CustomerOrgGroupId = customerOrgGroupId;
  694. }
  695. /// <summary>
  696. /// 生成条码号(规则,先找体检中心的配置,如未找到,就直接使用全局的)
  697. /// 模式(0 日期+尾号 1.顺序递增)
  698. /// </summary>
  699. /// <param name="medicalCenterId">体检中心ID</param>
  700. /// <returns></returns>
  701. public async Task<string> CreatePatientRegisterNo(Guid medicalCenterId)
  702. {
  703. string PatientRegisterNo = ""; //条码号
  704. List<SysParmValue> spvlist = await _sysParmValueRepository.GetListAsync();
  705. var spv_tjzx = spvlist.Where(m => m.MedicalCenterId == medicalCenterId); //体检中心配置
  706. var spv_global = spvlist.Where(m => m.MedicalCenterId == Guid.Empty); //全局配置
  707. var patient_register_no_rule_coding = ""; // 模式(0 日期+尾号 1.顺序递增)
  708. var patient_register_no_rule_tail_len = ""; //尾号长度
  709. var patient_register_no_rule_prefix = ""; //前缀
  710. patient_register_no_rule_coding = await _sysParmValueManager.GetSysParmValueAsync(medicalCenterId, "patient_register_no_rule_coding");
  711. patient_register_no_rule_tail_len = await _sysParmValueManager.GetSysParmValueAsync(medicalCenterId, "patient_register_no_rule_tail_len");
  712. patient_register_no_rule_prefix = await _sysParmValueManager.GetSysParmValueAsync(medicalCenterId, "patient_register_no_rule_prefix");
  713. if (string.IsNullOrWhiteSpace(patient_register_no_rule_tail_len))
  714. {
  715. throw new UserFriendlyException("人员条码号尾号长度不能为空");
  716. }
  717. int tailLen;
  718. if (!int.TryParse(patient_register_no_rule_tail_len, out tailLen))
  719. {
  720. throw new UserFriendlyException("人员条码号尾号长度必须是数字");
  721. };
  722. if (tailLen < 4)
  723. {
  724. throw new UserFriendlyException("人员条码号尾号长度必须大于等于4");
  725. }
  726. var primarykeyBuilderEnt = await _primarykeyBuilderRepository.FirstOrDefaultAsync(f => f.PrimarykeyBuilderId == "patient_register_no");
  727. string maxnum = "1";
  728. string date = DateTime.Now.ToString("yyMMdd");// 当天
  729. if (patient_register_no_rule_coding == "0")
  730. {
  731. //日期+尾号
  732. #region 模式0 日期+尾号
  733. if (primarykeyBuilderEnt != null)
  734. {
  735. if (primarykeyBuilderEnt.DateString != date)
  736. {
  737. //新的日期 为1 maxnum
  738. primarykeyBuilderEnt.DateString = date;
  739. maxnum = "1";
  740. }
  741. else
  742. {
  743. maxnum = (Convert.ToInt32(primarykeyBuilderEnt.SerialNo) + 1).ToString();
  744. }
  745. primarykeyBuilderEnt.SerialNo = maxnum; //更新新的序列号
  746. PatientRegisterNo = patient_register_no_rule_prefix + date + maxnum.PadLeft(Convert.ToInt32(patient_register_no_rule_tail_len), '0');
  747. await _primarykeyBuilderRepository.UpdateAsync(primarykeyBuilderEnt);
  748. }
  749. else
  750. {
  751. //初始写入
  752. PatientRegisterNo = patient_register_no_rule_prefix + date + maxnum.PadLeft(Convert.ToInt32(patient_register_no_rule_tail_len), '0');
  753. primarykeyBuilderEnt = new PrimarykeyBuilder
  754. {
  755. PrimarykeyBuilderId = "patient_register_no",
  756. DateString = date,
  757. SerialNo = maxnum
  758. };
  759. await _primarykeyBuilderRepository.InsertAsync(primarykeyBuilderEnt,true);
  760. }
  761. #endregion
  762. }
  763. else
  764. {
  765. //模式1 顺序递增
  766. #region 模式1 顺序递增
  767. if (primarykeyBuilderEnt != null)
  768. {
  769. maxnum = (Convert.ToInt32(primarykeyBuilderEnt.SerialNo) + 1).ToString();
  770. primarykeyBuilderEnt.SerialNo = maxnum; //更新num
  771. await _primarykeyBuilderRepository.UpdateAsync(primarykeyBuilderEnt);
  772. }
  773. else
  774. {
  775. //初始写入
  776. primarykeyBuilderEnt = new PrimarykeyBuilder
  777. {
  778. PrimarykeyBuilderId = "patient_register_no",
  779. DateString = "",
  780. SerialNo = maxnum
  781. };
  782. await _primarykeyBuilderRepository.InsertAsync(primarykeyBuilderEnt,true);
  783. }
  784. PatientRegisterNo = patient_register_no_rule_prefix + maxnum.PadLeft(Convert.ToInt32(patient_register_no_rule_tail_len), '0');
  785. #endregion
  786. }
  787. return PatientRegisterNo;
  788. }
  789. public async Task<string> CreatePrePatientRegisterNo()
  790. {
  791. string maxnum = "1";
  792. string date = DateTime.Now.ToString("yyMMdd");// 当天
  793. var primarykeyBuilderEnt = await _primarykeyBuilderRepository.FirstOrDefaultAsync(f => f.PrimarykeyBuilderId == "pre_patient_register_no");
  794. string prePatientRegisterNo;
  795. if (primarykeyBuilderEnt != null)
  796. {
  797. if (primarykeyBuilderEnt.DateString != date)
  798. {
  799. //新的日期 为1 maxnum
  800. primarykeyBuilderEnt.DateString = date;
  801. }
  802. maxnum = (Convert.ToInt32(primarykeyBuilderEnt.SerialNo) + 1).ToString();
  803. primarykeyBuilderEnt.SerialNo = maxnum; //更新新的序列号
  804. prePatientRegisterNo = date + maxnum.PadLeft(Convert.ToInt32(5), '0');
  805. await _primarykeyBuilderRepository.UpdateAsync(primarykeyBuilderEnt);
  806. }
  807. else
  808. {
  809. //初始写入
  810. prePatientRegisterNo = date + maxnum.PadLeft(Convert.ToInt32(5), '0');
  811. primarykeyBuilderEnt = new PrimarykeyBuilder
  812. {
  813. PrimarykeyBuilderId = "patient_register_no",
  814. DateString = date,
  815. SerialNo = maxnum
  816. };
  817. await _primarykeyBuilderRepository.InsertAsync(primarykeyBuilderEnt, true);
  818. }
  819. return prePatientRegisterNo;
  820. }
  821. ///// <summary>
  822. ///// 生成条码号(规则,先找体检中心的配置,如未找到,就直接使用全局的)
  823. ///// 模式(0 日期+尾号 1.顺序递增)
  824. ///// </summary>
  825. ///// <param name="OrganizationUnitId">体检中心ID</param>
  826. ///// <returns></returns>
  827. //public async Task<string> CreatePatientNo(Guid OrganizationUnitId)
  828. //{
  829. // string PatientNo = "";
  830. // string prefix = ""; //前缀
  831. // int lastLength = 0;
  832. // string mode = "0";
  833. // string beginValue = "0";
  834. // List<SysParmValue> spvlist = await _sysParmValueRepository.GetListAsync();
  835. // if (CustomerOrgId == null || CustomerOrgId == Guid.Empty)
  836. // {
  837. // //个人
  838. // spvlist = spvlist.Where(m => m.OrganizationUnitId == Guid.Empty).ToList();
  839. // }
  840. // else
  841. // {
  842. // var OrganizationUnitId = (await _customerOrgRepository.FindAsync(m => m.Id == CustomerOrgId)).OrganizationUnitId;
  843. // if (OrganizationUnitId == null || OrganizationUnitId == Guid.Empty)
  844. // {
  845. // spvlist = spvlist.Where(m => m.OrganizationUnitId == Guid.Empty).ToList();
  846. // }
  847. // else
  848. // {
  849. // spvlist = spvlist.Where(m => m.OrganizationUnitId == OrganizationUnitId).ToList();
  850. // }
  851. // }
  852. // var org_barcode_no_begin_value = spvlist.Where(m => m.SysParmId == "org_barcode_no_begin_value").FirstOrDefault(); //条码号建立规则--单位登记条码号开始值
  853. // var barcode_no_build_rule_mode = spvlist.Where(m => m.SysParmId == "barcode_no_build_rule_mode").FirstOrDefault(); //条码号建立规则--编码方式
  854. // var barcode_no_build_rule_length = spvlist.Where(m => m.SysParmId == "barcode_no_build_rule_length").FirstOrDefault(); //条码号建立规则--尾号长度
  855. // var barcode_no_build_rule_prefix = spvlist.Where(m => m.SysParmId == "barcode_no_build_rule_prefix").FirstOrDefault(); //条码号建立规则--条码号前缀
  856. // if (barcode_no_build_rule_mode != null)
  857. // {
  858. // mode = barcode_no_build_rule_mode.ParmValue; //编码方式
  859. // }
  860. // if (org_barcode_no_begin_value != null)
  861. // {
  862. // beginValue = org_barcode_no_begin_value.ParmValue; //单位起始值
  863. // }
  864. // if (barcode_no_build_rule_length != null)
  865. // {
  866. // lastLength = Convert.ToInt32(barcode_no_build_rule_length.ParmValue); //尾号长度
  867. // }
  868. // if (barcode_no_build_rule_prefix != null)
  869. // {
  870. // prefix = barcode_no_build_rule_prefix.ParmValue; //前缀
  871. // }
  872. // var primarykeyBuilderEnt = await _primarykeyBuilderRepository.FindAsync(f => f.PrimarykeyBuilderId == "patient_register");
  873. // string writenum = "0";
  874. // string maxnum = "0";
  875. // string date = DateTime.Now.ToString("yyyyMMdd");// 当天
  876. // if (mode == "0")
  877. // {
  878. // //日期+尾号 区分个人跟单位 个人1开始 单位未设置的单位起始值开始
  879. // #region 模式0 日期+尾号
  880. // if (primarykeyBuilderEnt != null)
  881. // {
  882. // if (primarykeyBuilderEnt.DateString == date)
  883. // {
  884. // //当天
  885. // if (CustomerOrgId == null || CustomerOrgId == Guid.Empty)
  886. // {
  887. // //个人
  888. // maxnum = (Convert.ToInt32(primarykeyBuilderEnt.SerialNo) + 1).ToString();
  889. // }
  890. // else
  891. // {
  892. // maxnum = (Convert.ToInt32(beginValue) + Convert.ToInt32(primarykeyBuilderEnt.SerialNo) + 1).ToString();
  893. // }
  894. // writenum = (Convert.ToInt32(primarykeyBuilderEnt.SerialNo) + 1).ToString();
  895. // PatientNo = prefix + date + maxnum.PadLeft(lastLength, '0');
  896. // }
  897. // else
  898. // {
  899. // //已更换日期 重新初始化
  900. // if (CustomerOrgId == null || CustomerOrgId == Guid.Empty)
  901. // {
  902. // //个人
  903. // maxnum = (Convert.ToInt32(maxnum) + 1).ToString();
  904. // }
  905. // else
  906. // {
  907. // maxnum = (Convert.ToInt32(beginValue) + Convert.ToInt32(maxnum) + 1).ToString();
  908. // }
  909. // writenum = (Convert.ToInt32(maxnum) + 1).ToString();
  910. // PatientNo = prefix + date + maxnum.PadLeft(lastLength, '0');
  911. // }
  912. // primarykeyBuilderEnt.DateString = date;
  913. // primarykeyBuilderEnt.SerialNo = writenum;
  914. // await _primarykeyBuilderRepository.UpdateAsync(primarykeyBuilderEnt);
  915. // }
  916. // else
  917. // {
  918. // //初始写入
  919. // // maxnum = (Convert.ToInt32(maxnum) + 1).ToString();
  920. // writenum = (Convert.ToInt32(maxnum) + 1).ToString();
  921. // if (CustomerOrgId == null || CustomerOrgId == Guid.Empty)
  922. // {
  923. // //个人
  924. // maxnum = (Convert.ToInt32(maxnum) + 1).ToString();
  925. // }
  926. // else
  927. // {
  928. // maxnum = (Convert.ToInt32(beginValue) + Convert.ToInt32(maxnum) + 1).ToString();
  929. // }
  930. // PatientNo = prefix + date + maxnum.PadLeft(lastLength, '0');
  931. // primarykeyBuilderEnt = new PrimarykeyBuilder
  932. // {
  933. // PrimarykeyBuilderId = "patient_register",
  934. // DateString = date,
  935. // SerialNo = writenum
  936. // };
  937. // await _primarykeyBuilderRepository.InsertAsync(primarykeyBuilderEnt);
  938. // }
  939. // #endregion
  940. // }
  941. // else
  942. // {
  943. // //模式1 顺序递增 不区分个人跟单位
  944. // #region 模式1 顺序递增
  945. // if (primarykeyBuilderEnt != null)
  946. // {
  947. // maxnum = (Convert.ToInt32(primarykeyBuilderEnt.SerialNo) + 1).ToString();
  948. // }
  949. // else
  950. // {
  951. // //初始写入
  952. // maxnum = (Convert.ToInt32(maxnum) + 1).ToString();
  953. // primarykeyBuilderEnt = new PrimarykeyBuilder
  954. // {
  955. // PrimarykeyBuilderId = "patient_register",
  956. // DateString = "",
  957. // SerialNo = maxnum
  958. // };
  959. // await _primarykeyBuilderRepository.InsertAsync(primarykeyBuilderEnt);
  960. // }
  961. // PatientNo = prefix + maxnum.PadLeft(lastLength, '0');
  962. // #endregion
  963. // }
  964. // return PatientNo;
  965. //}
  966. /// <summary>
  967. /// 获取体检次数 最后一次加一
  968. /// </summary>
  969. /// <param name="PatientId"></param>
  970. /// <returns></returns>
  971. private async Task<short> GetPatientCount(Guid PatientId)
  972. {
  973. short MaxMedicalTimes = 0;
  974. var patientRegisterEntList = await _repository.GetListAsync(m => m.PatientId == PatientId);
  975. if (patientRegisterEntList.Count > 0)
  976. MaxMedicalTimes = patientRegisterEntList.Max(m => m.MedicalTimes);
  977. return (short)(MaxMedicalTimes + 1);
  978. }
  979. /// <summary>
  980. /// 获取体检次数 不加一
  981. /// </summary>
  982. /// <param name="PatientId"></param>
  983. /// <returns></returns>
  984. public short GetPatientCountNoAsync(Guid PatientId)
  985. {
  986. short MaxMedicalTimes = 0;
  987. var patientRegisterEntList = _repository.GetListAsync(m => m.PatientId == PatientId).Result;
  988. if (patientRegisterEntList.Count > 0)
  989. MaxMedicalTimes = patientRegisterEntList.Max(m => m.MedicalTimes);
  990. return MaxMedicalTimes;
  991. }
  992. /// <summary>
  993. /// 获取最后登记时间
  994. /// </summary>
  995. /// <param name="PatientId"></param>
  996. /// <returns></returns>
  997. public DateTime? GetPatientRegisterLastTime(Guid PatientId)
  998. {
  999. DateTime? LastTime = null;
  1000. var patientRegisterEntList = _repository.GetListAsync(m => m.PatientId == PatientId).Result;
  1001. if (patientRegisterEntList.Count > 0)
  1002. LastTime = patientRegisterEntList.OrderByDescending(o => o.CreationTime).First().CreationTime;
  1003. return LastTime;
  1004. }
  1005. /// <summary>
  1006. /// 回收表格
  1007. /// </summary>
  1008. /// <param name="PatientRegisterId">登记表ID</param>
  1009. /// <returns></returns>
  1010. public async Task<PatientRegister> UpdateRecoverGuideAsync(Guid PatientRegisterId)
  1011. {
  1012. var entity = await _repository.FindAsync(f => f.Id == PatientRegisterId);
  1013. if (entity != null)
  1014. {
  1015. entity.IsRecoverGuide = 'Y';
  1016. return await _repository.UpdateAsync(entity);
  1017. }
  1018. else
  1019. {
  1020. return entity;
  1021. }
  1022. }
  1023. private bool IsParmNotNUll(object obj)
  1024. {
  1025. if (obj == null)
  1026. return false;
  1027. else return true;
  1028. }
  1029. /// <summary>
  1030. /// 删除 已废弃
  1031. /// </summary>
  1032. /// <param name="PatientRegisterId">人员登记表ID</param>
  1033. /// <returns></returns>
  1034. public async Task IsDelete(Guid PatientRegisterId)
  1035. {
  1036. //体检开始不能删 有一项收费了不能删
  1037. var ent = await _repository.GetAsync(PatientRegisterId);
  1038. if (ent != null)
  1039. {
  1040. if (ent.IsMedicalStart == 'Y')
  1041. {
  1042. throw new UserFriendlyException($"体检已开始,不能删除");
  1043. }
  1044. else
  1045. {
  1046. var registerAsbitemList = await _registerAsbitemRepository.GetListAsync(m => m.PatientRegisterId == PatientRegisterId);
  1047. if (registerAsbitemList.Count > 0)
  1048. {
  1049. foreach (var item in registerAsbitemList)
  1050. {
  1051. await _registerAsbitemManager.DeleteAsync(item.Id); //删除登记组合项目
  1052. }
  1053. }
  1054. await _repository.DeleteAsync(ent); //执行删除
  1055. }
  1056. }
  1057. else
  1058. {
  1059. throw new UserFriendlyException($"数据有误");
  1060. }
  1061. }
  1062. /// <summary>
  1063. /// 1.已有已检组合项目的不允许删除。
  1064. /// 2.已有收费的不允许删除。
  1065. /// 3.删除的时候,同步删除(组合项目(register_asbitem),
  1066. /// 登记检查(register_check),register_check_critical_value(危急值),
  1067. /// register_check_item,register_check_picture,register_check_suggestion,register_check_summary)
  1068. /// </summary>
  1069. /// <param name="id"></param>
  1070. /// <returns></returns>
  1071. /// <exception cref="UserFriendlyException"></exception>
  1072. public async Task CheckAndDeleteAsync(Guid id)
  1073. {
  1074. //var patientRegisterEnt = await _repository.FindAsync(m => m.Id == id);
  1075. var patientRegisterEnt = (await _repository.GetDbSetAsync())
  1076. .Include(x => x.RegisterCheckAsbitems)
  1077. .Where(m => m.Id == id)
  1078. .FirstOrDefault();
  1079. if (patientRegisterEnt != null)
  1080. {
  1081. if (patientRegisterEnt.IsLock == 'Y')
  1082. {
  1083. throw new UserFriendlyException("已加锁,不能删除");
  1084. }
  1085. var patientRegisterQueryable = (from a in await _repository.GetQueryableAsync()
  1086. join b in await _registerAsbitemRepository.GetQueryableAsync() on a.Id equals b.PatientRegisterId
  1087. join c in await _registerCheckRepository.GetQueryableAsync() on b.RegisterCheckId equals c.Id
  1088. where a.Id == id
  1089. select new
  1090. {
  1091. RegisterAsbitemId = b.Id,
  1092. RegisterCheckId = c.Id,
  1093. IsCharge = b.IsCharge,
  1094. PatientName = a.PatientName,
  1095. CompleteFlag = c.CompleteFlag,
  1096. }).AsQueryable();
  1097. if (patientRegisterQueryable.Where(m => m.IsCharge == 'Y').Count() > 0)
  1098. {
  1099. throw new UserFriendlyException($"人员\"{patientRegisterEnt.PatientName}\"已有项目收费,不能删除");
  1100. }
  1101. if (patientRegisterQueryable.Where(m => m.CompleteFlag == RegisterCheckCompleteFlag.Checked).Count() > 0)
  1102. {
  1103. throw new UserFriendlyException($"人员\"{patientRegisterEnt.PatientName}\"已有项目检查,不能删除");
  1104. }
  1105. //删除逻辑
  1106. var RegisterCheckIds = patientRegisterQueryable.Select(s => s.RegisterCheckId);
  1107. var RegisterAsbitemIds = patientRegisterQueryable.Select(s => s.RegisterAsbitemId);
  1108. if (RegisterCheckIds.Any())
  1109. {
  1110. //删除registerCheckSuggestion
  1111. await _registerCheckSuggestionManager.CheckAndDeleteAsync(RegisterCheckIds.ToList());
  1112. //删除registerCheckSummary
  1113. await _registerCheckSummaryManager.CheckAndDeleteAsync(RegisterCheckIds.ToList());
  1114. //删除registerCheckPicture
  1115. await _registerCheckPictureManager.CheckAndDeleteAsync(RegisterCheckIds.ToList());
  1116. //删除registerCheckItem
  1117. await _registerCheckItemManager.CheckAndDeleteAsync(RegisterCheckIds.ToList());
  1118. //删除registerCheckCriticalValue危警值
  1119. await _registerCheckCriticalValueManager.CheckAndDeleteAsync(RegisterCheckIds.ToList());
  1120. //删除registerCheck
  1121. await _registerCheckManager.CheckAndDeleteAsync(RegisterCheckIds.ToList());
  1122. }
  1123. if (RegisterAsbitemIds.Any())
  1124. //删除registerAsbitem
  1125. await _registerAsbitemManager.DeleteManyAsync(RegisterAsbitemIds.ToList());
  1126. //var registerAsbitemList = await _registerAsbitemRepository.GetListAsync(m => RegisterAsbitemIds.Contains(m.Id));
  1127. //if (registerAsbitemList.Any())
  1128. //{
  1129. // await _registerAsbitemRepository.DeleteManyAsync(registerAsbitemList);
  1130. //}
  1131. //删除登记表数据
  1132. await _repository.DeleteAsync(id);
  1133. }
  1134. else
  1135. {
  1136. throw new UserFriendlyException("数据不存在");
  1137. }
  1138. }
  1139. /// <summary>
  1140. /// 更改总检审核医生信息(保存时,医生跟日期不传时取默认登录的用户跟当前时间;取消时,如果不传就不更新医生跟日期,只更新状态)
  1141. /// </summary>
  1142. /// <param name="entitydto"></param>
  1143. /// <param name="entity"></param>
  1144. /// <returns></returns>
  1145. /// <exception cref="UserFriendlyException"></exception>
  1146. public async Task<PatientRegister> UpdatePatientRegisterAuditorDoctorAsync(
  1147. PatientRegister entitydto,
  1148. PatientRegister entity)
  1149. {
  1150. if (entity == null)
  1151. {
  1152. throw new UserFriendlyException("请求参数有误");
  1153. }
  1154. VerifyUpdate(entity);
  1155. if (entitydto.IsAudit == 'Y')
  1156. {
  1157. if (entitydto.AuditDoctorId == null || entitydto.AuditDoctorId == Guid.Empty)
  1158. entity.AuditDoctorId = _currentUser.Id != null ? _currentUser.Id : null;
  1159. else
  1160. entity.AuditDoctorId = entitydto.AuditDoctorId;
  1161. if (entitydto.AuditDate == null)
  1162. entity.AuditDate = DateTime.Now;
  1163. else
  1164. entity.AuditDate = entitydto.AuditDate;
  1165. }
  1166. entity.IsAudit = entitydto.IsAudit;
  1167. return await _repository.UpdateAsync(entity);
  1168. }
  1169. /// <summary>
  1170. /// 更改总检检查医生信息(保存时,医生跟日期不传时取默认登录的用户跟当前时间;取消时,如果不传就不更新医生跟日期,只更新状态)
  1171. /// </summary>
  1172. /// <param name="entitydto"></param>
  1173. /// <param name="entity"></param>
  1174. /// <returns></returns>
  1175. /// <exception cref="UserFriendlyException"></exception>
  1176. public async Task<PatientRegister> UpdatePatientRegisterSummaryDoctorAsync(
  1177. PatientRegister entitydto,
  1178. PatientRegister entity)
  1179. {
  1180. if (entity == null)
  1181. {
  1182. throw new UserFriendlyException("请求参数有误");
  1183. }
  1184. VerifyUpdate(entity);
  1185. if (entitydto.SummaryDoctorId == null || entitydto.SummaryDoctorId == Guid.Empty)
  1186. entity.SummaryDoctorId = _currentUser.Id != null ? _currentUser.Id : null;
  1187. else
  1188. entity.SummaryDoctorId = entitydto.SummaryDoctorId;
  1189. if (entitydto.SummaryDate == null)
  1190. entity.SummaryDate = DateTime.Now;
  1191. else
  1192. entity.SummaryDate = entitydto.SummaryDate;
  1193. entity.CompleteFlag = PatientRegisterCompleteFlag.SumCheck;
  1194. entity.MedicalConclusionId = entitydto.MedicalConclusionId;
  1195. return await _repository.UpdateAsync(entity);
  1196. }
  1197. public void VerifyUpdate(PatientRegister entity)
  1198. {
  1199. if (entity.IsLock == 'Y')
  1200. {
  1201. throw new UserFriendlyException("已加锁,不能更新");
  1202. }
  1203. }
  1204. }
  1205. }