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 { /// /// 团检单位设置 /// public class CustomerOrgManager : DomainService { private readonly IRepository _repository; private readonly IRepository _customerOrgTypeRepository; private readonly IRepository _patientRegisterRepository; private readonly IRepository _customerOrgRegisterRepository; private readonly IRepository _customerOrgGroupRepository; private readonly IRepository _contactPersonRepository; private readonly CustomerOrgRegisterManager _customerOrgRegisterManager; private readonly CustomerOrgGroupManager _customerOrgGroupManager; private readonly ContactPersonManager _contactPersonManager; private readonly PeisOrganizationUnitManager _peisOrganizationUnitManager; public CustomerOrgManager( IRepository repository, IRepository patientRegisterRepository, IRepository customerOrgRegisterRepository, IRepository customerOrgGroupRepository, IRepository contactPersonRepository, CustomerOrgRegisterManager customerOrgRegisterManager, CustomerOrgGroupManager customerOrgGroupManager, ContactPersonManager contactPersonManager, PeisOrganizationUnitManager peisOrganizationUnitManager, IRepository 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; } /// /// 创建 /// /// /// public async Task 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(_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 }; } /// /// 更新 /// /// /// /// 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; } /// /// 验证新增、修改字段 /// /// /// 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不是体检中心"); } } /// /// 修改排序 置顶,置底 /// /// 需要修改的ID /// 修改方式:1 置顶 2 置底 /// public async Task UpdateManySortAsync(Guid id, int SortType) { var entity = await _repository.GetAsync(id); List UptList = new List(); 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); } /// /// 修改排序 拖拽 /// /// /// /// /// public async Task UpdateSortManyAsync(UpdateSortManyDto input) { await EntityHelper.UpdateSortManyAsync(_repository, input); } /// /// 自动生成pathcode /// /// /// public async Task 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; } /// /// 获取包含子级的ID /// /// /// public async Task> 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 CustomerOrgIds = new List(); GetChildren(customerOrgEntList, CustomerOrgId, CustomerOrgIds); return CustomerOrgIds; } private void GetChildren(List customerOrgEntList, Guid CustomerOrgId, List 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 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; } /// /// 检查是否有子节点数据 /// /// 返回字节点数量 public async Task CheckIsChildren(Guid id) { var childrenCount = await _repository.CountAsync(m => m.ParentId == id); return childrenCount; } /// /// 1.删除单位时候,判断是否已有人员登记(patient_register),已有的禁止删除。 /// 2.删除单位的时候,删除单位体检次数。 /// /// /// /// 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); } } }