ShwasherAsyncCrudAppService.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Linq.Dynamic.Core;
  5. using System.Threading.Tasks;
  6. using Abp.Application.Services.Dto;
  7. using Abp.Auditing;
  8. using Abp.Domain.Entities;
  9. using Abp.Domain.Repositories;
  10. using Abp.Extensions;
  11. using Abp.Linq;
  12. using Abp.ObjectMapping;
  13. using ShwasherSys.Lambda;
  14. using IwbZero.AppServiceBase;
  15. using IwbZero.IdentityFramework;
  16. using Microsoft.AspNet.Identity;
  17. namespace ShwasherSys
  18. {
  19. public abstract class ShwasherAsyncCrudAppService<TEntity, TEntityDto, TPrimaryKey, TGetAllInput, TCreateInput, TUpdateInput, TGetInput, TDeleteInput>
  20. : IwbCrudAppServiceBase<TEntity, TEntityDto, TPrimaryKey, TGetAllInput, TCreateInput, TUpdateInput>
  21. where TEntity : class, IEntity<TPrimaryKey>
  22. where TEntityDto : IEntityDto<TPrimaryKey>
  23. where TUpdateInput : IEntityDto<TPrimaryKey>
  24. where TGetInput : IEntityDto<TPrimaryKey>
  25. where TDeleteInput : IEntityDto<TPrimaryKey>
  26. where TGetAllInput : IPagedRequest
  27. {
  28. public IAsyncQueryableExecuter AsyncQueryableExecuter { get; set; }
  29. protected string KeyFiledName { get; set; }
  30. protected virtual bool KeyIsAuto { get; set; } = true;
  31. protected virtual string ExistMessage { get; set; } = "编号已存在,请检查后再操作!";
  32. protected virtual string NotExistMessage { get; set; } = "编号不存在,请检查后再操作!";
  33. protected ShwasherAsyncCrudAppService(IRepository<TEntity, TPrimaryKey> repository, string keyFiledName = null)
  34. : base(repository)
  35. {
  36. AsyncQueryableExecuter = NullAsyncQueryableExecuter.Instance;
  37. LocalizationSourceName = ShwasherConsts.LocalizationSourceName;
  38. ObjectMapper = NullObjectMapper.Instance;
  39. KeyFiledName = keyFiledName;
  40. }
  41. [DisableAuditing]
  42. public virtual async Task<TEntityDto> Get(TGetInput input)
  43. {
  44. CheckGetPermission();
  45. if (!KeyFiledName.IsNullOrEmpty() && input.Id == null)
  46. {
  47. return await GetDtoByNoAsync(input.GetFiledValue<string>(KeyFiledName));
  48. }
  49. var entity = await GetEntityByIdAsync(input.Id);
  50. return MapToEntityDto(entity);
  51. }
  52. [DisableAuditing]
  53. public virtual async Task<PagedResultDto<TEntityDto>> GetAll(TGetAllInput input)
  54. {
  55. CheckGetAllPermission();
  56. var query = CreateFilteredQuery(input);
  57. var property = typeof(TEntity).GetProperty("IsLock");
  58. if (property != null)
  59. {
  60. LambdaObject objLambdaObject = new LambdaObject()
  61. {
  62. FieldType = LambdaFieldType.S,
  63. FieldName = "IsLock",
  64. FieldValue = "N",
  65. ExpType = LambdaExpType.Equal
  66. };
  67. var expIsLock = objLambdaObject.GetExp<TEntity>();
  68. query = query.Where(expIsLock);
  69. }
  70. //if (!input.KeyWords.IsNullOrEmpty())
  71. //{
  72. // LambdaObject obj = new LambdaObject()
  73. // {
  74. // FieldType = LambdaFieldType.S,
  75. // FieldName = input.KeyField,
  76. // FieldValue = input.KeyWords,
  77. // ExpType = LambdaExpType.Contains
  78. // };
  79. // var exp = obj.GetExp<TEntity>();
  80. // query = query.Where(exp);
  81. //}
  82. if (input.SearchList != null && input.SearchList.Count > 0)
  83. {
  84. List<LambdaObject> objList = new List<LambdaObject>();
  85. foreach (var o in input.SearchList)
  86. {
  87. if (o.KeyWords.IsNullOrEmpty())
  88. continue;
  89. object keyWords = o.KeyWords;
  90. objList.Add(new LambdaObject
  91. {
  92. FieldType = (LambdaFieldType)o.FieldType,
  93. FieldName = o.KeyField,
  94. FieldValue = keyWords,
  95. ExpType = (LambdaExpType)o.ExpType
  96. });
  97. }
  98. var exp = objList.GetExp<TEntity>();
  99. query = query.Where(exp);
  100. }
  101. var totalCount = await AsyncQueryableExecuter.CountAsync(query);
  102. query = ApplySorting(query, input);
  103. query = ApplyPaging(query, input);
  104. var entities = await AsyncQueryableExecuter.ToListAsync(query);
  105. var dtos = new PagedResultDto<TEntityDto>(
  106. totalCount,
  107. entities.Select(MapToEntityDto).ToList()
  108. );
  109. return dtos;
  110. }
  111. public virtual async Task<TEntityDto> Create(TCreateInput input)
  112. {
  113. CheckCreatePermission();
  114. return await CreateEntity(input);
  115. }
  116. public virtual async Task<TEntityDto> Update(TUpdateInput input)
  117. {
  118. CheckCreatePermission();
  119. return await UpdateEntity(input);
  120. }
  121. public virtual Task Delete(TDeleteInput input)
  122. {
  123. CheckDeletePermission();
  124. return Repository.DeleteAsync(input.Id);
  125. }
  126. protected virtual IQueryable<TEntity> ApplyFilter(IQueryable<TEntity> query, TGetAllInput input)
  127. {
  128. if (input.SearchList != null && input.SearchList.Count > 0)
  129. {
  130. List<LambdaObject> objList = new List<LambdaObject>();
  131. foreach (var o in input.SearchList)
  132. {
  133. if (o.KeyWords.IsNullOrEmpty())
  134. continue;
  135. object keyWords = o.KeyWords;
  136. objList.Add(new LambdaObject
  137. {
  138. FieldType = (LambdaFieldType)o.FieldType,
  139. FieldName = o.KeyField,
  140. FieldValue = keyWords,
  141. ExpType = (LambdaExpType)o.ExpType
  142. });
  143. }
  144. var exp = objList.GetExp<TEntity>();
  145. query = query.Where(exp);
  146. }
  147. return query;
  148. }
  149. /// <summary>
  150. /// Should apply sorting if needed.
  151. /// </summary>
  152. /// <param name="query">The query.</param>
  153. /// <param name="input">The input.</param>
  154. protected new virtual IQueryable<TEntity> ApplySorting(IQueryable<TEntity> query, TGetAllInput input)
  155. {
  156. //Try to sort query if available
  157. // ReSharper disable once SuspiciousTypeConversion.Global
  158. if (input is ISortedResultRequest sortInput)
  159. {
  160. if (!sortInput.Sorting.IsNullOrWhiteSpace())
  161. {
  162. return query.OrderBy(sortInput.Sorting);
  163. }
  164. }
  165. //IQueryable.Task requires sorting, so we should sort if Take will be used.
  166. // ReSharper disable once IsExpressionAlwaysTrue
  167. if (input is ILimitedResultRequest)
  168. {
  169. if (!input.Sorting.IsNullOrWhiteSpace())
  170. {
  171. return query.OrderBy(input.Sorting);
  172. }
  173. return query.OrderByDescending(e => e.Id);
  174. }
  175. //No sorting
  176. return query;
  177. }
  178. /// <summary>
  179. /// Should apply paging if needed.
  180. /// </summary>
  181. /// <param name="query">The query.</param>
  182. /// <param name="input">The input.</param>
  183. protected new virtual IQueryable<TEntity> ApplyPaging(IQueryable<TEntity> query, TGetAllInput input)
  184. {
  185. //Try to use paging if available
  186. if (input is IPagedResultRequest pagedInput)
  187. {
  188. return query.Skip(pagedInput.SkipCount).Take(pagedInput.MaxResultCount);
  189. }
  190. //No paging
  191. return query;
  192. }
  193. protected virtual Task<TEntity> GetEntityByIdAsync(TPrimaryKey id)
  194. {
  195. return Repository.GetAsync(id);
  196. }
  197. protected virtual Task<TEntity> GetEntityByNoAsync(string no)
  198. {
  199. if (KeyFiledName.IsNullOrEmpty())
  200. {
  201. CheckErrors(IwbIdentityResult.Failed("编码/编号字段不明确,请检查后再操作!"));
  202. }
  203. LambdaObject obj = new LambdaObject()
  204. {
  205. FieldType = LambdaFieldType.S,
  206. FieldName = KeyFiledName,
  207. FieldValue = no,
  208. ExpType = LambdaExpType.Equal
  209. };
  210. var exp = obj.GetExp<TEntity>();
  211. return Repository.FirstOrDefaultAsync(exp);
  212. }
  213. protected virtual async Task<TEntityDto> GetDtoByNoAsync(string no)
  214. {
  215. var entity = await GetEntityByNoAsync(no);
  216. return MapToEntityDto(entity);
  217. }
  218. #region 使用ABP实体
  219. protected async Task<TEntityDto> CreateEntity1(TCreateInput input)
  220. {
  221. if (!KeyFiledName.IsNullOrEmpty())
  222. {
  223. if (KeyIsAuto)
  224. {
  225. input.SetFiledValue(KeyFiledName, Guid.NewGuid().ToString("N"));
  226. }
  227. else
  228. {
  229. LambdaObject obj = new LambdaObject()
  230. {
  231. FieldType = LambdaFieldType.S,
  232. FieldName = KeyFiledName,
  233. FieldValue = input.GetFiledValue<string>(KeyFiledName),
  234. ExpType = LambdaExpType.Equal
  235. };
  236. var exp = obj.GetExp<TEntity>();
  237. if (await Repository.FirstOrDefaultAsync(exp) != null)
  238. {
  239. CheckErrors(IwbIdentityResult.Failed(ExistMessage));
  240. }
  241. }
  242. }
  243. var entity = MapToEntity(input);
  244. await Repository.InsertAsync(entity);
  245. await CurrentUnitOfWork.SaveChangesAsync();
  246. return MapToEntityDto(entity);
  247. }
  248. protected async Task<TEntityDto> UpdateEntity1(TUpdateInput input)
  249. {
  250. if (!KeyIsAuto && !KeyFiledName.IsNullOrEmpty())
  251. {
  252. LambdaObject obj = new LambdaObject()
  253. {
  254. FieldType = LambdaFieldType.S,
  255. FieldName = KeyFiledName,
  256. FieldValue = input.GetFiledValue<string>(KeyFiledName),
  257. ExpType = LambdaExpType.Equal
  258. };
  259. var exp = obj.GetExp<TEntity>();
  260. if (await Repository.FirstOrDefaultAsync(exp) == null)
  261. {
  262. CheckErrors(IwbIdentityResult.Failed(NotExistMessage));
  263. }
  264. }
  265. var entity = await GetEntityByIdAsync(input.Id);
  266. MapToEntity(input, entity);
  267. await CurrentUnitOfWork.SaveChangesAsync();
  268. return MapToEntityDto(entity);
  269. }
  270. #endregion
  271. protected async Task<TEntityDto> CreateEntity(TCreateInput input)
  272. {
  273. if (!KeyFiledName.IsNullOrEmpty())
  274. {
  275. if (KeyIsAuto)
  276. {
  277. input.SetFiledValue(KeyFiledName, Guid.NewGuid().ToString("N"));
  278. }
  279. else
  280. {
  281. LambdaObject obj = new LambdaObject()
  282. {
  283. FieldType = LambdaFieldType.S,
  284. FieldName = KeyFiledName,
  285. FieldValue = input.GetFiledValue<string>(KeyFiledName),
  286. ExpType = LambdaExpType.Equal
  287. };
  288. var exp = obj.GetExp<TEntity>();
  289. if (await Repository.FirstOrDefaultAsync(exp) != null)
  290. {
  291. CheckErrors(IwbIdentityResult.Failed(ExistMessage));
  292. }
  293. }
  294. }
  295. var entity = MapToEntity(input);
  296. #region shwasher temp
  297. AddCommonPropertyValue("TimeCreated", DateTime.Now, ref entity);
  298. AddCommonPropertyValue("TimeLastMod", DateTime.Now, ref entity);
  299. AddCommonPropertyValue("UserIDLastMod", AbpSession.UserName, ref entity);
  300. AddCommonPropertyValue("CreatorUserId", AbpSession.UserName, ref entity);
  301. AddCommonPropertyValue("IsLock", "N", ref entity);
  302. #endregion
  303. await Repository.InsertAsync(entity);
  304. await CurrentUnitOfWork.SaveChangesAsync();
  305. return MapToEntityDto(entity);
  306. }
  307. protected async Task<TEntityDto> UpdateEntity(TUpdateInput input)
  308. {
  309. if (!KeyIsAuto && !KeyFiledName.IsNullOrEmpty())
  310. {
  311. LambdaObject obj = new LambdaObject()
  312. {
  313. FieldType = LambdaFieldType.S,
  314. FieldName = KeyFiledName,
  315. FieldValue = input.GetFiledValue<string>(KeyFiledName),
  316. ExpType = LambdaExpType.Equal
  317. };
  318. var exp = obj.GetExp<TEntity>();
  319. if (await Repository.FirstOrDefaultAsync(exp) == null)
  320. {
  321. CheckErrors(IwbIdentityResult.Failed(NotExistMessage));
  322. }
  323. }
  324. var entity = await GetEntityByIdAsync(input.Id);
  325. #region shwasher temp
  326. GetCommonPropertyValue("TimeCreated", entity, ref input);
  327. GetCommonPropertyValue("TimeLastMod", entity, ref input);
  328. GetCommonPropertyValue("IsLock", entity, ref input);
  329. #endregion
  330. MapToEntity(input, entity);
  331. #region shwasher temp
  332. AddCommonPropertyValue("TimeLastMod", DateTime.Now, ref entity);
  333. AddCommonPropertyValue("UserIDLastMod", AbpSession.UserName, ref entity);
  334. AddCommonPropertyValue("IsLock", "N", ref entity);
  335. #endregion
  336. await CurrentUnitOfWork.SaveChangesAsync();
  337. return MapToEntityDto(entity);
  338. }
  339. /// <summary>
  340. /// shwasher temp GetCommonProperty
  341. /// </summary>
  342. /// <param name="pcPropertyName"></param>
  343. /// <param name="entity"></param>
  344. /// <param name="input"></param>
  345. protected void GetCommonPropertyValue(string pcPropertyName, TEntity entity, ref TUpdateInput input)
  346. {
  347. var property = input.GetType().GetProperty(pcPropertyName);
  348. var value= entity.GetType().GetValue(pcPropertyName);
  349. if (property != null&& value!=null)
  350. {
  351. if (property.PropertyType.IsGenericType && property.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
  352. {
  353. property.SetValue(input,
  354. Convert.ChangeType(value, Nullable.GetUnderlyingType(property.PropertyType) ?? throw new InvalidOperationException(), null));
  355. }
  356. else
  357. {
  358. property.SetValue(input, Convert.ChangeType(value, property.PropertyType), null);
  359. }
  360. }
  361. }
  362. /// <summary>
  363. /// shwasher temp AddCommonProperty
  364. /// </summary>
  365. /// <param name="pcPropertyName"></param>
  366. /// <param name="value"></param>
  367. /// <param name="entity"></param>
  368. protected void AddCommonPropertyValue(string pcPropertyName, object value, ref TEntity entity)
  369. {
  370. var property = entity.GetType().GetProperty(pcPropertyName);
  371. if (property != null)
  372. {
  373. if (property.PropertyType.IsGenericType && property.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
  374. {
  375. property.SetValue(entity,
  376. Convert.ChangeType(value, Nullable.GetUnderlyingType(property.PropertyType) ?? throw new InvalidOperationException(), null));
  377. }
  378. else
  379. {
  380. property.SetValue(entity, Convert.ChangeType(value, property.PropertyType), null);
  381. }
  382. }
  383. }
  384. public virtual async Task<TEntityDto> LockRecord(TDeleteInput input)
  385. {
  386. CheckDeletePermission();
  387. var entity = await GetEntityByIdAsync(input.Id);
  388. #region shwasher temp
  389. AddCommonPropertyValue("TimeLastMod", DateTime.Now, ref entity);
  390. AddCommonPropertyValue("UserIDLastMod", AbpSession.UserName, ref entity);
  391. AddCommonPropertyValue("IsLock", "Y", ref entity);
  392. #endregion
  393. await CurrentUnitOfWork.SaveChangesAsync();
  394. return MapToEntityDto(entity);
  395. }
  396. protected virtual void CheckErrors(IdentityResult identityResult)
  397. {
  398. identityResult.CheckErrors(LocalizationManager);
  399. }
  400. }
  401. #region AppService
  402. public abstract class ShwasherAsyncCrudAppService<TEntity, TEntityDto>
  403. : ShwasherAsyncCrudAppService<TEntity, TEntityDto, int>
  404. where TEntity : class, IEntity<int>
  405. where TEntityDto : IEntityDto<int>
  406. {
  407. protected ShwasherAsyncCrudAppService(IRepository<TEntity, int> repository, string keyFiledName = null)
  408. : base(repository)
  409. {
  410. KeyFiledName = keyFiledName;
  411. }
  412. }
  413. public abstract class ShwasherAsyncCrudAppService<TEntity, TEntityDto, TPrimaryKey>
  414. : ShwasherAsyncCrudAppService<TEntity, TEntityDto, TPrimaryKey, IPagedRequest>
  415. where TEntity : class, IEntity<TPrimaryKey>
  416. where TEntityDto : IEntityDto<TPrimaryKey>
  417. {
  418. protected ShwasherAsyncCrudAppService(IRepository<TEntity, TPrimaryKey> repository, string keyFiledName = null)
  419. : base(repository)
  420. {
  421. KeyFiledName = keyFiledName;
  422. }
  423. }
  424. public abstract class ShwasherAsyncCrudAppService<TEntity, TEntityDto, TPrimaryKey, TGetAllInput>
  425. : ShwasherAsyncCrudAppService<TEntity, TEntityDto, TPrimaryKey, TGetAllInput, TEntityDto, TEntityDto>
  426. where TEntity : class, IEntity<TPrimaryKey>
  427. where TEntityDto : IEntityDto<TPrimaryKey>
  428. where TGetAllInput : IPagedRequest
  429. {
  430. protected ShwasherAsyncCrudAppService(IRepository<TEntity, TPrimaryKey> repository, string keyFiledName = null)
  431. : base(repository)
  432. {
  433. KeyFiledName = keyFiledName;
  434. }
  435. }
  436. public abstract class ShwasherAsyncCrudAppService<TEntity, TEntityDto, TPrimaryKey, TGetAllInput, TCreateInput>
  437. : ShwasherAsyncCrudAppService<TEntity, TEntityDto, TPrimaryKey, TGetAllInput, TCreateInput, TCreateInput>
  438. where TGetAllInput : IPagedRequest
  439. where TEntity : class, IEntity<TPrimaryKey>
  440. where TEntityDto : IEntityDto<TPrimaryKey>
  441. where TCreateInput : IEntityDto<TPrimaryKey>
  442. {
  443. protected ShwasherAsyncCrudAppService(IRepository<TEntity, TPrimaryKey> repository, string keyFiledName = null)
  444. : base(repository)
  445. {
  446. KeyFiledName = keyFiledName;
  447. }
  448. }
  449. public abstract class ShwasherAsyncCrudAppService<TEntity, TEntityDto, TPrimaryKey, TGetAllInput, TCreateInput, TUpdateInput>
  450. : ShwasherAsyncCrudAppService<TEntity, TEntityDto, TPrimaryKey, TGetAllInput, TCreateInput, TUpdateInput, EntityDto<TPrimaryKey>>
  451. where TEntity : class, IEntity<TPrimaryKey>
  452. where TEntityDto : IEntityDto<TPrimaryKey>
  453. where TUpdateInput : IEntityDto<TPrimaryKey>
  454. where TGetAllInput : IPagedRequest
  455. {
  456. protected ShwasherAsyncCrudAppService(IRepository<TEntity, TPrimaryKey> repository, string keyFiledName = null)
  457. : base(repository)
  458. {
  459. KeyFiledName = keyFiledName;
  460. }
  461. }
  462. public abstract class ShwasherAsyncCrudAppService<TEntity, TEntityDto, TPrimaryKey, TGetAllInput, TCreateInput, TUpdateInput, TGetInput>
  463. : ShwasherAsyncCrudAppService<TEntity, TEntityDto, TPrimaryKey, TGetAllInput, TCreateInput, TUpdateInput, TGetInput, EntityDto<TPrimaryKey>>
  464. where TEntity : class, IEntity<TPrimaryKey>
  465. where TEntityDto : IEntityDto<TPrimaryKey>
  466. where TUpdateInput : IEntityDto<TPrimaryKey>
  467. where TGetInput : IEntityDto<TPrimaryKey>
  468. where TGetAllInput : IPagedRequest
  469. {
  470. protected ShwasherAsyncCrudAppService(IRepository<TEntity, TPrimaryKey> repository, string keyFiledName = null)
  471. : base(repository)
  472. {
  473. KeyFiledName = keyFiledName;
  474. }
  475. }
  476. #endregion
  477. }