using LSFE.Domain.Contracts.Maintenance; using LSFE.Infrastructure.Database; using LSFE.Infrastructure.Dto.Maintenance; using LSFE.Infrastructure.Model; using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Text; using System.Threading.Tasks; namespace LSFE.Domain.Services.Maintenance { public class MaintenanceRepo : IMaintenanceRepo where TEntity : class { private readonly LSFEDbContext _context; private readonly DbSet _dbSet; public MaintenanceRepo(LSFEDbContext context) { _context = context; _dbSet = _context.Set(); } public async Task AnyAsync(Expression> predicate) { return await _dbSet.AnyAsync(predicate); } public IQueryable Query() { return _context.Set().AsQueryable(); } public async Task> GetAllAsync(params Expression>[] includeProperties) { IQueryable query = _context.Set(); if (includeProperties != null) { foreach (var includeProperty in includeProperties) { query = query.Include(includeProperty); } } return await query.ToListAsync(); } public async Task AddOrUpdateAsync(TEntity entity) { try { var keyProperty = _context.Model.FindEntityType(typeof(TEntity))? .FindPrimaryKey()?.Properties.First(); if (keyProperty == null) throw new InvalidOperationException($"No key defined for {typeof(TEntity).Name}"); var keyValue = keyProperty.PropertyInfo?.GetValue(entity); var existing = await _context.Set().FindAsync(keyValue); if (existing == null) { await _context.Set().AddAsync(entity); } else { _context.Entry(existing).CurrentValues.SetValues(entity); _context.Entry(existing).State = EntityState.Modified; } return new Response { Success = true, MessCode = 1 }; } catch (Exception ex) { return new Response { Success = false, Message = ex.ToString(), MessCode = 0 }; } } public async Task SoftDeleteAsync(int id) { var entity = await _context.Set().FindAsync(id); if (entity == null) return new Response { Success = false, Message = "Not found" }; var prop = typeof(TEntity).GetProperty("IsActive"); if (prop != null) prop.SetValue(entity, false); return new Response { Success = true }; } public async Task FindAsync(Expression> predicate) { return await _context.Set().FirstOrDefaultAsync(predicate); } public async Task GetByIdAsync(object id) { return await _context.Set().FindAsync(id); } public async Task> FindAllAsync(Expression> predicate) { return await _context.Set() .Where(predicate) .AsNoTracking() .ToListAsync(); } public async Task AddRangeAsync(IEnumerable entities) { if (entities == null || !entities.Any()) { return; } await _context.Set().AddRangeAsync(entities); } public async Task DeleteAsync(Guid id) { var entity = await _context.Set().FindAsync(id); if (entity == null) return new Response { Success = false, Message = "Not found" }; _context.Set().Remove(entity); await _context.SaveChangesAsync(); return new Response { Success = true }; } } }