package monitor

import (
	"context"
	"errors"

	"git.huangwc.com/pig/pig-farm-controller/internal/app/controller"
	"git.huangwc.com/pig/pig-farm-controller/internal/app/dto"
	"git.huangwc.com/pig/pig-farm-controller/internal/app/service"
	"git.huangwc.com/pig/pig-farm-controller/internal/infra/logs"
	"git.huangwc.com/pig/pig-farm-controller/internal/infra/repository"

	"github.com/labstack/echo/v4"
)

// Controller 监控控制器，封装了所有与数据监控相关的业务逻辑
type Controller struct {
	ctx            context.Context
	monitorService service.MonitorService
}

// NewController 创建一个新的监控控制器实例
func NewController(ctx context.Context, monitorService service.MonitorService) *Controller {
	return &Controller{
		ctx:            ctx,
		monitorService: monitorService,
	}
}

// ListSensorData godoc
// @Summary      获取传感器数据列表
// @Description  根据提供的过滤条件，分页获取传感器数据
// @Tags         数据监控
// @Security     BearerAuth
// @Produce      json
// @Param        query query dto.ListSensorDataRequest true "查询参数"
// @Success      200 {object} controller.Response{data=dto.ListSensorDataResponse}
// @Router       /api/v1/monitor/sensor-data [get]
func (c *Controller) ListSensorData(ctx echo.Context) error {
	reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "ListSensorData")
	const actionType = "获取传感器数据列表"

	var req dto.ListSensorDataRequest
	if err := ctx.Bind(&req); err != nil {
		logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
		return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的查询参数: "+err.Error(), actionType, "参数绑定失败", req)
	}

	resp, err := c.monitorService.ListSensorData(reqCtx, &req)
	if err != nil {
		if errors.Is(err, repository.ErrInvalidPagination) {
			logger.Warnf("%s: 无效的分页参数: %v", actionType, err)
			return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的分页参数: "+err.Error(), actionType, "无效分页参数", req)
		}

		logger.Errorf("%s: 服务层查询失败: %v", actionType, err)
		return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取传感器数据失败: "+err.Error(), actionType, "服务层查询失败", req)
	}

	logger.Infof("%s: 成功, 获取到 %d 条记录, 总计 %d 条", actionType, len(resp.List), resp.Pagination.Total)
	return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取传感器数据成功", resp, actionType, "获取传感器数据成功", req)
}

// ListDeviceCommandLogs godoc
// @Summary      获取设备命令日志列表
// @Description  根据提供的过滤条件，分页获取设备命令日志
// @Tags         数据监控
// @Security     BearerAuth
// @Produce      json
// @Param        query query dto.ListDeviceCommandLogRequest true "查询参数"
// @Success      200 {object} controller.Response{data=dto.ListDeviceCommandLogResponse}
// @Router       /api/v1/monitor/device-command-logs [get]
func (c *Controller) ListDeviceCommandLogs(ctx echo.Context) error {
	reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "ListDeviceCommandLogs")
	const actionType = "获取设备命令日志列表"

	var req dto.ListDeviceCommandLogRequest
	if err := ctx.Bind(&req); err != nil {
		logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
		return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的查询参数: "+err.Error(), actionType, "参数绑定失败", req)
	}

	resp, err := c.monitorService.ListDeviceCommandLogs(reqCtx, &req)
	if err != nil {
		if errors.Is(err, repository.ErrInvalidPagination) {
			logger.Warnf("%s: 无效的分页参数: %v", actionType, err)
			return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的分页参数: "+err.Error(), actionType, "无效分页参数", req)
		}

		logger.Errorf("%s: 服务层查询失败: %v", actionType, err)
		return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取设备命令日志失败: "+err.Error(), actionType, "服务层查询失败", req)
	}

	logger.Infof("%s: 成功, 获取到 %d 条记录, 总计 %d 条", actionType, len(resp.List), resp.Pagination.Total)
	return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取设备命令日志成功", resp, actionType, "获取设备命令日志成功", req)
}

