123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- 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
- {
- /// <summary>
- /// 小组广场
- /// </summary>
- public class GroupCategroyRepository : IGroupCategroyRepository
- {
- private readonly Repository<Entity.tede2.Group.GroupCategroy> _repository;
- private readonly IMapper _mapper;
- private readonly IDistributedCache _cache;
- public GroupCategroyRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IMapper mapper, IDistributedCache cache)
- {
- var databaseType = StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
- var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
- _mapper = mapper;
- _cache = cache;
- _repository = new Repository<Entity.tede2.Group.GroupCategroy>(database);
- }
- public IDatabase Database => _repository.Database;
- public string TableName => _repository.TableName;
- public List<TableColumn> TableColumns => _repository.TableColumns;
- /// <summary>
- /// 添加小组广场
- /// </summary>
- /// <param name="groupCategroy"></param>
- /// <returns></returns>
- public async Task<int> InsertAsync(Entity.tede2.Group.GroupCategroy groupCategroy)
- {
- return await _repository.InsertAsync(groupCategroy);
- }
- public async Task<bool> 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<Entity.tede2.Group.GroupCategroy> GetAsync(int id)
- {
- return await _repository.GetAsync(id);
- }
- public async Task<bool> DeleteAsync(int id)
- {
- try
- {
- using (var transactionScope = new TransactionScope())
- {
- await _repository.DeleteAsync(id);
- transactionScope.Complete();
- }
- }
- catch
- {
- return false;
- }
- return true;
- }
- /// <summary>
- /// 根据parendId获取
- /// </summary>
- /// <param name="parentId"></param>
- /// <returns></returns>
- public async Task<IEnumerable<GroupCategoryResult>> GetAllAsync(int parentId)
- {
- var result = await _repository.GetAllAsync<GroupCategoryResult>(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;
- }
- }
- }
|