TispRegionService.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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.Text;
  14. using System.Threading.Tasks;
  15. namespace Ropin.Inspection.Service
  16. {
  17. public class TispRegionService : ITispRegionService
  18. {
  19. private readonly ITispRegionRepository _repository;
  20. private readonly IMapper _mapper;
  21. private readonly IClaimsAccessor _claims;
  22. public TispRegionService(IClaimsAccessor claims, ITispRegionRepository repository, IMapper mapper)
  23. {
  24. _repository = repository;
  25. _mapper = mapper;
  26. _claims = claims;
  27. }
  28. public async Task CreateOneAsync(TispRegionViewModel viewModel)
  29. {
  30. var content = _mapper.Map<TISP_Region>(viewModel);
  31. content.C_CreateBy = _claims.ApiUserId;
  32. content.D_CreateOn = DateTime.Now;
  33. content.C_LastUpdatedBy = _claims.ApiUserId;
  34. content.D_LastUpdatedOn = DateTime.Now;
  35. content.C_Status = '1';
  36. _repository.Create(content);
  37. var result = await _repository.SaveAsync();
  38. if (!result)
  39. {
  40. throw new Exception("创建失败");
  41. }
  42. }
  43. public async Task DeleteAsync(Guid id)
  44. {
  45. var content = await _repository.GetByIdAsync(id);
  46. if (content == null)
  47. {
  48. throw new Exception("没有此巡检路线");
  49. }
  50. //_repository.Delete(content);
  51. //var result = await _repository.SaveAsync();
  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<TispRegionViewModel>> GetAllAsync()
  63. {
  64. var pagedList = await _repository.GetAllAsync();
  65. var contentDtoList = _mapper.Map<IEnumerable<TispRegionViewModel>>(pagedList.Where(i => i.C_Status == '1').ToList());
  66. return contentDtoList.ToList();
  67. }
  68. public async Task<IEnumerable<TispRegionViewModel>> GetRegionsConditionAsync(TispRegionSearchModel searchModel)
  69. {
  70. var predicate = PredicateBuilder.New<TISP_Region>(true);//查询条件,推荐后台使用这种方式灵活筛选
  71. #region 添加条件查询
  72. predicate = predicate.And(i => i.C_Status.Equals('1'));
  73. if (!string.IsNullOrEmpty(searchModel.C_Name))
  74. {
  75. predicate = predicate.And(i => i.C_Name.Contains(searchModel.C_Name));
  76. }
  77. #endregion
  78. var list = await _repository.GetPageAsync(predicate, "C_Name,-D_CreateOn", searchModel.IsPagination, searchModel.PageIndex, searchModel.PageSize);
  79. searchModel.TotalCount = list.Totals;
  80. var dtoList = _mapper.Map<List<TISP_Region>, List<TispRegionViewModel>>(list.Rows);
  81. return dtoList;
  82. }
  83. public async Task<TispRegionViewModel> GetByIdAsync(Guid id)
  84. {
  85. var content = await _repository.GetByIdAsync(id);
  86. var contentDto = _mapper.Map<TispRegionViewModel>(content);
  87. return contentDto;
  88. }
  89. public async Task UpdateAsync(Guid id, TispRegionUpdateViewModel updateModel)
  90. {
  91. var content = await _repository.GetByIdAsync(id);
  92. if (content == null)
  93. {
  94. throw new Exception("没有此巡检路线");
  95. }
  96. content.C_LastUpdatedBy = _claims.ApiUserId;
  97. content.D_LastUpdatedOn = DateTime.Now;
  98. _mapper.Map(updateModel, content, typeof(TispRegionUpdateViewModel), typeof(TISP_Region));
  99. _repository.Update(content);
  100. var result = await _repository.SaveAsync();
  101. if (!result)
  102. {
  103. throw new Exception("更新失败");
  104. }
  105. }
  106. public Task<int> UpdateOneAsync(TispRegionViewModel viewModel, params string[] fields)
  107. {
  108. throw new NotImplementedException();
  109. }
  110. public Task<bool> IsExistAsync(Guid id)
  111. {
  112. throw new NotImplementedException();
  113. }
  114. public Task<IEnumerable<TispRegionViewModel>> GetByConditionAsync(Expression<Func<TispRegionViewModel, bool>> expression)
  115. {
  116. throw new NotImplementedException();
  117. }
  118. }
  119. }