TsysPrivService.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. using AutoMapper;
  2. using Ropin.Inspection.Common.Accessor.Interface;
  3. using Ropin.Inspection.Model.Entities;
  4. using Ropin.Inspection.Model;
  5. using Ropin.Inspection.Repository;
  6. using Ropin.Inspection.Repository.SYS.Interface;
  7. using Ropin.Inspection.Service.SYS.Interface;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using Ropin.Inspection.Model.ViewModel.SYS;
  14. using Ropin.Inspection.Model.ViewModel;
  15. namespace Ropin.Inspection.Service.SYS
  16. {
  17. public class TsysPrivService: ITsysPrivService
  18. {
  19. private readonly ITsysPrivRepository _repository;
  20. private readonly IMapper _mapper;
  21. private readonly IClaimsAccessor _claims;
  22. private readonly ITsysLicenseTypePrivRepository _sysLicenseTypePrivRepository;
  23. public TsysPrivService(IClaimsAccessor claims, ITsysPrivRepository repository, IMapper mapper, ITsysLicenseTypePrivRepository sysLicenseTypePrivRepository)
  24. {
  25. _repository = repository;
  26. _mapper = mapper;
  27. _claims = claims;
  28. _sysLicenseTypePrivRepository = sysLicenseTypePrivRepository;
  29. }
  30. /// <summary>
  31. /// 新增
  32. /// </summary>
  33. /// <param name="viewModel"></param>
  34. /// <returns></returns>
  35. /// <exception cref="Exception"></exception>
  36. public async Task CreateOneAsync(TsysPrivViewModel viewModel)
  37. {
  38. var pagedList = await _repository.GetAllAsync();
  39. TSYS_Priv content = _mapper.Map<TSYS_Priv>(viewModel);
  40. if (string.IsNullOrEmpty(viewModel.C_Code))
  41. {
  42. //content.C_Code = Guid.NewGuid().ToString();
  43. throw new Exception("请输入编号!");
  44. }
  45. int num = pagedList.Where(t => t.C_Code == viewModel.C_Code).Count();
  46. if (num >= 1)
  47. {
  48. throw new Exception("编号已存在!");
  49. }
  50. content.C_Status = "1";
  51. _repository.Create(content);
  52. var result = await _repository.SaveAsync();
  53. if (!result)
  54. {
  55. throw new Exception("创建失败");
  56. }
  57. }
  58. /// <summary>
  59. /// 真删除数据
  60. /// </summary>
  61. /// <param name="id"></param>
  62. /// <returns></returns>
  63. /// <exception cref="Exception"></exception>
  64. public async Task DeleteAsync(string id)
  65. {
  66. var content = _repository.GetPrivEntity(id);
  67. if (content == null)
  68. {
  69. throw new Exception("数据库中没有此数据");
  70. }
  71. var delList = await _repository.GetByConditionAsync(t=>t.C_ParentCode==id);
  72. if (delList != null&&delList.Any())
  73. {
  74. throw new Exception("该数据下存在子级,删除失败!");
  75. }
  76. var icenseTypePriv = await _sysLicenseTypePrivRepository.GetByConditionAsync(t => t.C_PrivilegeCode == content.C_Code);
  77. if (icenseTypePriv?.Count()>0)
  78. {
  79. throw new Exception("该关联其它表,删除失败!");
  80. }
  81. _repository.Delete(content);
  82. var result = await _repository.SaveAsync();
  83. if (!result)
  84. {
  85. throw new Exception("删除失败");
  86. }
  87. }
  88. /// <summary>
  89. /// 假删除-修改状态为禁用
  90. /// </summary>
  91. /// <param name="id"></param>
  92. /// <returns></returns>
  93. /// <exception cref="Exception"></exception>
  94. public async Task UpdateStatusAsync(string id)
  95. {
  96. var content = _repository.GetPrivEntity(id);
  97. if (content == null)
  98. {
  99. throw new Exception("数据库中没有此数据");
  100. }
  101. content.C_Status = "0";
  102. _repository.Update(content);
  103. var result = await _repository.SaveAsync();
  104. if (!result)
  105. {
  106. throw new Exception("删除失败");
  107. }
  108. }
  109. /// <summary>
  110. /// 修改
  111. /// </summary>
  112. /// <param name="id"></param>
  113. /// <param name="updateModel"></param>
  114. /// <returns></returns>
  115. /// <exception cref="Exception"></exception>
  116. public async Task UpdateOneAsync(string id, TsysPrivViewModel updateModel)
  117. {
  118. var content = _repository.GetPrivEntity(id);
  119. if (content == null)
  120. {
  121. throw new Exception("没有此数据");
  122. }
  123. _mapper.Map(updateModel, content, typeof(TsysPrivViewModel), typeof(TSYS_Priv));
  124. _repository.Update(content);
  125. var result = await _repository.SaveAsync();
  126. if (!result)
  127. {
  128. throw new Exception("更新失败");
  129. }
  130. }
  131. /// <summary>
  132. /// 树型列表
  133. /// </summary>
  134. /// <param name="model"></param>
  135. /// <returns></returns>
  136. public async Task<List<TsysPrivTreeModel>> GetPrivTree(TsysPrivSearch model)
  137. {
  138. var list = await _repository.GetPrivTree(model);
  139. if (!string.IsNullOrWhiteSpace(model.C_Name))
  140. {
  141. if (list != null&&list.Count>0)
  142. {
  143. list = list.Where(t => t.C_Name.Contains(model.C_Name)).ToList();
  144. }
  145. }
  146. return list;
  147. }
  148. public async Task<TsysPrivViewModel> GetByIdAsync(string id)
  149. {
  150. var content = _repository.GetPrivEntity(id);
  151. var contentDto = _mapper.Map<TsysPrivViewModel>(content);
  152. return contentDto;
  153. }
  154. public Task<bool> IsExistAsync(string id)
  155. {
  156. throw new NotImplementedException();
  157. }
  158. }
  159. }