using System.Collections.Generic; using System.Threading.Tasks; using AutoMapper; using Dapper; using Datory; using GxPress.Common.AppOptions; using GxPress.Common.Page; using GxPress.Common.Tools; using GxPress.Entity; using GxPress.Repository.Interface.Visit; using GxPress.Request.App.Visit; using GxPress.Result.App.Visit; using Microsoft.Extensions.Options; using SqlKata; namespace GxPress.Repository.Implement.Visit { public class VisitRepository : IVisitRepository { private readonly Repository _repository; private readonly IMapper _mapper; private readonly string _connectionString; private readonly string _databaseTypestr; public VisitRepository(IOptionsMonitor dbOptionsAccessor, IMapper mapper) { _databaseTypestr = dbOptionsAccessor.CurrentValue.DatabaseType; _connectionString = dbOptionsAccessor.CurrentValue.ConnectionString; var databaseType = StringUtils.ToEnum(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql); var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString); _repository = new Repository(database); _mapper = mapper; } public IDatabase Database => _repository.Database; public string TableName => _repository.TableName; public List TableColumns => _repository.TableColumns; /// /// 分页显示访问数据 /// /// /// public async Task> GetVisitResultAsync(VisitPageSearchRequest request) { var resultData = new PagedList(); 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}"; var databaseType = StringUtils.ToEnum(_databaseTypestr, DatabaseType.MySql); var database = new Database(databaseType, _connectionString); var connection = database.GetConnection(); var result = await connection .QueryAsync(sql, (visitResult, user, department) => { visitResult.DepartmentName = department.Name; visitResult.Name = user.Name; visitResult.AvatarUrl = StringUtils.AddDomainMin(user.AvatarUrl); return visitResult; }, splitOn: "Name"); 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}"; resultData.Items = result; resultData.Total = await connection.ExecuteScalarAsync(sqlCount); return resultData; } /// /// 添加访客 /// /// /// public Task InsertAsync(Entity.Visit.Visit request) { return _repository.InsertAsync(request); } public Task ExistsAsync(SqlKata.Query query) { return _repository.ExistsAsync(query); } public async Task GetAsync(Query query) { return await _repository.GetAsync(query); } public async Task CountAsync(Query query) { return await _repository.CountAsync(query); } } }