using Cronos; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; using NPOI.OpenXmlFormats.Wordprocessing; using Shentun.Peis.Enums; using Shentun.Peis.Models; using Shentun.Peis.PlugIns.Sms; using Shentun.Peis.SmsSends; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using Volo.Abp; using Volo.Abp.Application.Services; using Volo.Abp.Domain.Repositories; namespace Shentun.Peis.SmsSends { /// /// 短信随访记录 /// [ApiExplorerSettings(GroupName = "Work")] [Authorize] public class SmsSendAppService : ApplicationService { private readonly IRepository _smsSendRepository; private readonly CacheService _cacheService; private readonly IRepository _followUpRepository; private readonly IRepository _patientRegisterRepository; private readonly IRepository _patientRepository; private readonly IRepository _thirdInterfaceRepository; public SmsSendAppService( CacheService cacheService, IRepository followUpRepository, IRepository patientRegisterRepository, IRepository smsSendRepository, IRepository patientRepository, IRepository thirdInterfaceRepository) { _cacheService = cacheService; _followUpRepository = followUpRepository; _patientRegisterRepository = patientRegisterRepository; _smsSendRepository = smsSendRepository; _patientRepository = patientRepository; _thirdInterfaceRepository = thirdInterfaceRepository; } /// /// 获取短信随访记录信息 /// /// /// [HttpPost("api/app/SmsSend/Get")] public async Task GetAsync(SmsSendIdInputDto input) { var smsSendEnt = await _smsSendRepository.GetAsync(input.SmsSendId); var entityDto = ObjectMapper.Map(smsSendEnt); entityDto.CreatorName = await _cacheService.GetSurnameAsync(entityDto.CreatorId); entityDto.LastModifierName = await _cacheService.GetSurnameAsync(entityDto.LastModifierId); return entityDto; } /// /// 获取短信随访记录信息 /// /// /// [HttpPost("api/app/SmsSend/GetList")] public async Task> GetListAsync(SmsSendListInputDto input) { var query = from smsSend in await _smsSendRepository.GetQueryableAsync() orderby smsSend.FollowUpId, smsSend.PlanSendDate select new { smsSend }; if (input.FollowUpId != null) { query = query.Where(m => m.smsSend.FollowUpId == input.FollowUpId); } if (!string.IsNullOrWhiteSpace(input.PatientName)) { query = query.Where(m => !string.IsNullOrWhiteSpace(m.smsSend.PatientName) && m.smsSend.PatientName.Contains(input.PatientName)); } if (!string.IsNullOrWhiteSpace(input.MobileTelephone)) { query = query.Where(m => !string.IsNullOrWhiteSpace(m.smsSend.MobileTelephone) && m.smsSend.MobileTelephone.Contains(input.MobileTelephone)); } if (input.IsComplete != null) { query = query.Where(m => m.smsSend.IsComplete == input.IsComplete); } if (!string.IsNullOrWhiteSpace(input.StartDate) && !string.IsNullOrWhiteSpace(input.EndDate)) { query = query.Where(m => m.smsSend.PlanSendDate >= Convert.ToDateTime(input.StartDate) && m.smsSend.PlanSendDate <= Convert.ToDateTime(input.EndDate).AddDays(1)); } var entListDto = query.ToList().Select(s => new SmsSendDto { FollowUpId = s.smsSend.FollowUpId, CreationTime = s.smsSend.CreationTime, CreatorId = s.smsSend.CreatorId, Id = s.smsSend.Id, IsComplete = s.smsSend.IsComplete, LastModificationTime = s.smsSend.LastModificationTime, LastModifierId = s.smsSend.LastModifierId, CreatorName = _cacheService.GetSurnameAsync(s.smsSend.CreatorId).GetAwaiter().GetResult(), LastModifierName = _cacheService.GetSurnameAsync(s.smsSend.LastModifierId).GetAwaiter().GetResult(), PatientName = s.smsSend.PatientName, Content = s.smsSend.Content, MobileTelephone = s.smsSend.MobileTelephone, PlanSendDate = DataHelper.ConversionDateToString(s.smsSend.PlanSendDate), PatientId = s.smsSend.PatientId, SmsTypeId = s.smsSend.SmsTypeId }).ToList(); return entListDto; } /// /// 自动生成短信随访记录 /// /// /// [HttpPost("api/app/SmsSend/AutoCreate")] public async Task AutoCreateAsync(AutoCreateSmsSendDto input) { if (string.IsNullOrWhiteSpace(input.StartDate)) { throw new UserFriendlyException("开始时间不能为空"); } var isSmsSend = await _smsSendRepository.CountAsync(c => c.FollowUpId == input.FollowUpId); if (isSmsSend > 0) { throw new UserFriendlyException("已存在短信随访记录,不允许重复生成"); } var followUpEnt = await _followUpRepository.FirstOrDefaultAsync(f => f.Id == input.FollowUpId); if (followUpEnt == null) { throw new UserFriendlyException("随访ID不正确"); } List smsSendList = new List(); var patientRegisterEnt = (from followUp in await _followUpRepository.GetQueryableAsync() join patientRegister in await _patientRegisterRepository.GetQueryableAsync() on followUp.PatientRegisterId equals patientRegister.Id join patient in await _patientRepository.GetQueryableAsync() on patientRegister.PatientId equals patient.Id where followUp.Id == input.FollowUpId select new { patientRegister = patientRegister, patientName = patientRegister.PatientName, mobileTelephone = patient.MobileTelephone, patientId = patient.Id }).FirstOrDefault(); if (patientRegisterEnt == null) { throw new UserFriendlyException("随访数据不存在"); } for (int i = 0; i < input.GenerateCount; i++) { DateTime planFollowDate = Convert.ToDateTime(input.StartDate); planFollowDate = planFollowDate.AddDays(i * input.IntervalDays); var smsSendEntity = new SmsSend(GuidGenerator.Create()) { FollowUpId = input.FollowUpId, Content = string.IsNullOrWhiteSpace(input.Content) ? patientRegisterEnt.patientName : input.Content, MobileTelephone = patientRegisterEnt.mobileTelephone, PatientName = patientRegisterEnt.patientName, PatientId = patientRegisterEnt.patientId, PlanSendDate = planFollowDate, SmsTypeId = "01", IsComplete = 'Y' }; smsSendList.Add(smsSendEntity); } if (smsSendList.Any()) { await _smsSendRepository.InsertManyAsync(smsSendList); followUpEnt.IsPhoneComplete = 'Y'; await _followUpRepository.UpdateAsync(followUpEnt); //生成短信平台推送计划 await PushCriticalSmsAsync(patientRegisterEnt.patientRegister, smsSendList.Select(s => DataHelper.ConversionDateToString(s.PlanSendDate)).ToList()); } } /// /// 删除 /// /// /// [HttpPost("api/app/SmsSend/Delete")] public async Task DeleteAsync(SmsSendIdInputDto input) { var smsSendEnt = (from smsSend in await _smsSendRepository.GetQueryableAsync() join followUp in await _followUpRepository.GetQueryableAsync() on smsSend.FollowUpId equals followUp.Id join patientRegister in await _patientRegisterRepository.GetQueryableAsync() on followUp.PatientRegisterId equals patientRegister.Id where smsSend.Id == input.SmsSendId select new { smsSend, patientRegister }).FirstOrDefault(); if (smsSendEnt != null) { //删除任务 await DeleteCriticalSmsByThirdIdWithPlanSendTimeAsync(smsSendEnt.patientRegister, smsSendEnt.smsSend.Content, smsSendEnt.smsSend.MobileTelephone, new List { DataHelper.ConversionDateToString(smsSendEnt.smsSend.PlanSendDate) }); //删除短信记录 await _smsSendRepository.DeleteAsync(input.SmsSendId, true); if ((await _smsSendRepository.CountAsync(c => c.FollowUpId == smsSendEnt.smsSend.FollowUpId)) == 0) { var followUpEnt = await _followUpRepository.FirstOrDefaultAsync(f => f.Id == smsSendEnt.smsSend.FollowUpId); if (followUpEnt != null) { followUpEnt.IsSmsComplete = 'N'; await _followUpRepository.UpdateAsync(followUpEnt); } } } } /// /// 推送随访通知短信 /// /// /// /// private async Task PushCriticalSmsAsync(PatientRegister patientRegister, List planSendTimes) { var smsThirdInterface = await _thirdInterfaceRepository.FirstOrDefaultAsync(o => o.ThirdInterfaceType == ThirdInterfaceTypeFlag.CriticalSmsPush); if (smsThirdInterface != null && smsThirdInterface.IsActive == 'Y') { var parmValue = smsThirdInterface.ParmValue; var configurationBuilder = new ConfigurationBuilder() .AddJsonStream(new MemoryStream(System.Text.Encoding.UTF8.GetBytes(parmValue))); var interfaceConfig = configurationBuilder.Build(); var pushApiAddress = interfaceConfig.GetSection("Interface").GetSection("PushApiAddress").Value; var isEnableSms = interfaceConfig.GetSection("Interface").GetSection("IsActive").Value; if (!string.IsNullOrWhiteSpace(isEnableSms) && isEnableSms == "Y") { SmsPlugIns smsPlugIns = new SmsPlugIns(smsThirdInterface.Id); var smsAppId = Guid.Parse(interfaceConfig.GetSection("Interface").GetSection("SmsAppId").Value); var smsTypeId = Guid.Parse(interfaceConfig.GetSection("Interface").GetSection("SmsTypeId").Value); var patientEnt = await _patientRepository.FirstOrDefaultAsync(f => f.Id == patientRegister.PatientId); if (patientEnt != null) { var inputDto = new CreateThirdPartySmsTaskInputDto { SmsAppId = smsAppId, SmsTypeId = smsTypeId, Content = patientRegister.PatientName, CountryCode = "86", MobileTelephone = patientEnt.MobileTelephone, PersonId = patientRegister.PatientId.ToString(), PersonName = patientRegister.PatientName, SenderId = "admin", SenderName = "体检", StopTime = "", TaskCorn = "", TaskCycleType = '2', ThirdId = patientRegister.Id.ToString(), PlanSendTimes = planSendTimes }; await smsPlugIns.CallSmsAppServiceAsync(pushApiAddress, inputDto); } } } } /// /// 移除随访短信任务 /// /// /// /// [RemoteService(false)] public async Task DeleteCriticalSmsAsync(PatientRegister patientRegister, string Content) { var smsThirdInterface = await _thirdInterfaceRepository.FirstOrDefaultAsync(o => o.ThirdInterfaceType == ThirdInterfaceTypeFlag.CriticalSmsPush); if (smsThirdInterface != null && smsThirdInterface.IsActive == 'Y') { var parmValue = smsThirdInterface.ParmValue; var configurationBuilder = new ConfigurationBuilder() .AddJsonStream(new MemoryStream(System.Text.Encoding.UTF8.GetBytes(parmValue))); var interfaceConfig = configurationBuilder.Build(); var removeApiAddress = interfaceConfig.GetSection("Interface").GetSection("RemoveApiAddress").Value; var isEnableSms = interfaceConfig.GetSection("Interface").GetSection("IsActive").Value; if (!string.IsNullOrWhiteSpace(isEnableSms) && isEnableSms == "Y") { SmsPlugIns smsPlugIns = new SmsPlugIns(smsThirdInterface.Id); var patientEnt = await _patientRepository.FirstOrDefaultAsync(f => f.Id == patientRegister.PatientId); if (patientEnt != null) { var inputDto = new DeleteThirdPartySmsTaskByThirdIdInputDto { Content = Content, ThirdId = patientRegister.Id.ToString() }; await smsPlugIns.CallSmsAppServiceAsync(removeApiAddress, inputDto); } } } } /// /// 删除指定危急值短信任务 /// /// /// /// /// /// [RemoteService(false)] public async Task DeleteCriticalSmsByThirdIdWithPlanSendTimeAsync(PatientRegister patientRegister, string content, string mobileTelephone, List planSendTimes) { var smsThirdInterface = await _thirdInterfaceRepository.FirstOrDefaultAsync(o => o.ThirdInterfaceType == ThirdInterfaceTypeFlag.CriticalSmsPush); if (smsThirdInterface != null && smsThirdInterface.IsActive == 'Y') { var parmValue = smsThirdInterface.ParmValue; var configurationBuilder = new ConfigurationBuilder() .AddJsonStream(new MemoryStream(System.Text.Encoding.UTF8.GetBytes(parmValue))); var interfaceConfig = configurationBuilder.Build(); var removeApiAddress = interfaceConfig.GetSection("Interface").GetSection("DeleteSpecificTasksApiAddress").Value; var isEnableSms = interfaceConfig.GetSection("Interface").GetSection("IsActive").Value; if (!string.IsNullOrWhiteSpace(isEnableSms) && isEnableSms == "Y") { SmsPlugIns smsPlugIns = new SmsPlugIns(smsThirdInterface.Id); var patientEnt = await _patientRepository.FirstOrDefaultAsync(f => f.Id == patientRegister.PatientId); if (patientEnt != null) { var inputDto = new DeleteThirdPartySmsTaskByThirdIdWithPlanSendTimeInputDto { Content = content, ThirdId = patientRegister.Id.ToString(), MobileTelephone = mobileTelephone, PlanSendTimes = planSendTimes }; await smsPlugIns.CallSmsAppServiceAsync(removeApiAddress, inputDto); } } } } } }