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.

349 lines
13 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. using NPOI.POIFS.Properties;
  2. using NPOI.SS.Formula.Functions;
  3. using Shentun.Peis.ContactMethods;
  4. using Shentun.Peis.ContactPersons;
  5. using Shentun.Peis.CustomerOrgGroups;
  6. using Shentun.Peis.CustomerOrgRegisters;
  7. using Shentun.Peis.HelperDto;
  8. using Shentun.Peis.Models;
  9. using Shentun.Utilities;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Linq;
  13. using System.Security.Cryptography.X509Certificates;
  14. using System.Text;
  15. using System.Threading.Tasks;
  16. using TencentCloud.Ams.V20200608.Models;
  17. using TencentCloud.Sqlserver.V20180328.Models;
  18. using Volo.Abp;
  19. using Volo.Abp.Domain.Repositories;
  20. using Volo.Abp.Domain.Services;
  21. namespace Shentun.Peis.CustomerOrgs
  22. {
  23. /// <summary>
  24. /// 团检单位设置
  25. /// </summary>
  26. public class CustomerOrgManager : DomainService
  27. {
  28. private readonly IRepository<CustomerOrg, Guid> _repository;
  29. private readonly IRepository<PatientRegister, Guid> _patientRegisterRepository;
  30. private readonly IRepository<CustomerOrgRegister, Guid> _customerOrgRegisterRepository;
  31. private readonly IRepository<CustomerOrgGroup, Guid> _customerOrgGroupRepository;
  32. private readonly IRepository<ContactPerson, Guid> _contactPersonRepository;
  33. private readonly CustomerOrgRegisterManager _customerOrgRegisterManager;
  34. private readonly CustomerOrgGroupManager _customerOrgGroupManager;
  35. private readonly ContactPersonManager _contactPersonManager;
  36. public CustomerOrgManager(
  37. IRepository<CustomerOrg, Guid> repository,
  38. IRepository<PatientRegister, Guid> patientRegisterRepository,
  39. IRepository<CustomerOrgRegister, Guid> customerOrgRegisterRepository,
  40. IRepository<CustomerOrgGroup, Guid> customerOrgGroupRepository,
  41. IRepository<ContactPerson, Guid> contactPersonRepository,
  42. CustomerOrgRegisterManager customerOrgRegisterManager,
  43. CustomerOrgGroupManager customerOrgGroupManager,
  44. ContactPersonManager contactPersonManager
  45. )
  46. {
  47. _repository = repository;
  48. this._patientRegisterRepository = patientRegisterRepository;
  49. this._customerOrgRegisterRepository = customerOrgRegisterRepository;
  50. this._customerOrgGroupRepository = customerOrgGroupRepository;
  51. this._contactPersonRepository = contactPersonRepository;
  52. this._customerOrgRegisterManager = customerOrgRegisterManager;
  53. this._customerOrgGroupManager = customerOrgGroupManager;
  54. this._contactPersonManager = contactPersonManager;
  55. }
  56. /// <summary>
  57. /// 创建
  58. /// </summary>
  59. /// <param name="entity"></param>
  60. /// <returns></returns>
  61. public async Task<CustomerOrg> CreateAsync(
  62. CustomerOrg entity
  63. )
  64. {
  65. DataHelper.CheckStringIsNull(entity.DisplayName, "名称");
  66. //await EntityHelper.CheckSameName<CustomerOrg, Guid>(_repository, entity.DisplayName);
  67. #region 验证同名
  68. if (await _repository.FirstOrDefaultAsync(m => m.DisplayName == entity.DisplayName && m.ParentId == entity.ParentId) != null)
  69. {
  70. if (entity.ParentId == null)
  71. {
  72. throw new UserFriendlyException("单位:" + entity.DisplayName + "已存在");
  73. }
  74. else
  75. {
  76. throw new UserFriendlyException("同级目录下已存在:" + entity.DisplayName + "部门");
  77. }
  78. }
  79. #endregion
  80. return new CustomerOrg
  81. {
  82. DisplayName = entity.DisplayName,
  83. SimpleCode = LanguageConverter.GetPYSimpleCode(entity.DisplayName),
  84. DisplayOrder = await EntityHelper.CreateMaxDisplayOrder<CustomerOrg>(_repository),
  85. Accounts = entity.Accounts,
  86. Address = entity.Address,
  87. Bank = entity.Bank,
  88. Fax = entity.Fax,
  89. InvoiceName = entity.InvoiceName,
  90. IsLock = entity.IsLock,
  91. OrganizationUnitId = entity.OrganizationUnitId,
  92. OrgTypeId = entity.OrgTypeId,
  93. ParentId = entity.ParentId,
  94. PathCode = await CreatePathCode(entity.ParentId),
  95. PostalCode = entity.PostalCode,
  96. Remark = entity.Remark,
  97. ShortName = entity.ShortName,
  98. IsActive = entity.IsActive,
  99. Telephone = entity.Telephone
  100. };
  101. }
  102. /// <summary>
  103. /// 更新
  104. /// </summary>
  105. /// <param name="sourceEntity"></param>
  106. /// <param name="targetEntity"></param>
  107. /// <returns></returns>
  108. public async Task UpdateAsync(
  109. CustomerOrg sourceEntity,
  110. CustomerOrg targetEntity
  111. )
  112. {
  113. DataHelper.CheckStringIsNull(sourceEntity.DisplayName, "名称");
  114. if (sourceEntity.DisplayName != targetEntity.DisplayName)
  115. {
  116. if (await _repository.FirstOrDefaultAsync(m => m.DisplayName == sourceEntity.DisplayName && m.ParentId == sourceEntity.ParentId) != null)
  117. {
  118. if (sourceEntity.ParentId == null)
  119. {
  120. throw new UserFriendlyException("单位:" + sourceEntity.DisplayName + "已存在");
  121. }
  122. else
  123. {
  124. throw new UserFriendlyException("同级目录下已存在:" + sourceEntity.DisplayName + "部门");
  125. }
  126. }
  127. targetEntity.DisplayName = sourceEntity.DisplayName;
  128. targetEntity.SimpleCode = LanguageConverter.GetPYSimpleCode(targetEntity.DisplayName);
  129. }
  130. targetEntity.Accounts = sourceEntity.Accounts;
  131. targetEntity.Address = sourceEntity.Address;
  132. targetEntity.Bank = sourceEntity.Bank;
  133. targetEntity.Fax = sourceEntity.Fax;
  134. targetEntity.InvoiceName = sourceEntity.InvoiceName;
  135. targetEntity.IsLock = sourceEntity.IsLock;
  136. targetEntity.OrganizationUnitId = sourceEntity.OrganizationUnitId;
  137. targetEntity.OrgTypeId = sourceEntity.OrgTypeId;
  138. //targetEntity.ParentId = targetEntity.ParentId;
  139. //targetEntity.PathCode = targetEntity.PathCode;
  140. targetEntity.PostalCode = sourceEntity.PostalCode;
  141. targetEntity.Remark = sourceEntity.Remark;
  142. targetEntity.ShortName = sourceEntity.ShortName;
  143. targetEntity.IsActive = sourceEntity.IsActive;
  144. targetEntity.Telephone = sourceEntity.Telephone;
  145. }
  146. /// <summary>
  147. /// 修改排序 相邻之间
  148. /// </summary>
  149. /// <param name="id">需要修改的ID</param>
  150. /// <param name="targetid">目标ID</param>
  151. public async Task UpdateSortAsync(Guid id, Guid targetid)
  152. {
  153. await EntityHelper.UpdateSort(_repository, id, targetid);
  154. }
  155. /// <summary>
  156. /// 修改排序 置顶,置底
  157. /// </summary>
  158. /// <param name="id">需要修改的ID</param>
  159. /// <param name="SortType">修改方式:1 置顶 2 置底</param>
  160. /// <returns></returns>
  161. public async Task UpdateManySortAsync(Guid id, int SortType)
  162. {
  163. await EntityHelper.UpdateManySortAsync(_repository, id, SortType);
  164. }
  165. /// <summary>
  166. /// 修改排序 拖拽
  167. /// </summary>
  168. /// <typeparam name="TEntity"></typeparam>
  169. /// <param name="repository"></param>
  170. /// <param name="input"></param>
  171. /// <returns></returns>
  172. public async Task UpdateSortManyAsync(UpdateSortManyDto input)
  173. {
  174. await EntityHelper.UpdateSortManyAsync(_repository, input);
  175. }
  176. /// <summary>
  177. /// 自动生成pathcode
  178. /// </summary>
  179. /// <param name="parentId"></param>
  180. /// <returns></returns>
  181. private async Task<string> CreatePathCode(Guid? parentId)
  182. {
  183. string PathCode = "00001";
  184. //一级
  185. if (parentId == null || parentId == Guid.Empty)
  186. {
  187. //最大pathcode
  188. var LastPathCode = (await _repository.GetListAsync(o => o.ParentId == Guid.Empty || o.ParentId == null))
  189. .OrderByDescending(o => o.CreationTime).FirstOrDefault();
  190. if (LastPathCode != null)
  191. {
  192. PathCode = (Convert.ToInt32(LastPathCode.PathCode) + 1).ToString().PadLeft(5, '0');
  193. }
  194. else
  195. {
  196. PathCode = "00001";
  197. }
  198. }
  199. else
  200. {
  201. //二级以及以上
  202. //上级pathcode
  203. var ParentPathCode = (await _repository.GetListAsync(o => o.Id == parentId)).FirstOrDefault().PathCode;
  204. //最大pathcode
  205. var LastPathCode = (await _repository.GetListAsync(o => o.ParentId == parentId))
  206. .OrderByDescending(o => o.CreationTime).Select(s => s.PathCode).FirstOrDefault();
  207. if (!string.IsNullOrEmpty(LastPathCode))
  208. {
  209. var MaxCode = LastPathCode.Split('.').Last();
  210. PathCode = ParentPathCode + "." + (Convert.ToInt32(MaxCode) + 1).ToString().PadLeft(5, '0');
  211. }
  212. else
  213. {
  214. PathCode = ParentPathCode + ".00001";
  215. }
  216. }
  217. return PathCode;
  218. }
  219. /// <summary>
  220. /// 获取包含子级的ID
  221. /// </summary>
  222. /// <param name="CustomerOrgId"></param>
  223. /// <returns></returns>
  224. public async Task<List<Guid?>> GetCustomerOrgChildrenId(Guid CustomerOrgId)
  225. {
  226. var customerOrgEntList = await _repository.GetListAsync();
  227. List<Guid?> CustomerOrgIds = new List<Guid?>();
  228. GetChildren(customerOrgEntList, CustomerOrgId, CustomerOrgIds);
  229. return CustomerOrgIds;
  230. }
  231. private void GetChildren(List<CustomerOrg> customerOrgEntList, Guid CustomerOrgId, List<Guid?> CustomerOrgIds)
  232. {
  233. CustomerOrgIds.Add(CustomerOrgId);
  234. var entlist = customerOrgEntList.Where(m => m.ParentId == CustomerOrgId).ToList();
  235. if (entlist.Count > 0)
  236. {
  237. foreach (var ent in entlist)
  238. {
  239. GetChildren(customerOrgEntList, ent.Id, CustomerOrgIds);
  240. }
  241. }
  242. }
  243. /// <summary>
  244. /// 检查是否有子节点数据
  245. /// </summary>
  246. /// <returns>返回字节点数量</returns>
  247. public async Task<int> CheckIsChildren(Guid id)
  248. {
  249. var childrenCount = await _repository.CountAsync(m => m.ParentId == id);
  250. return childrenCount;
  251. }
  252. /// <summary>
  253. /// 1.删除单位时候,判断是否已有人员登记(patient_register),已有的禁止删除。
  254. /// 2.删除单位的时候,删除单位体检次数。
  255. /// </summary>
  256. /// <param name="id"></param>
  257. /// <returns></returns>
  258. /// <exception cref="UserFriendlyException"></exception>
  259. public async Task CheckAndDeleteAsync(Guid id)
  260. {
  261. var customerOrgEnt = await _repository.FirstOrDefaultAsync(m => m.Id == id);
  262. if (customerOrgEnt != null)
  263. {
  264. var patientRegisterEnt = await _patientRegisterRepository.FirstOrDefaultAsync(m => m.CustomerOrgId == id);
  265. if (patientRegisterEnt != null)
  266. {
  267. throw new UserFriendlyException($"单位\"{customerOrgEnt.DisplayName}\"已被体检人员\"{patientRegisterEnt.PatientName}\"登记使用,不能删除");
  268. }
  269. var contactPersonList = await _contactPersonRepository.GetListAsync(m => m.CustomerOrgId == id);
  270. if (contactPersonList.Any())
  271. {
  272. throw new UserFriendlyException($"单位\"{customerOrgEnt.DisplayName}\"下存在联系人\"{contactPersonList[0].DisplayName}\",不能删除");
  273. }
  274. //删除逻辑
  275. //单位下的单位体检次数
  276. var customerOrgRegisters = await _customerOrgRegisterRepository.GetListAsync(m => m.CustomerOrgId == id);
  277. if (customerOrgRegisters.Any())
  278. {
  279. //删除单位体检次数
  280. foreach (var customerOrgRegister in customerOrgRegisters)
  281. {
  282. await _customerOrgRegisterManager.CheckAndDeleteAsync(customerOrgRegister.Id);
  283. }
  284. }
  285. await _repository.DeleteAsync(id);
  286. }
  287. else
  288. {
  289. throw new UserFriendlyException("数据不存在");
  290. }
  291. }
  292. }
  293. }