using System.Diagnostics;
using DMS.Core.Interfaces.Repositories;
using DMS.Core.Models;
using DMS.Infrastructure.Data;
using DMS.Infrastructure.Entities;
using AutoMapper;
using Microsoft.Extensions.Logging;

namespace DMS.Infrastructure.Repositories;

/// <summary>
/// 变量表仓储实现类，负责变量表数据的持久化操作。
/// 继承自 <see cref="BaseRepository{DbVariableTable}"/> 并实现 <see cref="IVariableTableRepository"/> 接口。
/// </summary>
public class VariableTableRepository : BaseRepository<DbVariableTable>, IVariableTableRepository
{
    private readonly IMapper _mapper;
    private readonly IVariableRepository _variableRepository;

    /// <summary>
    /// 构造函数，注入 AutoMapper 和 SqlSugarDbContext。
    /// </summary>
    /// <param name="mapper">AutoMapper 实例，用于实体模型和数据库模型之间的映射。</param>
    /// <param name="dbContext">SqlSugar 数据库上下文，用于数据库操作。</param>
    /// <param name="logger">日志记录器实例。</param>
    public VariableTableRepository(IMapper mapper, SqlSugarDbContext dbContext, ILogger<VariableTableRepository> logger,
                                   IVariableRepository variableRepository)
        : base(dbContext, logger)
    {
        _mapper = mapper;
        _variableRepository = variableRepository;
    }

    /// <summary>
    /// 异步根据ID获取单个变量表。
    /// </summary>
    /// <param name="id">变量表的唯一标识符。</param>
    /// <returns>对应的变量表实体，如果不存在则为null。</returns>
    public async Task<List<VariableTable>> GetByDeviceIdAsync(int deviceId)
    {
        var variableTables = await _dbContext.GetInstance()
                                             .Queryable<DbVariableTable>()
                                             .Where(d => d.DeviceId == deviceId)
                                             .ToListAsync();
        return _mapper.Map<List<VariableTable>>(variableTables);
    }

    /// <summary>
    /// 异步根据ID获取单个变量表。
    /// </summary>
    /// <param name="id">变量表的唯一标识符。</param>
    /// <returns>对应的变量表实体，如果不存在则为null。</returns>
    public async Task<VariableTable> GetByIdAsync(int id)
    {
        var dbVariableTable = await base.GetByIdAsync(id);
        return _mapper.Map<VariableTable>(dbVariableTable);
    }

    /// <summary>
    /// 异步获取所有变量表。
    /// </summary>
    /// <returns>包含所有变量表实体的列表。</returns>
    public async Task<List<VariableTable>> GetAllAsync()
    {
        var dbList = await base.GetAllAsync();
        return _mapper.Map<List<VariableTable>>(dbList);
    }

    /// <summary>
    /// 异步添加新变量表。
    /// </summary>
    /// <param name="entity">要添加的变量表实体。</param>
    /// <returns>添加成功后的变量表实体（包含数据库生成的ID等信息）。</returns>
    public async Task<VariableTable> AddAsync(VariableTable entity)
    {
        var dbVariableTable = await base.AddAsync(_mapper.Map<DbVariableTable>(entity));
        return _mapper.Map(dbVariableTable, entity);
    }

    /// <summary>
    /// 异步更新现有变量表。
    /// </summary>
    /// <param name="entity">要更新的变量表实体。</param>
    /// <returns>受影响的行数。</returns>
    public async Task<int> UpdateAsync(VariableTable entity) =>
        await base.UpdateAsync(_mapper.Map<DbVariableTable>(entity));

    /// <summary>
    /// 异步删除变量表。
    /// </summary>
    /// <param name="entity">要删除的变量表实体。</param>
    /// <returns>受影响的行数。</returns>
    public async Task<int> DeleteAsync(VariableTable entity)
    {
        //删除变量表中的所有变量
        await  _variableRepository.DeleteByVariableTableIdAsync(entity.Id);
        //删除变量表
        return   await base.DeleteAsync(_mapper.Map<DbVariableTable>(entity));
    }
        

    /// <summary>
    /// 异步根据ID删除变量表。
    /// </summary>
    /// <param name="id">要删除变量表的唯一标识符。</param>
    /// <returns>受影响的行数。</returns>
    public async Task<int> DeleteByIdAsync(int id)
    {
        var stopwatch = new Stopwatch();
        stopwatch.Start();
        //删除变量表中的所有变量
        await  _variableRepository.DeleteByVariableTableIdAsync(id);
        //删除变量表
        var result = await _dbContext.GetInstance()
                                     .Deleteable(new DbVariableTable() { Id = id })
                                     .ExecuteCommandAsync();
        stopwatch.Stop();
        _logger.LogInformation($"Delete {typeof(DbVariableTable)},ID={id},耗时：{stopwatch.ElapsedMilliseconds}ms");
        return result;
    }

    /// <summary>
    /// 异步获取指定数量的变量表。
    /// </summary>
    /// <param name="number">要获取的变量表数量。</param>
    /// <returns>包含指定数量变量表实体的列表。</returns>
    public new async Task<List<VariableTable>> TakeAsync(int number)
    {
        var dbList = await base.TakeAsync(number);
        return _mapper.Map<List<VariableTable>>(dbList);
    }

    public async Task<List<VariableTable>> AddBatchAsync(List<VariableTable> entities)
    {
        var dbEntities = _mapper.Map<List<DbVariableTable>>(entities);
        var addedEntities = await base.AddBatchAsync(dbEntities);
        return _mapper.Map<List<VariableTable>>(addedEntities);
    }

    /// <summary>
    /// 异步根据设备ID删除所有关联的变量表。
    /// </summary>
    /// <param name="deviceId">设备的唯一标识符。</param>
    /// <returns>受影响的行数。</returns>
    public async Task<int> DeleteByDeviceIdAsync(int deviceId)
    {
        var stopwatch = new Stopwatch();
        stopwatch.Start();
        int result = 0;
        var variableTables = await GetByDeviceIdAsync(deviceId);
        foreach (var variableTable in variableTables)
        {
          var res=  await DeleteByIdAsync(variableTable.Id);
          result += res;
        }

        stopwatch.Stop();
        _logger.LogInformation($"Delete VariableTable by DeviceId={deviceId}, 耗时：{stopwatch.ElapsedMilliseconds}ms");
        return result;
    }
}