// ListPlanExecutionLogs godoc
// @Summary      获取计划执行日志列表
// @Description  根据提供的过滤条件，分页获取计划执行日志
// @Tags         数据监控
// @Security     BearerAuth
// @Produce      json
// @Param        query query dto.ListPlanExecutionLogRequest true "查询参数"
// @Success      200 {object} controller.Response{data=dto.ListPlanExecutionLogResponse}
// @Router       /api/v1/monitor/plan-execution-logs [get]
func (c *Controller) ListPlanExecutionLogs(ctx echo.Context) error {
	reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "ListPlanExecutionLogs")
	const actionType = "获取计划执行日志列表"

	var req dto.ListPlanExecutionLogRequest
	if err := ctx.Bind(&req); err != nil {
		logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
		return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的查询参数: "+err.Error(), actionType, "参数绑定失败", req)
	}

	resp, err := c.monitorService.ListPlanExecutionLogs(reqCtx, &req)
	if err != nil {
		if errors.Is(err, repository.ErrInvalidPagination) {
			logger.Warnf("%s: 无效的分页参数: %v", actionType, err)
			return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的分页参数: "+err.Error(), actionType, "无效分页参数", req)
		}

		logger.Errorf("%s: 服务层查询失败: %v", actionType, err)
		return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取计划执行日志失败: "+err.Error(), actionType, "服务层查询失败", req)
	}

	logger.Infof("%s: 成功, 获取到 %d 条记录, 总计 %d 条", actionType, len(resp.List), resp.Pagination.Total)
	return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取计划执行日志成功", resp, actionType, "获取计划执行日志成功", req)
}

// ListTaskExecutionLogs godoc
// @Summary      获取任务执行日志列表
// @Description  根据提供的过滤条件，分页获取任务执行日志
// @Tags         数据监控
// @Security     BearerAuth
// @Produce      json
// @Param        query query dto.ListTaskExecutionLogRequest true "查询参数"
// @Success      200 {object} controller.Response{data=dto.ListTaskExecutionLogResponse}
// @Router       /api/v1/monitor/task-execution-logs [get]
func (c *Controller) ListTaskExecutionLogs(ctx echo.Context) error {
	reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "ListTaskExecutionLogs")
	const actionType = "获取任务执行日志列表"

	var req dto.ListTaskExecutionLogRequest
	if err := ctx.Bind(&req); err != nil {
		logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
		return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的查询参数: "+err.Error(), actionType, "参数绑定失败", req)
	}

	resp, err := c.monitorService.ListTaskExecutionLogs(reqCtx, &req)
	if err != nil {
		if errors.Is(err, repository.ErrInvalidPagination) {
			logger.Warnf("%s: 无效的分页参数: %v", actionType, err)
			return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的分页参数: "+err.Error(), actionType, "无效分页参数", req)
		}

		logger.Errorf("%s: 服务层查询失败: %v", actionType, err)
		return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取任务执行日志失败: "+err.Error(), actionType, "服务层查询失败", req)
	}

	logger.Infof("%s: 成功, 获取到 %d 条记录, 总计 %d 条", actionType, len(resp.List), resp.Pagination.Total)
	return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取任务执行日志成功", resp, actionType, "获取任务执行日志成功", req)
}

// ListPendingCollections godoc
// @Summary      获取待采集请求列表
// @Description  根据提供的过滤条件，分页获取待采集请求
// @Tags         数据监控
// @Security     BearerAuth
// @Produce      json
// @Param        query query dto.ListPendingCollectionRequest true "查询参数"
// @Success      200 {object} controller.Response{data=dto.ListPendingCollectionResponse}
// @Router       /api/v1/monitor/pending-collections [get]
func (c *Controller) ListPendingCollections(ctx echo.Context) error {
	reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "ListPendingCollections")
	const actionType = "获取待采集请求列表"

	var req dto.ListPendingCollectionRequest
	if err := ctx.Bind(&req); err != nil {
		logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
		return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的查询参数: "+err.Error(), actionType, "参数绑定失败", req)
	}

	resp, err := c.monitorService.ListPendingCollections(reqCtx, &req)
	if err != nil {
		if errors.Is(err, repository.ErrInvalidPagination) {
			logger.Warnf("%s: 无效的分页参数: %v", actionType, err)
			return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的分页参数: "+err.Error(), actionType, "无效分页参数", req)
		}

		logger.Errorf("%s: 服务层查询失败: %v", actionType, err)
		return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取待采集请求失败: "+err.Error(), actionType, "服务层查询失败", req)
	}

	logger.Infof("%s: 成功, 获取到 %d 条记录, 总计 %d 条", actionType, len(resp.List), resp.Pagination.Total)
	return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取待采集请求成功", resp, actionType, "获取待采集请求成功", req)
}

