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.

458 lines
22 KiB

using Microsoft.Extensions.Configuration;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json;
using Shentun.Peis.Enums;
using Shentun.Peis.Models;
using Shentun.Peis.PatientRegisters;
using Shentun.Peis.PlugIns.Sms;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http.Headers;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using NPOI.POIFS.Crypt.Dsig;
using NPOI.SS.Formula.Functions;
namespace Shentun.Peis.ThirdBookingPushs
{
/// <summary>
/// 推送体检信息到人寿
/// </summary>
[Authorize]
[ApiExplorerSettings(GroupName = "Work")]
public class ThirdBookingPushAppService : ApplicationService
{
private readonly IRepository<ThirdInterface, Guid> _thirdInterfaceRepository;
private readonly IRepository<PatientRegister, Guid> _patientRegisterRepository;
private readonly IRepository<ThirdBooking, Guid> _thirdBookingRepository;
private readonly IRepository<CustomerOrgGroup, Guid> _customerOrgGroupRepository;
public ThirdBookingPushAppService(
IRepository<ThirdInterface, Guid> thirdInterfaceRepository,
IRepository<PatientRegister, Guid> patientRegisterRepository,
IRepository<ThirdBooking, Guid> thirdBookingRepository,
IRepository<CustomerOrgGroup, Guid> customerOrgGroupRepository
)
{
_thirdInterfaceRepository = thirdInterfaceRepository;
_patientRegisterRepository = patientRegisterRepository;
_thirdBookingRepository = thirdBookingRepository;
_customerOrgGroupRepository = customerOrgGroupRepository;
}
/// <summary>
/// 推送登记
/// </summary>
/// <returns></returns>
[HttpPost("api/app/ThirdBookingPush/PushRegister")]
public async Task PushRegisterAsync(PatientRegisterIdInputDto input)
{
var thirdBookingInterface = await _thirdInterfaceRepository.FirstOrDefaultAsync(o => o.ThirdInterfaceType ==
ThirdInterfaceTypeFlag.ThirdBooking);
if (thirdBookingInterface != null && thirdBookingInterface.IsActive == 'Y')
{
var parmValue = thirdBookingInterface.ParmValue;
var configurationBuilder = new ConfigurationBuilder()
.AddJsonStream(new MemoryStream(System.Text.Encoding.UTF8.GetBytes(parmValue)));
var interfaceConfig = configurationBuilder.Build();
var baseApiAddress = interfaceConfig.GetSection("Interface").GetSection("BaseApiAddress").Value;
var registerApiAddress = interfaceConfig.GetSection("Interface").GetSection("RegisterApiAddress").Value;
var publicKey = interfaceConfig.GetSection("Interface").GetSection("YuanFuPublicKey").Value;
var privateKey = interfaceConfig.GetSection("Interface").GetSection("MinZhongPrivateKey").Value;
var medicalYear = interfaceConfig.GetSection("Interface").GetSection("MedicalYear").Value;
var isActive = interfaceConfig.GetSection("Interface").GetSection("IsActive").Value;
if (!string.IsNullOrWhiteSpace(isActive)
&& isActive == "Y")
{
var patientRegisterEnt = await _patientRegisterRepository.GetAsync(input.PatientRegisterId);
if (patientRegisterEnt != null
&& !string.IsNullOrWhiteSpace(patientRegisterEnt.ThirdBookingId))
{
var thirdBookingEnt = await _thirdBookingRepository.FirstOrDefaultAsync(f => f.Id == Guid.Parse(patientRegisterEnt.ThirdBookingId));
string iCode = thirdBookingEnt.ICode;
if (thirdBookingEnt.MedicalStatus == '0')
{
var customerOrgGroupEnt = await _customerOrgGroupRepository.GetAsync(g => g.Id == Guid.Parse(thirdBookingEnt.CustomerOrgGroupId));
var pushRegisterInput = new PushRegisterInputDto
{
BOOKINGDATE = thirdBookingEnt.BookingDate.ToString("yyyy-MM-dd"),
BOOKINGDATEDESC = "",
BOOKINGID = patientRegisterEnt.ThirdBookingId,
BOOKINGTIME = thirdBookingEnt.BookingDate.ToString("HH:mm:ss"),
BOOKINGTYPE = "1",
BOOKINGTYPEELSEDESC = "",
CONFIRMORG = "MINZ",
CONFIRMORGCENTERDESC = "民众",
CONFIRMTIME = patientRegisterEnt.CreationTime.ToString("yyyy-MM-dd HH:mm:ss"),
CONFIRMTYPE = "1",
EXAMPACKAGECODE = thirdBookingEnt.CustomerOrgGroupId,
EXAMPACKAGEDESC = customerOrgGroupEnt.DisplayName,
IDNO = thirdBookingEnt.IdNo,
IDTYPE = thirdBookingEnt.IdType,
MOBILE = thirdBookingEnt.Phone,
NAME = patientRegisterEnt.PatientName,
SOURCE = "BjGJ",
YEAR = medicalYear
};
var inputPara = GetEncryptData(JsonConvert.SerializeObject(pushRegisterInput), iCode, publicKey, privateKey);
var resultPara = await CallBookingAppServiceAsync<EncryDataRequstDto, EncryDataResultDto>(baseApiAddress, registerApiAddress, inputPara, "application/json;charset=utf-8");
if (resultPara.FLAG)
{
var decryptDataPara = GetDecryptData<DecryptBaseDto>(resultPara, publicKey, privateKey);
if (decryptDataPara.STATUS == 0)
{
thirdBookingEnt.MedicalStatus = '1';
await _thirdBookingRepository.UpdateAsync(thirdBookingEnt);
}
}
}
}
}
}
}
/// <summary>
/// 推送完成体检数据
/// </summary>
/// <returns></returns>
[HttpPost("api/app/ThirdBookingPush/PushCompletePhysical")]
public async Task PushCompletePhysicalAsync(PatientRegisterIdInputDto input)
{
var thirdBookingInterface = await _thirdInterfaceRepository.FirstOrDefaultAsync(o => o.ThirdInterfaceType ==
ThirdInterfaceTypeFlag.ThirdBooking);
if (thirdBookingInterface != null && thirdBookingInterface.IsActive == 'Y')
{
var parmValue = thirdBookingInterface.ParmValue;
var configurationBuilder = new ConfigurationBuilder()
.AddJsonStream(new MemoryStream(System.Text.Encoding.UTF8.GetBytes(parmValue)));
var interfaceConfig = configurationBuilder.Build();
var baseApiAddress = interfaceConfig.GetSection("Interface").GetSection("BaseApiAddress").Value;
var completeApiAddress = interfaceConfig.GetSection("Interface").GetSection("CompleteApiAddress").Value;
var publicKey = interfaceConfig.GetSection("Interface").GetSection("YuanFuPublicKey").Value;
var privateKey = interfaceConfig.GetSection("Interface").GetSection("MinZhongPrivateKey").Value;
var isActive = interfaceConfig.GetSection("Interface").GetSection("IsActive").Value;
var medicalYear = interfaceConfig.GetSection("Interface").GetSection("MedicalYear").Value;
if (!string.IsNullOrWhiteSpace(isActive)
&& isActive == "Y")
{
var patientRegisterEnt = await _patientRegisterRepository.GetAsync(input.PatientRegisterId);
//已总检才能上传
if (patientRegisterEnt != null
&& !string.IsNullOrWhiteSpace(patientRegisterEnt.ThirdBookingId))
{
var thirdBookingEnt = await _thirdBookingRepository.FirstOrDefaultAsync(f => f.Id == Guid.Parse(patientRegisterEnt.ThirdBookingId));
string iCode = thirdBookingEnt.ICode;
if (thirdBookingEnt.MedicalStatus == '1')
{
var customerOrgGroupEnt = await _customerOrgGroupRepository.GetAsync(g => g.Id == Guid.Parse(thirdBookingEnt.CustomerOrgGroupId));
var pushCompletePhysicalInput = new PushCompletePhysicalInputDto
{
BOOKINGDATE = thirdBookingEnt.BookingDate.ToString("yyyy-MM-dd"),
BOOKINGDATEDESC = "",
BOOKINGID = patientRegisterEnt.ThirdBookingId,
BOOKINGTIME = thirdBookingEnt.BookingDate.ToString("HH:mm:ss"),
BOOKINGTYPE = "1",
BOOKINGTYPEELSEDESC = "",
CONFIRMORG = "MINZ",
CONFIRMORGCENTERDESC = "民众",
CONFIRMTIME = patientRegisterEnt.CreationTime.ToString("yyyy-MM-dd HH:mm:ss"),
CONFIRMTYPE = "1",
EXAMPACKAGECODE = thirdBookingEnt.CustomerOrgGroupId,
EXAMPACKAGEDESC = customerOrgGroupEnt.DisplayName,
IDNO = thirdBookingEnt.IdNo,
IDTYPE = thirdBookingEnt.IdType,
MOBILE = thirdBookingEnt.Phone,
NAME = patientRegisterEnt.PatientName,
SOURCE = "BjGJ",
YEAR = medicalYear,
AGE = patientRegisterEnt.Age == null ? (short)0 : patientRegisterEnt.Age.Value,
BIRTHDAY = patientRegisterEnt.BirthDate != null ? patientRegisterEnt.BirthDate.Value.ToString("yyyy-MM-dd") : "",
EXAMBEGINTIME = patientRegisterEnt.MedicalStartDate != null ? patientRegisterEnt.MedicalStartDate.Value.ToString("yyyy-MM-dd HH:mm:ss") : "",
EXAMCOMPLETETIME = patientRegisterEnt.SummaryDate != null ? patientRegisterEnt.SummaryDate.Value.ToString("yyyy-MM-dd HH:mm:ss") : "",
EXAMID = patientRegisterEnt.Id.ToString(),
GENDER = GetSexId(patientRegisterEnt.SexId)
};
var inputPara = GetEncryptData(JsonConvert.SerializeObject(pushCompletePhysicalInput), iCode, publicKey, privateKey);
var resultPara = await CallBookingAppServiceAsync<EncryDataRequstDto, EncryDataResultDto>(baseApiAddress, completeApiAddress, inputPara, "application/json;charset=utf-8");
if (resultPara.FLAG)
{
var decryptDataPara = GetDecryptData<DecryptBaseDto>(resultPara, publicKey, privateKey);
if (decryptDataPara.STATUS == 0)
{
thirdBookingEnt.MedicalStatus = '2';
await _thirdBookingRepository.UpdateAsync(thirdBookingEnt);
}
}
}
}
}
}
}
/// <summary>
/// 推送体检报告
/// </summary>
/// <returns></returns>
[HttpPost("api/app/ThirdBookingPush/PushReportFile")]
public async Task PushReportFileAsync(PushReportFileInputDto input)
{
var thirdBookingInterface = await _thirdInterfaceRepository.FirstOrDefaultAsync(o => o.ThirdInterfaceType ==
ThirdInterfaceTypeFlag.ThirdBooking);
if (thirdBookingInterface != null && thirdBookingInterface.IsActive == 'Y')
{
var parmValue = thirdBookingInterface.ParmValue;
var configurationBuilder = new ConfigurationBuilder()
.AddJsonStream(new MemoryStream(System.Text.Encoding.UTF8.GetBytes(parmValue)));
var interfaceConfig = configurationBuilder.Build();
var baseApiAddress = interfaceConfig.GetSection("Interface").GetSection("BaseApiAddress").Value;
var completeApiAddress = interfaceConfig.GetSection("Interface").GetSection("CompleteApiAddress").Value;
var publicKey = interfaceConfig.GetSection("Interface").GetSection("YuanFuPublicKey").Value;
var privateKey = interfaceConfig.GetSection("Interface").GetSection("MinZhongPrivateKey").Value;
var aesKey = interfaceConfig.GetSection("Interface").GetSection("AesKey").Value;
var isActive = interfaceConfig.GetSection("Interface").GetSection("IsActive").Value;
var medicalYear = interfaceConfig.GetSection("Interface").GetSection("MedicalYear").Value;
if (!string.IsNullOrWhiteSpace(isActive)
&& isActive == "Y")
{
var patientRegisterEnt = await _patientRegisterRepository.GetAsync(input.PatientRegisterId);
if (patientRegisterEnt != null
&& !string.IsNullOrWhiteSpace(patientRegisterEnt.ThirdBookingId)
&& patientRegisterEnt.CompleteFlag == PatientRegisterCompleteFlag.SumCheck)
{
var thirdBookingEnt = await _thirdBookingRepository.FirstOrDefaultAsync(f => f.Id == Guid.Parse(patientRegisterEnt.ThirdBookingId));
string iCode = thirdBookingEnt.ICode;
if (thirdBookingEnt.MedicalStatus == '2')
{
var customerOrgGroupEnt = await _customerOrgGroupRepository.GetAsync(g => g.Id == Guid.Parse(thirdBookingEnt.CustomerOrgGroupId));
#region 上传文件
byte[] filebytes = Convert.FromBase64String(input.ReportBase64);
string fileDataEncrypt = AesHelper.AESEncrypt(filebytes, aesKey);
string fileSign = AesHelper.GetMD5Hash(fileDataEncrypt);
var fileInputParm = new EncryDataRequstDto
{
DATA = fileDataEncrypt,
SIGN = fileSign,
ICODE = thirdBookingEnt.ICode
};
var fileResultEncry = await CallBookingAppServiceAsync<EncryDataRequstDto, EncryDataResultDto>(baseApiAddress, completeApiAddress, fileInputParm, "multipart/form-data");
if (fileResultEncry.FLAG)
{
//验签
if (fileResultEncry.SIGN == AesHelper.GetMD5Hash(fileResultEncry.DATA))
{
//解密
string fileResultStr = AesHelper.AESDecrypt(fileResultEncry.DATA, aesKey); //报文ID
#region 上传报告
var pushMedicalReportInput = new PushMedicalReportInputDto
{
EXAMPACKAGECODE = thirdBookingEnt.CustomerOrgGroupId,
IDNO = thirdBookingEnt.IdNo,
IDTYPE = thirdBookingEnt.IdType,
NAME = patientRegisterEnt.PatientName,
SOURCE = "BjGJ",
YEAR = medicalYear,
BUSINESSID = patientRegisterEnt.Id.ToString(),
JSONBASE64 = "",
ORGCODE = "MINZ",
PDFBASE64 = "",
PDFFILEID = fileResultStr,
PUSHTIME = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
};
var pushMedicalReportInputEncrypt = GetEncryptData(JsonConvert.SerializeObject(pushMedicalReportInput), iCode, publicKey, privateKey);
var reportResultPara = await CallBookingAppServiceAsync<EncryDataRequstDto, EncryDataResultDto>(baseApiAddress, completeApiAddress, pushMedicalReportInputEncrypt, "application/json;charset=utf-8");
if (reportResultPara.FLAG)
{
var decryptDataPara = GetDecryptData<PushReportDecryptDto>(reportResultPara, publicKey, privateKey);
if (decryptDataPara.FLAG == true)
{
thirdBookingEnt.MedicalStatus = '3';
await _thirdBookingRepository.UpdateAsync(thirdBookingEnt);
}
}
#endregion
}
}
#endregion
}
}
}
}
}
/// <summary>
/// 发起请求
/// </summary>
/// <typeparam name="TInput"></typeparam>
/// <typeparam name="TOut"></typeparam>
/// <param name="baseApiAddress"></param>
/// <param name="url"></param>
/// <param name="data"></param>
/// <param name="contentType"></param>
/// <param name="method"></param>
/// <returns></returns>
private async Task<TOut> CallBookingAppServiceAsync<TInput, TOut>(string baseApiAddress, string url, TInput data, string contentType, string method = "post")
{
using (var httpClientHandler = new HttpClientHandler())
{
using (var httpClient = new HttpClient(httpClientHandler))
{
httpClient.BaseAddress = new Uri(baseApiAddress);
httpClient.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));//设置accept标头,告诉JSON是可接受的响应类型
var sendData = JsonConvert.SerializeObject(data);
using (HttpContent httpContent = new StringContent(sendData))
{
httpContent.Headers.ContentType = new MediaTypeHeaderValue(contentType);
HttpResponseMessage response = null;
if (method == "post")
{
response = await httpClient.PostAsync(url, httpContent);
}
else
{
response = await httpClient.GetAsync(url);
}
string result;
if (!response.IsSuccessStatusCode)
{
result = response.Content.ReadAsStringAsync().Result;
//throw new Exception("http通信错误:" + response.StatusCode + ",结果:" + result);
}
result = await response.Content.ReadAsStringAsync();
var resultDto = JsonConvert.DeserializeObject<TOut>(result);
return resultDto;
}
}
}
}
/// <summary>
/// 生成请求密文数据
/// </summary>
/// <param name="resultJson"></param>
/// <param name="ICODE"></param>
/// <param name="publicKey"></param>
/// <param name="privateKey"></param>
/// <returns></returns>
private static EncryDataRequstDto GetEncryptData(string resultJson, string ICODE, string publicKey, string privateKey)
{
string data = newRsaHelper.RSAEncrypt(resultJson, publicKey);
string sign = newRsaHelper.SignData(data, privateKey);
var result = new EncryDataRequstDto
{
DATA = data,
SIGN = sign,
ICODE = ICODE
};
return result;
}
/// <summary>
/// 返回数据密文解密
/// </summary>
/// <param name="encryDataResultDto">返回的密文对象</param>
/// <param name="publicKey">对方公钥</param>
/// <param name="privateKey">自己私钥</param>
/// <returns></returns>
private static TOut GetDecryptData<TOut>(EncryDataResultDto encryDataResultDto, string publicKey, string privateKey)
{
bool isVerify = newRsaHelper.VerifySignature(encryDataResultDto.DATA, Convert.FromBase64String(encryDataResultDto.SIGN), publicKey);
if (isVerify)
{
string resultStr = newRsaHelper.RSADecrypt(encryDataResultDto.DATA, privateKey);
TOut resultDto = JsonConvert.DeserializeObject<TOut>(resultStr);
return resultDto;
}
else
{
return default(TOut);
}
}
/// <summary>
/// 转换性别
/// </summary>
/// <returns></returns>
private string GetSexId(char SexId)
{
string SexName = "";
if (SexId == 'M')
{
SexName = "0";
}
if (SexId == 'F')
{
SexName = "1";
}
return SexName;
}
}
}