123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- 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<Entity.SystemLabel.SystemLabel> _repository;
- private readonly IMapper _mapper;
- private readonly string _connectionString;
- private readonly string _databaseTypestr;
- private readonly Repository<Entity.tede2.Media.MediaLable> mediaLableRepository;
- public SystemLabelRepository(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<Entity.SystemLabel.SystemLabel>(database);
- _mapper = mapper;
- mediaLableRepository = new Repository<Entity.tede2.Media.MediaLable>(database);
- }
- public IDatabase Database => _repository.Database;
- public string TableName => _repository.TableName;
- public List<TableColumn> TableColumns => _repository.TableColumns;
- public async Task<Entity.SystemLabel.SystemLabel> GetAsync(int id)
- {
- return await _repository.GetAsync(id);
- }
- public async Task<Entity.SystemLabel.SystemLabel> GetByNameAsync(string name)
- {
- return await _repository.GetAsync(Q.WhereLike(nameof(Entity.SystemLabel.SystemLabel.LabelName), $"%{name}%"));
- }
- public async Task<bool> DeleteAsync(int id)
- {
- return await _repository.DeleteAsync(id);
- }
- public async Task<int> InsertAsync(Entity.SystemLabel.SystemLabel note)
- {
- return await _repository.InsertAsync(note);
- }
- public async Task<bool> 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<bool> UpdateAsync(SqlKata.Query query)
- {
- return await _repository.UpdateAsync(query) > 0;
- }
- /// <summary>
- /// 获取标签
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- public async Task<PagedList<SystemLabelResult>> GetAllPageAsync(SystemLabelRequest request)
- {
- var result = new PagedList<SystemLabelResult>();
- 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<SystemLabelResult>(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;
- }
- /// <summary>
- /// 获取明栏
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- public async Task<PagedList<SystemLabelResult>> GetAllStarLablePageAsync(Common.Page.PageParameter request)
- {
- var result = new PagedList<SystemLabelResult>();
- 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<SystemLabelResult>(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;
- }
- /// <summary>
- /// 获取明栏
- /// </summary>
- /// <returns></returns>
- public async Task<IEnumerable<SystemLabelResult>> 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<SystemLabelResult>(query);
- return result;
- }
- public async Task<IEnumerable<SystemLabelResult>> 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<SystemLabelResult>(query);
- return result;
- }
- public async Task<IEnumerable<Entity.SystemLabel.SystemLabel>> GetAllAsync(List<int> ids)
- {
- return await _repository.GetAllAsync(Q.WhereIn(nameof(Entity.SystemLabel.SystemLabel.Id), ids));
- }
- }
- }
|