PhoneQuestionApplicationService.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. using Abp.Application.Services.Dto;
  2. using Abp.Auditing;
  3. using Abp.Authorization;
  4. using Abp.Domain.Repositories;
  5. using Abp.Runtime.Caching;
  6. using IwbZero.AppServiceBase;
  7. using IwbZero.Auditing;
  8. using System.Data.Entity;
  9. using System.Linq;
  10. using System.Threading.Tasks;
  11. using WeApp.Authorization;
  12. using WeApp.BasicInfo.PhoneQuestion.Dto;
  13. using WeApp.Configuration;
  14. namespace WeApp.BasicInfo.PhoneQuestion
  15. {
  16. [AbpAuthorize, AuditLog("电话提问维护")]
  17. public class PhoneQuestionAppService : IwbAsyncCrudAppService<PhoneQuestionInfo, PhoneQuestionDto, string, IwbPagedRequestDto, PhoneQuestionCreateDto, PhoneQuestionUpdateDto>, IPhoneQuestionAppService
  18. {
  19. public PhoneQuestionAppService(
  20. ICacheManager cacheManager,
  21. IRepository<PhoneQuestionInfo, string> repository, IRepository<PhoneAnswerInfo> answerRepository) : base(repository, "Id")
  22. {
  23. AnswerRepository = answerRepository;
  24. CacheManager = cacheManager;
  25. }
  26. public IRepository<PhoneAnswerInfo> AnswerRepository { get; }
  27. protected override bool KeyIsAuto { get; set; } = false;
  28. #region CURD
  29. [AbpAuthorize(PermissionNames.PagesBasicMgPhoneQuestionMgCreate)]
  30. public override async Task Create(PhoneQuestionCreateDto input)
  31. {
  32. input.Id = await AppGuidManager.GetNextRecordIdAsync(DataLibType.PhoneQuestion);
  33. input.Type = PhoneQuestionTypeDefinition.Default;
  34. if (input.Answers.Any())
  35. {
  36. foreach (var answerDto in input.Answers)
  37. {
  38. var answer = new PhoneAnswerInfo()
  39. {
  40. Content = answerDto.Content,
  41. Keywords = answerDto.Keywords,
  42. Type = answerDto.Type,
  43. QuestionNo = input.Id
  44. };
  45. await AnswerRepository.InsertAsync(answer);
  46. }
  47. }
  48. await CreateEntity(input);
  49. }
  50. [AbpAuthorize(PermissionNames.PagesBasicMgPhoneQuestionMgUpdate)]
  51. public override async Task Update(PhoneQuestionUpdateDto input)
  52. {
  53. var dto = await UpdateEntity(input);
  54. if (dto != null && input.Answers.Any())
  55. {
  56. var answerOld = await AnswerRepository.GetAll().Where(a => a.QuestionNo == dto.Id).Select(a => a.Id)
  57. .ToListAsync();
  58. foreach (var answerDto in input.Answers)
  59. {
  60. if (answerDto.Id == null || answerDto.Id == 0)
  61. {
  62. var answer = new PhoneAnswerInfo()
  63. {
  64. Content = answerDto.Content,
  65. Keywords = answerDto.Keywords,
  66. Type = answerDto.Type,
  67. QuestionNo = dto.Id
  68. };
  69. await AnswerRepository.InsertAsync(answer);
  70. }
  71. else
  72. {
  73. var answer = await AnswerRepository.FirstOrDefaultAsync(a => a.Id == answerDto.Id);
  74. if (answer != null)
  75. {
  76. answerOld.Remove(answer.Id);
  77. answer.Keywords = answerDto.Keywords;
  78. answer.Content = answerDto.Content;
  79. answer.Type = answerDto.Type;
  80. answer.QuestionNo = dto.Id;
  81. }
  82. await AnswerRepository.UpdateAsync(answer);
  83. }
  84. }
  85. if (answerOld.Any())
  86. {
  87. await AnswerRepository.DeleteAsync(a => answerOld.Contains(a.Id));
  88. }
  89. }
  90. }
  91. [AbpAuthorize(PermissionNames.PagesBasicMgPhoneQuestionMgDelete)]
  92. public override Task Delete(EntityDto<string> input)
  93. {
  94. return DeleteEntity(input);
  95. }
  96. [DisableAuditing]
  97. public override async Task<PagedResultDto<PhoneQuestionDto>> GetAll(IwbPagedRequestDto input)
  98. {
  99. var query = CreateFilteredQuery(input);
  100. query = ApplyFilter(query, input);
  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 dtoList = new PagedResultDto<PhoneQuestionDto>(totalCount, entities.Select(MapToEntityDto).ToList());
  106. return dtoList;
  107. }
  108. #region GetEntity/Dto
  109. /// <summary>
  110. /// 查询实体Dto
  111. /// </summary>
  112. /// <param name="input"></param>
  113. /// <returns></returns>
  114. [DisableAuditing]
  115. public override async Task<PhoneQuestionDto> GetDto(EntityDto<string> input)
  116. {
  117. var entity = await GetEntity(input);
  118. return MapToEntityDto(entity);
  119. }
  120. /// <summary>
  121. /// 查询实体Dto
  122. /// </summary>
  123. /// <param name="id"></param>
  124. /// <returns></returns>
  125. [DisableAuditing]
  126. public override async Task<PhoneQuestionDto> GetDtoById(string id)
  127. {
  128. var entity = await GetEntityById(id);
  129. return MapToEntityDto(entity);
  130. }
  131. /// <summary>
  132. /// 查询实体Dto(需指明自定义字段)
  133. /// </summary>
  134. /// <param name="no"></param>
  135. /// <returns></returns>
  136. [DisableAuditing]
  137. public override async Task<PhoneQuestionDto> GetDtoByNo(string no)
  138. {
  139. var entity = await GetEntityByNo(no);
  140. return MapToEntityDto(entity);
  141. }
  142. /// <summary>
  143. /// 查询实体
  144. /// </summary>
  145. /// <param name="input"></param>
  146. /// <returns></returns>
  147. [DisableAuditing]
  148. public override async Task<PhoneQuestionInfo> GetEntity(EntityDto<string> input)
  149. {
  150. var entity = await GetEntityById(input.Id);
  151. return entity;
  152. }
  153. /// <summary>
  154. /// 查询实体
  155. /// </summary>
  156. /// <param name="id"></param>
  157. /// <returns></returns>
  158. [DisableAuditing]
  159. public override async Task<PhoneQuestionInfo> GetEntityById(string id)
  160. {
  161. return await Repository.FirstOrDefaultAsync(a => a.Id == id);
  162. }
  163. /// <summary>
  164. /// 查询实体(需指明自定义字段)
  165. /// </summary>
  166. /// <param name="no"></param>
  167. /// <returns></returns>
  168. [DisableAuditing]
  169. public override async Task<PhoneQuestionInfo> GetEntityByNo(string no)
  170. {
  171. //CheckGetPermission();
  172. if (string.IsNullOrEmpty(KeyFiledName))
  173. {
  174. ThrowError("NoKeyFieldName");
  175. }
  176. return await base.GetEntityByNo(no);
  177. }
  178. #endregion GetEntity/Dto
  179. #region Hide
  180. ///// <summary>
  181. ///// 根据给定的<see cref="IwbPagedRequestDto"/>创建 <see cref="IQueryable{PhoneQuestionInfo}"/>过滤查询.
  182. ///// </summary>
  183. ///// <param name="input">The input.</param>
  184. //protected override IQueryable<PhoneQuestionInfo> CreateFilteredQuery(IwbPagedRequestDto input)
  185. //{
  186. // var query = Repository.GetAll();
  187. // var pagedInput = input as IIwbPagedRequest;
  188. // if (pagedInput == null)
  189. // {
  190. // return query;
  191. // }
  192. // if (!string.IsNullOrEmpty(pagedInput.KeyWords))
  193. // {
  194. // object keyWords = pagedInput.KeyWords;
  195. // LambdaObject obj = new LambdaObject()
  196. // {
  197. // FieldType = (LambdaFieldType)pagedInput.FieldType,
  198. // FieldName = pagedInput.KeyField,
  199. // FieldValue = keyWords,
  200. // ExpType = (LambdaExpType)pagedInput.ExpType
  201. // };
  202. // var exp = obj.GetExp<PhoneQuestionInfo>();
  203. // query = exp != null ? query.Where(exp) : query;
  204. // }
  205. // if (pagedInput.SearchList != null && pagedInput.SearchList.Count > 0)
  206. // {
  207. // List<LambdaObject> objList = new List<LambdaObject>();
  208. // foreach (var o in pagedInput.SearchList)
  209. // {
  210. // if (string.IsNullOrEmpty(o.KeyWords))
  211. // continue;
  212. // object keyWords = o.KeyWords;
  213. // objList.Add(new LambdaObject
  214. // {
  215. // FieldType = (LambdaFieldType)o.FieldType,
  216. // FieldName = o.KeyField,
  217. // FieldValue = keyWords,
  218. // ExpType = (LambdaExpType)o.ExpType
  219. // });
  220. // }
  221. // var exp = objList.GetExp<PhoneQuestionInfo>();
  222. // query = exp != null ? query.Where(exp) : query;
  223. // }
  224. // return query;
  225. //}
  226. //protected override IQueryable<PhoneQuestionInfo> ApplySorting(IQueryable<PhoneQuestionInfo> query, IwbPagedRequestDto input)
  227. //{
  228. // return query.OrderBy(a => a.No);
  229. //}
  230. //protected override IQueryable<PhoneQuestionInfo> ApplyPaging(IQueryable<PhoneQuestionInfo> query, IwbPagedRequestDto input)
  231. //{
  232. // if (input is IPagedResultRequest pagedInput)
  233. // {
  234. // return query.Skip(pagedInput.SkipCount).Take(pagedInput.MaxResultCount);
  235. // }
  236. // return query;
  237. //}
  238. #endregion Hide
  239. #endregion CURD
  240. }
  241. }