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.

447 lines
17 KiB

using NPOI.POIFS.Properties;
using NPOI.SS.Formula.Functions;
using Shentun.Peis.ContactMethods;
using Shentun.Peis.ContactPersons;
using Shentun.Peis.CustomerOrgGroups;
using Shentun.Peis.CustomerOrgRegisters;
using Shentun.Peis.Enums;
using Shentun.Peis.HelperDto;
using Shentun.Peis.Models;
using Shentun.Peis.OrganizationUnits;
using Shentun.Utilities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Domain.Services;
namespace Shentun.Peis.CustomerOrgs
{
/// <summary>
/// 团检单位设置
/// </summary>
public class CustomerOrgManager : DomainService
{
private readonly IRepository<CustomerOrg, Guid> _repository;
private readonly IRepository<CustomerOrgType, Guid> _customerOrgTypeRepository;
private readonly IRepository<PatientRegister, Guid> _patientRegisterRepository;
private readonly IRepository<CustomerOrgRegister, Guid> _customerOrgRegisterRepository;
private readonly IRepository<CustomerOrgGroup, Guid> _customerOrgGroupRepository;
private readonly IRepository<ContactPerson, Guid> _contactPersonRepository;
private readonly CustomerOrgRegisterManager _customerOrgRegisterManager;
private readonly CustomerOrgGroupManager _customerOrgGroupManager;
private readonly ContactPersonManager _contactPersonManager;
private readonly PeisOrganizationUnitManager _peisOrganizationUnitManager;
public CustomerOrgManager(
IRepository<CustomerOrg, Guid> repository,
IRepository<PatientRegister, Guid> patientRegisterRepository,
IRepository<CustomerOrgRegister, Guid> customerOrgRegisterRepository,
IRepository<CustomerOrgGroup, Guid> customerOrgGroupRepository,
IRepository<ContactPerson, Guid> contactPersonRepository,
CustomerOrgRegisterManager customerOrgRegisterManager,
CustomerOrgGroupManager customerOrgGroupManager,
ContactPersonManager contactPersonManager,
PeisOrganizationUnitManager peisOrganizationUnitManager,
IRepository<CustomerOrgType, Guid> customerOrgTypeRepository
)
{
_repository = repository;
this._patientRegisterRepository = patientRegisterRepository;
this._customerOrgRegisterRepository = customerOrgRegisterRepository;
this._customerOrgGroupRepository = customerOrgGroupRepository;
this._contactPersonRepository = contactPersonRepository;
this._customerOrgRegisterManager = customerOrgRegisterManager;
this._customerOrgGroupManager = customerOrgGroupManager;
this._contactPersonManager = contactPersonManager;
_peisOrganizationUnitManager = peisOrganizationUnitManager;
_customerOrgTypeRepository = customerOrgTypeRepository;
}
/// <summary>
/// 创建
/// </summary>
/// <param name="entity"></param>
/// <returns></returns>
public async Task<CustomerOrg> CreateAsync(
CustomerOrg entity
)
{
if (entity.OrgTypeId == Guid.Empty)
{
entity.OrgTypeId = (await _customerOrgTypeRepository.GetListAsync()).FirstOrDefault().Id;
}
await Verify(entity);
#region 验证同名
if (await _repository.FirstOrDefaultAsync(m => m.DisplayName == entity.DisplayName && m.ParentId == entity.ParentId) != null)
{
if (entity.ParentId == null)
{
throw new UserFriendlyException("单位:" + entity.DisplayName + "已存在");
}
else
{
throw new UserFriendlyException("同级目录下已存在:" + entity.DisplayName + "部门");
}
}
#endregion
return new CustomerOrg(GuidGenerator.Create())
{
DisplayName = entity.DisplayName,
SimpleCode = LanguageConverter.GetPYSimpleCode(entity.DisplayName),
DisplayOrder = await EntityHelper.CreateMaxDisplayOrder<CustomerOrg>(_repository),
Accounts = entity.Accounts,
Address = entity.Address,
Bank = entity.Bank,
Fax = entity.Fax,
InvoiceName = entity.InvoiceName,
IsLock = entity.IsLock,
MedicalCenterId = entity.MedicalCenterId,
OrgTypeId = entity.OrgTypeId,
ParentId = entity.ParentId,
PathCode = await CreatePathCode(entity.ParentId),
PostalCode = entity.PostalCode,
Remark = entity.Remark,
ShortName = entity.ShortName,
IsActive = entity.IsActive,
Telephone = entity.Telephone,
CountryOrgCode = entity.CountryOrgCode,
SalesPerson = entity.SalesPerson,
SalesPersonPhone = entity.SalesPersonPhone
};
}
/// <summary>
/// 更新
/// </summary>
/// <param name="sourceEntity"></param>
/// <param name="targetEntity"></param>
/// <returns></returns>
public async Task UpdateAsync(
CustomerOrg sourceEntity,
CustomerOrg targetEntity
)
{
DataHelper.CheckEntityIsNull(targetEntity);
await Verify(sourceEntity);
if (sourceEntity.DisplayName != targetEntity.DisplayName)
{
if (await _repository.FirstOrDefaultAsync(m => m.DisplayName == sourceEntity.DisplayName && m.ParentId == sourceEntity.ParentId) != null)
{
if (sourceEntity.ParentId == null)
{
throw new UserFriendlyException("单位:" + sourceEntity.DisplayName + "已存在");
}
else
{
throw new UserFriendlyException("同级目录下已存在:" + sourceEntity.DisplayName + "部门");
}
}
targetEntity.DisplayName = sourceEntity.DisplayName;
targetEntity.SimpleCode = LanguageConverter.GetPYSimpleCode(targetEntity.DisplayName);
}
if (sourceEntity.ParentId != null && sourceEntity.ParentId != targetEntity.ParentId)
{
throw new UserFriendlyException("ParentId参数不能修改");
}
if (!string.IsNullOrWhiteSpace(sourceEntity.PathCode) && sourceEntity.PathCode != targetEntity.PathCode)
{
throw new UserFriendlyException("PathCode参数不能修改");
}
targetEntity.Accounts = sourceEntity.Accounts;
targetEntity.Address = sourceEntity.Address;
targetEntity.Bank = sourceEntity.Bank;
targetEntity.Fax = sourceEntity.Fax;
targetEntity.InvoiceName = sourceEntity.InvoiceName;
targetEntity.IsLock = sourceEntity.IsLock;
targetEntity.MedicalCenterId = sourceEntity.MedicalCenterId;
targetEntity.OrgTypeId = sourceEntity.OrgTypeId;
targetEntity.PostalCode = sourceEntity.PostalCode;
targetEntity.Remark = sourceEntity.Remark;
targetEntity.ShortName = sourceEntity.ShortName;
targetEntity.IsActive = sourceEntity.IsActive;
targetEntity.Telephone = sourceEntity.Telephone;
targetEntity.CountryOrgCode = sourceEntity.CountryOrgCode;
targetEntity.SalesPerson = sourceEntity.SalesPerson;
targetEntity.SalesPersonPhone = sourceEntity.SalesPersonPhone;
}
/// <summary>
/// 验证新增、修改字段
/// </summary>
/// <param name="entity"></param>
/// <exception cref="ArgumentException"></exception>
private async Task Verify(CustomerOrg entity)
{
DataHelper.CheckEntityIsNull(entity);
DataHelper.CheckStringIsNull(entity.DisplayName, "名称");
DataHelper.CheckStringIsNull(entity.ShortName, "简称");
DataHelper.CheckGuidIsDefaultValue(entity.OrgTypeId, "单位性质");
DataHelper.CheckCharIsYOrN(entity.IsLock, "是否锁住");
DataHelper.CheckCharIsYOrN(entity.IsActive, "是否启用");
DataHelper.CheckGuidIsDefaultValue(entity.MedicalCenterId, "体检中心");
if (!await _peisOrganizationUnitManager.IsPeis(entity.MedicalCenterId))
{
throw new UserFriendlyException("组织单位ID不是体检中心");
}
}
/// <summary>
/// 修改排序 置顶,置底
/// </summary>
/// <param name="id">需要修改的ID</param>
/// <param name="SortType">修改方式:1 置顶 2 置底</param>
/// <returns></returns>
public async Task UpdateManySortAsync(Guid id, int SortType)
{
var entity = await _repository.GetAsync(id);
List<CustomerOrg> UptList = new List<CustomerOrg>();
if (SortType == 1)
{
UptList = (await _repository.GetListAsync(o => o.ParentId == entity.ParentId && o.DisplayOrder > entity.DisplayOrder)).OrderBy(o => o.DisplayOrder).ToList();
if (UptList.Count > 0)
{
int indexnum = entity.DisplayOrder; //原有值
entity.DisplayOrder = UptList[UptList.Count - 1].DisplayOrder; //修改当前排序值为最大
//置顶操作,往上一行开始,逐渐替换
foreach (var item in UptList)
{
int dqnum = item.DisplayOrder;
item.DisplayOrder = indexnum;
indexnum = dqnum;
}
}
}
else
{
UptList = (await _repository.GetListAsync(o => o.ParentId == entity.ParentId && o.DisplayOrder < entity.DisplayOrder)).OrderByDescending(o => o.DisplayOrder).ToList(); ;
if (UptList.Count > 0)
{
int indexnum = entity.DisplayOrder; //原有值
entity.DisplayOrder = UptList[UptList.Count - 1].DisplayOrder; //修改当前排序值为最小
//置底操作,往下一行开始,逐渐替换
foreach (var item in UptList)
{
int dqnum = item.DisplayOrder;
item.DisplayOrder = indexnum;
indexnum = dqnum;
}
}
}
UptList.Add(entity);
await _repository.UpdateManyAsync(UptList);
}
/// <summary>
/// 修改排序 拖拽
/// </summary>
/// <typeparam name="TEntity"></typeparam>
/// <param name="repository"></param>
/// <param name="input"></param>
/// <returns></returns>
public async Task UpdateSortManyAsync(UpdateSortManyDto input)
{
await EntityHelper.UpdateSortManyAsync(_repository, input);
}
/// <summary>
/// 自动生成pathcode
/// </summary>
/// <param name="parentId"></param>
/// <returns></returns>
public async Task<string> CreatePathCode(Guid? parentId)
{
string PathCode = "00001";
//一级
if (parentId == null || parentId == Guid.Empty)
{
//最大pathcode
var LastPathCode = (await _repository.GetListAsync(o => o.ParentId == Guid.Empty || o.ParentId == null))
.OrderByDescending(o =>
{
var sortCode = o.PathCode.Replace(".", "");
return Convert.ToInt32(sortCode);
}
).FirstOrDefault();
if (LastPathCode != null)
{
PathCode = (Convert.ToInt32(LastPathCode.PathCode) + 1).ToString().PadLeft(5, '0');
}
else
{
PathCode = "00001";
}
}
else
{
//二级以及以上
//上级pathcode
var ParentPathCode = (await _repository.GetListAsync(o => o.Id == parentId)).FirstOrDefault().PathCode;
//最大pathcode
var LastPathCode = (await _repository.GetListAsync(o => o.ParentId == parentId))
.OrderByDescending(o =>
{
var sortCode = o.PathCode.Replace(".", "");
return sortCode;
}).Select(s => s.PathCode).FirstOrDefault();
if (!string.IsNullOrEmpty(LastPathCode))
{
var MaxCode = LastPathCode.Split('.').Last();
PathCode = ParentPathCode + "." + (Convert.ToInt32(MaxCode) + 1).ToString().PadLeft(5, '0');
}
else
{
PathCode = ParentPathCode + ".00001";
}
}
return PathCode;
}
/// <summary>
/// 获取包含子级的ID
/// </summary>
/// <param name="CustomerOrgId"></param>
/// <returns></returns>
public async Task<List<Guid?>> GetCustomerOrgChildrenId(Guid CustomerOrgId)
{
//var customerOrgEntList = await _repository.GetListAsync();
var customerOrg = await _repository.GetAsync(CustomerOrgId);
var customerOrgEntList = await _repository.GetListAsync(o => o.PathCode.StartsWith(customerOrg.PathCode));
List<Guid?> CustomerOrgIds = new List<Guid?>();
GetChildren(customerOrgEntList, CustomerOrgId, CustomerOrgIds);
return CustomerOrgIds;
}
private void GetChildren(List<CustomerOrg> customerOrgEntList, Guid CustomerOrgId, List<Guid?> CustomerOrgIds)
{
CustomerOrgIds.Add(CustomerOrgId);
var entlist = customerOrgEntList.Where(m => m.ParentId == CustomerOrgId).ToList();
if (entlist.Count > 0)
{
foreach (var ent in entlist)
{
GetChildren(customerOrgEntList, ent.Id, CustomerOrgIds);
}
}
}
public async Task<CustomerOrg> GetTopCustomerOrgAsync(Guid CustomerOrgId)
{
var entity = await _repository.GetAsync(CustomerOrgId);
if (entity.Id == GuidFlag.PersonCustomerOrgId)
{
return entity;
}
if (entity.PathCode.Length == 5)
{
return entity;
}
entity = await _repository.GetAsync(o => o.PathCode == entity.PathCode.Substring(0, 5));
return entity;
}
/// <summary>
/// 检查是否有子节点数据
/// </summary>
/// <returns>返回字节点数量</returns>
public async Task<int> CheckIsChildren(Guid id)
{
var childrenCount = await _repository.CountAsync(m => m.ParentId == id);
return childrenCount;
}
/// <summary>
/// 1.删除单位时候,判断是否已有人员登记(patient_register),已有的禁止删除。
/// 2.删除单位的时候,删除单位体检次数。
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
/// <exception cref="UserFriendlyException"></exception>
public async Task CheckAndDeleteAsync(Guid id)
{
var customerOrgEnt = await _repository.GetAsync(m => m.Id == id);
var patientRegisterEnt = await _patientRegisterRepository.FirstOrDefaultAsync(m => m.CustomerOrgId == id);
if (patientRegisterEnt != null)
{
throw new UserFriendlyException($"单位\"{customerOrgEnt.DisplayName}\"已被体检人员\"{patientRegisterEnt.PatientName}\"登记使用,不能删除");
}
var contactPersonList = await _contactPersonRepository.GetListAsync(m => m.CustomerOrgId == id);
if (contactPersonList.Any())
{
throw new UserFriendlyException($"单位\"{customerOrgEnt.DisplayName}\"下存在联系人\"{contactPersonList[0].DisplayName}\",不能删除");
}
//删除逻辑
//单位下的单位体检次数
var customerOrgRegisters = await _customerOrgRegisterRepository.GetListAsync(m => m.CustomerOrgId == id);
if (customerOrgRegisters.Any())
{
//删除单位体检次数
foreach (var customerOrgRegister in customerOrgRegisters)
{
await _customerOrgRegisterManager.CheckAndDeleteAsync(customerOrgRegister.Id);
}
}
await _repository.DeleteAsync(id);
}
}
}