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.

251 lines
9.9 KiB

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;
using Shentun.Peis.Enums;
using Shentun.Peis.Models;
using Shentun.Peis.PlugIns.Sms;
using SqlSugar;
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.ThirdBookings
{
/// <summary>
/// 第三方预约记录
/// </summary>
[Authorize]
[ApiExplorerSettings(GroupName = "Work")]
public class ThirdBookingAppService : ApplicationService
{
private readonly IRepository<ThirdBooking, Guid> _thirdBookingRepository;
private readonly IRepository<ThirdInterface, Guid> _thirdInterfaceRepository;
private readonly IRepository<CustomerOrg, Guid> _customerOrgRepository;
private readonly CacheService _cacheService;
public ThirdBookingAppService(
IRepository<ThirdBooking, Guid> thirdBookingRepository,
IRepository<ThirdInterface, Guid> thirdInterfaceRepository,
IRepository<CustomerOrg, Guid> customerOrgRepository,
CacheService cacheService)
{
_thirdBookingRepository = thirdBookingRepository;
_thirdInterfaceRepository = thirdInterfaceRepository;
_customerOrgRepository = customerOrgRepository;
_cacheService = cacheService;
}
/// <summary>
/// 获取人寿预约记录
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost("api/app/ThirdBooking/GetThirdBookingList")]
public async Task<List<GetThirdBookingListDto>> GetThirdBookingListAsync(GetThirdBookingListInputDto input)
{
Guid? customerOrgRegisterId = null;
Guid? customerOrgId = null;
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 isActive = interfaceConfig.GetSection("Interface").GetSection("IsActive").Value;
var customerOrgRegisterIdPara = interfaceConfig.GetSection("Interface").GetSection("CustomerOrgRegisterId").Value;
var customerOrgIdPara = interfaceConfig.GetSection("Interface").GetSection("CustomerOrgId").Value;
if (!string.IsNullOrWhiteSpace(isActive)
&& isActive == "Y")
{
customerOrgRegisterId = Guid.Parse(customerOrgRegisterIdPara);
customerOrgId = Guid.Parse(customerOrgIdPara);
}
}
else
{
throw new UserFriendlyException("请开启第三方预约");
}
var query = await _thirdBookingRepository.GetQueryableAsync();
if (!string.IsNullOrWhiteSpace(input.StartDate) && !string.IsNullOrWhiteSpace(input.StartDate))
{
query = query.Where(m => m.BookingDate >= Convert.ToDateTime(input.StartDate) &&
m.BookingDate < Convert.ToDateTime(input.EndDate).AddDays(1));
}
if (!string.IsNullOrWhiteSpace(input.KeyWord))
{
query = query.Where(m => m.PatientName == input.KeyWord
|| m.IdNo == input.KeyWord
|| m.Phone == input.KeyWord
|| m.ChildCompanyName == input.KeyWord
|| m.DepartmentName == input.KeyWord);
}
if (input.MedicalStatus != null)
{
query = query.Where(m => m.MedicalStatus == input.MedicalStatus);
}
var customerOrgList = await _customerOrgRepository.GetListAsync();
var entListDto = query.ToList().Select(s => new GetThirdBookingListDto
{
ThirdBookingId = s.Id,
Age = s.Age,
BookingDate = s.BookingDate,
CustomerOrgGroupId = s.CustomerOrgGroupId,
IdNo = s.IdNo,
MedicalStatus = s.MedicalStatus,
PatientName = s.PatientName,
Phone = s.Phone,
SexId = GetSexId(s.SexName),
ChildCompanyName = s.ChildCompanyName,
DepartmentName = s.DepartmentName,
CustomerOrgId = GetCustomerOrgId(customerOrgList, customerOrgId, s.ChildCompanyName, s.DepartmentName),
CustomerOrgRegisterId = customerOrgRegisterId,
PositionName = s.PositionName,
Position2 = s.Position2
}).ToList();
foreach (var item in entListDto)
{
Guid customerOrgGroupId;
if (!Guid.TryParse(item.CustomerOrgGroupId, out customerOrgGroupId))
{
customerOrgGroupId = Guid.Empty;
}
if (customerOrgGroupId != Guid.Empty)
{
item.CustomerOrgGroupName = _cacheService.GetCustomerOrgGroupAsync(customerOrgGroupId).GetAwaiter().GetResult().DisplayName;
}
if (item.CustomerOrgId != null && item.CustomerOrgId != Guid.Empty)
{
item.CustomerOrgName = _cacheService.GetCustomerOrgAsync(item.CustomerOrgId.Value).GetAwaiter().GetResult().DisplayName;
}
}
return entListDto;
}
/// <summary>
/// 导入体检预约
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost("api/app/ThirdBooking/CreateBookingMedicalByExcel")]
public async Task CreateBookingMedicalByExcelAsync(CreateBookingMedicalByExcelInputDto input)
{
if (string.IsNullOrWhiteSpace(input.PatientName))
throw new UserFriendlyException("名字不能为空");
if (string.IsNullOrWhiteSpace(input.BookingDate))
throw new UserFriendlyException("日期不能为空");
if (!string.IsNullOrWhiteSpace(input.MaritalStatus)
&& input.MaritalStatus != "0"
&& input.MaritalStatus != "1")
{
throw new UserFriendlyException("婚姻状况格式不正确");
}
if (!string.IsNullOrWhiteSpace(input.EmpStatus)
&& input.EmpStatus != "0"
&& input.EmpStatus != "1")
{
throw new UserFriendlyException("是否在职格式不正确");
}
var thirdBookingEnt = new ThirdBooking(GuidGenerator.Create())
{
Age = input.Age,
BookingDate = Convert.ToDateTime(input.BookingDate),
ChildCompanyName = input.ChildCompanyName,
CustomerOrgGroupId = input.CustomerOrgGroupId,
DepartmentName = input.DepartmentName,
EmpStatus = input.EmpStatus,
ICode = "",
IdNo = input.IdNo,
IdType = "0",
MaritalStatus = input.MaritalStatus,
MedicalStatus = '0',
PatientName = input.PatientName,
Phone = input.Phone,
PositionName = input.PositionName,
SexName = input.SexName,
SourceChannel = "1",
BookingType = "4",
ConfirmType = "2",
ThirdMedicalCenterId = input.ThirdMedicalCenterId
};
await _thirdBookingRepository.InsertAsync(thirdBookingEnt);
}
/// <summary>
/// 转换性别
/// </summary>
/// <returns></returns>
private static char GetSexId(string SexName)
{
char SexId = 'U';
if (SexName == "0" || SexName == "男")
{
SexId = 'M';
}
if (SexName == "1" || SexName == "女")
{
SexId = 'F';
}
return SexId;
}
/// <summary>
/// 获取部门ID
/// </summary>
/// <param name="customerOrgList"></param>
/// <param name="topCustomerOrgId">一级单位ID 集团ID</param>
/// <param name="childCompanyName">子公司名称 预约填的</param>
/// <param name="departmentName">部门名称 部门</param>
/// <returns></returns>
private static Guid? GetCustomerOrgId(List<CustomerOrg> customerOrgList, Guid? topCustomerOrgId, string childCompanyName, string departmentName)
{
if (topCustomerOrgId != null)
{
if (!string.IsNullOrWhiteSpace(childCompanyName))
{
var childCompanyEnt = customerOrgList.FirstOrDefault(f => f.ParentId == topCustomerOrgId && f.DisplayName == childCompanyName);
if (childCompanyEnt != null)
{
topCustomerOrgId = childCompanyEnt.Id;
if (!string.IsNullOrWhiteSpace(departmentName))
{
var departmentNameEnt = customerOrgList.FirstOrDefault(f => f.ParentId == topCustomerOrgId && f.DisplayName == departmentName);
if (departmentNameEnt != null)
{
topCustomerOrgId = departmentNameEnt.Id;
}
}
}
}
}
return topCustomerOrgId;
}
}
}