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.Article; using System; using GxPress.EnumConst; using AutoMapper; using GxPress.Result.ArticleGroupUser; namespace GxPress.Repository.Implement { public class ArticleGroupRepository : IArticleGroupRepository { private readonly Repository _repository; private readonly string _connectionString; private readonly string _databaseTypeStr; private readonly IMapper _mapper; public ArticleGroupRepository(IOptionsMonitor dbOptionsAccessor, IMapper _mapper) { _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); this._mapper = _mapper; } 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) { var result = new ArticleGroupResult(); if (id > 0) { var articleGroup = await _repository.GetAsync(id); result = _mapper.Map(articleGroup); } if (result == null) result = new ArticleGroupResult(); result.StyleCssDictionary = new Dictionary(); Type t = typeof(AppPageStyleCssConst); Array arrays = Enum.GetValues(t); for (int i = 0; i < arrays.LongLength; i++) { var appPageStyleCssConst = (AppPageStyleCssConst)arrays.GetValue(i); var descriptionattrib = appPageStyleCssConst.GetDescriptionOriginal(); result.StyleCssDictionary.Add(descriptionattrib, appPageStyleCssConst.ToString()); } return result; } public async Task GetArticleGroupAsync(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 order by Sort desc"; 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, StyleCss = item.StyleCss, IsSystemDefault = item.IsSystemDefault }); } 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, IsMiddleLable = request.IsMiddleLable, IsSystemDefault = request.IsSystemDefault, MiddleLableId = request.MiddleLableId, StyleCss = request.StyleCss }; group.Id = await InsertAsync(group); return group; } /// /// 修改 /// /// /// /// public async Task UpdateAsync(int id, ArticleGroupAddRequest request) { var group = await GetArticleGroupAsync(id); if (group == null) throw new BusinessException("该分组不存在"); if (group.Name != request.Name) { var exist = await IsNameExistsAsync(request.Name); if (exist) throw new BusinessException("分组名不能重复"); } group.Name = request.Name; group.Sort = request.Sort; group.IsMiddleLable = request.IsMiddleLable; group.IsSystemDefault = request.IsSystemDefault; group.MiddleLableId = request.MiddleLableId; group.StyleCss = request.StyleCss; 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); } /// /// 根据样式获取导航 /// /// /// public async Task GetArticleGroupAsync(string styleCss) { return await _repository.GetAsync(Q.Where(nameof(ArticleGroup.StyleCss), styleCss)); } } }