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

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
{
/// <summary>
/// 短信随访记录
/// </summary>
[ApiExplorerSettings(GroupName = "Work")]
[Authorize]
public class SmsSendAppService : ApplicationService
{
private readonly IRepository<SmsSend, Guid> _smsSendRepository;
private readonly CacheService _cacheService;
private readonly IRepository<FollowUp, Guid> _followUpRepository;
private readonly IRepository<PatientRegister, Guid> _patientRegisterRepository;
private readonly IRepository<Patient, Guid> _patientRepository;
private readonly IRepository<ThirdInterface, Guid> _thirdInterfaceRepository;
public SmsSendAppService(
CacheService cacheService,
IRepository<FollowUp, Guid> followUpRepository,
IRepository<PatientRegister, Guid> patientRegisterRepository,
IRepository<SmsSend, Guid> smsSendRepository,
IRepository<Patient, Guid> patientRepository,
IRepository<ThirdInterface, Guid> thirdInterfaceRepository)
{
_cacheService = cacheService;
_followUpRepository = followUpRepository;
_patientRegisterRepository = patientRegisterRepository;
_smsSendRepository = smsSendRepository;
_patientRepository = patientRepository;
_thirdInterfaceRepository = thirdInterfaceRepository;
}
/// <summary>
/// 获取短信随访记录信息
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost("api/app/SmsSend/Get")]
public async Task<SmsSendDto> GetAsync(SmsSendIdInputDto input)
{
var smsSendEnt = await _smsSendRepository.GetAsync(input.SmsSendId);
var entityDto = ObjectMapper.Map<SmsSend, SmsSendDto>(smsSendEnt);
entityDto.CreatorName = await _cacheService.GetSurnameAsync(entityDto.CreatorId);
entityDto.LastModifierName = await _cacheService.GetSurnameAsync(entityDto.LastModifierId);
return entityDto;
}
/// <summary>
/// 获取短信随访记录信息
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost("api/app/SmsSend/GetList")]
public async Task<List<SmsSendDto>> 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;
}
/// <summary>
/// 自动生成短信随访记录
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[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<SmsSend> smsSendList = new List<SmsSend>();
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());
}
}
/// <summary>
/// 删除
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[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<string> { 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);
}
}
}
}
/// <summary>
/// 推送随访通知短信
/// </summary>
/// <param name="patientRegister"></param>
/// <param name="planSendTimes"></param>
/// <returns></returns>
private async Task PushCriticalSmsAsync(PatientRegister patientRegister, List<string> 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<CreateThirdPartySmsTaskInputDto, Task>(pushApiAddress, inputDto);
}
}
}
}
/// <summary>
/// 移除随访短信任务
/// </summary>
/// <param name="patientRegister"></param>
/// <param name="Content"></param>
/// <returns></returns>
[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<DeleteThirdPartySmsTaskByThirdIdInputDto, Task>(removeApiAddress, inputDto);
}
}
}
}
/// <summary>
/// 删除指定危急值短信任务
/// </summary>
/// <param name="patientRegister"></param>
/// <param name="content"></param>
/// <param name="mobileTelephone"></param>
/// <param name="planSendTimes"></param>
/// <returns></returns>
[RemoteService(false)]
public async Task DeleteCriticalSmsByThirdIdWithPlanSendTimeAsync(PatientRegister patientRegister, string content, string mobileTelephone, List<string> 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<DeleteThirdPartySmsTaskByThirdIdWithPlanSendTimeInputDto, Task>(removeApiAddress, inputDto);
}
}
}
}
}
}