ListMedicationLogs

This commit is contained in:
2025-10-18 16:22:59 +08:00
parent 4c6843afb4
commit e150969ee3
4 changed files with 212 additions and 2 deletions

View File

@@ -453,3 +453,53 @@ func (c *Controller) ListFeedUsageRecords(ctx *gin.Context) {
c.logger.Infof("%s: 成功, 获取到 %d 条记录, 总计 %d 条", actionType, len(data), total)
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 *gin.Context) {
const actionType = "获取用药记录列表"
var req dto.ListMedicationLogRequest
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.MedicationLogListOptions{
PigBatchID: req.PigBatchID,
MedicationID: req.MedicationID,
OperatorID: req.OperatorID,
OrderBy: req.OrderBy,
StartTime: req.StartTime,
EndTime: req.EndTime,
}
if req.Reason != nil {
reason := models.MedicationReasonType(*req.Reason)
opts.Reason = &reason
}
data, total, err := c.monitorService.ListMedicationLogs(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.NewListMedicationLogResponse(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

@@ -191,7 +191,7 @@ type ListTaskExecutionLogRequest struct {
// TaskDTO 是用于API响应的简化版任务结构
type TaskDTO struct {
ID uint `json:"id"`
ID int `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
}
@@ -227,7 +227,7 @@ func NewListTaskExecutionLogResponse(data []models.TaskExecutionLog, total int64
PlanExecutionLogID: item.PlanExecutionLogID,
TaskID: item.TaskID,
Task: TaskDTO{
ID: uint(item.Task.ID),
ID: item.Task.ID,
Name: item.Task.Name,
Description: item.Task.Description,
},
@@ -569,3 +569,75 @@ func NewListFeedUsageRecordResponse(data []models.FeedUsageRecord, total int64,
},
}
}
// --- MedicationLog ---
// ListMedicationLogRequest 定义了获取用药记录列表的请求参数
type ListMedicationLogRequest struct {
Page int `form:"page,default=1"`
PageSize int `form:"pageSize,default=10"`
PigBatchID *uint `form:"pig_batch_id"`
MedicationID *uint `form:"medication_id"`
Reason *string `form:"reason"`
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"`
}
// MedicationDTO 是用于API响应的简化版药品结构
type MedicationDTO struct {
ID uint `json:"id"`
Name string `json:"name"`
}
// MedicationLogDTO 是用于API响应的用药记录结构
type MedicationLogDTO struct {
ID uint `json:"id"`
PigBatchID uint `json:"pig_batch_id"`
MedicationID uint `json:"medication_id"`
Medication MedicationDTO `json:"medication"`
DosageUsed float64 `json:"dosage_used"`
TargetCount int `json:"target_count"`
Reason models.MedicationReasonType `json:"reason"`
Description string `json:"description"`
OperatorID uint `json:"operator_id"`
HappenedAt time.Time `json:"happened_at"`
}
// ListMedicationLogResponse 是获取用药记录列表的响应结构
type ListMedicationLogResponse struct {
List []MedicationLogDTO `json:"list"`
Pagination PaginationDTO `json:"pagination"`
}
// NewListMedicationLogResponse 从模型数据创建列表响应 DTO
func NewListMedicationLogResponse(data []models.MedicationLog, total int64, page, pageSize int) *ListMedicationLogResponse {
dtos := make([]MedicationLogDTO, len(data))
for i, item := range data {
dtos[i] = MedicationLogDTO{
ID: item.ID,
PigBatchID: item.PigBatchID,
MedicationID: item.MedicationID,
Medication: MedicationDTO{
ID: item.Medication.ID,
Name: item.Medication.Name,
},
DosageUsed: item.DosageUsed,
TargetCount: item.TargetCount,
Reason: item.Reason,
Description: item.Description,
OperatorID: item.OperatorID,
HappenedAt: item.HappenedAt,
}
}
return &ListMedicationLogResponse{
List: dtos,
Pagination: PaginationDTO{
Total: total,
Page: page,
PageSize: pageSize,
},
}
}

View File

@@ -13,6 +13,7 @@ type MonitorService struct {
pendingCollectionRepo repository.PendingCollectionRepository
userActionLogRepo repository.UserActionLogRepository
rawMaterialRepo repository.RawMaterialRepository
medicationRepo repository.MedicationRepository
// 在这里可以添加其他超表模型的仓库依赖
}
@@ -24,6 +25,7 @@ func NewMonitorService(
pendingCollectionRepo repository.PendingCollectionRepository,
userActionLogRepo repository.UserActionLogRepository,
rawMaterialRepo repository.RawMaterialRepository,
medicationRepo repository.MedicationRepository,
) *MonitorService {
return &MonitorService{
sensorDataRepo: sensorDataRepo,
@@ -32,6 +34,7 @@ func NewMonitorService(
pendingCollectionRepo: pendingCollectionRepo,
userActionLogRepo: userActionLogRepo,
rawMaterialRepo: rawMaterialRepo,
medicationRepo: medicationRepo,
}
}
@@ -79,3 +82,8 @@ func (s *MonitorService) ListRawMaterialStockLogs(opts repository.RawMaterialSto
func (s *MonitorService) ListFeedUsageRecords(opts repository.FeedUsageRecordListOptions, page, pageSize int) ([]models.FeedUsageRecord, int64, error) {
return s.rawMaterialRepo.ListFeedUsageRecords(opts, page, pageSize)
}
// ListMedicationLogs 负责处理查询用药记录列表的业务逻辑
func (s *MonitorService) ListMedicationLogs(opts repository.MedicationLogListOptions, page, pageSize int) ([]models.MedicationLog, int64, error) {
return s.medicationRepo.ListMedicationLogs(opts, page, pageSize)
}