123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- 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<ArticleGroup> _repository;
- private readonly string _connectionString;
- private readonly string _databaseTypeStr;
- private readonly IMapper _mapper;
- public ArticleGroupRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IMapper _mapper)
- {
- _databaseTypeStr = dbOptionsAccessor.CurrentValue.DatabaseType;
- _connectionString = dbOptionsAccessor.CurrentValue.ConnectionString;
- var databaseType = StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
- var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
- _repository = new Repository<ArticleGroup>(database);
- this._mapper = _mapper;
- }
- public IDatabase Database => _repository.Database;
- public string TableName => _repository.TableName;
- public List<TableColumn> TableColumns => _repository.TableColumns;
- public async Task<bool> ExistsAsync(int id)
- {
- return await _repository.ExistsAsync(id);
- }
- public async Task<bool> IsNameExistsAsync(string name)
- {
- return await _repository.ExistsAsync(Q.Where(nameof(ArticleGroup.Name), name));
- }
- public async Task<int> InsertAsync(ArticleGroup processGroup)
- {
- return await _repository.InsertAsync(processGroup);
- }
- public async Task<ArticleGroupResult> GetAsync(int id)
- {
- var result = new ArticleGroupResult();
- if (id > 0)
- {
- var articleGroup = await _repository.GetAsync(id);
- result = _mapper.Map<ArticleGroupResult>(articleGroup);
- }
- if (result == null)
- result = new ArticleGroupResult();
- result.StyleCssDictionary = new Dictionary<string, string>();
- 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<ArticleGroup> GetArticleGroupAsync(int id)
- {
- return await _repository.GetAsync(id);
- }
- public async Task<bool> UpdateAsync(ArticleGroup processGroup)
- {
- return await _repository.UpdateAsync(processGroup);
- }
- public async Task<IEnumerable<ArticleGroupUserListResult>> 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<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
- var database = new Database(databaseType, _connectionString);
- var connection = database.GetConnection();
- var result = await connection.QueryAsync<ArticleGroup>(sql);
- var list = new List<ArticleGroupUserListResult>();
- 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;
- }
- /// <summary>
- /// 添加
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- public async Task<ArticleGroup> 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;
- }
- /// <summary>
- /// 修改
- /// </summary>
- /// <param name="id"></param>
- /// <param name="request"></param>
- /// <returns></returns>
- public async Task<bool> 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);
- }
- /// <summary>
- /// 删除
- /// </summary>
- /// <param name="id"></param>
- /// <returns></returns>
- public async Task<bool> DeleteAsync(int id)
- {
- var group = await GetAsync(id);
- if (group == null) throw new BusinessException("该文章组不存在");
- return await _repository.DeleteAsync(id);
- }
- /// <summary>
- /// 根据样式获取导航
- /// </summary>
- /// <param name="styleCss"></param>
- /// <returns></returns>
- public async Task<ArticleGroup> GetArticleGroupAsync(string styleCss)
- {
- return await _repository.GetAsync(Q.Where(nameof(ArticleGroup.StyleCss), styleCss));
- }
- }
- }
|