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.

431 lines
17 KiB

2 years ago
2 years ago
3 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
2 years ago
3 years ago
3 years ago
3 years ago
2 years ago
2 years ago
3 years ago
2 years ago
2 years ago
2 years ago
3 years ago
3 years ago
2 years ago
2 years ago
3 years ago
2 years ago
3 years ago
2 years ago
3 years ago
2 years ago
3 years ago
2 years ago
2 years ago
2 years ago
3 years ago
2 years ago
3 years ago
2 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
2 years ago
2 years ago
2 years ago
2 years ago
3 years ago
3 years ago
2 years ago
3 years ago
3 years ago
3 years ago
2 years ago
3 years ago
3 years ago
3 years ago
2 years ago
3 years ago
2 years ago
3 years ago
2 years ago
3 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
2 years ago
3 years ago
2 years ago
3 years ago
2 years ago
2 years ago
3 years ago
2 years ago
3 years ago
2 years ago
3 years ago
2 years ago
3 years ago
2 years ago
2 years ago
2 years ago
3 years ago
2 years ago
2 years ago
2 years ago
3 years ago
2 years ago
2 years ago
2 years ago
2 years ago
3 years ago
2 years ago
3 years ago
2 years ago
3 years ago
2 years ago
3 years ago
2 years ago
2 years ago
3 years ago
2 years ago
2 years ago
3 years ago
2 years ago
3 years ago
2 years ago
3 years ago
3 years ago
  1. using Microsoft.AspNetCore.Authorization;
  2. using Microsoft.AspNetCore.Mvc;
  3. using Microsoft.Extensions.Caching.Memory;
  4. using Shentun.Peis.CustomerOrgRegisters;
  5. using Shentun.Peis.Enums;
  6. using Shentun.Peis.HelperDto;
  7. using Shentun.Peis.Models;
  8. using Shentun.Peis.OrganizationUnits;
  9. using Shentun.Peis.PersonnelTypes;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Linq;
  13. using System.Threading.Tasks;
  14. using Volo.Abp;
  15. using Volo.Abp.Application.Dtos;
  16. using Volo.Abp.Application.Services;
  17. using Volo.Abp.Domain.Entities;
  18. using Volo.Abp.Domain.Repositories;
  19. using Volo.Abp.Identity;
  20. using Volo.Abp.ObjectMapping;
  21. namespace Shentun.Peis.CustomerOrgs
  22. {
  23. /// <summary>
  24. /// 团检单位设置
  25. /// </summary>
  26. [ApiExplorerSettings(GroupName = "Work")]
  27. [Authorize]
  28. public class CustomerOrgAppService : CrudAppService<
  29. CustomerOrg, //The Book entity
  30. CustomerOrgDto, //Used to show books
  31. Guid, //Primary key of the book entity
  32. PagedAndSortedResultRequestDto, //Used for paging/sorting
  33. CreateCustomerOrgDto,
  34. UpdateCustomerOrgDto>
  35. {
  36. private readonly IRepository<IdentityUser, Guid> _userRepository;
  37. private readonly IRepository<CustomerOrgRegister, Guid> _customerOrgRegisterRepository;
  38. private readonly CustomerOrgManager _manager;
  39. private readonly CustomerOrgRegisterManager _customerOrgRegisterManager;
  40. private readonly CacheService _cacheService;
  41. private readonly IMemoryCache _customerOrgCache;
  42. public CustomerOrgAppService(
  43. IRepository<CustomerOrg, Guid> repository,
  44. IRepository<IdentityUser, Guid> userRepository,
  45. CustomerOrgManager manager,
  46. CustomerOrgRegisterManager customerOrgRegisterManager,
  47. IRepository<CustomerOrgRegister, Guid> customerOrgRegisterRepository,
  48. CacheService cacheService,
  49. IMemoryCache customerOrgCache)
  50. : base(repository)
  51. {
  52. _userRepository = userRepository;
  53. _manager = manager;
  54. this._customerOrgRegisterManager = customerOrgRegisterManager;
  55. this._customerOrgRegisterRepository = customerOrgRegisterRepository;
  56. _cacheService = cacheService;
  57. _customerOrgCache = customerOrgCache;
  58. }
  59. /// <summary>
  60. /// 获取通过主键
  61. /// </summary>
  62. /// <param name="id"></param>
  63. /// <returns></returns>
  64. public override async Task<CustomerOrgDto> GetAsync(Guid id)
  65. {
  66. var entityDto = await base.GetAsync(id);
  67. entityDto.CreatorName = await _cacheService.GetSurnameAsync(entityDto.CreatorId);
  68. entityDto.LastModifierName = await _cacheService.GetSurnameAsync(entityDto.LastModifierId);
  69. return entityDto;
  70. }
  71. //[AllowAnonymous]
  72. [HttpPost("api/app/CustomerOrg/GetById")]
  73. public async Task<CustomerOrgDto> GetByIdAsync(CustomerOrgIdInputDto input)
  74. {
  75. var entity = await Repository.GetAsync(input.CustomerOrgId);
  76. var entityDto = ObjectMapper.Map<CustomerOrg, CustomerOrgDto>(entity);
  77. entityDto.CreatorName = await _cacheService.GetSurnameAsync(entityDto.CreatorId);
  78. entityDto.LastModifierName = await _cacheService.GetSurnameAsync(entityDto.LastModifierId);
  79. return entityDto;
  80. }
  81. /// <summary>
  82. /// 获取所有一级子单位
  83. /// </summary>
  84. /// <param name="input"></param>
  85. /// <returns></returns>
  86. [HttpPost("api/app/CustomerOrg/GetChildCustomerOrgsById")]
  87. public async Task<List<CustomerOrgDto>> GetChildCustomerOrgsByIdAsync(CustomerOrgIdInputDto input)
  88. {
  89. var list = await Repository.GetListAsync(o => o.ParentId == input.CustomerOrgId);
  90. var dtos = new List<CustomerOrgDto>();
  91. foreach (var customerOrg in list)
  92. {
  93. var entityDto = ObjectMapper.Map<CustomerOrg, CustomerOrgDto>(customerOrg);
  94. entityDto.CreatorName = await _cacheService.GetSurnameAsync(entityDto.CreatorId);
  95. entityDto.LastModifierName = await _cacheService.GetSurnameAsync(entityDto.LastModifierId);
  96. dtos.Add(entityDto);
  97. }
  98. return dtos;
  99. }
  100. /// <summary>
  101. /// 获取列表 团检单位设置
  102. /// </summary>
  103. /// <param name="input"></param>
  104. /// <returns></returns>
  105. [RemoteService(false)]
  106. public override async Task<PagedResultDto<CustomerOrgDto>> GetListAsync(PagedAndSortedResultRequestDto input)
  107. {
  108. return await base.GetListAsync(input);
  109. }
  110. /// <summary>
  111. /// 获取列表 团检单位设置 可以带名称搜索
  112. /// </summary>
  113. /// <param name="input"></param>
  114. /// <returns></returns>
  115. public async Task<List<CustomerOrgDto>> GetListInFilterAsync(GetListDto input)
  116. {
  117. var oldlist = await Repository.GetQueryableAsync();
  118. if (!string.IsNullOrEmpty(input.Filter))
  119. oldlist = oldlist.Where(m => m.DisplayName.Contains(input.Filter));
  120. if (input.IsHidePerson == 1)
  121. oldlist = oldlist.Where(m => m.Id != GuidFlag.PersonCustomerOrgId);
  122. var entdto = oldlist.Select(s => new CustomerOrgDto
  123. {
  124. CreationTime = s.CreationTime,
  125. CreatorId = s.CreatorId,
  126. DisplayName = s.DisplayName,
  127. DisplayOrder = s.DisplayOrder,
  128. Id = s.Id,
  129. LastModificationTime = s.LastModificationTime,
  130. LastModifierId = s.LastModifierId,
  131. SimpleCode = s.SimpleCode,
  132. Accounts = s.Accounts,
  133. Telephone = s.Telephone,
  134. IsActive = s.IsActive,
  135. ShortName = s.ShortName,
  136. Remark = s.Remark,
  137. PostalCode = s.PostalCode,
  138. PathCode = s.PathCode,
  139. ParentId = s.ParentId,
  140. OrgTypeId = s.OrgTypeId,
  141. Address = s.Address,
  142. Bank = s.Bank,
  143. Fax = s.Fax,
  144. InvoiceName = s.InvoiceName,
  145. IsLock = s.IsLock,
  146. SalesPerson = s.SalesPerson,
  147. SalesPersonPhone = s.SalesPersonPhone,
  148. MedicalCenterId = s.MedicalCenterId,
  149. CountryOrgCode = s.CountryOrgCode,
  150. CreatorName = _cacheService.GetSurnameAsync(s.CreatorId).Result,
  151. LastModifierName = _cacheService.GetSurnameAsync(s.LastModifierId).Result
  152. }).OrderBy(m => m.DisplayOrder).ToList();
  153. return entdto;
  154. }
  155. /// <summary>
  156. /// 创建单位 单位为树型结构 一级目录默认生成体检次数
  157. /// </summary>
  158. /// <param name="input"></param>
  159. /// <returns></returns>
  160. [HttpPost("api/app/customerorg/create")]
  161. public override async Task<CustomerOrgDto> CreateAsync(CreateCustomerOrgDto input)
  162. {
  163. var createEntity = ObjectMapper.Map<CreateCustomerOrgDto, CustomerOrg>(input);
  164. var entity = await _manager.CreateAsync(createEntity);
  165. entity = await Repository.InsertAsync(entity);
  166. if (entity != null && (input.ParentId == null || input.ParentId == Guid.Empty))
  167. {
  168. //生成体检次数
  169. var customerOrgRegisterEntity = await _customerOrgRegisterManager.CreateAsync(entity.Id);
  170. await _customerOrgRegisterRepository.InsertAsync(customerOrgRegisterEntity);
  171. }
  172. _customerOrgCache.Set(entity.Id, entity);
  173. var dto = ObjectMapper.Map<CustomerOrg, CustomerOrgDto>(entity);
  174. return dto;
  175. }
  176. /// <summary>
  177. /// 更新
  178. /// </summary>
  179. /// <param name="id"></param>
  180. /// <param name="input"></param>
  181. /// <returns></returns>
  182. public override async Task<CustomerOrgDto> UpdateAsync(Guid id, UpdateCustomerOrgDto input)
  183. {
  184. var entity = await Repository.GetAsync(id);
  185. var sourceEntity = ObjectMapper.Map<UpdateCustomerOrgDto, CustomerOrg>(input);
  186. await _manager.UpdateAsync(sourceEntity, entity);
  187. entity = await Repository.UpdateAsync(entity);
  188. _customerOrgCache.Set(entity.Id, entity);
  189. return ObjectMapper.Map<CustomerOrg, CustomerOrgDto>(entity);
  190. }
  191. /// <summary>
  192. /// 删除
  193. /// </summary>
  194. /// <param name="id"></param>
  195. /// <returns></returns>
  196. public override async Task DeleteAsync(Guid id)
  197. {
  198. var CustomerOrgChildrenIds = await _manager.GetCustomerOrgChildrenId(id); //遍历找出下级ID
  199. if (CustomerOrgChildrenIds.Any())
  200. {
  201. foreach (var CustomerOrgChildrenId in CustomerOrgChildrenIds)
  202. {
  203. if (CustomerOrgChildrenId != null)
  204. {
  205. await _manager.CheckAndDeleteAsync(CustomerOrgChildrenId.Value);
  206. _customerOrgCache.Remove(CustomerOrgChildrenId);
  207. }
  208. }
  209. }
  210. else
  211. {
  212. throw new UserFriendlyException("参数有误");
  213. }
  214. }
  215. /// <summary>
  216. /// 修改排序 置顶,置底
  217. /// </summary>
  218. /// <param name="id">需要修改的ID</param>
  219. /// <param name="SortType">修改方式:1 置顶 2 置底</param>
  220. /// <returns></returns>
  221. [HttpPut("api/app/customerorg/updatemanysort")]
  222. public async Task UpdateManySortAsync(Guid id, int SortType)
  223. {
  224. await _manager.UpdateManySortAsync(id, SortType);
  225. }
  226. /// <summary>
  227. /// 修改排序 拖拽
  228. /// </summary>
  229. /// <param name="input"></param>
  230. /// <returns></returns>
  231. [HttpPut("api/app/customerorg/updatesortmany")]
  232. public async Task UpdateSortManyAsync(UpdateSortManyDto input)
  233. {
  234. await _manager.UpdateSortManyAsync(input);
  235. }
  236. /// <summary>
  237. /// 获取一级目录 单位列表
  238. /// </summary>
  239. /// <param name="Filter">名字搜索,支持模糊查找</param>
  240. /// <returns></returns>
  241. public async Task<List<CustomerOrg>> GetParentAllAsync(string Filter)
  242. {
  243. var dataList = await Repository.GetListAsync(m => m.ParentId == null || m.ParentId == Guid.Empty);
  244. if (dataList.Count > 0 && !string.IsNullOrEmpty(Filter))
  245. dataList = dataList.Where(m => m.DisplayName.Contains(Filter) || m.ShortName.Contains(Filter)).ToList();
  246. return dataList.OrderBy(o => o.DisplayOrder).ToList();
  247. }
  248. /// <summary>
  249. /// 获取单位树型结构
  250. /// </summary>
  251. /// <param name="IsHidePerson">是否隐藏个人信息 1 隐藏 0不隐藏 默认为0</param>
  252. /// <param name="Filter">名字搜索,支持模糊查找</param>
  253. /// <returns></returns>
  254. [HttpGet("api/app/customerorg/getbycodeall")]
  255. public async Task<List<CustomerOrgTreeChildDto>> GetByCodeAllAsync(string Filter, int IsHidePerson = 0)
  256. {
  257. List<CustomerOrgTreeChildDto> result = new List<CustomerOrgTreeChildDto>();
  258. var customerOrgPerson = await Repository.FirstOrDefaultAsync(f => f.Id == GuidFlag.PersonCustomerOrgId);
  259. var dataList = (await Repository.GetQueryableAsync());
  260. if (!string.IsNullOrEmpty(Filter))
  261. dataList = dataList.Where(m => m.DisplayName.Contains(Filter) || m.ShortName.Contains(Filter));
  262. if (IsHidePerson == 1)
  263. {
  264. dataList = dataList.Where(m => m.Id != GuidFlag.PersonCustomerOrgId);
  265. }
  266. else
  267. {
  268. if (customerOrgPerson != null)
  269. {
  270. result.Add(new CustomerOrgTreeChildDto
  271. {
  272. Code = customerOrgPerson.PathCode,
  273. CustomerOrgCode = customerOrgPerson.CustomerOrgCode,
  274. DisplayName = customerOrgPerson.DisplayName,
  275. DisplayOrder = customerOrgPerson.DisplayOrder,
  276. Id = customerOrgPerson.Id,
  277. ParentId = customerOrgPerson.ParentId,
  278. ShortName = customerOrgPerson.ShortName,
  279. SimpleCode = customerOrgPerson.SimpleCode,
  280. TreeChildren = null
  281. });
  282. }
  283. }
  284. var customerOrgList = dataList.ToList();
  285. var items = from p in customerOrgList.Where(m => m.Id != GuidFlag.PersonCustomerOrgId).OrderByDescending(o => o.DisplayOrder).AsParallel()
  286. select new CustomerOrgTreeChildDto()
  287. {
  288. Id = p.Id,
  289. ParentId = p.ParentId,
  290. Code = p.PathCode,
  291. DisplayName = p.DisplayName,
  292. SimpleCode = p.SimpleCode,
  293. ShortName = p.ShortName,
  294. CustomerOrgCode = p.CustomerOrgCode,
  295. TreeChildren = new List<CustomerOrgTreeChildDto>()
  296. };
  297. var customerOrgTreeChildList = GetTree(items.ToList(), null);
  298. //var tree1 = items.Where(m => m.ParentId == null).ToList();
  299. //foreach (var item1 in tree1)
  300. //{
  301. // item1.TreeChildren = items.Where(m => m.ParentId == item1.Id).ToList();
  302. // foreach (var item2 in item1.TreeChildren)
  303. // {
  304. // item2.TreeChildren = items.Where(m => m.ParentId == item2.Id).ToList();
  305. // foreach (var item3 in item2.TreeChildren)
  306. // {
  307. // item3.TreeChildren = items.Where(m => m.ParentId == item3.Id).ToList();
  308. // foreach (var item4 in item3.TreeChildren)
  309. // {
  310. // item4.TreeChildren = items.Where(m => m.ParentId == item4.Id).ToList();
  311. // }
  312. // }
  313. // }
  314. //}
  315. result.AddRange(customerOrgTreeChildList);
  316. return result;
  317. }
  318. /// <summary>
  319. /// 使用parentId进行递归
  320. /// </summary>
  321. /// <param name="items"></param>
  322. /// <param name="parentId"></param>
  323. /// <returns></returns>
  324. private List<CustomerOrgTreeChildDto> GetTree(List<CustomerOrgTreeChildDto> items, Guid? parentId)
  325. {
  326. return (from p in items.AsParallel()
  327. where p.ParentId == parentId
  328. let subs = GetTree(items, p.Id)
  329. select new CustomerOrgTreeChildDto()
  330. {
  331. Id = p.Id,
  332. ParentId = p.ParentId,
  333. Code = p.Code,
  334. DisplayName = p.DisplayName,
  335. SimpleCode = p.SimpleCode,
  336. ShortName = p.ShortName,
  337. CustomerOrgCode = p.CustomerOrgCode,
  338. TreeChildren = subs.ToList()
  339. }
  340. ).ToList();
  341. }
  342. ///// <summary>
  343. ///// 使用Code进行递归
  344. ///// </summary>
  345. ///// <param name="items"></param>
  346. ///// <param name="deep"></param>
  347. ///// <param name="prefix"></param>
  348. ///// <returns></returns>
  349. //private List<CustomerOrgTreeChildDto> GetTree(List<CustomerOrgTreeChildDto> items, int deep, string prefix)
  350. //{
  351. // return (from p in items
  352. // where p.Code.StartsWith(prefix) && p.Code.Count(a => a == '.') == deep
  353. // let subs = GetTree(items, deep + 1, p.Code)
  354. // select new CustomerOrgTreeChildDto()
  355. // {
  356. // Id = p.Id,
  357. // ParentId = p.ParentId,
  358. // Code = p.Code,
  359. // DisplayName = p.DisplayName,
  360. // SimpleCode = p.SimpleCode,
  361. // ShortName = p.ShortName,
  362. // CustomerOrgCode = p.CustomerOrgCode,
  363. // TreeChildren = subs.ToList()
  364. // }
  365. // ).ToList();
  366. //}
  367. /// <summary>
  368. /// 获取顶级目录ID
  369. /// </summary>
  370. /// <param name="CustomerOrgId"></param>
  371. /// <returns></returns>
  372. public async Task<Guid> GetParent(Guid CustomerOrgId)
  373. {
  374. var entity = await GetAsync(CustomerOrgId);
  375. var parentEntity = (await Repository.GetQueryableAsync()).Where(o => o.PathCode == entity.PathCode.Substring(0, 5)).Single();
  376. return parentEntity.Id;
  377. //return EntityHelper.GetParentNoSql(await Repository.GetListAsync(), CustomerOrgId);
  378. }
  379. }
  380. }