// ListUserActionLogs godoc
// @Summary      获取用户操作日志列表
// @Description  根据提供的过滤条件，分页获取用户操作日志
// @Tags         数据监控
// @Security     BearerAuth
// @Produce      json
// @Param        query query dto.ListUserActionLogRequest true "查询参数"
// @Success      200 {object} controller.Response{data=dto.ListUserActionLogResponse}
// @Router       /api/v1/monitor/user-action-logs [get]
func (c *Controller) ListUserActionLogs(ctx echo.Context) error {
	reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "ListUserActionLogs")
	const actionType = "获取用户操作日志列表"

	var req dto.ListUserActionLogRequest
	if err := ctx.Bind(&req); err != nil {
		logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
		return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的查询参数: "+err.Error(), actionType, "参数绑定失败", req)
	}

	resp, err := c.monitorService.ListUserActionLogs(reqCtx, &req)
	if err != nil {
		if errors.Is(err, repository.ErrInvalidPagination) {
			logger.Warnf("%s: 无效的分页参数: %v", actionType, err)
			return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的分页参数: "+err.Error(), actionType, "无效分页参数", req)
		}

		logger.Errorf("%s: 服务层查询失败: %v", actionType, err)
		return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取用户操作日志失败: "+err.Error(), actionType, "服务层查询失败", req)
	}

	logger.Infof("%s: 成功, 获取到 %d 条记录, 总计 %d 条", actionType, len(resp.List), resp.Pagination.Total)
	return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取用户操作日志成功", resp, actionType, "获取用户操作日志成功", req)
}

// ListMedicationLogs godoc
// @Summary      获取用药记录列表
// @Description  根据提供的过滤条件，分页获取用药记录
// @Tags         数据监控
// @Security     BearerAuth
// @Produce      json
// @Param        query query dto.ListMedicationLogRequest true "查询参数"
// @Success      200 {object} controller.Response{data=dto.ListMedicationLogResponse}
// @Router       /api/v1/monitor/medication-logs [get]
func (c *Controller) ListMedicationLogs(ctx echo.Context) error {
	reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "ListMedicationLogs")
	const actionType = "获取用药记录列表"

	var req dto.ListMedicationLogRequest
	if err := ctx.Bind(&req); err != nil {
		logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
		return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的查询参数: "+err.Error(), actionType, "参数绑定失败", req)
	}

	resp, err := c.monitorService.ListMedicationLogs(reqCtx, &req)
	if err != nil {
		if errors.Is(err, repository.ErrInvalidPagination) {
			logger.Warnf("%s: 无效的分页参数: %v", actionType, err)
			return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的分页参数: "+err.Error(), actionType, "无效分页参数", req)
		}

		logger.Errorf("%s: 服务层查询失败: %v", actionType, err)
		return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取用药记录失败: "+err.Error(), actionType, "服务层查询失败", req)
	}

	logger.Infof("%s: 成功, 获取到 %d 条记录, 总计 %d 条", actionType, len(resp.List), resp.Pagination.Total)
	return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取用药记录成功", resp, actionType, "获取用药记录成功", req)
}

