TispContentService.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. using AutoMapper;
  2. using LinqKit;
  3. using Ropin.Inspection.Common.Accessor.Interface;
  4. using Ropin.Inspection.Model.Entities;
  5. using Ropin.Inspection.Model.SearchModel;
  6. using Ropin.Inspection.Model.ViewModel;
  7. using Ropin.Inspection.Repository.Interface;
  8. using Ropin.Inspection.Service.Interface;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using System.Linq.Expressions;
  13. using System.Threading.Tasks;
  14. namespace Ropin.Inspection.Service
  15. {
  16. /// <summary>
  17. /// 巡检内容
  18. /// </summary>
  19. public class TispContentService : ITispContentService
  20. {
  21. private readonly ITispContentRepository _repository;
  22. private readonly IMapper _mapper;
  23. private readonly IClaimsAccessor _claims;
  24. private readonly IUnitOfWork _unitOfWork;
  25. public TispContentService(IClaimsAccessor claims, ITispContentRepository repository, IMapper mapper, IUnitOfWork unitOfWork)
  26. {
  27. _repository = repository;
  28. _mapper = mapper;
  29. _claims = claims;
  30. _unitOfWork = unitOfWork;
  31. }
  32. public async Task CreateOneAsync(TispContentViewModel viewModel)
  33. {
  34. var content = _mapper.Map<TISP_Content>(viewModel);
  35. content.C_ID = Guid.NewGuid();
  36. content.C_CreateBy = _claims.ApiUserId;
  37. content.D_CreateOn = DateTime.Now;
  38. content.C_Status = "1";
  39. _repository.Create(content);
  40. var result = await _repository.SaveAsync();
  41. if (!result)
  42. {
  43. throw new Exception("创建失败");
  44. }
  45. }
  46. public async Task<bool> CreateIspContent(List<TISP_Content> contentArray, List<TISP_ContentGroup> contentGroupArray, List<TISP_ContentGroupItem> groupItemArray)
  47. {
  48. bool result= false;
  49. try
  50. {
  51. if (contentArray.Count > 0)
  52. {
  53. result = await _unitOfWork.RegisterRangeNew(contentArray);
  54. }
  55. if (contentGroupArray.Count > 0)
  56. {
  57. result = await _unitOfWork.RegisterRangeNew(contentGroupArray);
  58. }
  59. if (groupItemArray.Count > 0)
  60. {
  61. result = await _unitOfWork.RegisterRangeNew(groupItemArray);
  62. }
  63. }
  64. catch
  65. {
  66. _unitOfWork.Rollback();
  67. throw;
  68. }
  69. finally
  70. {
  71. if (result)
  72. await _unitOfWork.CommitAsync();
  73. else
  74. _unitOfWork.Rollback();
  75. }
  76. return result;
  77. }
  78. public async Task DeleteAsync(Guid id)
  79. {
  80. var content = await _repository.GetByIdAsync(id);
  81. if (content == null)
  82. {
  83. throw new Exception("没有此巡检内容");
  84. }
  85. //_repository.Delete(content);
  86. content.C_LastUpdatedBy = _claims.ApiUserId;
  87. content.D_LastUpdatedOn = DateTime.Now;
  88. content.C_Status = "0";
  89. _repository.Update(content);
  90. var result = await _repository.SaveAsync();
  91. if (!result)
  92. {
  93. throw new Exception("删除失败");
  94. }
  95. }
  96. public async Task<IEnumerable<TispContentViewModel>> GetAllAsync(string storeCode)
  97. {
  98. var pagedList = await _repository.GetAllAsync();
  99. var contentDtoList = _mapper.Map<IEnumerable<TispContentViewModel>>(pagedList.Where(i=>i.C_StoreCode == storeCode));
  100. return contentDtoList.ToList();
  101. }
  102. public async Task<IEnumerable<TispContentViewModel>> GetContentsConditionAsync(TispContentSearchModel searchModel)
  103. {
  104. var predicate = PredicateBuilder.New<TISP_Content>(true);//查询条件,推荐后台使用这种方式灵活筛选
  105. #region 添加条件查询
  106. predicate = predicate.And(i => i.C_StoreCode.Equals(searchModel.C_StoreCode));
  107. if (!string.IsNullOrEmpty(searchModel.C_Status))
  108. {
  109. predicate = predicate.And(i => i.C_Status.Contains(searchModel.C_Status));
  110. }
  111. if (!string.IsNullOrEmpty(searchModel.C_Name))
  112. {
  113. predicate = predicate.And(i => i.C_Name.Contains(searchModel.C_Name));
  114. }
  115. #endregion
  116. var list = await _repository.GetPageAsync(predicate, "I_Sort", searchModel.IsPagination, searchModel.PageIndex, searchModel.PageSize);//I_Sort,D_CreateOn
  117. searchModel.TotalCount = list.Totals;
  118. var dtoList = _mapper.Map<List<TISP_Content>, List<TispContentViewModel>>(list.Rows);
  119. return dtoList;
  120. }
  121. public Task<IEnumerable<TispContentViewModel>> GetByConditionAsync(Expression<Func<TispContentViewModel, bool>> expression)
  122. {
  123. throw new NotImplementedException();
  124. }
  125. public async Task<TispContentViewModel> GetByIdAsync(Guid id)
  126. {
  127. var content = await _repository.GetByIdAsync(id);
  128. var contentDto = _mapper.Map<TispContentViewModel>(content);
  129. return contentDto;
  130. }
  131. public Task<bool> IsExistAsync(Guid id)
  132. {
  133. throw new NotImplementedException();
  134. }
  135. public async Task UpdateAsync(Guid id, TispContentUpdateViewModel updateModel)
  136. {
  137. var content = await _repository.GetByIdAsync(id);
  138. if (content == null)
  139. {
  140. throw new Exception("没有此巡检点");
  141. }
  142. _mapper.Map(updateModel, content, typeof(TispContentUpdateViewModel), typeof(TISP_Content));
  143. content.C_LastUpdatedBy = _claims.ApiUserId;
  144. content.D_LastUpdatedOn = DateTime.Now;
  145. content.C_Status = updateModel.C_Status;
  146. _repository.Update(content);
  147. var result = await _repository.SaveAsync();
  148. if (!result)
  149. {
  150. throw new Exception("更新失败");
  151. }
  152. }
  153. public Task<int> UpdateOneAsync(TispContentViewModel viewModel, params string[] fields)
  154. {
  155. throw new NotImplementedException();
  156. }
  157. }
  158. }