TispSpotUserService.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. using AutoMapper;
  2. using Ropin.Inspection.Common.Accessor.Interface;
  3. using Ropin.Inspection.Model.Entities;
  4. using Ropin.Inspection.Model.SearchModel;
  5. using Ropin.Inspection.Model.ViewModel;
  6. using Ropin.Inspection.Repository;
  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 TispSpotUserService : ITispSpotUserService
  18. {
  19. private readonly ITispSpotUserRepository _repository;
  20. private readonly IMapper _mapper;
  21. private readonly IClaimsAccessor _claims;
  22. public TispSpotUserService(IClaimsAccessor claims, ITispSpotUserRepository repository, IMapper mapper)
  23. {
  24. _repository = repository;
  25. _mapper = mapper;
  26. _claims = claims;
  27. }
  28. public async Task CreateOneAsync(TispSpotUserViewModel viewModel)
  29. {
  30. var content = _mapper.Map<TISP_SpotUser>(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. _repository.Create(content);
  36. var result = await _repository.SaveAsync();
  37. if (!result)
  38. {
  39. throw new Exception("创建失败");
  40. }
  41. }
  42. public async Task CreateSpotsUsersAsync(TispSpotUserCreateViewModel viewModel)
  43. {
  44. foreach (Guid spotId in viewModel.spotIdList)
  45. {
  46. await _repository.DeleteBySpotIdAsync(spotId);
  47. foreach (Guid userId in viewModel.userIdList)
  48. {
  49. //int iCount = await _repository.GetBySpotIdAndUserIdAsync(spotId, userId);
  50. //if (iCount >0)
  51. //{
  52. // continue;
  53. //}
  54. TISP_SpotUser spotUser = new TISP_SpotUser()
  55. {
  56. C_SpotCode = spotId,
  57. C_UserCode = userId,
  58. C_Remark = viewModel.C_Remark,
  59. C_CreateBy = _claims.ApiUserId,
  60. D_CreateOn = DateTime.Now,
  61. //C_LastUpdatedBy = _claims.ApiUserId,
  62. //D_LastUpdatedOn = DateTime.Now,
  63. //C_Status = '1',
  64. };
  65. _repository.Create(spotUser);
  66. }
  67. }
  68. var result = await _repository.SaveAsync();
  69. if (!result)
  70. {
  71. throw new Exception("创建失败");
  72. }
  73. }
  74. public async Task CreateSpotsUserAsync(TispSpotUserCreateViewModel viewModel)
  75. {
  76. foreach (Guid userId in viewModel.userIdList)
  77. {
  78. await _repository.DeleteByUserIdAsync(userId);
  79. foreach (Guid spotId in viewModel.spotIdList)
  80. {
  81. TISP_SpotUser spotUser = new TISP_SpotUser()
  82. {
  83. C_SpotCode = spotId,
  84. C_UserCode = userId,
  85. C_Remark = viewModel.C_Remark,
  86. C_CreateBy = _claims.ApiUserId,
  87. D_CreateOn = DateTime.Now,
  88. //C_LastUpdatedBy = _claims.ApiUserId,
  89. //D_LastUpdatedOn = DateTime.Now,
  90. //C_Status = '1',
  91. };
  92. _repository.Create(spotUser);
  93. }
  94. }
  95. var result = await _repository.SaveAsync();
  96. if (!result)
  97. {
  98. throw new Exception("创建失败");
  99. }
  100. }
  101. //public async Task UpdateSpotsUsersAsync(TispSpotUserCreateViewModel viewModel)
  102. //{
  103. //}
  104. public async Task<IEnumerable<TispSpotUsersViewModel>> GetSpotUsersAsyncByPage(int pageSize, int pageIndex)
  105. {
  106. var pagedList = await _repository.GetGroupBySpotAsyncByPage(pageSize, pageIndex);
  107. return pagedList;
  108. }
  109. public async Task<IEnumerable<TispSpotUsersViewModel>> GetSpotUsersAsyncByPage(TispSpotUserSearchModel searchViewModel)
  110. {
  111. var pagedList = await _repository.GetGroupBySpotAsyncByPage(searchViewModel);
  112. searchViewModel.TotalCount = pagedList == null ? 0 : pagedList.Totals;
  113. return pagedList.Rows;
  114. }
  115. public async Task DeleteAsync(Guid id)
  116. {
  117. var content = await _repository.GetByIdAsync(id);
  118. if (content == null)
  119. {
  120. throw new Exception("没有此巡检点内容");
  121. }
  122. _repository.Delete(content);
  123. var result = await _repository.SaveAsync();
  124. if (!result)
  125. {
  126. throw new Exception("删除失败");
  127. }
  128. }
  129. public async Task<IEnumerable<TispSpotUserViewModel>> GetAllAsync()
  130. {
  131. var pagedList = await _repository.GetAllAsync();
  132. var contentDtoList = _mapper.Map<IEnumerable<TispSpotUserViewModel>>(pagedList);
  133. return contentDtoList.ToList();
  134. }
  135. public async Task<IEnumerable<TispSpotUserViewModel>> GetAsyncByPage(int pageSize, int pageIndex)
  136. {
  137. var pagedList = await _repository.GetAsyncByPage(pageSize, pageIndex);
  138. return pagedList;
  139. }
  140. public Task<IEnumerable<TispSpotUserViewModel>> GetByConditionAsync(Expression<Func<TispSpotUserViewModel, bool>> expression)
  141. {
  142. throw new NotImplementedException();
  143. }
  144. public async Task<TispSpotUserViewModel> GetByIdAsync(Guid id)
  145. {
  146. var content = await _repository.GetByIdAsync(id);
  147. var contentDto = _mapper.Map<TispSpotUserViewModel>(content);
  148. return contentDto;
  149. }
  150. public async Task<IEnumerable<TispSpotUserViewModel>> GetBySpotIdAsync(Guid spotId)
  151. {
  152. var content = await _repository.GetBySpotIdAsync(spotId);
  153. return content;
  154. }
  155. public async Task<IEnumerable<TispUserStoreRouteSpotViewModel>> GetByUserIdAsync(Guid userId)
  156. {
  157. var content = await _repository.GetByUserIdAsync(userId);
  158. return content;
  159. }
  160. public Task<bool> IsExistAsync(Guid id)
  161. {
  162. throw new NotImplementedException();
  163. }
  164. public async Task UpdateAsync(Guid id, TispSpotUserUpdateViewModel updateModel)
  165. {
  166. var content = await _repository.GetByIdAsync(id);
  167. if (content == null)
  168. {
  169. throw new Exception("没有此巡检点人员");
  170. }
  171. //content.C_LastUpdatedBy = _claims.ApiUserId;
  172. //content.D_LastUpdatedOn = DateTime.Now;
  173. _mapper.Map(updateModel, content, typeof(TispSpotUserUpdateViewModel), typeof(TISP_SpotContent));
  174. _repository.Update(content);
  175. var result = await _repository.SaveAsync();
  176. if (!result)
  177. {
  178. throw new Exception("更新失败");
  179. }
  180. }
  181. public Task<int> UpdateOneAsync(TispSpotUserViewModel viewModel, params string[] fields)
  182. {
  183. throw new NotImplementedException();
  184. }
  185. }
  186. }