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.
 
 
 

235 lines
8.6 KiB

using Microsoft.Extensions.Configuration.UserSecrets;
using Shentun.Utilities;
using Shentun.Utilities.Enums;
using Shentun.WebPeis.Models;
using Shentun.WebPeis.SysParmValues;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.Domain.Entities;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Domain.Services;
using Volo.Abp.Identity;
namespace Shentun.WebPeis.Persons
{
public class PersonManager : DomainService
{
private readonly IRepository<Person> _repository;
private readonly IRepository<IdentityUser> _identityUserRepository;
private readonly IdentityUserManager _identityUserManager;
private readonly IRepository<Nation> _nationRepository;
private readonly SysParmValueManager _sysParmValueManager;
private readonly IRepository<PrimarykeyBuilder> _primarykeyBuilderRepository;
public PersonManager(IRepository<Person> repository,
IRepository<IdentityUser> identityUserRepository,
IdentityUserManager identityUserManager,
IRepository<Nation> nationRepository,
SysParmValueManager sysParmValueManager,
IRepository<PrimarykeyBuilder> primarykeyBuilderRepository)
{
_repository = repository;
_identityUserRepository = identityUserRepository;
_identityUserManager = identityUserManager;
_nationRepository = nationRepository;
_sysParmValueManager = sysParmValueManager;
_primarykeyBuilderRepository = primarykeyBuilderRepository;
}
public async Task<UserWithPerson> CreateAsync(
Person entity,
string name,
string? email,
string phone
)
{
await Verify(entity);
Check.NotNullOrWhiteSpace(name, "姓名");
Check.NotNullOrWhiteSpace(phone, "手机");
if (!string.IsNullOrWhiteSpace(entity.IdNo))
{
var existPerson = await _repository.FirstOrDefaultAsync(o => o.IdNo == entity.IdNo);
if (existPerson != null)
{
throw new UserFriendlyException("该身份证号已经在人员信息中注册");
}
entity.BirthDate = ConvertExtr.ToBirthDateByIdNo(entity.IdNo);
}
var users = (from identityUser in await _identityUserRepository.GetQueryableAsync()
join person in await _repository.GetQueryableAsync()
on identityUser.Id equals person.PersonId
where identityUser.PhoneNumber == phone
select
identityUser
).ToList();
if (users.Any())
{
throw new UserFriendlyException("该手机号已经在人员信息中注册");
}
var wechatPerson = await _repository.FindAsync(o => o.WechatOpenId == entity.WechatOpenId
&& !string.IsNullOrWhiteSpace(entity.WechatOpenId));
if (wechatPerson != null)
{
throw new UserFriendlyException("该微信号已经注册");
}
entity.PersonNo = await CreatePersonNoAsync(entity.MedicalCenterId);
if(string.IsNullOrWhiteSpace(email))
{
email = entity.PersonNo + "@qq.com";
}
var user = new IdentityUser(GuidGenerator.Create(), entity.PersonNo, email)
{
Name = name,
};
user.SetPhoneNumber(phone, false);
// await _identityUserManager.CreateAsync(user);
if (!string.IsNullOrWhiteSpace(entity.IdNo))
{
var existPatient = await _repository.FirstOrDefaultAsync(o => o.IdNo == entity.IdNo);
if (existPatient != null)
{
throw new UserFriendlyException("该身份证号已经在人员信息中注册");
}
entity.BirthDate = ConvertExtr.ToBirthDateByIdNo(entity.IdNo);
}
entity.PersonId = user.Id;
entity.SimpleCode = LanguageConverter.GetPYSimpleCode(name);
var userWithPerson = new UserWithPerson()
{
User = user,
Person = entity,
};
return userWithPerson;
}
private async Task Verify(Person entity)
{
Check.NotNull(entity, "entity");
Check.NotNullOrWhiteSpace(entity.IdNo, "身份证号");
Check.NotNullOrWhiteSpace(entity.NationId, "民族");
CheckExter.CheckSex(entity.SexId);
CheckExter.CheckMaritalStatus(entity.MaritalStatusId);
if(string.IsNullOrWhiteSpace(entity.IdTypeId))
{
entity.IdTypeId = "01";
}
if (string.IsNullOrWhiteSpace(entity.CountryCode))
{
entity.CountryCode = "86";
}
if (entity.IsAllowBind == null)
{
entity.IsAllowBind = 'Y';
}
if (entity.MedicalCenterId == null || entity.MedicalCenterId == Guid.Empty)
{
throw new UserFriendlyException("体检中心不能为空");
}
if (!string.IsNullOrEmpty(entity.IdNo))
{
entity.IdNo = entity.IdNo.Trim();
if (entity.IdNo.Length != 18)
{
throw new UserFriendlyException("身份证长度必须为18位");
}
var sexByIdNo = ConvertExtr.ToSexByIdNo(entity.IdNo).ToCharArray();
if (entity.SexId == default(char))
{
entity.SexId = sexByIdNo[0];
}
else
{
if (sexByIdNo[0] != entity.SexId)
{
throw new UserFriendlyException("身份证号解析出的性别与填入的性别不一致");
}
}
}
if (!string.IsNullOrWhiteSpace(entity.NationId))
{
if ((await _nationRepository.GetQueryableAsync()).
Where(o => o.NationId == entity.NationId).Count() == 0)
{
throw new UserFriendlyException("民族ID不存在");
}
}
}
private async Task<string> CreatePersonNoAsync(Guid medicalCenterId)
{
var person_id_rule_prefix = ""; //前缀
var person_id_rule_tail_len = ""; //尾号长度
person_id_rule_tail_len = await _sysParmValueManager.GetSysParmValueAsync(medicalCenterId, "person_id_rule_tail_len");
person_id_rule_prefix = await _sysParmValueManager.GetSysParmValueAsync(medicalCenterId, "person_id_rule_prefix");
if (string.IsNullOrWhiteSpace(person_id_rule_tail_len))
{
throw new UserFriendlyException("人员号尾号长度不能为空");
}
int tailLen;
if (!int.TryParse(person_id_rule_tail_len, out tailLen))
{
throw new UserFriendlyException("人员号尾号长度必须是数字");
};
if (tailLen < 5)
{
throw new UserFriendlyException("人员号尾号长度必须大于5");
}
string maxnum = "1"; //未补位的档案号
var primarykeyBuilderEnt = await _primarykeyBuilderRepository
.FirstOrDefaultAsync(f => f.PrimarykeyBuilderId == "person_no");
if (primarykeyBuilderEnt != null)
{
//获取最新的,加1,并更新数据
maxnum = (Convert.ToInt32(primarykeyBuilderEnt.SerialNo) + 1).ToString();
primarykeyBuilderEnt.SerialNo = maxnum;
await _primarykeyBuilderRepository.UpdateAsync(primarykeyBuilderEnt);
}
else
{
//生成首位,并添加
primarykeyBuilderEnt = new PrimarykeyBuilder
{
PrimarykeyBuilderId = "person_no",
DateString = "",
SerialNo = maxnum
};
await _primarykeyBuilderRepository.InsertAsync(primarykeyBuilderEnt, true);
}
return person_id_rule_prefix + maxnum.PadLeft(Convert.ToInt32(person_id_rule_tail_len), '0');
}
}
}