123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- using System.Collections.Generic;
- using System.Threading.Tasks;
- using AutoMapper;
- using Dapper;
- using Datory;
- using GxPress.Common.AppOptions;
- using GxPress.Common.Tools;
- using GxPress.Entity.Topic;
- using GxPress.EnumConst;
- using GxPress.Repository.Interface.Topic;
- using GxPress.Request.App.Topic;
- using GxPress.Result;
- using Microsoft.Extensions.Options;
- using SqlKata;
- using System.Transactions;
- namespace GxPress.Repository.Implement.Topic
- {
- public class TopicGroupRepository : ITopicGroupRepository
- {
- private readonly Repository<TopicGroup> _repository;
- private readonly IMapper _mapper;
- private readonly string _connectionString;
- private readonly string _databaseTypestr;
- public TopicGroupRepository(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<TopicGroup>(database);
- _mapper = mapper;
- }
- public IDatabase Database => _repository.Database;
- public string TableName => _repository.TableName;
- public List<TableColumn> TableColumns => _repository.TableColumns;
- public async Task<TopicGroup> GetAsync(int id)
- {
- return await _repository.GetAsync(id);
- }
- /// <summary>
- /// 根据ID删除分组
- /// </summary>
- /// <param name="ids"></param>
- /// <returns></returns>
- public async Task<bool> DeleteAsync(List<int> ids)
- {
- return await _repository.DeleteAsync(Q.WhereIn(nameof(TopicGroup.Id), ids)) > 0;
- }
- /// <summary>
- /// 插入
- /// </summary>
- /// <param name="topicGroup"></param>
- /// <returns></returns>
- public async Task<AppResultJson<Entity.Topic.TopicGroup>> InsertAsync(TopicGroup topicGroup)
- {
- var result = new AppResultJson<Entity.Topic.TopicGroup>()
- {
- Code = StatusCodeConst.SucceedCode.GetHashCode(),
- Success = true
- };
- try
- {
- using (var transactionScope = new TransactionScope())
- {
- var id = await _repository.InsertAsync(topicGroup);
- await _repository.UpdateAsync(Q.Set(nameof(TopicGroup.Sort), id).Where(nameof(TopicGroup.Id), id));
- transactionScope.Complete();
- result.Data = await _repository.GetAsync(id);
- }
- }
- catch
- {
- result.Success = false;
- result.Code = StatusCodeConst.ErrorCode.GetHashCode();
- }
- return result;
- }
- /// <summary>
- /// 根据用户ID获取分组
- /// </summary>
- /// <param name="userId"></param>
- /// <returns></returns>
- public async Task<IEnumerable<TopicGroup>> GetTopicGroupsAsync(int userId)
- {
- string sql = $@"SELECT
- Id,
- UserId,
- Name,
- Sort,
- (SELECT
- COUNT(1)
- FROM
- tede_user
- WHERE
- id IN (SELECT
- UserId
- FROM
- tede_topic_group_user
- WHERE
- TopicGroupId = a.Id)) AS IncludeCount
- FROM
- tede_topic_group a
- WHERE
- a.userId = {userId}
- ORDER BY a.Sort DESC";
- var databaseType = _databaseTypestr.ToEnum<DatabaseType>(DatabaseType.MySql);
- var database = new Database(databaseType, _connectionString);
- var connection = database.GetConnection();
- return await connection.QueryAsync<Entity.Topic.TopicGroup>(sql);
- }
- /// <summary>
- /// 修改分组名称
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- public async Task<bool> UpdateAsync(TopicGroupInRequest request)
- {
- return await _repository.UpdateAsync(Q.Set(nameof(TopicGroup.Name), request.Name)
- .Where(nameof(TopicGroup.Id), request.Id)) > 0;
- }
- /// <summary>
- /// 排序
- /// </summary>
- /// <param name="firstId"></param>
- /// <param name="secondId"></param>
- /// <param name="userId"></param>
- /// <returns></returns>
- public async Task<bool> MoveSortAsync(int firstId, int secondId, int userId)
- {
- //获取第一个
- var topicGroupFirst = await _repository.GetAsync(firstId);
- //获取第二个数据
- var topicGroupSecond = await _repository.GetAsync(secondId);
- var sort = 0;
- var databaseType = _databaseTypestr.ToEnum<DatabaseType>(DatabaseType.MySql);
- var database = new Database(databaseType, _connectionString);
- var connection = database.GetConnection();
- if (topicGroupFirst.Sort < topicGroupSecond.Sort)
- {
- //修改第一个排序
- await _repository.UpdateAsync(Q.Set(nameof(TopicGroupUser.Sort), topicGroupSecond.Sort)
- .Where(nameof(TopicGroup.Id), firstId));
- sort = topicGroupSecond.Sort;
- var sql =
- $"update tede_topic_group set Sort=Sort-1 where UserId={userId} and Sort<={sort} and Id not in({firstId}) and id>0";
- return await connection.ExecuteAsync(sql) > 0;
- }
- else
- {
- //修改第一个排序
- await _repository.UpdateAsync(Q.Set(nameof(TopicGroupUser.Sort), topicGroupSecond.Sort)
- .Where(nameof(TopicGroup.Id), firstId));
- sort = topicGroupSecond.Sort;
- var sql =
- $"update tede_topic_group set Sort=Sort+1 where UserId={userId} and Sort>={sort} and Id not in({firstId}) and id>0";
- return await connection.ExecuteAsync(sql) > 0;
- }
- }
- public async Task<bool> UpdateAsync(Query query)
- {
- return await _repository.UpdateAsync(query) > 0;
- }
- }
- }
|