using System;
using CncService.Interface;
using CncModels.Dto;
using CncModels.Entity;
using CncModels.Constants;
using CncRepository.Interface;

namespace CncService.Impl
{
    /// <summary>
    /// 采集数据查询实现
    /// </summary>
    public class CollectDataService : ICollectDataService
    {
        private readonly ICollectRawRepository _collectRawRepository;

        public CollectDataService(ICollectRawRepository collectRawRepository)
        {
            _collectRawRepository = collectRawRepository ?? throw new ArgumentNullException(nameof(collectRawRepository));
        }

        /// <inheritdoc/>
        public PagedResult<CollectRaw> GetRawByAddress(int addressId, int page, int pageSize)
        {
            if (addressId <= 0) throw new BusinessException(ErrorCode.BadRequest, "无效的地址ID");
            if (page <= 0) throw new BusinessException(ErrorCode.BadRequest, "页码无效");
            if (pageSize <= 0) throw new BusinessException(ErrorCode.BadRequest, "页大小无效");
            return _collectRawRepository.GetByAddressId(addressId, page, pageSize);
        }

        /// <inheritdoc/>
        public CollectRaw GetLatestRaw(int addressId)
        {
            if (addressId <= 0) throw new BusinessException(ErrorCode.BadRequest, "无效的地址ID");
            return _collectRawRepository.GetLatestByAddressId(addressId);
        }
    }
}
