ListPigTransferLogs

This commit is contained in:
2025-10-19 14:11:18 +08:00
parent 53845422c1
commit 1830fcd43e
6 changed files with 191 additions and 1 deletions

View File

@@ -642,3 +642,54 @@ func (c *Controller) ListWeighingRecords(ctx *gin.Context) {
c.logger.Infof("%s: 成功, 获取到 %d 条记录, 总计 %d 条", actionType, len(data), total)
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 *gin.Context) {
const actionType = "获取猪只迁移日志列表"
var req dto.ListPigTransferLogRequest
if err := ctx.ShouldBindQuery(&req); err != nil {
c.logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的查询参数: "+err.Error(), actionType, "参数绑定失败", req)
return
}
opts := repository.PigTransferLogListOptions{
PigBatchID: req.PigBatchID,
PenID: req.PenID,
OperatorID: req.OperatorID,
CorrelationID: req.CorrelationID,
OrderBy: req.OrderBy,
StartTime: req.StartTime,
EndTime: req.EndTime,
}
if req.TransferType != nil {
transferType := models.PigTransferType(*req.TransferType)
opts.TransferType = &transferType
}
data, total, err := c.monitorService.ListPigTransferLogs(opts, req.Page, req.PageSize)
if err != nil {
if errors.Is(err, repository.ErrInvalidPagination) {
c.logger.Warnf("%s: 无效的分页参数: %v", actionType, err)
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的分页参数: "+err.Error(), actionType, "无效分页参数", req)
return
}
c.logger.Errorf("%s: 服务层查询失败: %v", actionType, err)
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取猪只迁移日志失败: "+err.Error(), actionType, "服务层查询失败", req)
return
}
resp := dto.NewListPigTransferLogResponse(data, total, req.Page, req.PageSize)
c.logger.Infof("%s: 成功, 获取到 %d 条记录, 总计 %d 条", actionType, len(data), total)
controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取猪只迁移日志成功", resp, actionType, "获取猪只迁移日志成功", req)
}

View File

@@ -362,3 +362,33 @@ func NewListWeighingRecordResponse(data []models.WeighingRecord, total int64, pa
},
}
}
// NewListPigTransferLogResponse 从模型数据创建列表响应 DTO
func NewListPigTransferLogResponse(data []models.PigTransferLog, total int64, page, pageSize int) *ListPigTransferLogResponse {
dtos := make([]PigTransferLogDTO, len(data))
for i, item := range data {
// 注意PigTransferLog 的 ID, CreatedAt, UpdatedAt 字段是 gorm.Model 嵌入的
dtos[i] = PigTransferLogDTO{
ID: item.ID,
CreatedAt: item.CreatedAt,
UpdatedAt: item.UpdatedAt,
TransferTime: item.TransferTime,
PigBatchID: item.PigBatchID,
PenID: item.PenID,
Quantity: item.Quantity,
Type: item.Type,
CorrelationID: item.CorrelationID,
OperatorID: item.OperatorID,
Remarks: item.Remarks,
}
}
return &ListPigTransferLogResponse{
List: dtos,
Pagination: PaginationDTO{
Total: total,
Page: page,
PageSize: pageSize,
},
}
}

View File

@@ -460,3 +460,40 @@ type ListWeighingRecordResponse struct {
List []WeighingRecordDTO `json:"list"`
Pagination PaginationDTO `json:"pagination"`
}
// --- PigTransferLog ---
// ListPigTransferLogRequest 定义了获取猪只迁移日志列表的请求参数
type ListPigTransferLogRequest struct {
Page int `form:"page,default=1"`
PageSize int `form:"pageSize,default=10"`
PigBatchID *uint `form:"pig_batch_id"`
PenID *uint `form:"pen_id"`
TransferType *string `form:"transfer_type"`
OperatorID *uint `form:"operator_id"`
CorrelationID *string `form:"correlation_id"`
StartTime *time.Time `form:"start_time" time_format:"rfc3339"`
EndTime *time.Time `form:"end_time" time_format:"rfc3339"`
OrderBy string `form:"order_by"`
}
// PigTransferLogDTO 是用于API响应的猪只迁移日志结构
type PigTransferLogDTO struct {
ID uint `json:"id"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
TransferTime time.Time `json:"transfer_time"`
PigBatchID uint `json:"pig_batch_id"`
PenID uint `json:"pen_id"`
Quantity int `json:"quantity"`
Type models.PigTransferType `json:"type"`
CorrelationID string `json:"correlation_id"`
OperatorID uint `json:"operator_id"`
Remarks string `json:"remarks"`
}
// ListPigTransferLogResponse 是获取猪只迁移日志列表的响应结构
type ListPigTransferLogResponse struct {
List []PigTransferLogDTO `json:"list"`
Pagination PaginationDTO `json:"pagination"`
}

View File

@@ -16,6 +16,7 @@ type MonitorService struct {
medicationRepo repository.MedicationLogRepository
pigBatchRepo repository.PigBatchRepository
pigBatchLogRepo repository.PigBatchLogRepository
pigTransferLogRepo repository.PigTransferLogRepository
}
// NewMonitorService 创建一个新的 MonitorService 实例
@@ -29,6 +30,7 @@ func NewMonitorService(
medicationRepo repository.MedicationLogRepository,
pigBatchRepo repository.PigBatchRepository,
pigBatchLogRepo repository.PigBatchLogRepository,
pigTransferLogRepo repository.PigTransferLogRepository,
) *MonitorService {
return &MonitorService{
sensorDataRepo: sensorDataRepo,
@@ -40,6 +42,7 @@ func NewMonitorService(
medicationRepo: medicationRepo,
pigBatchRepo: pigBatchRepo,
pigBatchLogRepo: pigBatchLogRepo,
pigTransferLogRepo: pigTransferLogRepo,
}
}
@@ -107,3 +110,8 @@ func (s *MonitorService) ListWeighingBatches(opts repository.WeighingBatchListOp
func (s *MonitorService) ListWeighingRecords(opts repository.WeighingRecordListOptions, page, pageSize int) ([]models.WeighingRecord, int64, error) {
return s.pigBatchRepo.ListWeighingRecords(opts, page, pageSize)
}
// ListPigTransferLogs 负责处理查询猪只迁移日志列表的业务逻辑
func (s *MonitorService) ListPigTransferLogs(opts repository.PigTransferLogListOptions, page, pageSize int) ([]models.PigTransferLog, int64, error) {
return s.pigTransferLogRepo.ListPigTransferLogs(opts, page, pageSize)
}