TispContentService.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. public TispContentService(IClaimsAccessor claims, ITispContentRepository repository, IMapper mapper)
  25. {
  26. _repository = repository;
  27. _mapper = mapper;
  28. _claims = claims;
  29. }
  30. public async Task CreateOneAsync(TispContentViewModel viewModel)
  31. {
  32. var content = _mapper.Map<TISP_Content>(viewModel);
  33. content.C_ID = Guid.NewGuid();
  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. content.C_LastUpdatedBy = _claims.ApiUserId;
  53. content.D_LastUpdatedOn = DateTime.Now;
  54. content.C_Status = "0";
  55. _repository.Update(content);
  56. var result = await _repository.SaveAsync();
  57. if (!result)
  58. {
  59. throw new Exception("删除失败");
  60. }
  61. }
  62. public async Task<IEnumerable<TispContentViewModel>> GetAllAsync(string storeCode)
  63. {
  64. var pagedList = await _repository.GetAllAsync();
  65. var contentDtoList = _mapper.Map<IEnumerable<TispContentViewModel>>(pagedList.Where(i=>i.C_StoreCode == storeCode));
  66. return contentDtoList.ToList();
  67. }
  68. public async Task<IEnumerable<TispContentViewModel>> GetContentsConditionAsync(TispContentSearchModel searchModel)
  69. {
  70. var predicate = PredicateBuilder.New<TISP_Content>(true);//查询条件,推荐后台使用这种方式灵活筛选
  71. #region 添加条件查询
  72. predicate = predicate.And(i => i.C_StoreCode.Equals(searchModel.C_StoreCode));
  73. if (!string.IsNullOrEmpty(searchModel.C_Status))
  74. {
  75. predicate = predicate.And(i => i.C_Status.Contains(searchModel.C_Status));
  76. }
  77. if (!string.IsNullOrEmpty(searchModel.C_Name))
  78. {
  79. predicate = predicate.And(i => i.C_Name.Contains(searchModel.C_Name));
  80. }
  81. #endregion
  82. var list = await _repository.GetPageAsync(predicate, "I_Sort", searchModel.IsPagination, searchModel.PageIndex, searchModel.PageSize);//I_Sort,D_CreateOn
  83. searchModel.TotalCount = list.Totals;
  84. var dtoList = _mapper.Map<List<TISP_Content>, List<TispContentViewModel>>(list.Rows);
  85. return dtoList;
  86. }
  87. public Task<IEnumerable<TispContentViewModel>> GetByConditionAsync(Expression<Func<TispContentViewModel, bool>> expression)
  88. {
  89. throw new NotImplementedException();
  90. }
  91. public async Task<TispContentViewModel> GetByIdAsync(Guid id)
  92. {
  93. var content = await _repository.GetByIdAsync(id);
  94. var contentDto = _mapper.Map<TispContentViewModel>(content);
  95. return contentDto;
  96. }
  97. public Task<bool> IsExistAsync(Guid id)
  98. {
  99. throw new NotImplementedException();
  100. }
  101. public async Task UpdateAsync(Guid id, TispContentUpdateViewModel updateModel)
  102. {
  103. var content = await _repository.GetByIdAsync(id);
  104. if (content == null)
  105. {
  106. throw new Exception("没有此巡检点");
  107. }
  108. _mapper.Map(updateModel, content, typeof(TispContentUpdateViewModel), typeof(TISP_Content));
  109. content.C_LastUpdatedBy = _claims.ApiUserId;
  110. content.D_LastUpdatedOn = DateTime.Now;
  111. content.C_Status = updateModel.C_Status;
  112. _repository.Update(content);
  113. var result = await _repository.SaveAsync();
  114. if (!result)
  115. {
  116. throw new Exception("更新失败");
  117. }
  118. }
  119. public Task<int> UpdateOneAsync(TispContentViewModel viewModel, params string[] fields)
  120. {
  121. throw new NotImplementedException();
  122. }
  123. }
  124. }