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.

411 lines
19 KiB

1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
  1. using Cronos;
  2. using Microsoft.AspNetCore.Authorization;
  3. using Microsoft.AspNetCore.Mvc;
  4. using Microsoft.EntityFrameworkCore;
  5. using Shentun.Peis.CriticalFollowValues;
  6. using Shentun.Peis.Enums;
  7. using Shentun.Peis.Models;
  8. using SqlSugar;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. using Volo.Abp;
  15. using Volo.Abp.Application.Services;
  16. using Volo.Abp.Domain.Repositories;
  17. using Volo.Abp.ObjectMapping;
  18. namespace Shentun.Peis.PhoneFollowUps
  19. {
  20. /// <summary>
  21. /// 电话随访记录
  22. /// </summary>
  23. [ApiExplorerSettings(GroupName = "Work")]
  24. [Authorize]
  25. public class PhoneFollowUpAppService : ApplicationService
  26. {
  27. private readonly IRepository<PhoneFollowUp, Guid> _phoneFollowUpRepository;
  28. private readonly CacheService _cacheService;
  29. private readonly IRepository<FollowUp, Guid> _followUpRepository;
  30. private readonly IRepository<PatientRegister, Guid> _patientRegisterRepository;
  31. private readonly IRepository<RegisterCheck, Guid> _registerCheckRepository;
  32. private readonly IRepository<RegisterCheckItem> _registerCheckItemRepository;
  33. private readonly IRepository<RegisterCheckAsbitem, Guid> _registerCheckAsbitemRepository;
  34. private readonly IRepository<Asbitem, Guid> _asbitemRepository;
  35. private readonly IRepository<Item, Guid> _itemRepository;
  36. private readonly IRepository<Patient, Guid> _patientRepository;
  37. public PhoneFollowUpAppService(
  38. IRepository<PhoneFollowUp, Guid> phoneFollowUpRepository,
  39. CacheService cacheService,
  40. IRepository<FollowUp, Guid> followUpRepository,
  41. IRepository<PatientRegister, Guid> patientRegisterRepository,
  42. IRepository<RegisterCheck, Guid> registerCheckRepository,
  43. IRepository<RegisterCheckItem> registerCheckItemRepository,
  44. IRepository<RegisterCheckAsbitem, Guid> registerCheckAsbitemRepository,
  45. IRepository<Asbitem, Guid> asbitemRepository,
  46. IRepository<Item, Guid> itemRepository,
  47. IRepository<Patient, Guid> patientRepository)
  48. {
  49. _phoneFollowUpRepository = phoneFollowUpRepository;
  50. _cacheService = cacheService;
  51. _followUpRepository = followUpRepository;
  52. _patientRegisterRepository = patientRegisterRepository;
  53. _registerCheckRepository = registerCheckRepository;
  54. _registerCheckItemRepository = registerCheckItemRepository;
  55. _registerCheckAsbitemRepository = registerCheckAsbitemRepository;
  56. _asbitemRepository = asbitemRepository;
  57. _itemRepository = itemRepository;
  58. _patientRepository = patientRepository;
  59. }
  60. /// <summary>
  61. /// 获取电话随访记录信息
  62. /// </summary>
  63. /// <param name="input"></param>
  64. /// <returns></returns>
  65. [HttpPost("api/app/PhoneFollowUp/Get")]
  66. public async Task<PhoneFollowUpDto> GetAsync(PhoneFollowUpIdInputDto input)
  67. {
  68. var phoneFollowUpEnt = await _phoneFollowUpRepository.GetAsync(input.PhoneFollowUpId);
  69. var entityDto = ObjectMapper.Map<PhoneFollowUp, PhoneFollowUpDto>(phoneFollowUpEnt);
  70. entityDto.CreatorName = await _cacheService.GetSurnameAsync(entityDto.CreatorId);
  71. entityDto.LastModifierName = await _cacheService.GetSurnameAsync(entityDto.LastModifierId);
  72. return entityDto;
  73. }
  74. /// <summary>
  75. /// 获取电话随访记录信息
  76. /// </summary>
  77. /// <param name="input"></param>
  78. /// <returns></returns>
  79. [HttpPost("api/app/PhoneFollowUp/GetList")]
  80. public async Task<List<PhoneFollowUpWithPatientRegisterDto>> GetListAsync(PhoneFollowUpListInputDto input)
  81. {
  82. var query = from phoneFollowUp in await _phoneFollowUpRepository.GetQueryableAsync()
  83. join followUp in await _followUpRepository.GetQueryableAsync() on phoneFollowUp.FollowUpId equals followUp.Id
  84. join patientRegister in await _patientRegisterRepository.GetQueryableAsync() on followUp.PatientRegisterId equals patientRegister.Id
  85. orderby patientRegister.Id, phoneFollowUp.PlanFollowDate
  86. select new
  87. {
  88. patientName = patientRegister.PatientName,
  89. phoneFollowUp
  90. };
  91. if (input.FollowUpId != null)
  92. {
  93. query = query.Where(m => m.phoneFollowUp.FollowUpId == input.FollowUpId);
  94. }
  95. if (!string.IsNullOrWhiteSpace(input.KeyWord))
  96. {
  97. query = query.Where(m => (!string.IsNullOrWhiteSpace(m.phoneFollowUp.FollowUpContent) && m.phoneFollowUp.FollowUpContent.Contains(input.KeyWord))
  98. || (!string.IsNullOrWhiteSpace(m.phoneFollowUp.ReplyContent) && m.phoneFollowUp.ReplyContent.Contains(input.KeyWord))
  99. );
  100. }
  101. if (input.IsComplete != null)
  102. {
  103. query = query.Where(m => m.phoneFollowUp.IsComplete == input.IsComplete);
  104. }
  105. if (!string.IsNullOrWhiteSpace(input.StartDate) && !string.IsNullOrWhiteSpace(input.EndDate))
  106. {
  107. query = query.Where(m => m.phoneFollowUp.PlanFollowDate >= Convert.ToDateTime(input.StartDate)
  108. && m.phoneFollowUp.PlanFollowDate <= Convert.ToDateTime(input.EndDate).AddDays(1));
  109. }
  110. var entListDto = query.ToList().Select(s => new PhoneFollowUpWithPatientRegisterDto
  111. {
  112. FollowUpId = s.phoneFollowUp.FollowUpId,
  113. CreationTime = s.phoneFollowUp.CreationTime,
  114. ReplyContent = s.phoneFollowUp.ReplyContent,
  115. PlanFollowDate = DataHelper.ConversionDateToString(s.phoneFollowUp.PlanFollowDate),
  116. CreatorId = s.phoneFollowUp.CreatorId,
  117. FollowUpContent = s.phoneFollowUp.ReplyContent,
  118. Id = s.phoneFollowUp.Id,
  119. IsComplete = s.phoneFollowUp.IsComplete,
  120. LastModificationTime = s.phoneFollowUp.LastModificationTime,
  121. LastModifierId = s.phoneFollowUp.LastModifierId,
  122. CreatorName = _cacheService.GetSurnameAsync(s.phoneFollowUp.CreatorId).GetAwaiter().GetResult(),
  123. LastModifierName = _cacheService.GetSurnameAsync(s.phoneFollowUp.LastModifierId).GetAwaiter().GetResult(),
  124. PatientName = s.patientName
  125. }).ToList();
  126. return entListDto;
  127. }
  128. /// <summary>
  129. /// 获取人员随访记录
  130. /// </summary>
  131. /// <returns></returns>
  132. [HttpPost("api/app/PhoneFollowUp/GetPatientRegisterCriticalList")]
  133. public async Task<List<PhoneFollowUpWithCriticalItemDto>> GetPatientRegisterCriticalListAsync()
  134. {
  135. var query = from followUp in await _followUpRepository.GetQueryableAsync()
  136. join patientRegister in await _patientRegisterRepository.GetQueryableAsync() on followUp.PatientRegisterId equals patientRegister.Id
  137. join patient in await _patientRepository.GetQueryableAsync() on patientRegister.PatientId equals patient.Id
  138. join registerCheck in await _registerCheckRepository.GetQueryableAsync() on patientRegister.Id equals registerCheck.PatientRegisterId
  139. join registerCheckAsbitem in await _registerCheckAsbitemRepository.GetQueryableAsync() on registerCheck.Id equals registerCheckAsbitem.RegisterCheckId
  140. join asbitem in await _asbitemRepository.GetQueryableAsync() on registerCheckAsbitem.AsbitemId equals asbitem.Id
  141. join registerCheckItem in await _registerCheckItemRepository.GetQueryableAsync() on registerCheck.Id equals registerCheckItem.RegisterCheckId
  142. join item in await _itemRepository.GetQueryableAsync() on registerCheckItem.ItemId equals item.Id
  143. join phoneFollowUp in await _phoneFollowUpRepository.GetQueryableAsync() on followUp.Id equals phoneFollowUp.FollowUpId into phoneFollowUpTemp
  144. from phoneFollowUpEmpty in phoneFollowUpTemp.DefaultIfEmpty()
  145. select new
  146. {
  147. patientRegister,
  148. registerCheck,
  149. asbitem,
  150. registerCheckItem,
  151. item,
  152. followUp,
  153. phoneFollowUpEmpty,
  154. patient
  155. };
  156. var ff = query.ToQueryString();
  157. var followUpGroup = query.ToList().GroupBy(g => g.followUp);
  158. var entListDto = followUpGroup.Select(s => new PhoneFollowUpWithCriticalItemDto
  159. {
  160. PatientName = s.FirstOrDefault().patientRegister.PatientName,
  161. IdNo = s.FirstOrDefault().patient.IdNo,
  162. PatientRegisterNo = s.FirstOrDefault().patientRegister.PatientRegisterNo,
  163. PhoneFollowUpDetail = s.Where(m => m.phoneFollowUpEmpty != null).GroupBy(g => g.phoneFollowUpEmpty).Select(ss => new PhoneFollowUpSimpleDto
  164. {
  165. FollowUpContent = ss.Key.FollowUpContent,
  166. IsComplete = ss.Key.IsComplete,
  167. PlanFollowDate = DataHelper.ConversionDateToString(ss.Key.PlanFollowDate),
  168. ReplyContent = ss.Key.ReplyContent
  169. }).ToList(),
  170. AbnormalAsbitemDetail = s.Where(m => m.registerCheck.IsCriticalValue != null).GroupBy(g => g.registerCheck).Select(ss => new PhoneFollowUpWithCriticalItemAbnormalAsbitemDto
  171. {
  172. CriticalValueContent = ss.Key.CriticalValueContent,
  173. AsbitemName = string.Join(",", ss.Select(sss => sss.asbitem.DisplayName).Distinct()),
  174. IsCriticalValue = ss.Key.IsCriticalValue == null ? 'N' : ss.Key.IsCriticalValue.Value,
  175. IsReview = ss.Key.IsReview == null ? 'N' : ss.Key.IsReview.Value
  176. }).ToList(),
  177. AbnormalItemDetail = s.Where(m => m.registerCheckItem.IsCriticalValue != null).GroupBy(g => g.registerCheckItem).Select(ss => new PhoneFollowUpWithCriticalItemAbnormalItemDto
  178. {
  179. CriticalValueContent = ss.Key.CriticalValueContent,
  180. ItemName = ss.FirstOrDefault().item.DisplayName,
  181. IsCriticalValue = ss.Key.IsCriticalValue == null ? 'N' : ss.Key.IsCriticalValue.Value,
  182. IsReview = ss.Key.IsReview == null ? 'N' : ss.Key.IsReview.Value
  183. }).ToList()
  184. }).ToList();
  185. return entListDto;
  186. //var query = from phoneFollowUp in await _phoneFollowUpRepository.GetQueryableAsync()
  187. // join followUp in await _followUpRepository.GetQueryableAsync() on phoneFollowUp.FollowUpId equals followUp.Id
  188. // join patientRegister in await _patientRegisterRepository.GetQueryableAsync() on followUp.PatientRegisterId equals patientRegister.Id
  189. // orderby patientRegister.Id, phoneFollowUp.PlanFollowDate
  190. // select new
  191. // {
  192. // patientName = patientRegister.PatientName,
  193. // phoneFollowUp
  194. // };
  195. //if (input.FollowUpId != null)
  196. //{
  197. // query = query.Where(m => m.phoneFollowUp.FollowUpId == input.FollowUpId);
  198. //}
  199. //if (!string.IsNullOrWhiteSpace(input.KeyWord))
  200. //{
  201. // query = query.Where(m => (!string.IsNullOrWhiteSpace(m.phoneFollowUp.FollowUpContent) && m.phoneFollowUp.FollowUpContent.Contains(input.KeyWord))
  202. // || (!string.IsNullOrWhiteSpace(m.phoneFollowUp.ReplyContent) && m.phoneFollowUp.ReplyContent.Contains(input.KeyWord))
  203. // );
  204. //}
  205. //if (input.IsComplete != null)
  206. //{
  207. // query = query.Where(m => m.phoneFollowUp.IsComplete == input.IsComplete);
  208. //}
  209. //if (!string.IsNullOrWhiteSpace(input.StartDate) && !string.IsNullOrWhiteSpace(input.EndDate))
  210. //{
  211. // query = query.Where(m => m.phoneFollowUp.PlanFollowDate >= Convert.ToDateTime(input.StartDate)
  212. // && m.phoneFollowUp.PlanFollowDate <= Convert.ToDateTime(input.EndDate).AddDays(1));
  213. //}
  214. //var entListDto = query.ToList().Select(s => new PhoneFollowUpWithPatientRegisterDto
  215. //{
  216. // FollowUpId = s.phoneFollowUp.FollowUpId,
  217. // CreationTime = s.phoneFollowUp.CreationTime,
  218. // ReplyContent = s.phoneFollowUp.ReplyContent,
  219. // PlanFollowDate = DataHelper.ConversionDateToString(s.phoneFollowUp.PlanFollowDate),
  220. // CreatorId = s.phoneFollowUp.CreatorId,
  221. // FollowUpContent = s.phoneFollowUp.ReplyContent,
  222. // Id = s.phoneFollowUp.Id,
  223. // IsComplete = s.phoneFollowUp.IsComplete,
  224. // LastModificationTime = s.phoneFollowUp.LastModificationTime,
  225. // LastModifierId = s.phoneFollowUp.LastModifierId,
  226. // CreatorName = _cacheService.GetSurnameAsync(s.phoneFollowUp.CreatorId).GetAwaiter().GetResult(),
  227. // LastModifierName = _cacheService.GetSurnameAsync(s.phoneFollowUp.LastModifierId).GetAwaiter().GetResult(),
  228. // PatientName = s.patientName
  229. //}).ToList();
  230. //return entListDto;
  231. }
  232. /// <summary>
  233. /// 创建电话随访记录
  234. /// </summary>
  235. /// <param name="input"></param>
  236. /// <returns></returns>
  237. [HttpPost("api/app/PhoneFollowUp/Create")]
  238. public async Task CreateAsync(CreatePhoneFollowUpDto input)
  239. {
  240. if (string.IsNullOrWhiteSpace(input.ModeValue))
  241. {
  242. throw new UserFriendlyException("计划周期不能为空");
  243. }
  244. if (input.FollowUpMode == FollowUpModeFlag.Corn && string.IsNullOrWhiteSpace(input.EndDate))
  245. {
  246. throw new UserFriendlyException("采用corn表达式时需要截止日期");
  247. }
  248. var isPhoneFollowUp = await _phoneFollowUpRepository.CountAsync(c => c.FollowUpId == input.FollowUpId);
  249. if (isPhoneFollowUp > 0)
  250. {
  251. throw new UserFriendlyException("已存在电话随访记录,不允许重复生成");
  252. }
  253. List<PhoneFollowUp> phoneFollowUpList = new List<PhoneFollowUp>();
  254. if (input.FollowUpMode == FollowUpModeFlag.Corn)
  255. {
  256. //corn表达式
  257. #region 解析Cron表达式
  258. try
  259. {
  260. var schedule = CronExpression.Parse(input.ModeValue, CronFormat.IncludeSeconds);
  261. var occurrences = schedule.GetOccurrences(DateTime.UtcNow, Convert.ToDateTime(input.EndDate).ToUniversalTime()); //获取截止时间前所有的计划时间
  262. foreach (var occurrence in occurrences)
  263. {
  264. var phoneFollowUpEntity = new PhoneFollowUp(GuidGenerator.Create())
  265. {
  266. FollowUpContent = input.FollowUpContent,
  267. FollowUpId = input.FollowUpId,
  268. ReplyContent = input.ReplyContent,
  269. PlanFollowDate = occurrence,
  270. IsComplete = 'N'
  271. };
  272. phoneFollowUpList.Add(phoneFollowUpEntity);
  273. }
  274. }
  275. catch (Exception ex)
  276. {
  277. throw new UserFriendlyException("Corn表达式不正确");
  278. }
  279. #endregion
  280. }
  281. else
  282. {
  283. //其他模式
  284. int planCount = 0;
  285. try
  286. {
  287. planCount = Convert.ToInt32(input.ModeValue);
  288. }
  289. catch (Exception ex)
  290. {
  291. throw new UserFriendlyException("Corn表达式不正确");
  292. }
  293. for (int i = 0; i < planCount; i++)
  294. {
  295. DateTime planFollowDate = DateTime.Now;
  296. if (input.FollowUpMode == FollowUpModeFlag.Day)
  297. {
  298. planFollowDate = planFollowDate.AddDays(i);
  299. }
  300. else if (input.FollowUpMode == FollowUpModeFlag.Week)
  301. {
  302. planFollowDate = planFollowDate.AddDays(7 * i);
  303. }
  304. else if (input.FollowUpMode == FollowUpModeFlag.Month)
  305. {
  306. planFollowDate = planFollowDate.AddMonths(i);
  307. }
  308. else if (input.FollowUpMode == FollowUpModeFlag.Year)
  309. {
  310. planFollowDate = planFollowDate.AddYears(i);
  311. }
  312. var phoneFollowUpEntity = new PhoneFollowUp(GuidGenerator.Create())
  313. {
  314. FollowUpContent = input.FollowUpContent,
  315. FollowUpId = input.FollowUpId,
  316. ReplyContent = input.ReplyContent,
  317. PlanFollowDate = planFollowDate,
  318. IsComplete = 'N'
  319. };
  320. phoneFollowUpList.Add(phoneFollowUpEntity);
  321. }
  322. }
  323. if (phoneFollowUpList.Any())
  324. {
  325. await _phoneFollowUpRepository.InsertManyAsync(phoneFollowUpList);
  326. }
  327. }
  328. /// <summary>
  329. /// 修改电话随访记录
  330. /// </summary>
  331. /// <param name="input"></param>
  332. /// <returns></returns>
  333. [HttpPost("api/app/PhoneFollowUp/Update")]
  334. public async Task UpdateAsync(UpdatePhoneFollowUpDto input)
  335. {
  336. var entity = await _phoneFollowUpRepository.GetAsync(input.PhoneFollowUpId);
  337. entity.IsComplete = input.IsComplete;
  338. if (input.PlanFollowDate != null)
  339. {
  340. entity.PlanFollowDate = Convert.ToDateTime(input.PlanFollowDate);
  341. }
  342. if (!string.IsNullOrWhiteSpace(input.ReplyContent))
  343. {
  344. entity.ReplyContent = input.ReplyContent;
  345. }
  346. if (!string.IsNullOrWhiteSpace(input.FollowUpContent))
  347. {
  348. entity.FollowUpContent = input.FollowUpContent;
  349. }
  350. await _phoneFollowUpRepository.UpdateAsync(entity);
  351. }
  352. /// <summary>
  353. /// 删除
  354. /// </summary>
  355. /// <param name="input"></param>
  356. /// <returns></returns>
  357. [HttpPost("api/app/PhoneFollowUp/Delete")]
  358. public async Task DeleteAsync(PhoneFollowUpIdInputDto input)
  359. {
  360. await _phoneFollowUpRepository.DeleteAsync(input.PhoneFollowUpId);
  361. }
  362. }
  363. }