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.

397 lines
18 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 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.Extensions.Configuration;
  5. using NPOI.OpenXmlFormats.Wordprocessing;
  6. using Shentun.Peis.Enums;
  7. using Shentun.Peis.Models;
  8. using Shentun.Peis.PlugIns.Sms;
  9. using Shentun.Peis.SmsSends;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.IO;
  13. using System.Linq;
  14. using System.Text;
  15. using System.Threading.Tasks;
  16. using Volo.Abp;
  17. using Volo.Abp.Application.Services;
  18. using Volo.Abp.Domain.Repositories;
  19. namespace Shentun.Peis.SmsSends
  20. {
  21. /// <summary>
  22. /// 短信随访记录
  23. /// </summary>
  24. [ApiExplorerSettings(GroupName = "Work")]
  25. [Authorize]
  26. public class SmsSendAppService : ApplicationService
  27. {
  28. private readonly IRepository<SmsSend, Guid> _smsSendRepository;
  29. private readonly CacheService _cacheService;
  30. private readonly IRepository<FollowUp, Guid> _followUpRepository;
  31. private readonly IRepository<PatientRegister, Guid> _patientRegisterRepository;
  32. private readonly IRepository<Patient, Guid> _patientRepository;
  33. private readonly IRepository<ThirdInterface, Guid> _thirdInterfaceRepository;
  34. public SmsSendAppService(
  35. CacheService cacheService,
  36. IRepository<FollowUp, Guid> followUpRepository,
  37. IRepository<PatientRegister, Guid> patientRegisterRepository,
  38. IRepository<SmsSend, Guid> smsSendRepository,
  39. IRepository<Patient, Guid> patientRepository,
  40. IRepository<ThirdInterface, Guid> thirdInterfaceRepository)
  41. {
  42. _cacheService = cacheService;
  43. _followUpRepository = followUpRepository;
  44. _patientRegisterRepository = patientRegisterRepository;
  45. _smsSendRepository = smsSendRepository;
  46. _patientRepository = patientRepository;
  47. _thirdInterfaceRepository = thirdInterfaceRepository;
  48. }
  49. /// <summary>
  50. /// 获取短信随访记录信息
  51. /// </summary>
  52. /// <param name="input"></param>
  53. /// <returns></returns>
  54. [HttpPost("api/app/SmsSend/Get")]
  55. public async Task<SmsSendDto> GetAsync(SmsSendIdInputDto input)
  56. {
  57. var smsSendEnt = await _smsSendRepository.GetAsync(input.SmsSendId);
  58. var entityDto = ObjectMapper.Map<SmsSend, SmsSendDto>(smsSendEnt);
  59. entityDto.CreatorName = await _cacheService.GetSurnameAsync(entityDto.CreatorId);
  60. entityDto.LastModifierName = await _cacheService.GetSurnameAsync(entityDto.LastModifierId);
  61. return entityDto;
  62. }
  63. /// <summary>
  64. /// 获取短信随访记录信息
  65. /// </summary>
  66. /// <param name="input"></param>
  67. /// <returns></returns>
  68. [HttpPost("api/app/SmsSend/GetList")]
  69. public async Task<List<SmsSendDto>> GetListAsync(SmsSendListInputDto input)
  70. {
  71. var query = from smsSend in await _smsSendRepository.GetQueryableAsync()
  72. orderby smsSend.FollowUpId, smsSend.PlanSendDate
  73. select new
  74. {
  75. smsSend
  76. };
  77. if (input.FollowUpId != null)
  78. {
  79. query = query.Where(m => m.smsSend.FollowUpId == input.FollowUpId);
  80. }
  81. if (!string.IsNullOrWhiteSpace(input.PatientName))
  82. {
  83. query = query.Where(m => !string.IsNullOrWhiteSpace(m.smsSend.PatientName) && m.smsSend.PatientName.Contains(input.PatientName));
  84. }
  85. if (!string.IsNullOrWhiteSpace(input.MobileTelephone))
  86. {
  87. query = query.Where(m => !string.IsNullOrWhiteSpace(m.smsSend.MobileTelephone) && m.smsSend.MobileTelephone.Contains(input.MobileTelephone));
  88. }
  89. if (input.IsComplete != null)
  90. {
  91. query = query.Where(m => m.smsSend.IsComplete == input.IsComplete);
  92. }
  93. if (!string.IsNullOrWhiteSpace(input.StartDate) && !string.IsNullOrWhiteSpace(input.EndDate))
  94. {
  95. query = query.Where(m => m.smsSend.PlanSendDate >= Convert.ToDateTime(input.StartDate)
  96. && m.smsSend.PlanSendDate <= Convert.ToDateTime(input.EndDate).AddDays(1));
  97. }
  98. var entListDto = query.ToList().Select(s => new SmsSendDto
  99. {
  100. FollowUpId = s.smsSend.FollowUpId,
  101. CreationTime = s.smsSend.CreationTime,
  102. CreatorId = s.smsSend.CreatorId,
  103. Id = s.smsSend.Id,
  104. IsComplete = s.smsSend.IsComplete,
  105. LastModificationTime = s.smsSend.LastModificationTime,
  106. LastModifierId = s.smsSend.LastModifierId,
  107. CreatorName = _cacheService.GetSurnameAsync(s.smsSend.CreatorId).GetAwaiter().GetResult(),
  108. LastModifierName = _cacheService.GetSurnameAsync(s.smsSend.LastModifierId).GetAwaiter().GetResult(),
  109. PatientName = s.smsSend.PatientName,
  110. Content = s.smsSend.Content,
  111. MobileTelephone = s.smsSend.MobileTelephone,
  112. PlanSendDate = DataHelper.ConversionDateToString(s.smsSend.PlanSendDate),
  113. PatientId = s.smsSend.PatientId,
  114. SmsTypeId = s.smsSend.SmsTypeId
  115. }).ToList();
  116. return entListDto;
  117. }
  118. /// <summary>
  119. /// 自动生成短信随访记录
  120. /// </summary>
  121. /// <param name="input"></param>
  122. /// <returns></returns>
  123. [HttpPost("api/app/SmsSend/AutoCreate")]
  124. public async Task AutoCreateAsync(AutoCreateSmsSendDto input)
  125. {
  126. if (string.IsNullOrWhiteSpace(input.StartDate))
  127. {
  128. throw new UserFriendlyException("开始时间不能为空");
  129. }
  130. var isSmsSend = await _smsSendRepository.CountAsync(c => c.FollowUpId == input.FollowUpId);
  131. if (isSmsSend > 0)
  132. {
  133. throw new UserFriendlyException("已存在短信随访记录,不允许重复生成");
  134. }
  135. var followUpEnt = await _followUpRepository.FirstOrDefaultAsync(f => f.Id == input.FollowUpId);
  136. if (followUpEnt == null)
  137. {
  138. throw new UserFriendlyException("随访ID不正确");
  139. }
  140. List<SmsSend> smsSendList = new List<SmsSend>();
  141. var patientRegisterEnt = (from followUp in await _followUpRepository.GetQueryableAsync()
  142. join patientRegister in await _patientRegisterRepository.GetQueryableAsync()
  143. on followUp.PatientRegisterId equals patientRegister.Id
  144. join patient in await _patientRepository.GetQueryableAsync()
  145. on patientRegister.PatientId equals patient.Id
  146. where followUp.Id == input.FollowUpId
  147. select new
  148. {
  149. patientRegister = patientRegister,
  150. patientName = patientRegister.PatientName,
  151. mobileTelephone = patient.MobileTelephone,
  152. patientId = patient.Id
  153. }).FirstOrDefault();
  154. if (patientRegisterEnt == null)
  155. {
  156. throw new UserFriendlyException("随访数据不存在");
  157. }
  158. for (int i = 0; i < input.GenerateCount; i++)
  159. {
  160. DateTime planFollowDate = Convert.ToDateTime(input.StartDate);
  161. planFollowDate = planFollowDate.AddDays(i * input.IntervalDays);
  162. var smsSendEntity = new SmsSend(GuidGenerator.Create())
  163. {
  164. FollowUpId = input.FollowUpId,
  165. Content = string.IsNullOrWhiteSpace(input.Content) ? patientRegisterEnt.patientName : input.Content,
  166. MobileTelephone = patientRegisterEnt.mobileTelephone,
  167. PatientName = patientRegisterEnt.patientName,
  168. PatientId = patientRegisterEnt.patientId,
  169. PlanSendDate = planFollowDate,
  170. SmsTypeId = "01",
  171. IsComplete = 'Y'
  172. };
  173. smsSendList.Add(smsSendEntity);
  174. }
  175. if (smsSendList.Any())
  176. {
  177. await _smsSendRepository.InsertManyAsync(smsSendList);
  178. followUpEnt.IsPhoneComplete = 'Y';
  179. await _followUpRepository.UpdateAsync(followUpEnt);
  180. //生成短信平台推送计划
  181. await PushCriticalSmsAsync(patientRegisterEnt.patientRegister, smsSendList.Select(s => DataHelper.ConversionDateToString(s.PlanSendDate)).ToList());
  182. }
  183. }
  184. /// <summary>
  185. /// 删除
  186. /// </summary>
  187. /// <param name="input"></param>
  188. /// <returns></returns>
  189. [HttpPost("api/app/SmsSend/Delete")]
  190. public async Task DeleteAsync(SmsSendIdInputDto input)
  191. {
  192. var smsSendEnt = (from smsSend in await _smsSendRepository.GetQueryableAsync()
  193. join followUp in await _followUpRepository.GetQueryableAsync() on smsSend.FollowUpId equals followUp.Id
  194. join patientRegister in await _patientRegisterRepository.GetQueryableAsync() on followUp.PatientRegisterId equals patientRegister.Id
  195. where smsSend.Id == input.SmsSendId
  196. select new
  197. {
  198. smsSend,
  199. patientRegister
  200. }).FirstOrDefault();
  201. if (smsSendEnt != null)
  202. {
  203. //删除任务
  204. await DeleteCriticalSmsByThirdIdWithPlanSendTimeAsync(smsSendEnt.patientRegister,
  205. smsSendEnt.smsSend.Content,
  206. smsSendEnt.smsSend.MobileTelephone,
  207. new List<string> { DataHelper.ConversionDateToString(smsSendEnt.smsSend.PlanSendDate) });
  208. //删除短信记录
  209. await _smsSendRepository.DeleteAsync(input.SmsSendId, true);
  210. if ((await _smsSendRepository.CountAsync(c => c.FollowUpId == smsSendEnt.smsSend.FollowUpId)) == 0)
  211. {
  212. var followUpEnt = await _followUpRepository.FirstOrDefaultAsync(f => f.Id == smsSendEnt.smsSend.FollowUpId);
  213. if (followUpEnt != null)
  214. {
  215. followUpEnt.IsSmsComplete = 'N';
  216. await _followUpRepository.UpdateAsync(followUpEnt);
  217. }
  218. }
  219. }
  220. }
  221. /// <summary>
  222. /// 推送随访通知短信
  223. /// </summary>
  224. /// <param name="patientRegister"></param>
  225. /// <param name="planSendTimes"></param>
  226. /// <returns></returns>
  227. private async Task PushCriticalSmsAsync(PatientRegister patientRegister, List<string> planSendTimes)
  228. {
  229. var smsThirdInterface = await _thirdInterfaceRepository.FirstOrDefaultAsync(o => o.ThirdInterfaceType ==
  230. ThirdInterfaceTypeFlag.CriticalSmsPush);
  231. if (smsThirdInterface != null && smsThirdInterface.IsActive == 'Y')
  232. {
  233. var parmValue = smsThirdInterface.ParmValue;
  234. var configurationBuilder = new ConfigurationBuilder()
  235. .AddJsonStream(new MemoryStream(System.Text.Encoding.UTF8.GetBytes(parmValue)));
  236. var interfaceConfig = configurationBuilder.Build();
  237. var pushApiAddress = interfaceConfig.GetSection("Interface").GetSection("PushApiAddress").Value;
  238. var isEnableSms = interfaceConfig.GetSection("Interface").GetSection("IsActive").Value;
  239. if (!string.IsNullOrWhiteSpace(isEnableSms)
  240. && isEnableSms == "Y")
  241. {
  242. SmsPlugIns smsPlugIns = new SmsPlugIns(smsThirdInterface.Id);
  243. var smsAppId = Guid.Parse(interfaceConfig.GetSection("Interface").GetSection("SmsAppId").Value);
  244. var smsTypeId = Guid.Parse(interfaceConfig.GetSection("Interface").GetSection("SmsTypeId").Value);
  245. var patientEnt = await _patientRepository.FirstOrDefaultAsync(f => f.Id == patientRegister.PatientId);
  246. if (patientEnt != null)
  247. {
  248. var inputDto = new CreateThirdPartySmsTaskInputDto
  249. {
  250. SmsAppId = smsAppId,
  251. SmsTypeId = smsTypeId,
  252. Content = patientRegister.PatientName,
  253. CountryCode = "86",
  254. MobileTelephone = patientEnt.MobileTelephone,
  255. PersonId = patientRegister.PatientId.ToString(),
  256. PersonName = patientRegister.PatientName,
  257. SenderId = "admin",
  258. SenderName = "体检",
  259. StopTime = "",
  260. TaskCorn = "",
  261. TaskCycleType = '2',
  262. ThirdId = patientRegister.Id.ToString(),
  263. PlanSendTimes = planSendTimes
  264. };
  265. await smsPlugIns.CallSmsAppServiceAsync<CreateThirdPartySmsTaskInputDto, Task>(pushApiAddress, inputDto);
  266. }
  267. }
  268. }
  269. }
  270. /// <summary>
  271. /// 移除随访短信任务
  272. /// </summary>
  273. /// <param name="patientRegister"></param>
  274. /// <param name="Content"></param>
  275. /// <returns></returns>
  276. [RemoteService(false)]
  277. public async Task DeleteCriticalSmsAsync(PatientRegister patientRegister, string Content)
  278. {
  279. var smsThirdInterface = await _thirdInterfaceRepository.FirstOrDefaultAsync(o => o.ThirdInterfaceType ==
  280. ThirdInterfaceTypeFlag.CriticalSmsPush);
  281. if (smsThirdInterface != null && smsThirdInterface.IsActive == 'Y')
  282. {
  283. var parmValue = smsThirdInterface.ParmValue;
  284. var configurationBuilder = new ConfigurationBuilder()
  285. .AddJsonStream(new MemoryStream(System.Text.Encoding.UTF8.GetBytes(parmValue)));
  286. var interfaceConfig = configurationBuilder.Build();
  287. var removeApiAddress = interfaceConfig.GetSection("Interface").GetSection("RemoveApiAddress").Value;
  288. var isEnableSms = interfaceConfig.GetSection("Interface").GetSection("IsActive").Value;
  289. if (!string.IsNullOrWhiteSpace(isEnableSms)
  290. && isEnableSms == "Y")
  291. {
  292. SmsPlugIns smsPlugIns = new SmsPlugIns(smsThirdInterface.Id);
  293. var patientEnt = await _patientRepository.FirstOrDefaultAsync(f => f.Id == patientRegister.PatientId);
  294. if (patientEnt != null)
  295. {
  296. var inputDto = new DeleteThirdPartySmsTaskByThirdIdInputDto
  297. {
  298. Content = Content,
  299. ThirdId = patientRegister.Id.ToString()
  300. };
  301. await smsPlugIns.CallSmsAppServiceAsync<DeleteThirdPartySmsTaskByThirdIdInputDto, Task>(removeApiAddress, inputDto);
  302. }
  303. }
  304. }
  305. }
  306. /// <summary>
  307. /// 删除指定危急值短信任务
  308. /// </summary>
  309. /// <param name="patientRegister"></param>
  310. /// <param name="content"></param>
  311. /// <param name="mobileTelephone"></param>
  312. /// <param name="planSendTimes"></param>
  313. /// <returns></returns>
  314. [RemoteService(false)]
  315. public async Task DeleteCriticalSmsByThirdIdWithPlanSendTimeAsync(PatientRegister patientRegister, string content, string mobileTelephone, List<string> planSendTimes)
  316. {
  317. var smsThirdInterface = await _thirdInterfaceRepository.FirstOrDefaultAsync(o => o.ThirdInterfaceType ==
  318. ThirdInterfaceTypeFlag.CriticalSmsPush);
  319. if (smsThirdInterface != null && smsThirdInterface.IsActive == 'Y')
  320. {
  321. var parmValue = smsThirdInterface.ParmValue;
  322. var configurationBuilder = new ConfigurationBuilder()
  323. .AddJsonStream(new MemoryStream(System.Text.Encoding.UTF8.GetBytes(parmValue)));
  324. var interfaceConfig = configurationBuilder.Build();
  325. var removeApiAddress = interfaceConfig.GetSection("Interface").GetSection("DeleteSpecificTasksApiAddress").Value;
  326. var isEnableSms = interfaceConfig.GetSection("Interface").GetSection("IsActive").Value;
  327. if (!string.IsNullOrWhiteSpace(isEnableSms)
  328. && isEnableSms == "Y")
  329. {
  330. SmsPlugIns smsPlugIns = new SmsPlugIns(smsThirdInterface.Id);
  331. var patientEnt = await _patientRepository.FirstOrDefaultAsync(f => f.Id == patientRegister.PatientId);
  332. if (patientEnt != null)
  333. {
  334. var inputDto = new DeleteThirdPartySmsTaskByThirdIdWithPlanSendTimeInputDto
  335. {
  336. Content = content,
  337. ThirdId = patientRegister.Id.ToString(),
  338. MobileTelephone = mobileTelephone,
  339. PlanSendTimes = planSendTimes
  340. };
  341. await smsPlugIns.CallSmsAppServiceAsync<DeleteThirdPartySmsTaskByThirdIdWithPlanSendTimeInputDto, Task>(removeApiAddress, inputDto);
  342. }
  343. }
  344. }
  345. }
  346. }
  347. }