using System.Collections.Generic; using System.Threading.Tasks; using AutoMapper; using Datory; using GxPress.Common.AppOptions; using GxPress.Common.Tools; using GxPress.Repository.Interface.Group; using GxPress.Result.Web; using Microsoft.Extensions.Caching.Distributed; using Microsoft.Extensions.Options; using System.Transactions; namespace GxPress.Repository.Implement.Group { /// /// 小组广场 /// public class GroupCategroyRepository : IGroupCategroyRepository { private readonly Repository _repository; private readonly IMapper _mapper; private readonly IDistributedCache _cache; public GroupCategroyRepository(IOptionsMonitor dbOptionsAccessor, IMapper mapper, IDistributedCache cache) { var databaseType = StringUtils.ToEnum(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql); var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString); _mapper = mapper; _cache = cache; _repository = new Repository(database); } public IDatabase Database => _repository.Database; public string TableName => _repository.TableName; public List TableColumns => _repository.TableColumns; /// /// 添加小组广场 /// /// /// public async Task InsertAsync(Entity.tede2.Group.GroupCategroy groupCategroy) { return await _repository.InsertAsync(groupCategroy); } public async Task UpdateAsync(Entity.tede2.Group.GroupCategroy groupCategroy) { if (groupCategroy.Id > 0) { var model = await GetAsync(groupCategroy.Id); if (!string.IsNullOrEmpty(groupCategroy.PiazzaName)) model.PiazzaName = groupCategroy.PiazzaName; if (groupCategroy.ParentId > 0) model.ParentId = groupCategroy.ParentId; return await _repository.UpdateAsync(groupCategroy); } return false; } public async Task GetAsync(int id) { return await _repository.GetAsync(id); } public async Task DeleteAsync(int id) { try { using (var transactionScope = new TransactionScope()) { await _repository.DeleteAsync(id); transactionScope.Complete(); } } catch { return false; } return true; } /// /// 根据parendId获取 /// /// /// public async Task> GetAllAsync(int parentId) { var result = await _repository.GetAllAsync(Q.Where(nameof(Entity.tede2.Group.GroupCategroy.ParentId), parentId)); foreach (var item in result) item.IsChildren = await _repository.ExistsAsync(Q.Where(nameof(Entity.tede2.Group.GroupCategroy.ParentId), item.Id)); return result; } } }