using System.Collections.Generic; using System.Threading.Tasks; using AutoMapper; using Datory; using GxPress.Common.AppOptions; using GxPress.Common.Page; using GxPress.Common.Tools; using GxPress.Repository.Interface.SystemLabel; using GxPress.Request.SystemLabel; using GxPress.Result.SystemLabel; using Microsoft.Extensions.Options; namespace GxPress.Repository.Implement.SystemLabel { public class SystemLabelRepository : ISystemLabelRepository { private readonly Repository _repository; private readonly IMapper _mapper; private readonly string _connectionString; private readonly string _databaseTypestr; private readonly Repository mediaLableRepository; public SystemLabelRepository(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; mediaLableRepository = new Repository(database); } public IDatabase Database => _repository.Database; public string TableName => _repository.TableName; public List TableColumns => _repository.TableColumns; public async Task GetAsync(int id) { return await _repository.GetAsync(id); } public async Task GetByNameAsync(string name) { return await _repository.GetAsync(Q.WhereLike(nameof(Entity.SystemLabel.SystemLabel.LabelName), $"%{name}%")); } public async Task DeleteAsync(int id) { return await _repository.DeleteAsync(id); } public async Task InsertAsync(Entity.SystemLabel.SystemLabel note) { return await _repository.InsertAsync(note); } public async Task UpdateAsync(SystemLabelUpRequest request) { if (request.Id > 0) { var model = await GetAsync(request.Id); if (request.IsDisable > 0) model.IsDisable = request.IsDisable == 1; if (request.IsPage > 0) model.IsPage = request.IsPage == 1; if (request.IsSkip > 0) model.IsSkip = request.IsSkip == 1; if (!string.IsNullOrEmpty(request.LabelName)) model.LabelName = request.LabelName; if (!string.IsNullOrEmpty(request.ActionUrl)) model.ActionUrl = request.ActionUrl; if (!string.IsNullOrEmpty(request.LabelNameDescribe)) model.LabelNameDescribe = request.LabelNameDescribe; if (!string.IsNullOrEmpty(request.Remark)) model.Remark = request.Remark; if (request.ResourceType > 0) model.ResourceType = request.ResourceType; if (request.Sort > 0) model.Sort = request.Sort; if (!string.IsNullOrEmpty(request.StyleType)) model.StyleType = request.StyleType; if (request.IsShowLabelName > 0) model.IsShowLabelName = request.IsShowLabelName == 1; if (request.PageSize > 0) model.PageSize = request.PageSize; if (request.IsStarLable > 0) model.IsStarLable = request.IsStarLable == 1; return await _repository.UpdateAsync(model); } return false; } public async Task UpdateAsync(SqlKata.Query query) { return await _repository.UpdateAsync(query) > 0; } /// /// 获取标签 /// /// /// public async Task> GetAllPageAsync(SystemLabelRequest request) { var result = new PagedList(); var query = Q.NewQuery(); query.Where(nameof(Entity.SystemLabel.SystemLabel.IsDisable), false); query.Where(nameof(Entity.SystemLabel.SystemLabel.IsStarLable),request.TypeValue); result.Total = await _repository.CountAsync(query); query.OrderByDesc(nameof(Entity.Navigations.Navigation.Sort)); query.ForPage(request.Page, request.PerPage); result.Items = await _repository.GetAllAsync(query); foreach (var item in result.Items) { query = Q.NewQuery(); query.Where(nameof(Entity.tede2.Media.MediaLable.LableId), item.Id); //获取资源数量 item.ResourceCount = await mediaLableRepository.CountAsync(query); } return result; } /// /// 获取明栏 /// /// /// public async Task> GetAllStarLablePageAsync(Common.Page.PageParameter request) { var result = new PagedList(); var query = Q.NewQuery(); query.Where(nameof(Entity.SystemLabel.SystemLabel.IsDisable), false); query.Where(nameof(Entity.SystemLabel.SystemLabel.IsStarLable), true); result.Total = await _repository.CountAsync(query); query.OrderByDesc(nameof(Entity.Navigations.Navigation.Sort)); query.ForPage(request.Page, request.PerPage); result.Items = await _repository.GetAllAsync(query); foreach (var item in result.Items) { query = Q.NewQuery(); query.Where(nameof(Entity.tede2.Media.MediaLable.LableId), item.Id); //获取资源数量 item.ResourceCount = await mediaLableRepository.CountAsync(query); } return result; } /// /// 获取明栏 /// /// public async Task> GetStarLableAllAsync() { var query = Q.NewQuery(); query.Where(nameof(Entity.SystemLabel.SystemLabel.IsDisable), false); query.Where(nameof(Entity.SystemLabel.SystemLabel.IsStarLable), true); query.OrderByDesc(nameof(Entity.Navigations.Navigation.Sort)); var result = await _repository.GetAllAsync(query); return result; } public async Task> GetAllAsync() { var query = Q.NewQuery(); query.Where(nameof(Entity.SystemLabel.SystemLabel.IsDisable), false); query.OrderByDesc(nameof(Entity.Navigations.Navigation.Sort)); query.Where(nameof(Entity.SystemLabel.SystemLabel.IsStarLable), false); var result = await _repository.GetAllAsync(query); return result; } public async Task> GetAllAsync(List ids) { return await _repository.GetAllAsync(Q.WhereIn(nameof(Entity.SystemLabel.SystemLabel.Id), ids)); } } }