ListPigSickLogs
This commit is contained in:
@@ -693,3 +693,57 @@ func (c *Controller) ListPigTransferLogs(ctx *gin.Context) {
|
|||||||
c.logger.Infof("%s: 成功, 获取到 %d 条记录, 总计 %d 条", actionType, len(data), total)
|
c.logger.Infof("%s: 成功, 获取到 %d 条记录, 总计 %d 条", actionType, len(data), total)
|
||||||
controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取猪只迁移日志成功", resp, actionType, "获取猪只迁移日志成功", req)
|
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 *gin.Context) {
|
||||||
|
const actionType = "获取病猪日志列表"
|
||||||
|
|
||||||
|
var req dto.ListPigSickLogRequest
|
||||||
|
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.PigSickLogListOptions{
|
||||||
|
PigBatchID: req.PigBatchID,
|
||||||
|
PenID: req.PenID,
|
||||||
|
OperatorID: req.OperatorID,
|
||||||
|
OrderBy: req.OrderBy,
|
||||||
|
StartTime: req.StartTime,
|
||||||
|
EndTime: req.EndTime,
|
||||||
|
}
|
||||||
|
if req.Reason != nil {
|
||||||
|
reason := models.PigBatchSickPigReasonType(*req.Reason)
|
||||||
|
opts.Reason = &reason
|
||||||
|
}
|
||||||
|
if req.TreatmentLocation != nil {
|
||||||
|
treatmentLocation := models.PigBatchSickPigTreatmentLocation(*req.TreatmentLocation)
|
||||||
|
opts.TreatmentLocation = &treatmentLocation
|
||||||
|
}
|
||||||
|
|
||||||
|
data, total, err := c.monitorService.ListPigSickLogs(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.NewListPigSickLogResponse(data, total, req.Page, req.PageSize)
|
||||||
|
c.logger.Infof("%s: 成功, 获取到 %d 条记录, 总计 %d 条", actionType, len(data), total)
|
||||||
|
controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取病猪日志成功", resp, actionType, "获取病猪日志成功", req)
|
||||||
|
}
|
||||||
|
|||||||
@@ -392,3 +392,34 @@ func NewListPigTransferLogResponse(data []models.PigTransferLog, total int64, pa
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewListPigSickLogResponse 从模型数据创建列表响应 DTO
|
||||||
|
func NewListPigSickLogResponse(data []models.PigSickLog, total int64, page, pageSize int) *ListPigSickLogResponse {
|
||||||
|
dtos := make([]PigSickLogDTO, len(data))
|
||||||
|
for i, item := range data {
|
||||||
|
dtos[i] = PigSickLogDTO{
|
||||||
|
ID: item.ID,
|
||||||
|
CreatedAt: item.CreatedAt,
|
||||||
|
UpdatedAt: item.UpdatedAt,
|
||||||
|
PigBatchID: item.PigBatchID,
|
||||||
|
PenID: item.PenID,
|
||||||
|
ChangeCount: item.ChangeCount,
|
||||||
|
Reason: item.Reason,
|
||||||
|
BeforeCount: item.BeforeCount,
|
||||||
|
AfterCount: item.AfterCount,
|
||||||
|
Remarks: item.Remarks,
|
||||||
|
TreatmentLocation: item.TreatmentLocation,
|
||||||
|
OperatorID: item.OperatorID,
|
||||||
|
HappenedAt: item.HappenedAt,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return &ListPigSickLogResponse{
|
||||||
|
List: dtos,
|
||||||
|
Pagination: PaginationDTO{
|
||||||
|
Total: total,
|
||||||
|
Page: page,
|
||||||
|
PageSize: pageSize,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -497,3 +497,42 @@ type ListPigTransferLogResponse struct {
|
|||||||
List []PigTransferLogDTO `json:"list"`
|
List []PigTransferLogDTO `json:"list"`
|
||||||
Pagination PaginationDTO `json:"pagination"`
|
Pagination PaginationDTO `json:"pagination"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// --- PigSickLog ---
|
||||||
|
|
||||||
|
// ListPigSickLogRequest 定义了获取病猪日志列表的请求参数
|
||||||
|
type ListPigSickLogRequest struct {
|
||||||
|
Page int `form:"page,default=1"`
|
||||||
|
PageSize int `form:"pageSize,default=10"`
|
||||||
|
PigBatchID *uint `form:"pig_batch_id"`
|
||||||
|
PenID *uint `form:"pen_id"`
|
||||||
|
Reason *string `form:"reason"`
|
||||||
|
TreatmentLocation *string `form:"treatment_location"`
|
||||||
|
OperatorID *uint `form:"operator_id"`
|
||||||
|
StartTime *time.Time `form:"start_time" time_format:"rfc3339"`
|
||||||
|
EndTime *time.Time `form:"end_time" time_format:"rfc3339"`
|
||||||
|
OrderBy string `form:"order_by"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// PigSickLogDTO 是用于API响应的病猪日志结构
|
||||||
|
type PigSickLogDTO struct {
|
||||||
|
ID uint `json:"id"`
|
||||||
|
CreatedAt time.Time `json:"created_at"`
|
||||||
|
UpdatedAt time.Time `json:"updated_at"`
|
||||||
|
PigBatchID uint `json:"pig_batch_id"`
|
||||||
|
PenID uint `json:"pen_id"`
|
||||||
|
ChangeCount int `json:"change_count"`
|
||||||
|
Reason models.PigBatchSickPigReasonType `json:"reason"`
|
||||||
|
BeforeCount int `json:"before_count"`
|
||||||
|
AfterCount int `json:"after_count"`
|
||||||
|
Remarks string `json:"remarks"`
|
||||||
|
TreatmentLocation models.PigBatchSickPigTreatmentLocation `json:"treatment_location"`
|
||||||
|
OperatorID uint `json:"operator_id"`
|
||||||
|
HappenedAt time.Time `json:"happened_at"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// ListPigSickLogResponse 是获取病猪日志列表的响应结构
|
||||||
|
type ListPigSickLogResponse struct {
|
||||||
|
List []PigSickLogDTO `json:"list"`
|
||||||
|
Pagination PaginationDTO `json:"pagination"`
|
||||||
|
}
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ type MonitorService struct {
|
|||||||
pigBatchRepo repository.PigBatchRepository
|
pigBatchRepo repository.PigBatchRepository
|
||||||
pigBatchLogRepo repository.PigBatchLogRepository
|
pigBatchLogRepo repository.PigBatchLogRepository
|
||||||
pigTransferLogRepo repository.PigTransferLogRepository
|
pigTransferLogRepo repository.PigTransferLogRepository
|
||||||
|
pigSickLogRepo repository.PigSickLogRepository
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewMonitorService 创建一个新的 MonitorService 实例
|
// NewMonitorService 创建一个新的 MonitorService 实例
|
||||||
@@ -31,6 +32,7 @@ func NewMonitorService(
|
|||||||
pigBatchRepo repository.PigBatchRepository,
|
pigBatchRepo repository.PigBatchRepository,
|
||||||
pigBatchLogRepo repository.PigBatchLogRepository,
|
pigBatchLogRepo repository.PigBatchLogRepository,
|
||||||
pigTransferLogRepo repository.PigTransferLogRepository,
|
pigTransferLogRepo repository.PigTransferLogRepository,
|
||||||
|
pigSickLogRepo repository.PigSickLogRepository,
|
||||||
) *MonitorService {
|
) *MonitorService {
|
||||||
return &MonitorService{
|
return &MonitorService{
|
||||||
sensorDataRepo: sensorDataRepo,
|
sensorDataRepo: sensorDataRepo,
|
||||||
@@ -43,6 +45,7 @@ func NewMonitorService(
|
|||||||
pigBatchRepo: pigBatchRepo,
|
pigBatchRepo: pigBatchRepo,
|
||||||
pigBatchLogRepo: pigBatchLogRepo,
|
pigBatchLogRepo: pigBatchLogRepo,
|
||||||
pigTransferLogRepo: pigTransferLogRepo,
|
pigTransferLogRepo: pigTransferLogRepo,
|
||||||
|
pigSickLogRepo: pigSickLogRepo,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -115,3 +118,8 @@ func (s *MonitorService) ListWeighingRecords(opts repository.WeighingRecordListO
|
|||||||
func (s *MonitorService) ListPigTransferLogs(opts repository.PigTransferLogListOptions, page, pageSize int) ([]models.PigTransferLog, int64, error) {
|
func (s *MonitorService) ListPigTransferLogs(opts repository.PigTransferLogListOptions, page, pageSize int) ([]models.PigTransferLog, int64, error) {
|
||||||
return s.pigTransferLogRepo.ListPigTransferLogs(opts, page, pageSize)
|
return s.pigTransferLogRepo.ListPigTransferLogs(opts, page, pageSize)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ListPigSickLogs 负责处理查询病猪日志列表的业务逻辑
|
||||||
|
func (s *MonitorService) ListPigSickLogs(opts repository.PigSickLogListOptions, page, pageSize int) ([]models.PigSickLog, int64, error) {
|
||||||
|
return s.pigSickLogRepo.ListPigSickLogs(opts, page, pageSize)
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,11 +2,24 @@ package repository
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"time"
|
||||||
|
|
||||||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
|
"git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// PigSickLogListOptions 定义了查询病猪日志时的可选参数
|
||||||
|
type PigSickLogListOptions struct {
|
||||||
|
PigBatchID *uint
|
||||||
|
PenID *uint
|
||||||
|
Reason *models.PigBatchSickPigReasonType
|
||||||
|
TreatmentLocation *models.PigBatchSickPigTreatmentLocation
|
||||||
|
OperatorID *uint
|
||||||
|
StartTime *time.Time // 基于 happened_at 字段
|
||||||
|
EndTime *time.Time // 基于 happened_at 字段
|
||||||
|
OrderBy string // 例如 "happened_at desc"
|
||||||
|
}
|
||||||
|
|
||||||
// PigSickLogRepository 定义了与病猪日志模型相关的数据库操作接口。
|
// PigSickLogRepository 定义了与病猪日志模型相关的数据库操作接口。
|
||||||
type PigSickLogRepository interface {
|
type PigSickLogRepository interface {
|
||||||
// CreatePigSickLog 创建一条新的病猪日志记录
|
// CreatePigSickLog 创建一条新的病猪日志记录
|
||||||
@@ -15,6 +28,9 @@ type PigSickLogRepository interface {
|
|||||||
|
|
||||||
// GetLastLogByBatchTx 在事务中获取指定批次和猪栏的最新一条 PigSickLog 记录
|
// GetLastLogByBatchTx 在事务中获取指定批次和猪栏的最新一条 PigSickLog 记录
|
||||||
GetLastLogByBatchTx(tx *gorm.DB, batchID uint) (*models.PigSickLog, error)
|
GetLastLogByBatchTx(tx *gorm.DB, batchID uint) (*models.PigSickLog, error)
|
||||||
|
|
||||||
|
// ListPigSickLogs 支持分页和过滤的病猪日志列表查询
|
||||||
|
ListPigSickLogs(opts PigSickLogListOptions, page, pageSize int) ([]models.PigSickLog, int64, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// gormPigSickLogRepository 是 PigSickLogRepository 接口的 GORM 实现。
|
// gormPigSickLogRepository 是 PigSickLogRepository 接口的 GORM 实现。
|
||||||
@@ -51,3 +67,52 @@ func (r *gormPigSickLogRepository) GetLastLogByBatchTx(tx *gorm.DB, batchID uint
|
|||||||
}
|
}
|
||||||
return &lastLog, nil
|
return &lastLog, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ListPigSickLogs 实现了分页和过滤查询病猪日志的功能
|
||||||
|
func (r *gormPigSickLogRepository) ListPigSickLogs(opts PigSickLogListOptions, page, pageSize int) ([]models.PigSickLog, int64, error) {
|
||||||
|
if page <= 0 || pageSize <= 0 {
|
||||||
|
return nil, 0, ErrInvalidPagination
|
||||||
|
}
|
||||||
|
|
||||||
|
var results []models.PigSickLog
|
||||||
|
var total int64
|
||||||
|
|
||||||
|
query := r.db.Model(&models.PigSickLog{})
|
||||||
|
|
||||||
|
if opts.PigBatchID != nil {
|
||||||
|
query = query.Where("pig_batch_id = ?", *opts.PigBatchID)
|
||||||
|
}
|
||||||
|
if opts.PenID != nil {
|
||||||
|
query = query.Where("pen_id = ?", *opts.PenID)
|
||||||
|
}
|
||||||
|
if opts.Reason != nil {
|
||||||
|
query = query.Where("reason = ?", *opts.Reason)
|
||||||
|
}
|
||||||
|
if opts.TreatmentLocation != nil {
|
||||||
|
query = query.Where("treatment_location = ?", *opts.TreatmentLocation)
|
||||||
|
}
|
||||||
|
if opts.OperatorID != nil {
|
||||||
|
query = query.Where("operator_id = ?", *opts.OperatorID)
|
||||||
|
}
|
||||||
|
if opts.StartTime != nil {
|
||||||
|
query = query.Where("happened_at >= ?", *opts.StartTime)
|
||||||
|
}
|
||||||
|
if opts.EndTime != nil {
|
||||||
|
query = query.Where("happened_at <= ?", *opts.EndTime)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := query.Count(&total).Error; err != nil {
|
||||||
|
return nil, 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
orderBy := "happened_at DESC"
|
||||||
|
if opts.OrderBy != "" {
|
||||||
|
orderBy = opts.OrderBy
|
||||||
|
}
|
||||||
|
query = query.Order(orderBy)
|
||||||
|
|
||||||
|
offset := (page - 1) * pageSize
|
||||||
|
err := query.Limit(pageSize).Offset(offset).Find(&results).Error
|
||||||
|
|
||||||
|
return results, total, err
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user