using System.Collections.Generic; using System.Threading.Tasks; using AutoMapper; using Dapper; using Datory; using GxPress.Common.AppOptions; using GxPress.Common.Tools; using GxPress.Repository.Interface.ArticleLabel; using GxPress.Result.App.ArticleLabel; using Microsoft.Extensions.Options; using SqlKata; namespace GxPress.Repository.Implement.ArticleLabel { public class ArticleLabelRepository : IArticleLabelRepository { private readonly Repository _repository; private readonly IMapper _mapper; private readonly string _connectionString; private readonly string _databaseTypeStr; public ArticleLabelRepository(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); _mapper = mapper; } public IDatabase Database => _repository.Database; public string TableName => _repository.TableName; public List TableColumns => _repository.TableColumns; public async Task DeleteAsync(Query query) { return await _repository.DeleteAsync(query) > 0; } public async Task> GetAllAsync(Query query) { return await _repository.GetAllAsync(query); } public async Task GetAsync(int id) { return await _repository.GetAsync(id); } public async Task GetAsync(SqlKata.Query query) { return await _repository.GetAsync(query); } public async Task InsertAsync(Entity.ArticleLabel.ArticleLabel articleLabel) { return await _repository.InsertAsync(articleLabel); } public async Task UpdateAsync(Entity.ArticleLabel.ArticleLabel articleLabel) { return await _repository.UpdateAsync(articleLabel); } public async Task UpdateAsync(Query query) { return await _repository.UpdateAsync(query) > 0; } /// /// 根据文章ID获取文章标签 /// /// /// public async Task> GetArticleLabelsAsync(int articleId) { var sql = $"select a.*,b.LabelName from ccpph.tede_article_inner_label a inner join ccpph.tede_article_label b on a.ArticleLabelId=b.Id where a.ArticleId={articleId}"; var databaseType = StringUtils.ToEnum(_databaseTypeStr, DatabaseType.MySql); var database = new Database(databaseType, _connectionString); var connection = database.GetConnection(); var items = await connection .QueryAsync(sql, (articleInnerLabelResult, articleLabel) => { articleInnerLabelResult.LabelName = articleLabel != null ? articleLabel.LabelName : ""; return articleInnerLabelResult; }, splitOn: "LabelName"); return items; } } }