123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- 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;
- using Dapper;
- namespace GxPress.Repository.Implement.Group
- {
- /// <summary>
- /// 小组广场
- /// </summary>
- public class GroupCategroyRepository : IGroupCategroyRepository
- {
- private readonly Repository<Entity.tede2.Group.GroupCategroy> _repository;
- private readonly Repository<Entity.tede2.Group.UserGroupCategory> userGroupCategoryRepository;
- private readonly IMapper _mapper;
- private readonly IDistributedCache _cache;
- private readonly string _connectionString;
- private readonly string _databaseTypestr;
- public GroupCategroyRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IMapper mapper, IDistributedCache cache)
- {
- _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);
- _mapper = mapper;
- _cache = cache;
- _repository = new Repository<Entity.tede2.Group.GroupCategroy>(database);
- userGroupCategoryRepository = new Repository<Entity.tede2.Group.UserGroupCategory>(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)
- {
- groupCategroy.ImageUrls = StringUtils.RemoveDomain(groupCategroy.ImageUrls);
- 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;
- if (string.IsNullOrEmpty(groupCategroy.ImageUrls))
- model.ImageUrls = StringUtils.RemoveDomain(groupCategroy.ImageUrls);
- 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));
- item.ImageUrls = StringUtils.AddDomain(item.ImageUrls);
- }
- return result;
- }
- public async Task<IEnumerable<GroupCategoryResult>> GetUserGroupCategroyResult(int userId)
- {
- string sql = $@"SELECT
- a.*
- FROM
- tede_group_piazza a
- INNER JOIN
- tede_user_group_category b ON a.id = b.GroupCategroyId
- WHERE
- b.UserId = {userId}
- ORDER BY b.CreatedDate";
- var databaseType = _databaseTypestr.ToEnum<DatabaseType>(DatabaseType.MySql);
- var database = new Database(databaseType, _connectionString);
- var connection = database.GetConnection();
- var result = await connection.QueryAsync<GroupCategoryResult>(sql);
- foreach (var item in result)
- {
- item.ImageUrls = StringUtils.AddDomainMin(item.ImageUrls);
- item.IsChildren = await _repository.ExistsAsync(Q.Where(nameof(Entity.tede2.Group.GroupCategroy.ParentId), item.Id));
- }
- return result;
- }
- }
- }
|