123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- using System.Collections.Generic;
- using System.Threading.Tasks;
- using AutoMapper;
- using Datory;
- using GxPress.Common.AppOptions;
- using GxPress.Common.Tools;
- using GxPress.Repository.Interface.Navigation;
- using Microsoft.Extensions.Options;
- using System.Transactions;
- namespace GxPress.Repository.Implement.Navigation
- {
- public class MiddleLableNexusRepository : IMiddleLableNexusRepository
- {
- private readonly Repository<Entity.Navigations.MiddleLableNexus> _repository;
- private readonly IMapper _mapper;
- public MiddleLableNexusRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IMapper mapper)
- {
- var databaseType =
- StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
- var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
- _repository = new Repository<Entity.Navigations.MiddleLableNexus>(database);
- _mapper = mapper;
- }
- public IDatabase Database => _repository.Database;
- public string TableName => _repository.TableName;
- public List<TableColumn> TableColumns => _repository.TableColumns;
- /// <summary>
- /// 添加
- /// </summary>
- /// <param name="middleLableId"></param>
- /// <param name="ids"></param>
- /// <returns></returns>
- public async Task<bool> InsertAsync(int middleLableId, List<int> ids)
- {
- try
- {
- using (var transactionScope = new TransactionScope())
- {
- foreach (var item in ids)
- {
- var model = new Entity.Navigations.MiddleLableNexus()
- {
- LableId = item,
- MiddleLableId = middleLableId
- };
- await _repository.InsertAsync(model);
- }
- transactionScope.Complete();
- }
- }
- catch
- {
- return false;
- }
- return true;
- }
- /// <summary>
- /// 查询
- /// </summary>
- /// <param name="middleLableId"></param>
- /// <returns></returns>
- public async Task<IEnumerable<int>> GetAllTaskAsync(int middleLableId)
- {
- var query = Q.NewQuery();
- query.Select(nameof(Entity.Navigations.MiddleLableNexus.LableId));
- query.Where(nameof(Entity.Navigations.MiddleLableNexus.MiddleLableId), middleLableId);
- query.OrderByDesc(nameof(Entity.Navigations.MiddleLableNexus.Sort));
- return await _repository.GetAllAsync<int>(query);
- }
- public async Task<bool> DeleteByMiddleLableIdAsync(int middleLableId)
- {
- return await _repository.DeleteAsync(Q.Where(nameof(Entity.Navigations.MiddleLableNexus.MiddleLableId), middleLableId)) > 0;
- }
- public async Task<bool> UpdateAsync(int middleLableId, string lableId)
- {
- try
- {
- await DeleteByMiddleLableIdAsync(middleLableId);
- var stringId = StringUtils.StringCollectionToStringList(lableId);
- var intIds = new List<int>();
- foreach (var item in stringId)
- intIds.Add(int.Parse(item));
- await InsertAsync(middleLableId, intIds);
- }
- catch
- {
- return false;
- }
- return true;
- }
- }
- }
|