TdevMaintenanceKnowledgeService.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. using AutoMapper;
  2. using LinqKit;
  3. using Ropin.Inspection.Common.Accessor.Interface;
  4. using Ropin.Inspection.Model;
  5. using Ropin.Inspection.Model.Entities;
  6. using Ropin.Inspection.Repository;
  7. using Ropin.Inspection.Repository.Interface;
  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 TdevMaintenanceKnowledgeService : ITdevMaintenanceKnowledgeService
  17. {
  18. private readonly ITdevMaintenanceKnowledgeRepository _repository;
  19. private readonly IMapper _mapper;
  20. private readonly IClaimsAccessor _claims;
  21. public TdevMaintenanceKnowledgeService(IClaimsAccessor claims, ITdevMaintenanceKnowledgeRepository repository, IMapper mapper)
  22. {
  23. _repository = repository;
  24. _mapper = mapper;
  25. _claims = claims;
  26. }
  27. public async Task CreateOneAsync(TdevMaintenanceKnowledgeViewModel viewModel)
  28. {
  29. var content = _mapper.Map<TDEV_MaintenanceKnowledge>(viewModel);
  30. content.C_ID = Guid.NewGuid().ToString();
  31. content.C_CreateBy = _claims.ApiUserId;
  32. content.D_CreateOn = DateTime.Now;
  33. content.C_Status = "1";
  34. _repository.Create(content);
  35. var result = await _repository.SaveAsync();
  36. if (!result)
  37. {
  38. throw new Exception("创建失败");
  39. }
  40. }
  41. public async Task DeleteAsync(Guid id)
  42. {
  43. var content = await _repository.GetByIdAsync(id);
  44. if (content == null)
  45. {
  46. throw new Exception("数据库中没有此数据");
  47. }
  48. //_repository.Delete(content);
  49. //var result = await _repository.SaveAsync();
  50. content.C_LastUpdatedBy = _claims.ApiUserId;
  51. content.D_LastUpdatedOn = DateTime.Now;
  52. content.C_Status = "0";
  53. _repository.Update(content);
  54. var result = await _repository.SaveAsync();
  55. if (!result)
  56. {
  57. throw new Exception("删除失败");
  58. }
  59. }
  60. public async Task DeleteAsync(string code)
  61. {
  62. var items = await _repository.GetByConditionAsync(C => C.C_ID == code);
  63. var content = items.FirstOrDefault();
  64. if (content == null)
  65. {
  66. throw new Exception("没有此数据");
  67. }
  68. content.C_LastUpdatedBy = _claims.ApiUserId;
  69. content.D_LastUpdatedOn = DateTime.Now;
  70. content.C_Status = "0";
  71. _repository.Update(content);
  72. var result = await _repository.SaveAsync();
  73. if (!result)
  74. {
  75. throw new Exception("删除失败");
  76. }
  77. }
  78. public async Task<IEnumerable<TdevMaintenanceKnowledgeViewModel>> GetAllAsync()
  79. {
  80. var pagedList = await _repository.GetAllAsync();
  81. var contentDtoList = _mapper.Map<IEnumerable<TdevMaintenanceKnowledgeViewModel>>(pagedList.Where(i => i.C_Status == "1").ToList());
  82. return contentDtoList.ToList();
  83. }
  84. public async Task<IEnumerable<TdevMaintenanceKnowledgeViewModel>> GetConditionAsync(TdevMaintenanceKnowledgeSearchModel searchModel)
  85. {
  86. var predicate = PredicateBuilder.New<TDEV_MaintenanceKnowledge>(true);//查询条件,推荐后台使用这种方式灵活筛选
  87. #region 添加条件查询
  88. //predicate = predicate.And(i => i.C_Status.Equals("1"));
  89. if (!string.IsNullOrEmpty(searchModel.C_DevTempCode))
  90. {
  91. predicate = predicate.And(i => i.C_DevTempCode.Equals(searchModel.C_DevTempCode));
  92. }
  93. if (!string.IsNullOrEmpty(searchModel.C_ID))
  94. {
  95. predicate = predicate.And(i => i.C_ID.Equals(searchModel.C_ID));
  96. }
  97. if (!string.IsNullOrEmpty(searchModel.C_Name))
  98. {
  99. predicate = predicate.And(i => i.C_Name.Contains(searchModel.C_Name));
  100. }
  101. #endregion
  102. var list = await _repository.GetPageAsync(predicate, "C_Name,-D_CreateOn", searchModel.IsPagination, searchModel.PageIndex, searchModel.PageSize);
  103. searchModel.TotalCount = list.Totals;
  104. var dtoList = _mapper.Map<List<TDEV_MaintenanceKnowledge>, List<TdevMaintenanceKnowledgeViewModel>>(list.Rows);
  105. return dtoList;
  106. }
  107. public async Task<TdevMaintenanceKnowledgeViewModel> GetByIdAsync(Guid id)
  108. {
  109. var content = await _repository.GetByIdAsync(id);
  110. var contentDto = _mapper.Map<TdevMaintenanceKnowledgeViewModel>(content);
  111. return contentDto;
  112. }
  113. public async Task UpdateAsync(string code, TdevMaintenanceKnowledgeUpdateModel updateModel)
  114. {
  115. var items = await _repository.GetByConditionAsync(C => C.C_ID == code);
  116. var content = items.FirstOrDefault();
  117. if (content == null)
  118. {
  119. throw new Exception("没有此数据");
  120. }
  121. content.C_LastUpdatedBy = _claims.ApiUserId;
  122. content.D_LastUpdatedOn = DateTime.Now;
  123. _mapper.Map(updateModel, content, typeof(TdevMaintenanceKnowledgeUpdateModel), typeof(TDEV_MaintenanceKnowledge));
  124. _repository.Update(content);
  125. var result = await _repository.SaveAsync();
  126. if (!result)
  127. {
  128. throw new Exception("更新失败");
  129. }
  130. }
  131. public Task<int> UpdateOneAsync(TdevMaintenanceKnowledgeViewModel viewModel, params string[] fields)
  132. {
  133. throw new NotImplementedException();
  134. }
  135. public Task<bool> IsExistAsync(Guid id)
  136. {
  137. throw new NotImplementedException();
  138. }
  139. public Task<IEnumerable<TdevMaintenanceKnowledgeViewModel>> GetByConditionAsync(Expression<Func<TdevMaintenanceKnowledgeViewModel, bool>> expression)
  140. {
  141. throw new NotImplementedException();
  142. }
  143. public Task UpdateAsync(string code, TpntAreaUpdateModel updateModel)
  144. {
  145. throw new NotImplementedException();
  146. }
  147. public Task<IEnumerable<TdevMaintenanceKnowledgeViewModel>> GetConditionAsync(TpntAreaSearchModel searchModel)
  148. {
  149. throw new NotImplementedException();
  150. }
  151. }
  152. }