VisitRepository.cs 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. using System.Collections.Generic;
  2. using System.Threading.Tasks;
  3. using AutoMapper;
  4. using Dapper;
  5. using Datory;
  6. using GxPress.Common.AppOptions;
  7. using GxPress.Common.Page;
  8. using GxPress.Common.Tools;
  9. using GxPress.Entity;
  10. using GxPress.Repository.Interface.Visit;
  11. using GxPress.Request.App.Visit;
  12. using GxPress.Result.App.Visit;
  13. using Microsoft.Extensions.Options;
  14. using SqlKata;
  15. namespace GxPress.Repository.Implement.Visit
  16. {
  17. public class VisitRepository : IVisitRepository
  18. {
  19. private readonly Repository<Entity.Visit.Visit> _repository;
  20. private readonly IMapper _mapper;
  21. private readonly string _connectionString;
  22. private readonly string _databaseTypestr;
  23. public VisitRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IMapper mapper)
  24. {
  25. _databaseTypestr = dbOptionsAccessor.CurrentValue.DatabaseType;
  26. _connectionString = dbOptionsAccessor.CurrentValue.ConnectionString;
  27. var databaseType = StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
  28. var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
  29. _repository = new Repository<Entity.Visit.Visit>(database);
  30. _mapper = mapper;
  31. }
  32. public IDatabase Database => _repository.Database;
  33. public string TableName => _repository.TableName;
  34. public List<TableColumn> TableColumns => _repository.TableColumns;
  35. /// <summary>
  36. /// 分页显示访问数据
  37. /// </summary>
  38. /// <param name="request"></param>
  39. /// <returns></returns>
  40. public async Task<PagedList<VisitResult>> GetVisitResultAsync(VisitPageSearchRequest request)
  41. {
  42. var resultData = new PagedList<VisitResult>();
  43. string sql = $"SELECT a.*,b.Name,b.AvatarUrl,c.Name FROM ccpph.tede_visit a inner join ccpph.tede_user b on a.UserId=b.Id inner join ccpph.tede_department c on c.Id=b.DepartmentId where a.TypeId={request.TypeId} and a.SourceId={request.SouceId} order by a. CreatedDate desc limit {(request.Page - 1) * request.PerPage},{request.PerPage}";
  44. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypestr, DatabaseType.MySql);
  45. var database = new Database(databaseType, _connectionString);
  46. var connection = database.GetConnection();
  47. var result =
  48. await connection
  49. .QueryAsync<VisitResult, User, Department, VisitResult>(sql,
  50. (visitResult, user, department) =>
  51. {
  52. visitResult.DepartmentName = department.Name;
  53. visitResult.Name = user.Name;
  54. visitResult.AvatarUrl = StringUtils.AddDomainMin(user.AvatarUrl);
  55. return visitResult;
  56. }, splitOn: "Name");
  57. var sqlCount = $"SELECT count(1) FROM ccpph.tede_visit a inner join ccpph.tede_user b on a.UserId=b.Id inner join ccpph.tede_department c on c.Id=b.DepartmentId where a.TypeId={request.TypeId} and a.SourceId={request.SouceId}";
  58. resultData.Items = result;
  59. resultData.Total = await connection.ExecuteScalarAsync<int>(sqlCount);
  60. return resultData;
  61. }
  62. /// <summary>
  63. /// 添加访客
  64. /// </summary>
  65. /// <param name="request"></param>
  66. /// <returns></returns>
  67. public Task<int> InsertAsync(Entity.Visit.Visit request)
  68. {
  69. return _repository.InsertAsync(request);
  70. }
  71. public Task<bool> ExistsAsync(SqlKata.Query query)
  72. {
  73. return _repository.ExistsAsync(query);
  74. }
  75. public async Task<Entity.Visit.Visit> GetAsync(Query query)
  76. {
  77. return await _repository.GetAsync(query);
  78. }
  79. public async Task<int> CountAsync(Query query)
  80. {
  81. return await _repository.CountAsync(query);
  82. }
  83. }
  84. }