TsysLicenseService.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. using AutoMapper;
  2. using LinqKit;
  3. using Ropin.Inspection.Common;
  4. using Ropin.Inspection.Common.Accessor.Interface;
  5. using Ropin.Inspection.Model;
  6. using Ropin.Inspection.Model.Entities;
  7. using Ropin.Inspection.Repository;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.Linq.Expressions;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. namespace Ropin.Inspection.Service
  15. {
  16. public class TsysLicenseService : ITsysLicenseService
  17. {
  18. private readonly ITsysLicenseRepository _repository;
  19. private readonly ITsysLicenseTypePrivRepository _tsysLicenseTypePrivRepository;
  20. private readonly IMapper _mapper;
  21. private readonly IClaimsAccessor _claims;
  22. public TsysLicenseService(IClaimsAccessor claims, ITsysLicenseRepository repository, ITsysLicenseTypePrivRepository tsysLicenseTypePrivRepository, IMapper mapper)
  23. {
  24. _repository = repository;
  25. _tsysLicenseTypePrivRepository = tsysLicenseTypePrivRepository;
  26. _mapper = mapper;
  27. _claims = claims;
  28. }
  29. public async Task CreateOneAsync(TsysLicenseViewModel viewModel)
  30. {
  31. var content = _mapper.Map<TSYS_License>(viewModel);
  32. content.C_ID = Guid.NewGuid();
  33. content.C_Code = viewModel.C_TypeCode + Guid.NewGuid().ToString();
  34. content.C_CreateBy = _claims.ApiUserId;
  35. content.D_CreateOn = DateTime.Now;
  36. content.C_Status = "1";
  37. _repository.Create(content);
  38. var result = await _repository.SaveAsync();
  39. if (!result)
  40. {
  41. throw new Exception("创建失败");
  42. }
  43. }
  44. public async Task DeleteAsync(Guid id)
  45. {
  46. var content = await _repository.GetByIdAsync(id);
  47. if (content == null)
  48. {
  49. throw new Exception("数据库中没有此数据");
  50. }
  51. //_repository.Delete(content);
  52. //var result = await _repository.SaveAsync();
  53. content.C_LastUpdatedBy = _claims.ApiUserId;
  54. content.D_LastUpdatedOn = DateTime.Now;
  55. content.C_Status = "0";
  56. _repository.Update(content);
  57. var result = await _repository.SaveAsync();
  58. if (!result)
  59. {
  60. throw new Exception("删除失败");
  61. }
  62. }
  63. public async Task<IEnumerable<TsysLicenseViewModel>> GetAllAsync()
  64. {
  65. var pagedList = await _repository.GetAllAsync();
  66. var contentDtoList = _mapper.Map<IEnumerable<TsysLicenseViewModel>>(pagedList.ToList());
  67. return contentDtoList.ToList();
  68. }
  69. public async Task<IEnumerable<TsysLicenseViewModel>> GetAllByOrgTypeAsync()
  70. {
  71. IEnumerable<TSYS_License> pagedList;
  72. //if (_claims.OrgTypeCode == "PARTY_TYPE_005")
  73. if (_claims.LicenseTypeCode == TsysLicenseType.SYSTEM)
  74. {
  75. pagedList = await _repository.GetAllAsync();
  76. }
  77. else
  78. {
  79. pagedList = await _repository.GetByConditionAsync(a => a.C_Code.Equals(_claims.Linsence));
  80. }
  81. var contentDtoList = _mapper.Map<IEnumerable<TsysLicenseViewModel>>(pagedList.ToList());
  82. return contentDtoList.ToList();
  83. }
  84. public async Task<IEnumerable<TsysLicenseViewModel>> GetConditionAsync(TsysLicenseSearchModel searchModel)
  85. {
  86. var predicate = PredicateBuilder.New<TSYS_License>(true);//查询条件,推荐后台使用这种方式灵活筛选
  87. #region 添加条件查询
  88. if (!string.IsNullOrEmpty(searchModel.C_Status))
  89. {
  90. predicate = predicate.And(i => i.C_Status.Equals(searchModel.C_Status));
  91. }
  92. if (!string.IsNullOrEmpty(searchModel.C_Name))
  93. {
  94. predicate = predicate.And(i => i.C_Name.Contains(searchModel.C_Name));
  95. }
  96. #endregion
  97. var list = await _repository.GetPageAsync(predicate, "I_Sort", searchModel.IsPagination, searchModel.PageIndex, searchModel.PageSize);
  98. searchModel.TotalCount = list.Totals;
  99. var dtoList = _mapper.Map<List<TSYS_License>, List<TsysLicenseViewModel>>(list.Rows);
  100. return dtoList;
  101. }
  102. public async Task<TsysLicenseViewModel> GetByIdAsync(Guid id)
  103. {
  104. var content = await _repository.GetByIdAsync(id);
  105. var contentDto = _mapper.Map<TsysLicenseViewModel>(content);
  106. return contentDto;
  107. }
  108. public async Task UpdateAsync(Guid id, TsysLicenseUpdateModel updateModel)
  109. {
  110. var content = await _repository.GetByIdAsync(id);
  111. if (content == null)
  112. {
  113. throw new Exception("没有此数据");
  114. }
  115. content.C_LastUpdatedBy = _claims.ApiUserId;
  116. content.D_LastUpdatedOn = DateTime.Now;
  117. _mapper.Map(updateModel, content, typeof(TsysLicenseUpdateModel), typeof(TSYS_License));
  118. _repository.Update(content);
  119. var result = await _repository.SaveAsync();
  120. if (!result)
  121. {
  122. throw new Exception("更新失败");
  123. }
  124. }
  125. public Task<int> UpdateOneAsync(TsysLicenseViewModel viewModel, params string[] fields)
  126. {
  127. throw new NotImplementedException();
  128. }
  129. public Task<bool> IsExistAsync(Guid id)
  130. {
  131. throw new NotImplementedException();
  132. }
  133. public Task<IEnumerable<TsysLicenseViewModel>> GetByConditionAsync(Expression<Func<TsysLicenseViewModel, bool>> expression)
  134. {
  135. throw new NotImplementedException();
  136. }
  137. public async Task CreateLicenseTypeOneAsync(TSYS_LicenseType content)
  138. {
  139. var model = _repository.GetLicenseTypeByCode(content.C_Code);
  140. if (model!=null)
  141. {
  142. throw new Exception("编号已存在");
  143. }
  144. content.C_CreateBy = _claims.ApiUserId;
  145. content.D_CreateOn = DateTime.Now;
  146. content.C_Status = "1";
  147. await _repository.CreateLicenseTypeAsync(content);
  148. var result = await _repository.SaveAsync();
  149. if (!result)
  150. {
  151. throw new Exception("创建失败");
  152. }
  153. }
  154. public async Task UpdateLicenseTypeAsync(string id, TSYS_LicenseType updateModel)
  155. {
  156. var content = _repository.GetLicenseTypeByCode(id);
  157. if (content == null)
  158. {
  159. throw new Exception("没有此数据");
  160. }
  161. content.C_Name = updateModel.C_Name;
  162. content.I_MaxMemberQty = updateModel.I_MaxMemberQty;
  163. content.I_MaxSpotQty = updateModel.I_MaxSpotQty;
  164. content.I_MaxFileSize = updateModel.I_MaxFileSize;
  165. content.I_Sort = updateModel.I_Sort;
  166. content.C_Remark = updateModel.C_Remark;
  167. content.C_LastUpdatedBy = _claims.ApiUserId;
  168. content.D_LastUpdatedOn = DateTime.Now;
  169. _repository.UpdateLicenseType(content);
  170. var result = await _repository.SaveAsync();
  171. if (!result)
  172. {
  173. throw new Exception("更新失败");
  174. }
  175. }
  176. public Task<IList<TsysLicenseTypeViewModel>> GetALLLicenseType()
  177. {
  178. var v = _repository.GetALLLicenseType();
  179. var list = _mapper.Map<IList<TsysLicenseTypeViewModel>>(v);
  180. return Task.FromResult(list) ;
  181. }
  182. public Task<TsysLicenseTypePrivViewModel> GetLicensePrivByType(string typeCode)
  183. {
  184. if(null == typeCode)
  185. return _repository.GetLicensePrivByType(_claims.LicenseTypeCode);
  186. else
  187. return _repository.GetLicensePrivByType(typeCode);
  188. }
  189. public Task<IEnumerable<LicenseTypePrivViewModel>> GetLicensePrivsByTypeIdAsync(string typeId)
  190. {
  191. var v = _repository.GetLicensePrivsByTypeIdAsync(typeId);
  192. return v;
  193. }
  194. public async Task CreateLicensePrivByType(string typeCode, IEnumerable<string> privCodes)
  195. {
  196. //if (typeCode.Equals("SYSTEM"))
  197. if (_claims.LicenseTypeCode != "SYSTEM" && typeCode.Equals("SYSTEM"))
  198. {
  199. throw new Exception("禁止设置管理员权限");
  200. }
  201. var privs = await _tsysLicenseTypePrivRepository.GetByConditionAsync(i=>i.C_LicenseTypeCode == typeCode);
  202. await _tsysLicenseTypePrivRepository.RemoveRangeAsync(privs);
  203. var licenseTypePrivs = new List<TSYS_LicenseTypePriv>();
  204. foreach (var permissionId in privCodes)
  205. {
  206. licenseTypePrivs.Add(
  207. new TSYS_LicenseTypePriv
  208. {
  209. C_LicenseTypeCode = typeCode,
  210. C_PrivilegeCode = permissionId,
  211. C_CreateBy = _claims.ApiUserId,
  212. D_CreateOn = DateTime.Now
  213. }
  214. );
  215. }
  216. await _tsysLicenseTypePrivRepository.CreateRangeAsync(licenseTypePrivs);
  217. }
  218. }
  219. }