TmtnDevOpsContentService.cs 6.4 KB

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