using System.Collections.Generic; using System.Threading.Tasks; using GxPress.Common.AppOptions; using GxPress.Common.Exceptions; using GxPress.Common.Tools; using GxPress.Entity; using GxPress.Repository.Interface; using GxPress.Request.ArticleGroup; using Microsoft.Extensions.Options; using Datory; using Dapper; using GxPress.Result.ArticleGroupUser; namespace GxPress.Repository.Implement { public class ArticleGroupRepository : IArticleGroupRepository { private readonly Repository _repository; private readonly string _connectionString; private readonly string _databaseTypeStr; public ArticleGroupRepository(IOptionsMonitor dbOptionsAccessor) { _databaseTypeStr = dbOptionsAccessor.CurrentValue.DatabaseType; _connectionString = dbOptionsAccessor.CurrentValue.ConnectionString; var databaseType = StringUtils.ToEnum(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql); var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString); _repository = new Repository(database); } public IDatabase Database => _repository.Database; public string TableName => _repository.TableName; public List TableColumns => _repository.TableColumns; public async Task ExistsAsync(int id) { return await _repository.ExistsAsync(id); } public async Task IsNameExistsAsync(string name) { return await _repository.ExistsAsync(Q.Where(nameof(ArticleGroup.Name), name)); } public async Task InsertAsync(ArticleGroup processGroup) { return await _repository.InsertAsync(processGroup); } public async Task GetAsync(int id) { return await _repository.GetAsync(id); } public async Task UpdateAsync(ArticleGroup processGroup) { return await _repository.UpdateAsync(processGroup); } public async Task> GetListAsync(int userId) { string sql = $@"select * from tede_article_group where Id not in(SELECT ArticleGroupId FROM tede_article_group_user where UserId={userId}) and IsSystemDefault=0"; var databaseType = StringUtils.ToEnum(_databaseTypeStr, DatabaseType.MySql); var database = new Database(databaseType, _connectionString); var connection = database.GetConnection(); var result = await connection.QueryAsync(sql); var list = new List(); foreach (var item in result) { list.Add(new ArticleGroupUserListResult() { Id = item.Id, UserId = userId, ArticleGroupId = item.Id, ArticleGroupName = item.Name, IsSystemDefault = item.IsSystemDefault,Sort=item.Sort }); } return list; } /// /// 添加 /// /// /// public async Task AddAsync(ArticleGroupAddRequest request) { var exist = await IsNameExistsAsync(request.Name); if (exist) throw new BusinessException("分组名不能重复"); var group = new ArticleGroup { Name = request.Name, Sort = request.Sort, IsSystemDefault = request.IsSystemDefault }; group.Id = await InsertAsync(group); return group; } /// /// 修改 /// /// /// /// public async Task UpdateAsync(int id, ArticleGroupAddRequest request) { var group = await GetAsync(id); if (group == null) throw new BusinessException("该分组不存在"); if (group.Name != request.Name) { var exist = await IsNameExistsAsync(request.Name); if (exist) throw new BusinessException("分组名不能重复"); } group.IsSystemDefault = request.IsSystemDefault; group.Name = request.Name; group.Sort = request.Sort; return await UpdateAsync(group); } /// /// 删除 /// /// /// public async Task DeleteAsync(int id) { var group = await GetAsync(id); if (group == null) throw new BusinessException("该文章组不存在"); return await _repository.DeleteAsync(id); } } }