using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.Transactions; using AutoMapper; using GxPress.Common.AppOptions; using GxPress.Common.Exceptions; using GxPress.Common.Tools; using GxPress.Entity; using GxPress.Repository.Interface; using GxPress.Request.ArticleGroupUser; using GxPress.Result.ArticleGroupUser; using Microsoft.Extensions.Options; using Datory; namespace GxPress.Repository.Implement { public class ArticleGroupUserRepository : IArticleGroupUserRepository { private readonly Repository _repository; private readonly Repository _articleGroupRepository; private readonly Repository _userRepository; private readonly IMapper _mapper; public ArticleGroupUserRepository(IOptionsMonitor dbOptionsAccessor, IMapper mapper) { var databaseType = StringUtils.ToEnum(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql); var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString); _repository = new Repository(database); _articleGroupRepository = new Repository(database); _userRepository = new Repository(database); _mapper = mapper; } public IDatabase Database => _repository.Database; public string TableName => _repository.TableName; public List TableColumns => _repository.TableColumns; /// /// 添加 /// /// /// public async Task InsertAsync(ArticleGroupUserInRequest request) { var user = await _userRepository.GetAsync(request.UserId); if (user == null) throw new BusinessException("用户不存在"); var articleGroup = await _articleGroupRepository.GetAsync(request.ArticleGroupId); if (articleGroup == null) throw new BusinessException("频道不存在"); //查询是否存在 var articleGroupUserCount = await _repository.CountAsync(Q.Where(nameof(ArticleGroupUser.UserId), request.UserId) .Where(nameof(ArticleGroupUser.ArticleGroupId), request.ArticleGroupId)); if (articleGroupUserCount > 0) throw new BusinessException("频道已存在"); var articleGroupUserEntity = new ArticleGroupUser { ArticleGroupId = request.ArticleGroupId, UserId = request.UserId }; return await _repository.InsertAsync(articleGroupUserEntity); } /// /// 添加 /// /// /// public async Task InsertAsync(ArticleGroupUserInNewRequest request) { var user = await _userRepository.GetAsync(request.UserId); if (user == null) throw new BusinessException("用户不存在"); try { using (var transactionScope = new TransactionScope()) { //删除 await _repository.DeleteAsync(Q.Where(nameof(ArticleGroupUser.UserId), request.UserId)); foreach (var item in request.ArticleGroupId) { var articleGroupUserEntity = new ArticleGroupUser { ArticleGroupId = item, UserId = request.UserId }; await _repository.InsertAsync(articleGroupUserEntity); } transactionScope.Complete(); } } catch { return false; } return true; } /// /// 删除用户频道 /// /// /// public async Task DeleteAsync(int id) { var articleGroup = await _repository.GetAsync(id); if (articleGroup == null) throw new BusinessException("频道不存在"); return await _repository.DeleteAsync(id); } /// /// 根据用户ID查询用户的频道 /// /// /// public async Task> GetListAsync(ArticleGroupUserSearchRequest request) { var listArray = new List(); var articleGroups = await _articleGroupRepository.GetAllAsync(Q.Where(nameof(ArticleGroup.IsSystemDefault), true)); foreach (var item in articleGroups) { listArray.Add(new ArticleGroupUserListResult() { Id = 0, UserId = request.UserId, ArticleGroupId = item.Id, ArticleGroupName = item.Name, StyleCss = item.StyleCss, IsSystemDefault = item.IsSystemDefault }); } var list = await _repository.GetAllAsync(Q.Where(nameof(ArticleGroupUser.UserId), request.UserId).OrderBy(nameof(ArticleGroup.CreatedDate))); listArray.AddRange(list.Select(n => _mapper.Map(n)).ToList()); foreach (var item in listArray) { var articleGroup = await _articleGroupRepository.GetAsync(item.ArticleGroupId); item.ArticleGroupName = articleGroup.Name; item.StyleCss = articleGroup.StyleCss; } return listArray.Distinct(); } } }