// ListPigBatchLogs godoc
// @Summary      获取猪批次日志列表
// @Description  根据提供的过滤条件，分页获取猪批次日志
// @Tags         数据监控
// @Security     BearerAuth
// @Produce      json
// @Param        query query dto.ListPigBatchLogRequest true "查询参数"
// @Success      200 {object} controller.Response{data=dto.ListPigBatchLogResponse}
// @Router       /api/v1/monitor/pig-batch-logs [get]
func (c *Controller) ListPigBatchLogs(ctx echo.Context) error {
	reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "ListPigBatchLogs")
	const actionType = "获取猪批次日志列表"

	var req dto.ListPigBatchLogRequest
	if err := ctx.Bind(&req); err != nil {
		logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
		return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的查询参数: "+err.Error(), actionType, "参数绑定失败", req)
	}

	resp, err := c.monitorService.ListPigBatchLogs(reqCtx, &req)
	if err != nil {
		if errors.Is(err, repository.ErrInvalidPagination) {
			logger.Warnf("%s: 无效的分页参数: %v", actionType, err)
			return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的分页参数: "+err.Error(), actionType, "无效分页参数", req)
		}

		logger.Errorf("%s: 服务层查询失败: %v", actionType, err)
		return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取猪批次日志失败: "+err.Error(), actionType, "服务层查询失败", req)
	}

	logger.Infof("%s: 成功, 获取到 %d 条记录, 总计 %d 条", actionType, len(resp.List), resp.Pagination.Total)
	return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取猪批次日志成功", resp, actionType, "获取猪批次日志成功", req)
}

// ListWeighingBatches godoc
// @Summary      获取批次称重记录列表
// @Description  根据提供的过滤条件，分页获取批次称重记录
// @Tags         数据监控
// @Security     BearerAuth
// @Produce      json
// @Param        query query dto.ListWeighingBatchRequest true "查询参数"
// @Success      200 {object} controller.Response{data=dto.ListWeighingBatchResponse}
// @Router       /api/v1/monitor/weighing-batches [get]
func (c *Controller) ListWeighingBatches(ctx echo.Context) error {
	reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "ListWeighingBatches")
	const actionType = "获取批次称重记录列表"

	var req dto.ListWeighingBatchRequest
	if err := ctx.Bind(&req); err != nil {
		logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
		return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的查询参数: "+err.Error(), actionType, "参数绑定失败", req)
	}

	resp, err := c.monitorService.ListWeighingBatches(reqCtx, &req)
	if err != nil {
		if errors.Is(err, repository.ErrInvalidPagination) {
			logger.Warnf("%s: 无效的分页参数: %v", actionType, err)
			return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的分页参数: "+err.Error(), actionType, "无效分页参数", req)
		}

		logger.Errorf("%s: 服务层查询失败: %v", actionType, err)
		return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取批次称重记录失败: "+err.Error(), actionType, "服务层查询失败", req)
	}

	logger.Infof("%s: 成功, 获取到 %d 条记录, 总计 %d 条", actionType, len(resp.List), resp.Pagination.Total)
	return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取批次称重记录成功", resp, actionType, "获取批次称重记录成功", req)
}

// ListWeighingRecords godoc
// @Summary      获取单次称重记录列表
// @Description  根据提供的过滤条件，分页获取单次称重记录
// @Tags         数据监控
// @Security     BearerAuth
// @Produce      json
// @Param        query query dto.ListWeighingRecordRequest true "查询参数"
// @Success      200 {object} controller.Response{data=dto.ListWeighingRecordResponse}
// @Router       /api/v1/monitor/weighing-records [get]
func (c *Controller) ListWeighingRecords(ctx echo.Context) error {
	reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "ListWeighingRecords")
	const actionType = "获取单次称重记录列表"

	var req dto.ListWeighingRecordRequest
	if err := ctx.Bind(&req); err != nil {
		logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
		return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的查询参数: "+err.Error(), actionType, "参数绑定失败", req)
	}

	resp, err := c.monitorService.ListWeighingRecords(reqCtx, &req)
	if err != nil {
		if errors.Is(err, repository.ErrInvalidPagination) {
			logger.Warnf("%s: 无效的分页参数: %v", actionType, err)
			return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的分页参数: "+err.Error(), actionType, "无效分页参数", req)
		}

		logger.Errorf("%s: 服务层查询失败: %v", actionType, err)
		return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取单次称重记录失败: "+err.Error(), actionType, "服务层查询失败", req)
	}

	logger.Infof("%s: 成功, 获取到 %d 条记录, 总计 %d 条", actionType, len(resp.List), resp.Pagination.Total)
	return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取单次称重记录成功", resp, actionType, "获取单次称重记录成功", req)
}

