ListWeighingRecords
This commit is contained in:
@@ -596,3 +596,49 @@ func (c *Controller) ListWeighingBatches(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)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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 *gin.Context) {
|
||||||
|
const actionType = "获取单次称重记录列表"
|
||||||
|
|
||||||
|
var req dto.ListWeighingRecordRequest
|
||||||
|
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.WeighingRecordListOptions{
|
||||||
|
WeighingBatchID: req.WeighingBatchID,
|
||||||
|
PenID: req.PenID,
|
||||||
|
OperatorID: req.OperatorID,
|
||||||
|
OrderBy: req.OrderBy,
|
||||||
|
StartTime: req.StartTime,
|
||||||
|
EndTime: req.EndTime,
|
||||||
|
}
|
||||||
|
|
||||||
|
data, total, err := c.monitorService.ListWeighingRecords(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.NewListWeighingRecordResponse(data, total, req.Page, req.PageSize)
|
||||||
|
c.logger.Infof("%s: 成功, 获取到 %d 条记录, 总计 %d 条", actionType, len(data), total)
|
||||||
|
controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取单次称重记录成功", resp, actionType, "获取单次称重记录成功", req)
|
||||||
|
}
|
||||||
|
|||||||
@@ -335,3 +335,30 @@ func NewListWeighingBatchResponse(data []models.WeighingBatch, total int64, page
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewListWeighingRecordResponse 从模型数据创建列表响应 DTO
|
||||||
|
func NewListWeighingRecordResponse(data []models.WeighingRecord, total int64, page, pageSize int) *ListWeighingRecordResponse {
|
||||||
|
dtos := make([]WeighingRecordDTO, len(data))
|
||||||
|
for i, item := range data {
|
||||||
|
dtos[i] = WeighingRecordDTO{
|
||||||
|
ID: item.ID,
|
||||||
|
CreatedAt: item.CreatedAt,
|
||||||
|
UpdatedAt: item.UpdatedAt,
|
||||||
|
Weight: item.Weight,
|
||||||
|
WeighingBatchID: item.WeighingBatchID,
|
||||||
|
PenID: item.PenID,
|
||||||
|
OperatorID: item.OperatorID,
|
||||||
|
Remark: item.Remark,
|
||||||
|
WeighingTime: item.WeighingTime,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return &ListWeighingRecordResponse{
|
||||||
|
List: dtos,
|
||||||
|
Pagination: PaginationDTO{
|
||||||
|
Total: total,
|
||||||
|
Page: page,
|
||||||
|
PageSize: pageSize,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -427,3 +427,36 @@ type ListWeighingBatchResponse struct {
|
|||||||
List []WeighingBatchDTO `json:"list"`
|
List []WeighingBatchDTO `json:"list"`
|
||||||
Pagination PaginationDTO `json:"pagination"`
|
Pagination PaginationDTO `json:"pagination"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// --- WeighingRecord ---
|
||||||
|
|
||||||
|
// ListWeighingRecordRequest 定义了获取单次称重记录列表的请求参数
|
||||||
|
type ListWeighingRecordRequest struct {
|
||||||
|
Page int `form:"page,default=1"`
|
||||||
|
PageSize int `form:"pageSize,default=10"`
|
||||||
|
WeighingBatchID *uint `form:"weighing_batch_id"`
|
||||||
|
PenID *uint `form:"pen_id"`
|
||||||
|
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"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// WeighingRecordDTO 是用于API响应的单次称重记录结构
|
||||||
|
type WeighingRecordDTO struct {
|
||||||
|
ID uint `json:"id"`
|
||||||
|
CreatedAt time.Time `json:"created_at"`
|
||||||
|
UpdatedAt time.Time `json:"updated_at"`
|
||||||
|
Weight float64 `json:"weight"`
|
||||||
|
WeighingBatchID uint `json:"weighing_batch_id"`
|
||||||
|
PenID uint `json:"pen_id"`
|
||||||
|
OperatorID uint `json:"operator_id"`
|
||||||
|
Remark string `json:"remark"`
|
||||||
|
WeighingTime time.Time `json:"weighing_time"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// ListWeighingRecordResponse 是获取单次称重记录列表的响应结构
|
||||||
|
type ListWeighingRecordResponse struct {
|
||||||
|
List []WeighingRecordDTO `json:"list"`
|
||||||
|
Pagination PaginationDTO `json:"pagination"`
|
||||||
|
}
|
||||||
|
|||||||
@@ -16,7 +16,6 @@ type MonitorService struct {
|
|||||||
medicationRepo repository.MedicationLogRepository
|
medicationRepo repository.MedicationLogRepository
|
||||||
pigBatchRepo repository.PigBatchRepository
|
pigBatchRepo repository.PigBatchRepository
|
||||||
pigBatchLogRepo repository.PigBatchLogRepository
|
pigBatchLogRepo repository.PigBatchLogRepository
|
||||||
// 在这里可以添加其他超表模型的仓库依赖
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewMonitorService 创建一个新的 MonitorService 实例
|
// NewMonitorService 创建一个新的 MonitorService 实例
|
||||||
@@ -103,3 +102,8 @@ func (s *MonitorService) ListPigBatchLogs(opts repository.PigBatchLogListOptions
|
|||||||
func (s *MonitorService) ListWeighingBatches(opts repository.WeighingBatchListOptions, page, pageSize int) ([]models.WeighingBatch, int64, error) {
|
func (s *MonitorService) ListWeighingBatches(opts repository.WeighingBatchListOptions, page, pageSize int) ([]models.WeighingBatch, int64, error) {
|
||||||
return s.pigBatchRepo.ListWeighingBatches(opts, page, pageSize)
|
return s.pigBatchRepo.ListWeighingBatches(opts, page, pageSize)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ListWeighingRecords 负责处理查询单次称重记录列表的业务逻辑
|
||||||
|
func (s *MonitorService) ListWeighingRecords(opts repository.WeighingRecordListOptions, page, pageSize int) ([]models.WeighingRecord, int64, error) {
|
||||||
|
return s.pigBatchRepo.ListWeighingRecords(opts, page, pageSize)
|
||||||
|
}
|
||||||
|
|||||||
@@ -22,6 +22,9 @@ type PigBatchRepository interface {
|
|||||||
|
|
||||||
// ListWeighingBatches 支持分页和过滤的批次称重列表查询
|
// ListWeighingBatches 支持分页和过滤的批次称重列表查询
|
||||||
ListWeighingBatches(opts WeighingBatchListOptions, page, pageSize int) ([]models.WeighingBatch, int64, error)
|
ListWeighingBatches(opts WeighingBatchListOptions, page, pageSize int) ([]models.WeighingBatch, int64, error)
|
||||||
|
|
||||||
|
// ListWeighingRecords 支持分页和过滤的单次称重记录列表查询
|
||||||
|
ListWeighingRecords(opts WeighingRecordListOptions, page, pageSize int) ([]models.WeighingRecord, int64, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// WeighingBatchListOptions 定义了查询批次称重记录时的可选参数
|
// WeighingBatchListOptions 定义了查询批次称重记录时的可选参数
|
||||||
@@ -32,6 +35,16 @@ type WeighingBatchListOptions struct {
|
|||||||
OrderBy string // 例如 "weighing_time asc"
|
OrderBy string // 例如 "weighing_time asc"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WeighingRecordListOptions 定义了查询单次称重记录时的可选参数
|
||||||
|
type WeighingRecordListOptions struct {
|
||||||
|
WeighingBatchID *uint
|
||||||
|
PenID *uint
|
||||||
|
OperatorID *uint
|
||||||
|
StartTime *time.Time // 基于 weighing_time 字段
|
||||||
|
EndTime *time.Time // 基于 weighing_time 字段
|
||||||
|
OrderBy string // 例如 "weighing_time asc"
|
||||||
|
}
|
||||||
|
|
||||||
// gormPigBatchRepository 是 PigBatchRepository 的 GORM 实现
|
// gormPigBatchRepository 是 PigBatchRepository 的 GORM 实现
|
||||||
type gormPigBatchRepository struct {
|
type gormPigBatchRepository struct {
|
||||||
db *gorm.DB
|
db *gorm.DB
|
||||||
@@ -150,3 +163,46 @@ func (r *gormPigBatchRepository) ListWeighingBatches(opts WeighingBatchListOptio
|
|||||||
|
|
||||||
return results, total, err
|
return results, total, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ListWeighingRecords 实现了分页和过滤查询单次称重记录的功能
|
||||||
|
func (r *gormPigBatchRepository) ListWeighingRecords(opts WeighingRecordListOptions, page, pageSize int) ([]models.WeighingRecord, int64, error) {
|
||||||
|
if page <= 0 || pageSize <= 0 {
|
||||||
|
return nil, 0, ErrInvalidPagination
|
||||||
|
}
|
||||||
|
|
||||||
|
var results []models.WeighingRecord
|
||||||
|
var total int64
|
||||||
|
|
||||||
|
query := r.db.Model(&models.WeighingRecord{})
|
||||||
|
|
||||||
|
if opts.WeighingBatchID != nil {
|
||||||
|
query = query.Where("weighing_batch_id = ?", *opts.WeighingBatchID)
|
||||||
|
}
|
||||||
|
if opts.PenID != nil {
|
||||||
|
query = query.Where("pen_id = ?", *opts.PenID)
|
||||||
|
}
|
||||||
|
if opts.OperatorID != nil {
|
||||||
|
query = query.Where("operator_id = ?", *opts.OperatorID)
|
||||||
|
}
|
||||||
|
if opts.StartTime != nil {
|
||||||
|
query = query.Where("weighing_time >= ?", *opts.StartTime)
|
||||||
|
}
|
||||||
|
if opts.EndTime != nil {
|
||||||
|
query = query.Where("weighing_time <= ?", *opts.EndTime)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := query.Count(&total).Error; err != nil {
|
||||||
|
return nil, 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
orderBy := "weighing_time 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