package dto import ( "encoding/json" "time" "git.huangwc.com/pig/pig-farm-controller/internal/infra/models" ) // --- General --- // PaginationDTO 定义了分页信息的标准结构 type PaginationDTO struct { Total int64 `json:"total"` Page int `json:"page"` PageSize int `json:"pageSize"` } // --- SensorData --- // ListSensorDataRequest 定义了获取传感器数据列表的请求参数 type ListSensorDataRequest struct { Page int `form:"page,default=1"` PageSize int `form:"pageSize,default=10"` DeviceID *uint `form:"device_id"` SensorType *string `form:"sensor_type"` StartTime *time.Time `form:"start_time" time_format:"rfc3339"` EndTime *time.Time `form:"end_time" time_format:"rfc3339"` OrderBy string `form:"order_by"` } // SensorDataDTO 是用于API响应的传感器数据结构 type SensorDataDTO struct { Time time.Time `json:"time"` DeviceID uint `json:"device_id"` RegionalControllerID uint `json:"regional_controller_id"` SensorType models.SensorType `json:"sensor_type"` Data json.RawMessage `json:"data"` } // ListSensorDataResponse 是获取传感器数据列表的响应结构 type ListSensorDataResponse struct { List []SensorDataDTO `json:"list"` Pagination PaginationDTO `json:"pagination"` } // NewListSensorDataResponse 从模型数据创建列表响应 DTO func NewListSensorDataResponse(data []models.SensorData, total int64, page, pageSize int) *ListSensorDataResponse { dtos := make([]SensorDataDTO, len(data)) for i, item := range data { dtos[i] = SensorDataDTO{ Time: item.Time, DeviceID: item.DeviceID, RegionalControllerID: item.RegionalControllerID, SensorType: item.SensorType, Data: json.RawMessage(item.Data), } } return &ListSensorDataResponse{ List: dtos, Pagination: PaginationDTO{ Total: total, Page: page, PageSize: pageSize, }, } } // --- DeviceCommandLog --- // ListDeviceCommandLogRequest 定义了获取设备命令日志列表的请求参数 type ListDeviceCommandLogRequest struct { Page int `form:"page,default=1"` PageSize int `form:"pageSize,default=10"` DeviceID *uint `form:"device_id"` ReceivedSuccess *bool `form:"received_success"` StartTime *time.Time `form:"start_time" time_format:"rfc3339"` EndTime *time.Time `form:"end_time" time_format:"rfc3339"` OrderBy string `form:"order_by"` } // DeviceCommandLogDTO 是用于API响应的设备命令日志结构 type DeviceCommandLogDTO struct { MessageID string `json:"message_id"` DeviceID uint `json:"device_id"` SentAt time.Time `json:"sent_at"` AcknowledgedAt *time.Time `json:"acknowledged_at"` ReceivedSuccess bool `json:"received_success"` } // ListDeviceCommandLogResponse 是获取设备命令日志列表的响应结构 type ListDeviceCommandLogResponse struct { List []DeviceCommandLogDTO `json:"list"` Pagination PaginationDTO `json:"pagination"` } // NewListDeviceCommandLogResponse 从模型数据创建列表响应 DTO func NewListDeviceCommandLogResponse(data []models.DeviceCommandLog, total int64, page, pageSize int) *ListDeviceCommandLogResponse { dtos := make([]DeviceCommandLogDTO, len(data)) for i, item := range data { dtos[i] = DeviceCommandLogDTO{ MessageID: item.MessageID, DeviceID: item.DeviceID, SentAt: item.SentAt, AcknowledgedAt: item.AcknowledgedAt, ReceivedSuccess: item.ReceivedSuccess, } } return &ListDeviceCommandLogResponse{ List: dtos, Pagination: PaginationDTO{ Total: total, Page: page, PageSize: pageSize, }, } } // --- PlanExecutionLog --- // ListPlanExecutionLogRequest 定义了获取计划执行日志列表的请求参数 type ListPlanExecutionLogRequest struct { Page int `form:"page,default=1"` PageSize int `form:"pageSize,default=10"` PlanID *uint `form:"plan_id"` Status *string `form:"status"` StartTime *time.Time `form:"start_time" time_format:"rfc3339"` EndTime *time.Time `form:"end_time" time_format:"rfc3339"` OrderBy string `form:"order_by"` } // PlanExecutionLogDTO 是用于API响应的计划执行日志结构 type PlanExecutionLogDTO struct { ID uint `json:"id"` CreatedAt time.Time `json:"created_at"` UpdatedAt time.Time `json:"updated_at"` PlanID uint `json:"plan_id"` Status models.ExecutionStatus `json:"status"` StartedAt time.Time `json:"started_at"` EndedAt time.Time `json:"ended_at"` Error string `json:"error"` } // ListPlanExecutionLogResponse 是获取计划执行日志列表的响应结构 type ListPlanExecutionLogResponse struct { List []PlanExecutionLogDTO `json:"list"` Pagination PaginationDTO `json:"pagination"` } // NewListPlanExecutionLogResponse 从模型数据创建列表响应 DTO func NewListPlanExecutionLogResponse(data []models.PlanExecutionLog, total int64, page, pageSize int) *ListPlanExecutionLogResponse { dtos := make([]PlanExecutionLogDTO, len(data)) for i, item := range data { dtos[i] = PlanExecutionLogDTO{ ID: item.ID, CreatedAt: item.CreatedAt, UpdatedAt: item.UpdatedAt, PlanID: item.PlanID, Status: item.Status, StartedAt: item.StartedAt, EndedAt: item.EndedAt, Error: item.Error, } } return &ListPlanExecutionLogResponse{ List: dtos, Pagination: PaginationDTO{ Total: total, Page: page, PageSize: pageSize, }, } } // --- TaskExecutionLog --- // ListTaskExecutionLogRequest 定义了获取任务执行日志列表的请求参数 type ListTaskExecutionLogRequest struct { Page int `form:"page,default=1"` PageSize int `form:"pageSize,default=10"` PlanExecutionLogID *uint `form:"plan_execution_log_id"` TaskID *int `form:"task_id"` Status *string `form:"status"` StartTime *time.Time `form:"start_time" time_format:"rfc3339"` EndTime *time.Time `form:"end_time" time_format:"rfc3339"` OrderBy string `form:"order_by"` } // TaskDTO 是用于API响应的简化版任务结构 type TaskDTO struct { ID uint `json:"id"` Name string `json:"name"` Description string `json:"description"` } // TaskExecutionLogDTO 是用于API响应的任务执行日志结构 type TaskExecutionLogDTO struct { ID uint `json:"id"` CreatedAt time.Time `json:"created_at"` UpdatedAt time.Time `json:"updated_at"` PlanExecutionLogID uint `json:"plan_execution_log_id"` TaskID int `json:"task_id"` Task TaskDTO `json:"task"` // 嵌套的任务信息 Status models.ExecutionStatus `json:"status"` Output string `json:"output"` StartedAt time.Time `json:"started_at"` EndedAt time.Time `json:"ended_at"` } // ListTaskExecutionLogResponse 是获取任务执行日志列表的响应结构 type ListTaskExecutionLogResponse struct { List []TaskExecutionLogDTO `json:"list"` Pagination PaginationDTO `json:"pagination"` } // NewListTaskExecutionLogResponse 从模型数据创建列表响应 DTO func NewListTaskExecutionLogResponse(data []models.TaskExecutionLog, total int64, page, pageSize int) *ListTaskExecutionLogResponse { dtos := make([]TaskExecutionLogDTO, len(data)) for i, item := range data { dtos[i] = TaskExecutionLogDTO{ ID: item.ID, CreatedAt: item.CreatedAt, UpdatedAt: item.UpdatedAt, PlanExecutionLogID: item.PlanExecutionLogID, TaskID: item.TaskID, Task: TaskDTO{ ID: uint(item.Task.ID), Name: item.Task.Name, Description: item.Task.Description, }, Status: item.Status, Output: item.Output, StartedAt: item.StartedAt, EndedAt: item.EndedAt, } } return &ListTaskExecutionLogResponse{ List: dtos, Pagination: PaginationDTO{ Total: total, Page: page, PageSize: pageSize, }, } } // --- PendingCollection --- // ListPendingCollectionRequest 定义了获取待采集请求列表的请求参数 type ListPendingCollectionRequest struct { Page int `form:"page,default=1"` PageSize int `form:"pageSize,default=10"` DeviceID *uint `form:"device_id"` Status *string `form:"status"` StartTime *time.Time `form:"start_time" time_format:"rfc3339"` EndTime *time.Time `form:"end_time" time_format:"rfc3339"` OrderBy string `form:"order_by"` } // PendingCollectionDTO 是用于API响应的待采集请求结构 type PendingCollectionDTO struct { CorrelationID string `json:"correlation_id"` DeviceID uint `json:"device_id"` CommandMetadata models.UintArray `json:"command_metadata"` Status models.PendingCollectionStatus `json:"status"` FulfilledAt *time.Time `json:"fulfilled_at"` CreatedAt time.Time `json:"created_at"` } // ListPendingCollectionResponse 是获取待采集请求列表的响应结构 type ListPendingCollectionResponse struct { List []PendingCollectionDTO `json:"list"` Pagination PaginationDTO `json:"pagination"` } // NewListPendingCollectionResponse 从模型数据创建列表响应 DTO func NewListPendingCollectionResponse(data []models.PendingCollection, total int64, page, pageSize int) *ListPendingCollectionResponse { dtos := make([]PendingCollectionDTO, len(data)) for i, item := range data { dtos[i] = PendingCollectionDTO{ CorrelationID: item.CorrelationID, DeviceID: item.DeviceID, CommandMetadata: item.CommandMetadata, Status: item.Status, FulfilledAt: item.FulfilledAt, CreatedAt: item.CreatedAt, } } return &ListPendingCollectionResponse{ List: dtos, Pagination: PaginationDTO{ Total: total, Page: page, PageSize: pageSize, }, } } // --- UserActionLog --- // ListUserActionLogRequest 定义了获取用户操作日志列表的请求参数 type ListUserActionLogRequest struct { Page int `form:"page,default=1"` PageSize int `form:"pageSize,default=10"` UserID *uint `form:"user_id"` Username *string `form:"username"` ActionType *string `form:"action_type"` Status *string `form:"status"` StartTime *time.Time `form:"start_time" time_format:"rfc3339"` EndTime *time.Time `form:"end_time" time_format:"rfc3339"` OrderBy string `form:"order_by"` } // UserActionLogDTO 是用于API响应的用户操作日志结构 type UserActionLogDTO struct { ID uint `json:"id"` Time time.Time `json:"time"` UserID uint `json:"user_id"` Username string `json:"username"` SourceIP string `json:"source_ip"` ActionType string `json:"action_type"` TargetResource json.RawMessage `json:"target_resource"` Description string `json:"description"` Status models.AuditStatus `json:"status"` HTTPPath string `json:"http_path"` HTTPMethod string `json:"http_method"` ResultDetails string `json:"result_details"` } // ListUserActionLogResponse 是获取用户操作日志列表的响应结构 type ListUserActionLogResponse struct { List []UserActionLogDTO `json:"list"` Pagination PaginationDTO `json:"pagination"` } // NewListUserActionLogResponse 从模型数据创建列表响应 DTO func NewListUserActionLogResponse(data []models.UserActionLog, total int64, page, pageSize int) *ListUserActionLogResponse { dtos := make([]UserActionLogDTO, len(data)) for i, item := range data { dtos[i] = UserActionLogDTO{ ID: item.ID, Time: item.Time, UserID: item.UserID, Username: item.Username, SourceIP: item.SourceIP, ActionType: item.ActionType, TargetResource: json.RawMessage(item.TargetResource), Description: item.Description, Status: item.Status, HTTPPath: item.HTTPPath, HTTPMethod: item.HTTPMethod, ResultDetails: item.ResultDetails, } } return &ListUserActionLogResponse{ List: dtos, Pagination: PaginationDTO{ Total: total, Page: page, PageSize: pageSize, }, } } // --- RawMaterialPurchase --- // ListRawMaterialPurchaseRequest 定义了获取原料采购列表的请求参数 type ListRawMaterialPurchaseRequest struct { Page int `form:"page,default=1"` PageSize int `form:"pageSize,default=10"` RawMaterialID *uint `form:"raw_material_id"` Supplier *string `form:"supplier"` StartTime *time.Time `form:"start_time" time_format:"rfc3339"` EndTime *time.Time `form:"end_time" time_format:"rfc3339"` OrderBy string `form:"order_by"` } // RawMaterialDTO 是用于API响应的简化版原料结构 type RawMaterialDTO struct { ID uint `json:"id"` Name string `json:"name"` } // RawMaterialPurchaseDTO 是用于API响应的原料采购结构 type RawMaterialPurchaseDTO struct { ID uint `json:"id"` RawMaterialID uint `json:"raw_material_id"` RawMaterial RawMaterialDTO `json:"raw_material"` Supplier string `json:"supplier"` Amount float64 `json:"amount"` UnitPrice float64 `json:"unit_price"` TotalPrice float64 `json:"total_price"` PurchaseDate time.Time `json:"purchase_date"` CreatedAt time.Time `json:"created_at"` } // ListRawMaterialPurchaseResponse 是获取原料采购列表的响应结构 type ListRawMaterialPurchaseResponse struct { List []RawMaterialPurchaseDTO `json:"list"` Pagination PaginationDTO `json:"pagination"` } // NewListRawMaterialPurchaseResponse 从模型数据创建列表响应 DTO func NewListRawMaterialPurchaseResponse(data []models.RawMaterialPurchase, total int64, page, pageSize int) *ListRawMaterialPurchaseResponse { dtos := make([]RawMaterialPurchaseDTO, len(data)) for i, item := range data { dtos[i] = RawMaterialPurchaseDTO{ ID: item.ID, RawMaterialID: item.RawMaterialID, RawMaterial: RawMaterialDTO{ ID: item.RawMaterial.ID, Name: item.RawMaterial.Name, }, Supplier: item.Supplier, Amount: item.Amount, UnitPrice: item.UnitPrice, TotalPrice: item.TotalPrice, PurchaseDate: item.PurchaseDate, CreatedAt: item.CreatedAt, } } return &ListRawMaterialPurchaseResponse{ List: dtos, Pagination: PaginationDTO{ Total: total, Page: page, PageSize: pageSize, }, } } // --- RawMaterialStockLog --- // ListRawMaterialStockLogRequest 定义了获取原料库存日志列表的请求参数 type ListRawMaterialStockLogRequest struct { Page int `form:"page,default=1"` PageSize int `form:"pageSize,default=10"` RawMaterialID *uint `form:"raw_material_id"` SourceType *string `form:"source_type"` SourceID *uint `form:"source_id"` StartTime *time.Time `form:"start_time" time_format:"rfc3339"` EndTime *time.Time `form:"end_time" time_format:"rfc3339"` OrderBy string `form:"order_by"` } // RawMaterialStockLogDTO 是用于API响应的原料库存日志结构 type RawMaterialStockLogDTO struct { ID uint `json:"id"` RawMaterialID uint `json:"raw_material_id"` ChangeAmount float64 `json:"change_amount"` SourceType models.StockLogSourceType `json:"source_type"` SourceID uint `json:"source_id"` HappenedAt time.Time `json:"happened_at"` Remarks string `json:"remarks"` } // ListRawMaterialStockLogResponse 是获取原料库存日志列表的响应结构 type ListRawMaterialStockLogResponse struct { List []RawMaterialStockLogDTO `json:"list"` Pagination PaginationDTO `json:"pagination"` } // NewListRawMaterialStockLogResponse 从模型数据创建列表响应 DTO func NewListRawMaterialStockLogResponse(data []models.RawMaterialStockLog, total int64, page, pageSize int) *ListRawMaterialStockLogResponse { dtos := make([]RawMaterialStockLogDTO, len(data)) for i, item := range data { dtos[i] = RawMaterialStockLogDTO{ ID: item.ID, RawMaterialID: item.RawMaterialID, ChangeAmount: item.ChangeAmount, SourceType: item.SourceType, SourceID: item.SourceID, HappenedAt: item.HappenedAt, Remarks: item.Remarks, } } return &ListRawMaterialStockLogResponse{ List: dtos, Pagination: PaginationDTO{ Total: total, Page: page, PageSize: pageSize, }, } } // --- FeedUsageRecord --- // ListFeedUsageRecordRequest 定义了获取饲料使用记录列表的请求参数 type ListFeedUsageRecordRequest struct { Page int `form:"page,default=1"` PageSize int `form:"pageSize,default=10"` PenID *uint `form:"pen_id"` FeedFormulaID *uint `form:"feed_formula_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"` } // PenDTO 是用于API响应的简化版猪栏结构 type PenDTO struct { ID uint `json:"id"` Name string `json:"name"` } // FeedFormulaDTO 是用于API响应的简化版饲料配方结构 type FeedFormulaDTO struct { ID uint `json:"id"` Name string `json:"name"` } // FeedUsageRecordDTO 是用于API响应的饲料使用记录结构 type FeedUsageRecordDTO struct { ID uint `json:"id"` PenID uint `json:"pen_id"` Pen PenDTO `json:"pen"` FeedFormulaID uint `json:"feed_formula_id"` FeedFormula FeedFormulaDTO `json:"feed_formula"` Amount float64 `json:"amount"` RecordedAt time.Time `json:"recorded_at"` OperatorID uint `json:"operator_id"` Remarks string `json:"remarks"` } // ListFeedUsageRecordResponse 是获取饲料使用记录列表的响应结构 type ListFeedUsageRecordResponse struct { List []FeedUsageRecordDTO `json:"list"` Pagination PaginationDTO `json:"pagination"` } // NewListFeedUsageRecordResponse 从模型数据创建列表响应 DTO func NewListFeedUsageRecordResponse(data []models.FeedUsageRecord, total int64, page, pageSize int) *ListFeedUsageRecordResponse { dtos := make([]FeedUsageRecordDTO, len(data)) for i, item := range data { dtos[i] = FeedUsageRecordDTO{ ID: item.ID, PenID: item.PenID, Pen: PenDTO{ ID: item.Pen.ID, Name: item.Pen.PenNumber, }, FeedFormulaID: item.FeedFormulaID, FeedFormula: FeedFormulaDTO{ ID: item.FeedFormula.ID, Name: item.FeedFormula.Name, }, Amount: item.Amount, RecordedAt: item.RecordedAt, OperatorID: item.OperatorID, Remarks: item.Remarks, } } return &ListFeedUsageRecordResponse{ List: dtos, Pagination: PaginationDTO{ Total: total, Page: page, PageSize: pageSize, }, } } // --- 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, }, } } // --- PigBatchLog --- // ListPigBatchLogRequest 定义了获取猪批次日志列表的请求参数 type ListPigBatchLogRequest struct { Page int `form:"page,default=1"` PageSize int `form:"pageSize,default=10"` PigBatchID *uint `form:"pig_batch_id"` ChangeType *string `form:"change_type"` 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"` } // PigBatchLogDTO 是用于API响应的猪批次日志结构 type PigBatchLogDTO struct { ID uint `json:"id"` CreatedAt time.Time `json:"created_at"` UpdatedAt time.Time `json:"updated_at"` PigBatchID uint `json:"pig_batch_id"` ChangeType models.LogChangeType `json:"change_type"` ChangeCount int `json:"change_count"` Reason string `json:"reason"` BeforeCount int `json:"before_count"` AfterCount int `json:"after_count"` OperatorID uint `json:"operator_id"` HappenedAt time.Time `json:"happened_at"` } // ListPigBatchLogResponse 是获取猪批次日志列表的响应结构 type ListPigBatchLogResponse struct { List []PigBatchLogDTO `json:"list"` Pagination PaginationDTO `json:"pagination"` } // NewListPigBatchLogResponse 从模型数据创建列表响应 DTO func NewListPigBatchLogResponse(data []models.PigBatchLog, total int64, page, pageSize int) *ListPigBatchLogResponse { dtos := make([]PigBatchLogDTO, len(data)) for i, item := range data { dtos[i] = PigBatchLogDTO{ ID: item.ID, CreatedAt: item.CreatedAt, UpdatedAt: item.UpdatedAt, PigBatchID: item.PigBatchID, ChangeType: item.ChangeType, ChangeCount: item.ChangeCount, Reason: item.Reason, BeforeCount: item.BeforeCount, AfterCount: item.AfterCount, OperatorID: item.OperatorID, HappenedAt: item.HappenedAt, } } return &ListPigBatchLogResponse{ List: dtos, Pagination: PaginationDTO{ Total: total, Page: page, PageSize: pageSize, }, } }