// ListPigTransferLogs godoc
// @Summary      获取猪只迁移日志列表
// @Description  根据提供的过滤条件，分页获取猪只迁移日志
// @Tags         数据监控
// @Security     BearerAuth
// @Produce      json
// @Param        query query dto.ListPigTransferLogRequest true "查询参数"
// @Success      200 {object} controller.Response{data=dto.ListPigTransferLogResponse}
// @Router       /api/v1/monitor/pig-transfer-logs [get]
func (c *Controller) ListPigTransferLogs(ctx echo.Context) error {
	reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "ListPigTransferLogs")
	const actionType = "获取猪只迁移日志列表"

	var req dto.ListPigTransferLogRequest
	if err := ctx.Bind(&req); err != nil {
		logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
		return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的查询参数: "+err.Error(), actionType, "参数绑定失败", req)
	}

	resp, err := c.monitorService.ListPigTransferLogs(reqCtx, &req)
	if err != nil {
		if errors.Is(err, repository.ErrInvalidPagination) {
			logger.Warnf("%s: 无效的分页参数: %v", actionType, err)
			return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的分页参数: "+err.Error(), actionType, "无效分页参数", req)
		}

		logger.Errorf("%s: 服务层查询失败: %v", actionType, err)
		return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取猪只迁移日志失败: "+err.Error(), actionType, "服务层查询失败", req)
	}

	logger.Infof("%s: 成功, 获取到 %d 条记录, 总计 %d 条", actionType, len(resp.List), resp.Pagination.Total)
	return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取猪只迁移日志成功", resp, actionType, "获取猪只迁移日志成功", req)
}

// ListPigSickLogs godoc
// @Summary      获取病猪日志列表
// @Description  根据提供的过滤条件，分页获取病猪日志
// @Tags         数据监控
// @Security     BearerAuth
// @Produce      json
// @Param        query query dto.ListPigSickLogRequest true "查询参数"
// @Success      200 {object} controller.Response{data=dto.ListPigSickLogResponse}
// @Router       /api/v1/monitor/pig-sick-logs [get]
func (c *Controller) ListPigSickLogs(ctx echo.Context) error {
	reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "ListPigSickLogs")
	const actionType = "获取病猪日志列表"

	var req dto.ListPigSickLogRequest
	if err := ctx.Bind(&req); err != nil {
		logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
		return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的查询参数: "+err.Error(), actionType, "参数绑定失败", req)
	}

	resp, err := c.monitorService.ListPigSickLogs(reqCtx, &req)
	if err != nil {
		if errors.Is(err, repository.ErrInvalidPagination) {
			logger.Warnf("%s: 无效的分页参数: %v", actionType, err)
			return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的分页参数: "+err.Error(), actionType, "无效分页参数", req)
		}

		logger.Errorf("%s: 服务层查询失败: %v", actionType, err)
		return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取病猪日志失败: "+err.Error(), actionType, "服务层查询失败", req)
	}

	logger.Infof("%s: 成功, 获取到 %d 条记录, 总计 %d 条", actionType, len(resp.List), resp.Pagination.Total)
	return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取病猪日志成功", resp, actionType, "获取病猪日志成功", req)
}

