using System; using System.Collections.Generic; using System.Threading.Tasks; using AutoMapper; using Datory; using GxPress.Common.AppOptions; using GxPress.Common.Page; using GxPress.Common.Tools; using GxPress.Repository.Interface.TrackLog; using GxPress.Request.TrackLog; using Microsoft.Extensions.Options; namespace GxPress.Repository.Implement.TrackLog { public class TrackLogRepository : ITrackLogRepository { private readonly Repository _repository; private readonly IMapper _mapper; private readonly string _connectionString; private readonly string _databaseTypestr; public TrackLogRepository(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> GetAllAsync(TrackLogSearchRequest request) { var result = new PagedList(); var query = Q.NewQuery(); if (!string.IsNullOrEmpty(request.KeyWord)) query.WhereLike(nameof(Entity.tede2.TrackLog.TrackLog.Remark), $"%{request.KeyWord}%"); if (!string.IsNullOrEmpty(request.BeginTime) && !string.IsNullOrEmpty(request.EndTime)) { if (DateTime.TryParse(request.BeginTime, out var beginTime) && DateTime.TryParse(request.EndTime, out var endTime)) { query.WhereDate(nameof(Entity.tede2.TrackLog.TrackLog.CreatedDate), ">=", beginTime); query.WhereDate(nameof(Entity.tede2.TrackLog.TrackLog.CreatedDate), "<=", endTime); } } result.Total = await _repository.CountAsync(query); result.Items = await _repository.GetAllAsync(query.OrderByDesc(nameof(Entity.tede2.TrackLog.TrackLog.CreatedDate)).ForPage(request.Page, request.PerPage)); return result; } public async Task InsertAsync(Entity.tede2.TrackLog.TrackLog model) { return await _repository.InsertAsync(model) > 0; } public async Task InsertAsync(string creator, int adminId, string ip, string remark) { var model = new Entity.tede2.TrackLog.TrackLog() { Creator = creator, AdminId = adminId, Ip = ip, Remark = remark }; return await _repository.InsertAsync(model) > 0; } } }