// ListPigPurchases godoc
// @Summary      获取猪只采购记录列表
// @Description  根据提供的过滤条件，分页获取猪只采购记录
// @Tags         数据监控
// @Security     BearerAuth
// @Produce      json
// @Param        query query dto.ListPigPurchaseRequest true "查询参数"
// @Success      200 {object} controller.Response{data=dto.ListPigPurchaseResponse}
// @Router       /api/v1/monitor/pig-purchases [get]
func (c *Controller) ListPigPurchases(ctx echo.Context) error {
	reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "ListPigPurchases")
	const actionType = "获取猪只采购记录列表"

	var req dto.ListPigPurchaseRequest
	if err := ctx.Bind(&req); err != nil {
		logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
		return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的查询参数: "+err.Error(), actionType, "参数绑定失败", req)
	}

	resp, err := c.monitorService.ListPigPurchases(reqCtx, &req)
	if err != nil {
		if errors.Is(err, repository.ErrInvalidPagination) {
			logger.Warnf("%s: 无效的分页参数: %v", actionType, err)
			return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的分页参数: "+err.Error(), actionType, "无效分页参数", req)
		}

		logger.Errorf("%s: 服务层查询失败: %v", actionType, err)
		return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取猪只采购记录失败: "+err.Error(), actionType, "服务层查询失败", req)
	}

	logger.Infof("%s: 成功, 获取到 %d 条记录, 总计 %d 条", actionType, len(resp.List), resp.Pagination.Total)
	return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取猪只采购记录成功", resp, actionType, "获取猪只采购记录成功", req)
}

// ListPigSales godoc
// @Summary      获取猪只售卖记录列表
// @Description  根据提供的过滤条件，分页获取猪只售卖记录
// @Tags         数据监控
// @Security     BearerAuth
// @Produce      json
// @Param        query query dto.ListPigSaleRequest true "查询参数"
// @Success      200 {object} controller.Response{data=dto.ListPigSaleResponse}
// @Router       /api/v1/monitor/pig-sales [get]
func (c *Controller) ListPigSales(ctx echo.Context) error {
	reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "ListPigSales")
	const actionType = "获取猪只售卖记录列表"

	var req dto.ListPigSaleRequest
	if err := ctx.Bind(&req); err != nil {
		logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
		return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的查询参数: "+err.Error(), actionType, "参数绑定失败", req)
	}

	resp, err := c.monitorService.ListPigSales(reqCtx, &req)
	if err != nil {
		if errors.Is(err, repository.ErrInvalidPagination) {
			logger.Warnf("%s: 无效的分页参数: %v", actionType, err)
			return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的分页参数: "+err.Error(), actionType, "无效分页参数", req)
		}

		logger.Errorf("%s: 服务层查询失败: %v", actionType, err)
		return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取猪只售卖记录失败: "+err.Error(), actionType, "服务层查询失败", req)
	}

	logger.Infof("%s: 成功, 获取到 %d 条记录, 总计 %d 条", actionType, len(resp.List), resp.Pagination.Total)
	return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取猪只售卖记录成功", resp, actionType, "获取猪只售卖记录成功", req)
}

// ListNotifications godoc
// @Summary      批量查询通知
// @Description  根据提供的过滤条件，分页获取通知列表
// @Tags         数据监控
// @Security     BearerAuth
// @Produce      json
// @Param        query query dto.ListNotificationRequest true "查询参数"
// @Success      200 {object} controller.Response{data=dto.ListNotificationResponse}
// @Router       /api/v1/monitor/notifications [get]
func (c *Controller) ListNotifications(ctx echo.Context) error {
	reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "ListNotifications")
	const actionType = "批量查询通知"

	var req dto.ListNotificationRequest
	if err := ctx.Bind(&req); err != nil {
		logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
		return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的查询参数: "+err.Error(), actionType, "参数绑定失败", req)
	}

	resp, err := c.monitorService.ListNotifications(reqCtx, &req)
	if err != nil {
		if errors.Is(err, repository.ErrInvalidPagination) {
			logger.Warnf("%s: 无效的分页参数: %v", actionType, err)
			return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的分页参数: "+err.Error(), actionType, "无效分页参数", req)
		}

		logger.Errorf("%s: 服务层查询失败: %v", actionType, err)
		return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "批量查询通知失败: "+err.Error(), actionType, "服务层查询失败", req)
	}

	logger.Infof("%s: 成功, 获取到 %d 条记录, 总计 %d 条", actionType, len(resp.List), resp.Pagination.Total)
	return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "批量查询通知成功", resp, actionType, "批量查询通知成功", req)
}
