修改controller包
This commit is contained in:
@@ -1,135 +1,113 @@
|
|||||||
- **`internal/app/controller/user/user_controller.go` (`user.Controller`)**
|
- **`internal/app/controller/user/user_controller.go` (`user.Controller`)**
|
||||||
- **结构体改造**:
|
- **结构体改造**:
|
||||||
- [ ] 移除 `Controller` 结构体中的 `logger *logs.Logger` 成员。
|
- [x] 移除 `Controller` 结构体中的 `logger *logs.Logger` 成员。
|
||||||
- [ ] 新增 `selfCtx context.Context` 成员。
|
- [x] 新增 `Ctx context.Context` 成员。
|
||||||
- **构造函数改造 (`NewController`)**:
|
- **构造函数改造 (`NewController`)**:
|
||||||
- [ ] 修改函数签名,移除 `logger *logs.Logger` 参数,改为接收 `ctx context.Context`。
|
- [x] 修改函数签名,移除 `logger *logs.Logger` 参数,改为接收 `ctx context.Context`。
|
||||||
- [ ] 在函数内部,为 `user.Controller` 创建其专属的 `selfCtx`:
|
- [x] 将这个 `Ctx` 赋值给 `Controller` 结构体的 `Ctx` 成员。
|
||||||
`selfCtx := logs.AddCompName(ctx, "UserController")`。
|
|
||||||
- [ ] 将这个 `selfCtx` 赋值给 `Controller` 结构体的 `selfCtx` 成员。
|
|
||||||
- **公共方法改造 (`CreateUser`, `Login`, `SendTestNotification`)**:
|
- **公共方法改造 (`CreateUser`, `Login`, `SendTestNotification`)**:
|
||||||
- [ ] 在每个方法入口处,从 `echo.Context` 中提取 `request.Context()` 作为 `upstreamCtx`。
|
- [x] 在每个方法入口处,从 `echo.Context` 中提取 `request.Context()` 作为 `upstreamCtx`。
|
||||||
- [ ] 使用 `newCtx, logger := logs.Trace(upstreamCtx, c.selfCtx, "MethodName")` 获取新的 `context.Context` 和
|
- [x] 使用 `newCtx, logger := logs.Trace(upstreamCtx, c.Ctx, "MethodName")` 获取新的 `context.Context` 和
|
||||||
`logger` 实例。
|
`logger` 实例。
|
||||||
- [ ] 将所有对 `c.logger.Errorf` 和 `c.logger.Infof` 的调用替换为 `logger.Errorf` 和 `logger.Infof`。
|
- [x] 将所有对 `c.logger.Errorf` 和 `c.logger.Infof` 的调用替换为 `logger.Errorf` 和 `logger.Infof`。
|
||||||
- [ ] 对于 `SendTestNotification` 方法中的 `controller.SendErrorWithAudit` 和
|
- [x] 确保所有对 `c.userService` 的调用都将 `newCtx` 作为第一个参数传递。
|
||||||
`controller.SendSuccessWithAudit` 调用,需要将 `newCtx` 作为第一个参数传递。
|
|
||||||
- [ ] 确保所有对 `c.userService` 的调用都将 `newCtx` 作为第一个参数传递。
|
|
||||||
- **`internal/app/controller/device/device_controller.go` (`device.Controller`)**
|
- **`internal/app/controller/device/device_controller.go` (`device.Controller`)**
|
||||||
- **结构体改造**:
|
- **结构体改造**:
|
||||||
- [ ] 移除 `Controller` 结构体中的 `logger *logs.Logger` 成员。
|
- [x] 移除 `Controller` 结构体中的 `logger *logs.Logger` 成员。
|
||||||
- [ ] 新增 `selfCtx context.Context` 成员。
|
- [x] 新增 `Ctx context.Context` 成员。
|
||||||
- **构造函数改造 (`NewController`)**:
|
- **构造函数改造 (`NewController`)**:
|
||||||
- [ ] 修改函数签名,移除 `logger *logs.Logger` 参数,改为接收 `ctx context.Context`。
|
- [x] 修改函数签名,移除 `logger *logs.Logger` 参数,改为接收 `ctx context.Context`。
|
||||||
- [ ] 在函数内部,为 `device.Controller` 创建其专属的 `selfCtx`:
|
- [x] 将这个 `Ctx` 赋值给 `Controller` 结构体的 `Ctx` 成员。
|
||||||
`selfCtx := logs.AddCompName(ctx, "DeviceController")`。
|
|
||||||
- [ ] 将这个 `selfCtx` 赋值给 `Controller` 结构体的 `selfCtx` 成员。
|
|
||||||
- **公共方法改造 (`CreateDevice`, `GetDevice`, `ListDevices`, `UpdateDevice`, `DeleteDevice`, `ManualControl`,
|
- **公共方法改造 (`CreateDevice`, `GetDevice`, `ListDevices`, `UpdateDevice`, `DeleteDevice`, `ManualControl`,
|
||||||
`CreateAreaController`, `GetAreaController`, `ListAreaControllers`, `UpdateAreaController`,
|
`CreateAreaController`, `GetAreaController`, `ListAreaControllers`, `UpdateAreaController`,
|
||||||
`DeleteAreaController`, `CreateDeviceTemplate`, `GetDeviceTemplate`, `ListDeviceTemplates`,
|
`DeleteAreaController`, `CreateDeviceTemplate`, `GetDeviceTemplate`, `ListDeviceTemplates`,
|
||||||
`UpdateDeviceTemplate`, `DeleteDeviceTemplate`)**:
|
`UpdateDeviceTemplate`, `DeleteDeviceTemplate`)**:
|
||||||
- [ ] 在每个方法入口处,从 `echo.Context` 中提取 `request.Context()` 作为 `upstreamCtx`。
|
- [x] 在每个方法入口处,从 `echo.Context` 中提取 `request.Context()` 作为 `upstreamCtx`。
|
||||||
- [ ] 使用 `newCtx, logger := logs.Trace(upstreamCtx, c.selfCtx, "MethodName")` 获取新的 `context.Context` 和
|
- [x] 使用 `newCtx, logger := logs.Trace(upstreamCtx, c.Ctx, "MethodName")` 获取新的 `context.Context` 和
|
||||||
`logger` 实例。
|
`logger` 实例。
|
||||||
- [ ] 将所有对 `c.logger.Errorf`, `c.logger.Warnf`, `c.logger.Infof` 的调用替换为 `logger.Errorf`,
|
- [x] 将所有对 `c.logger.Errorf`, `c.logger.Warnf`, `c.logger.Infof` 的调用替换为 `logger.Errorf`,
|
||||||
`logger.Warnf`, `logger.Infof`。
|
`logger.Warnf`, `logger.Infof`。
|
||||||
- [ ] 对于 `controller.SendErrorWithAudit` 和 `controller.SendSuccessWithAudit` 调用,需要将 `newCtx`
|
- [x] 确保所有对 `c.deviceService` 的调用都将 `newCtx` 作为第一个参数传递。
|
||||||
作为第一个参数传递。
|
|
||||||
- [ ] 确保所有对 `c.deviceService` 的调用都将 `newCtx` 作为第一个参数传递。
|
|
||||||
- **`internal/app/controller/plan/plan_controller.go` (`plan.Controller`)**
|
- **`internal/app/controller/plan/plan_controller.go` (`plan.Controller`)**
|
||||||
- **结构体改造**:
|
- **结构体改造**:
|
||||||
- [ ] 移除 `Controller` 结构体中的 `logger *logs.Logger` 成员。
|
- [x] 移除 `Controller` 结构体中的 `logger *logs.Logger` 成员。
|
||||||
- [ ] 新增 `selfCtx context.Context` 成员。
|
- [x] 新增 `Ctx context.Context` 成员。
|
||||||
- **构造函数改造 (`NewController`)**:
|
- **构造函数改造 (`NewController`)**:
|
||||||
- [ ] 修改函数签名,移除 `logger *logs.Logger` 参数,改为接收 `ctx context.Context`。
|
- [x] 修改函数签名,移除 `logger *logs.Logger` 参数,改为接收 `ctx context.Context`。
|
||||||
- [ ] 在函数内部,为 `plan.Controller` 创建其专属的 `selfCtx`:
|
- [x] 将这个 `Ctx` 赋值给 `Controller` 结构体的 `Ctx` 成员。
|
||||||
`selfCtx := logs.AddCompName(ctx, "PlanController")`。
|
|
||||||
- [ ] 将这个 `selfCtx` 赋值给 `Controller` 结构体的 `selfCtx` 成员。
|
|
||||||
- **公共方法改造 (`CreatePlan`, `GetPlan`, `ListPlans`, `UpdatePlan`, `DeletePlan`, `StartPlan`, `StopPlan`)**:
|
- **公共方法改造 (`CreatePlan`, `GetPlan`, `ListPlans`, `UpdatePlan`, `DeletePlan`, `StartPlan`, `StopPlan`)**:
|
||||||
- [ ] 在每个方法入口处,从 `echo.Context` 中提取 `request.Context()` 作为 `upstreamCtx`。
|
- [x] 在每个方法入口处,从 `echo.Context` 中提取 `request.Context()` 作为 `upstreamCtx`。
|
||||||
- [ ] 使用 `newCtx, logger := logs.Trace(upstreamCtx, c.selfCtx, "MethodName")` 获取新的 `context.Context` 和
|
- [x] 使用 `newCtx, logger := logs.Trace(upstreamCtx, c.Ctx, "MethodName")` 获取新的 `context.Context` 和
|
||||||
`logger` 实例。
|
`logger` 实例。
|
||||||
- [ ] 将所有对 `c.logger.Errorf` 和 `c.logger.Infof` 的调用替换为 `logger.Errorf` 和 `logger.Infof`。
|
- [x] 将所有对 `c.logger.Errorf` 和 `c.logger.Infof` 的调用替换为 `logger.Errorf` 和 `logger.Infof`。
|
||||||
- [ ] 对于 `controller.SendErrorWithAudit` 和 `controller.SendSuccessWithAudit` 调用,需要将 `newCtx`
|
- [x] 确保所有对 `c.planService` 的调用都将 `newCtx` 作为第一个参数传递。
|
||||||
作为第一个参数传递。
|
|
||||||
- [ ] 确保所有对 `c.planService` 的调用都将 `newCtx` 作为第一个参数传递。
|
|
||||||
- **`internal/app/controller/management/pig_farm_controller.go` (`management.PigFarmController`)**
|
- **`internal/app/controller/management/pig_farm_controller.go` (`management.PigFarmController`)**
|
||||||
- **结构体改造**:
|
- **结构体改造**:
|
||||||
- [ ] 移除 `PigFarmController` 结构体中的 `logger *logs.Logger` 成员。
|
- [x] 移除 `PigFarmController` 结构体中的 `logger *logs.Logger` 成员。
|
||||||
- [ ] 新增 `selfCtx context.Context` 成员。
|
- [x] 新增 `Ctx context.Context` 成员。
|
||||||
- **构造函数改造 (`NewPigFarmController`)**:
|
- **构造函数改造 (`NewPigFarmController`)**:
|
||||||
- [ ] 修改函数签名,移除 `logger *logs.Logger` 参数,改为接收 `ctx context.Context`。
|
- [x] 修改函数签名,移除 `logger *logs.Logger` 参数,改为接收 `ctx context.Context`。
|
||||||
- [ ] 在函数内部,为 `PigFarmController` 创建其专属的 `selfCtx`:
|
- [x] 将这个 `Ctx` 赋值给 `PigFarmController` 结构体的 `Ctx` 成员。
|
||||||
`selfCtx := logs.AddCompName(ctx, "PigFarmController")`。
|
|
||||||
- [ ] 将这个 `selfCtx` 赋值给 `PigFarmController` 结构体的 `selfCtx` 成员。
|
|
||||||
- **公共方法改造 (`CreatePigHouse`, `GetPigHouse`, `ListPigHouses`, `UpdatePigHouse`, `DeletePigHouse`, `CreatePen`,
|
- **公共方法改造 (`CreatePigHouse`, `GetPigHouse`, `ListPigHouses`, `UpdatePigHouse`, `DeletePigHouse`, `CreatePen`,
|
||||||
`GetPen`, `ListPens`, `UpdatePen`, `DeletePen`, `UpdatePenStatus`)**:
|
`GetPen`, `ListPens`, `UpdatePen`, `DeletePen`, `UpdatePenStatus`)**:
|
||||||
- [ ] 在每个方法入口处,从 `echo.Context` 中提取 `request.Context()` 作为 `upstreamCtx`。
|
- [x] 在每个方法入口处,从 `echo.Context` 中提取 `request.Context()` 作为 `upstreamCtx`。
|
||||||
- [ ] 使用 `newCtx, logger := logs.Trace(upstreamCtx, c.selfCtx, "MethodName")` 获取新的 `context.Context` 和
|
- [x] 使用 `newCtx, logger := logs.Trace(upstreamCtx, c.Ctx, "MethodName")` 获取新的 `context.Context` 和
|
||||||
`logger` 实例。
|
`logger` 实例。
|
||||||
- [ ] 将所有对 `c.logger.Errorf` 和 `c.logger.Infof` 的调用替换为 `logger.Errorf` 和 `logger.Infof`。
|
- [x] 将所有对 `c.logger.Errorf` 和 `c.logger.Infof` 的调用替换为 `logger.Errorf` 和 `logger.Infof`。
|
||||||
- [ ] 对于 `controller.SendErrorWithAudit` 和 `controller.SendSuccessWithAudit` 调用,需要将 `newCtx`
|
- [x] 确保所有对 `c.service` 的调用都将 `newCtx` 作为第一个参数传递。
|
||||||
作为第一个参数传递。
|
|
||||||
- [ ] 确保所有对 `c.service` 的调用都将 `newCtx` 作为第一个参数传递。
|
|
||||||
- **`internal/app/controller/management/pig_batch_controller.go` (`management.PigBatchController`)**
|
- **`internal/app/controller/management/pig_batch_controller.go` (`management.PigBatchController`)**
|
||||||
- **结构体改造**:
|
- **结构体改造**:
|
||||||
- [ ] 移除 `PigBatchController` 结构体中的 `logger *logs.Logger` 成员。
|
- [x] 移除 `PigBatchController` 结构体中的 `logger *logs.Logger` 成员。
|
||||||
- [ ] 新增 `selfCtx context.Context` 成员。
|
- [x] 新增 `Ctx context.Context` 成员。
|
||||||
- **构造函数改造 (`NewPigBatchController`)**:
|
- **构造函数改造 (`NewPigBatchController`)**:
|
||||||
- [ ] 修改函数签名,移除 `logger *logs.Logger` 参数,改为接收 `ctx context.Context`。
|
- [x] 修改函数签名,移除 `logger *logs.Logger` 参数,改为接收 `ctx context.Context`。
|
||||||
- [ ] 在函数内部,为 `PigBatchController` 创建其专属的 `selfCtx`:
|
- [x] 将这个 `Ctx` 赋值给 `PigBatchController` 结构体的 `Ctx` 成员。
|
||||||
`selfCtx := logs.AddCompName(ctx, "PigBatchController")`。
|
|
||||||
- [ ] 将这个 `selfCtx` 赋值给 `PigBatchController` 结构体的 `selfCtx` 成员。
|
|
||||||
- **公共方法改造 (
|
- **公共方法改造 (
|
||||||
所有方法,例如 `CreatePigBatch`, `GetPigBatch`, `UpdatePigBatch`, `DeletePigBatch`, `ListPigBatches`,
|
所有方法,例如 `CreatePigBatch`, `GetPigBatch`, `UpdatePigBatch`, `DeletePigBatch`, `ListPigBatches`,
|
||||||
`AssignEmptyPensToBatch`, `ReclassifyPenToNewBatch`, `RemoveEmptyPenFromBatch`, `MovePigsIntoPen`)**:
|
`AssignEmptyPensToBatch`, `ReclassifyPenToNewBatch`, `RemoveEmptyPenFromBatch`, `MovePigsIntoPen`)**:
|
||||||
- [ ] 这些方法通过调用 `controller_helpers.go` 中的 `handleAPIRequest...` 系列函数来处理请求。
|
- [x] 这些方法通过调用 `controller_helpers.go` 中的 `handleAPIRequest...` 系列函数来处理请求。
|
||||||
- [ ] 确保传递给 `handleAPIRequest...` 系列函数的 `serviceExecutor` 匿名函数,其签名与 `controller_helpers.go`
|
- [x] 确保传递给 `handleAPIRequest...` 系列函数的 `serviceExecutor` 匿名函数,其签名与 `controller_helpers.go`
|
||||||
改造后期望的签名一致(即接收 `context.Context` 作为第一个参数)。
|
改造后期望的签名一致(即接收 `context.Context` 作为第一个参数)。
|
||||||
- [ ] 在 `serviceExecutor` 匿名函数内部,将接收到的 `context.Context` 传递给 `c.service` 的相应方法。
|
- [x] 在 `serviceExecutor` 匿名函数内部,将接收到的 `context.Context` 传递给 `c.service` 的相应方法。
|
||||||
- **`internal/app/controller/management/controller_helpers.go`**
|
- **`internal/app/controller/management/controller_helpers.go`**
|
||||||
- [ ] 修改 `mapAndSendError` 函数中的 `c.logger.Errorf` 调用,改为从 `echo.Context` 中提取 `context.Context`,并使用
|
- [x] 修改 `mapAndSendError` 函数中的 `c.logger.Errorf` 调用,改为从 `echo.Context` 中提取 `context.Context`,并使用
|
||||||
`logs.Trace` 或 `logs.GetLogger` 获取 `logger` 实例进行日志记录。
|
`logs.Trace` 或 `logs.GetLogger` 获取 `logger` 实例进行日志记录。
|
||||||
- [ ] 检查其他函数是否直接或间接依赖 `*PigBatchController` 的 `logger` 成员,并进行相应改造。
|
- [x] 检查其他函数是否直接或间接依赖 `*PigBatchController` 的 `logger` 成员,并进行相应改造。
|
||||||
- **`internal/app/controller/management/pig_batch_trade_controller.go` (`management.PigBatchController`)**
|
- **`internal/app/controller/management/pig_batch_trade_controller.go` (`management.PigBatchController`)**
|
||||||
- [ ] 修改 `NewController` 函数,移除 `logger` 参数,改为接收 `selfCtx context.Context`。
|
- [x] 修改 `NewController` 函数,移除 `logger` 参数,改为接收 `Ctx context.Context`。
|
||||||
- [ ] 移除 `Controller` 结构体中的 `logger` 成员,改为保存 `selfCtx`。
|
- [x] 移除 `Controller` 结构体中的 `logger` 成员,改为保存 `Ctx`。
|
||||||
- [ ] **所有公共方法**: 接收 `echo.Context` 的方法,需要从中提取 `request.Context()` 作为 `upstreamCtx`。
|
- [x] **所有公共方法**: 接收 `echo.Context` 的方法,需要从中提取 `request.Context()` 作为 `upstreamCtx`。
|
||||||
- **`internal/app/controller/management/pig_batch_health_controller.go` (`management.PigBatchController`)**
|
- **`internal/app/controller/management/pig_batch_health_controller.go` (`management.PigBatchController`)**
|
||||||
- [ ] 修改 `NewController` 函数,移除 `logger` 参数,改为接收 `selfCtx context.Context`。
|
- [x] 修改 `NewController` 函数,移除 `logger` 参数,改为接收 `Ctx context.Context`。
|
||||||
- [ ] 移除 `Controller` 结构体中的 `logger` 成员,改为保存 `selfCtx`。
|
- [x] 移除 `Controller` 结构体中的 `logger` 成员,改为保存 `Ctx`。
|
||||||
- [ ] **所有公共方法**: 接收 `echo.Context` 的方法,需要从中提取 `request.Context()` 作为 `upstreamCtx`。
|
- [x] **所有公共方法**: 接收 `echo.Context` 的方法,需要从中提取 `request.Context()` 作为 `upstreamCtx`。
|
||||||
- **`internal/app/controller/management/pig_batch_transfer_controller.go` (`management.PigBatchController`)**
|
- **`internal/app/controller/management/pig_batch_transfer_controller.go` (`management.PigBatchController`)**
|
||||||
- [ ] 修改 `NewController` 函数,移除 `logger` 参数,改为接收 `selfCtx context.Context`。
|
- [x] 修改 `NewController` 函数,移除 `logger` 参数,改为接收 `Ctx context.Context`。
|
||||||
- [ ] 移除 `Controller` 结构体中的 `logger` 成员,改为保存 `selfCtx`。
|
- [x] 移除 `Controller` 结构体中的 `logger` 成员,改为保存 `Ctx`。
|
||||||
- [ ] **所有公共方法**: 接收 `echo.Context` 的方法,需要从中提取 `request.Context()` 作为 `upstreamCtx`。
|
- [x] **所有公共方法**: 接收 `echo.Context` 的方法,需要从中提取 `request.Context()` 作为 `upstreamCtx`。
|
||||||
- **`internal/app/controller/monitor/monitor_controller.go` (`monitor.Controller`)**
|
- **`internal/app/controller/monitor/monitor_controller.go` (`monitor.Controller`)**
|
||||||
- **结构体改造**:
|
- **结构体改造**:
|
||||||
- [ ] 移除 `Controller` 结构体中的 `logger *logs.Logger` 成员。
|
- [x] 移除 `Controller` 结构体中的 `logger *logs.Logger` 成员。
|
||||||
- [ ] 新增 `selfCtx context.Context` 成员。
|
- [x] 新增 `Ctx context.Context` 成员。
|
||||||
- **构造函数改造 (`NewController`)**:
|
- **构造函数改造 (`NewController`)**:
|
||||||
- [ ] 修改函数签名,移除 `logger *logs.Logger` 参数,改为接收 `ctx context.Context`。
|
- [x] 修改函数签名,移除 `logger *logs.Logger` 参数,改为接收 `ctx context.Context`。
|
||||||
- [ ] 在函数内部,为 `monitor.Controller` 创建其专属的 `selfCtx`:
|
- [x] 将这个 `Ctx` 赋值给 `Controller` 结构体的 `Ctx` 成员。
|
||||||
`selfCtx := logs.AddCompName(ctx, "MonitorController")`。
|
|
||||||
- [ ] 将这个 `selfCtx` 赋值给 `Controller` 结构体的 `selfCtx` 成员。
|
|
||||||
- **公共方法改造 (
|
- **公共方法改造 (
|
||||||
所有方法,例如 `ListSensorData`, `ListDeviceCommandLogs`, `ListPlanExecutionLogs`, `ListTaskExecutionLogs`,
|
所有方法,例如 `ListSensorData`, `ListDeviceCommandLogs`, `ListPlanExecutionLogs`, `ListTaskExecutionLogs`,
|
||||||
`ListPendingCollections`, `ListUserActionLogs`, `ListRawMaterialPurchases`, `ListRawMaterialStockLogs`,
|
`ListPendingCollections`, `ListUserActionLogs`, `ListRawMaterialPurchases`, `ListRawMaterialStockLogs`,
|
||||||
`ListFeedUsageRecords`, `ListMedicationLogs`, `ListPigBatchLogs`, `ListWeighingBatches`, `ListWeighingRecords`,
|
`ListFeedUsageRecords`, `ListMedicationLogs`, `ListPigBatchLogs`, `ListWeighingBatches`, `ListWeighingRecords`,
|
||||||
`ListPigTransferLogs`, `ListPigSickLogs`, `ListPigPurchases`, `ListPigSales`, `ListNotifications`)**:
|
`ListPigTransferLogs`, `ListPigSickLogs`, `ListPigPurchases`, `ListPigSales`, `ListNotifications`)**:
|
||||||
- [ ] 在每个方法入口处,从 `echo.Context` 中提取 `request.Context()` 作为 `upstreamCtx`。
|
- [x] 在每个方法入口处,从 `echo.Context` 中提取 `request.Context()` 作为 `upstreamCtx`。
|
||||||
- [ ] 使用 `newCtx, logger := logs.Trace(upstreamCtx, c.selfCtx, actionType)` 获取新的 `context.Context` 和
|
- [x] 使用 `newCtx, logger := logs.Trace(upstreamCtx, c.Ctx, actionType)` 获取新的 `context.Context` 和
|
||||||
`logger` 实例(`actionType` 为方法内部定义的常量)。
|
`logger` 实例(`actionType` 为方法内部定义的常量)。
|
||||||
- [ ] 将所有对 `c.logger.Errorf`, `c.logger.Warnf`, `c.logger.Infof` 的调用替换为 `logger.Errorf`,
|
- [x] 将所有对 `c.logger.Errorf`, `c.logger.Warnf`, `c.logger.Infof` 的调用替换为 `logger.Errorf`,
|
||||||
`logger.Warnf`, `logger.Infof`。
|
`logger.Warnf`, `logger.Infof`。
|
||||||
- [ ] 对于 `controller.SendErrorWithAudit` 和 `controller.SendSuccessWithAudit` 调用,需要将 `newCtx`
|
- [x] 确保所有对 `c.monitorService` 的调用都将 `newCtx` 作为第一个参数传递。
|
||||||
作为第一个参数传递。
|
|
||||||
- [ ] 确保所有对 `c.monitorService` 的调用都将 `newCtx` 作为第一个参数传递。
|
|
||||||
|
|
||||||
|
|
||||||
- **`internal/app/controller/response.go`**
|
- **`internal/app/controller/response.go`**
|
||||||
- [ ] 修改 `SendSuccessWithAudit` 和 `SendErrorWithAudit` 函数签名,使其接收 `ctx context.Context` 作为第一个参数。
|
- [x] 修改 `SendSuccessWithAudit` 和 `SendErrorWithAudit` 函数签名,使其接收 `ctx context.Context` 作为第一个参数。
|
||||||
- [ ] 在 `setAuditDetails` 函数中,如果需要,从传入的 `context.Context` 中提取调用链信息,并将其添加到审计信息中。
|
- [x] 在 `setAuditDetails` 函数中,如果需要,从传入的 `context.Context` 中提取调用链信息,并将其添加到审计信息中。
|
||||||
- **`internal/app/controller/auth_utils.go`**
|
- **`internal/app/controller/auth_utils.go`**
|
||||||
- [ ] 检查 `GetOperatorIDFromContext` 和 `GetOperatorFromContext` 函数,确保它们能够正确地从 `echo.Context` 中获取
|
- [x] 检查 `GetOperatorIDFromContext` 和 `GetOperatorFromContext` 函数,确保它们能够正确地从 `echo.Context` 中获取
|
||||||
`request.Context()`,并从中提取用户信息。
|
`request.Context()`,并从中提取用户信息。
|
||||||
@@ -91,9 +91,9 @@ func NewAPI(cfg config.ServerConfig,
|
|||||||
config: cfg,
|
config: cfg,
|
||||||
listenHandler: listenHandler,
|
listenHandler: listenHandler,
|
||||||
// 在 NewAPI 中初始化用户控制器,并将其作为 API 结构体的成员
|
// 在 NewAPI 中初始化用户控制器,并将其作为 API 结构体的成员
|
||||||
userController: user.NewController(userService, baseCtx),
|
userController: user.NewController(baseCtx, userService),
|
||||||
// 在 NewAPI 中初始化设备控制器,并将其作为 API 结构体的成员
|
// 在 NewAPI 中初始化设备控制器,并将其作为 API 结构体的成员
|
||||||
deviceController: device.NewController(deviceService, baseCtx),
|
deviceController: device.NewController(baseCtx, deviceService),
|
||||||
// 在 NewAPI 中初始化计划控制器,并将其作为 API 结构体的成员
|
// 在 NewAPI 中初始化计划控制器,并将其作为 API 结构体的成员
|
||||||
planController: plan.NewController(baseCtx, planService),
|
planController: plan.NewController(baseCtx, planService),
|
||||||
// 在 NewAPI 中初始化猪场管理控制器
|
// 在 NewAPI 中初始化猪场管理控制器
|
||||||
@@ -101,7 +101,7 @@ func NewAPI(cfg config.ServerConfig,
|
|||||||
// 在 NewAPI 中初始化猪群控制器
|
// 在 NewAPI 中初始化猪群控制器
|
||||||
pigBatchController: management.NewPigBatchController(baseCtx, pigBatchService),
|
pigBatchController: management.NewPigBatchController(baseCtx, pigBatchService),
|
||||||
// 在 NewAPI 中初始化数据监控控制器
|
// 在 NewAPI 中初始化数据监控控制器
|
||||||
monitorController: monitor.NewController(monitorService, baseCtx),
|
monitorController: monitor.NewController(baseCtx, monitorService),
|
||||||
}
|
}
|
||||||
|
|
||||||
api.setupRoutes() // 设置所有路由
|
api.setupRoutes() // 设置所有路由
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package device
|
package device
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
@@ -14,18 +15,18 @@ import (
|
|||||||
|
|
||||||
// Controller 设备控制器,封装了所有与设备和区域主控相关的业务逻辑
|
// Controller 设备控制器,封装了所有与设备和区域主控相关的业务逻辑
|
||||||
type Controller struct {
|
type Controller struct {
|
||||||
|
ctx context.Context
|
||||||
deviceService service.DeviceService
|
deviceService service.DeviceService
|
||||||
logger *logs.Logger
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewController 创建一个新的设备控制器实例
|
// NewController 创建一个新的设备控制器实例
|
||||||
func NewController(
|
func NewController(
|
||||||
|
ctx context.Context,
|
||||||
deviceService service.DeviceService,
|
deviceService service.DeviceService,
|
||||||
logger *logs.Logger,
|
|
||||||
) *Controller {
|
) *Controller {
|
||||||
return &Controller{
|
return &Controller{
|
||||||
|
ctx: ctx,
|
||||||
deviceService: deviceService,
|
deviceService: deviceService,
|
||||||
logger: logger,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -42,20 +43,22 @@ func NewController(
|
|||||||
// @Success 200 {object} controller.Response{data=dto.DeviceResponse}
|
// @Success 200 {object} controller.Response{data=dto.DeviceResponse}
|
||||||
// @Router /api/v1/devices [post]
|
// @Router /api/v1/devices [post]
|
||||||
func (c *Controller) CreateDevice(ctx echo.Context) error {
|
func (c *Controller) CreateDevice(ctx echo.Context) error {
|
||||||
|
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "CreateDevice")
|
||||||
|
|
||||||
const actionType = "创建设备"
|
const actionType = "创建设备"
|
||||||
var req dto.CreateDeviceRequest
|
var req dto.CreateDeviceRequest
|
||||||
if err := ctx.Bind(&req); err != nil {
|
if err := ctx.Bind(&req); err != nil {
|
||||||
c.logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
|
logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req)
|
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req)
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := c.deviceService.CreateDevice(&req)
|
resp, err := c.deviceService.CreateDevice(reqCtx, &req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.logger.Errorf("%s: 服务层创建失败: %v", actionType, err)
|
logger.Errorf("%s: 服务层创建失败: %v", actionType, err)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "创建设备失败: "+err.Error(), actionType, "服务层创建失败", req)
|
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "创建设备失败: "+err.Error(), actionType, "服务层创建失败", req)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.logger.Infof("%s: 设备创建成功, ID: %d", actionType, resp.ID)
|
logger.Infof("%s: 设备创建成功, ID: %d", actionType, resp.ID)
|
||||||
return controller.SendSuccessWithAudit(ctx, controller.CodeCreated, "设备创建成功", resp, actionType, "设备创建成功", resp)
|
return controller.SendSuccessWithAudit(ctx, controller.CodeCreated, "设备创建成功", resp, actionType, "设备创建成功", resp)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -69,26 +72,28 @@ func (c *Controller) CreateDevice(ctx echo.Context) error {
|
|||||||
// @Success 200 {object} controller.Response{data=dto.DeviceResponse}
|
// @Success 200 {object} controller.Response{data=dto.DeviceResponse}
|
||||||
// @Router /api/v1/devices/{id} [get]
|
// @Router /api/v1/devices/{id} [get]
|
||||||
func (c *Controller) GetDevice(ctx echo.Context) error {
|
func (c *Controller) GetDevice(ctx echo.Context) error {
|
||||||
|
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "GetDevice")
|
||||||
|
|
||||||
const actionType = "获取设备"
|
const actionType = "获取设备"
|
||||||
deviceID := ctx.Param("id")
|
deviceID := ctx.Param("id")
|
||||||
|
|
||||||
id, err := strconv.ParseUint(deviceID, 10, 64)
|
id, err := strconv.ParseUint(deviceID, 10, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.logger.Errorf("%s: 无效的ID: %s", actionType, deviceID)
|
logger.Errorf("%s: 无效的ID: %s", actionType, deviceID)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的ID: "+deviceID, actionType, "无效的ID", deviceID)
|
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的ID: "+deviceID, actionType, "无效的ID", deviceID)
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := c.deviceService.GetDevice(uint(id))
|
resp, err := c.deviceService.GetDevice(reqCtx, uint(id))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
c.logger.Warnf("%s: 设备不存在, ID: %s", actionType, deviceID)
|
logger.Warnf("%s: 设备不存在, ID: %s", actionType, deviceID)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "设备未找到", actionType, "设备不存在", deviceID)
|
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "设备未找到", actionType, "设备不存在", deviceID)
|
||||||
}
|
}
|
||||||
c.logger.Errorf("%s: 服务层获取失败: %v, ID: %s", actionType, err, deviceID)
|
logger.Errorf("%s: 服务层获取失败: %v, ID: %s", actionType, err, deviceID)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取设备信息失败: "+err.Error(), actionType, "服务层获取失败", deviceID)
|
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取设备信息失败: "+err.Error(), actionType, "服务层获取失败", deviceID)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.logger.Infof("%s: 获取设备信息成功, ID: %d", actionType, resp.ID)
|
logger.Infof("%s: 获取设备信息成功, ID: %d", actionType, resp.ID)
|
||||||
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取设备信息成功", resp, actionType, "获取设备信息成功", resp)
|
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取设备信息成功", resp, actionType, "获取设备信息成功", resp)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -101,14 +106,16 @@ func (c *Controller) GetDevice(ctx echo.Context) error {
|
|||||||
// @Success 200 {object} controller.Response{data=[]dto.DeviceResponse}
|
// @Success 200 {object} controller.Response{data=[]dto.DeviceResponse}
|
||||||
// @Router /api/v1/devices [get]
|
// @Router /api/v1/devices [get]
|
||||||
func (c *Controller) ListDevices(ctx echo.Context) error {
|
func (c *Controller) ListDevices(ctx echo.Context) error {
|
||||||
|
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "ListDevices")
|
||||||
|
|
||||||
const actionType = "获取设备列表"
|
const actionType = "获取设备列表"
|
||||||
resp, err := c.deviceService.ListDevices()
|
resp, err := c.deviceService.ListDevices(reqCtx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.logger.Errorf("%s: 服务层获取列表失败: %v", actionType, err)
|
logger.Errorf("%s: 服务层获取列表失败: %v", actionType, err)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取设备列表失败: "+err.Error(), actionType, "服务层获取列表失败", nil)
|
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取设备列表失败: "+err.Error(), actionType, "服务层获取列表失败", nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.logger.Infof("%s: 获取设备列表成功, 数量: %d", actionType, len(resp))
|
logger.Infof("%s: 获取设备列表成功, 数量: %d", actionType, len(resp))
|
||||||
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取设备列表成功", resp, actionType, "获取设备列表成功", resp)
|
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取设备列表成功", resp, actionType, "获取设备列表成功", resp)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -124,32 +131,34 @@ func (c *Controller) ListDevices(ctx echo.Context) error {
|
|||||||
// @Success 200 {object} controller.Response{data=dto.DeviceResponse}
|
// @Success 200 {object} controller.Response{data=dto.DeviceResponse}
|
||||||
// @Router /api/v1/devices/{id} [put]
|
// @Router /api/v1/devices/{id} [put]
|
||||||
func (c *Controller) UpdateDevice(ctx echo.Context) error {
|
func (c *Controller) UpdateDevice(ctx echo.Context) error {
|
||||||
|
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "UpdateDevice")
|
||||||
|
|
||||||
const actionType = "更新设备"
|
const actionType = "更新设备"
|
||||||
deviceID := ctx.Param("id")
|
deviceID := ctx.Param("id")
|
||||||
|
|
||||||
var req dto.UpdateDeviceRequest
|
var req dto.UpdateDeviceRequest
|
||||||
if err := ctx.Bind(&req); err != nil {
|
if err := ctx.Bind(&req); err != nil {
|
||||||
c.logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
|
logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req)
|
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req)
|
||||||
}
|
}
|
||||||
|
|
||||||
id, err := strconv.ParseUint(deviceID, 10, 64)
|
id, err := strconv.ParseUint(deviceID, 10, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.logger.Errorf("%s: 无效的ID: %s", actionType, deviceID)
|
logger.Errorf("%s: 无效的ID: %s", actionType, deviceID)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的ID: "+deviceID, actionType, "无效的ID", deviceID)
|
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的ID: "+deviceID, actionType, "无效的ID", deviceID)
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := c.deviceService.UpdateDevice(uint(id), &req)
|
resp, err := c.deviceService.UpdateDevice(reqCtx, uint(id), &req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
c.logger.Warnf("%s: 设备不存在, ID: %s", actionType, deviceID)
|
logger.Warnf("%s: 设备不存在, ID: %s", actionType, deviceID)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "设备未找到", actionType, "设备不存在", deviceID)
|
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "设备未找到", actionType, "设备不存在", deviceID)
|
||||||
}
|
}
|
||||||
c.logger.Errorf("%s: 服务层更新失败: %v, ID: %s", actionType, err, deviceID)
|
logger.Errorf("%s: 服务层更新失败: %v, ID: %s", actionType, err, deviceID)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "更新设备失败: "+err.Error(), actionType, "服务层更新失败", deviceID)
|
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "更新设备失败: "+err.Error(), actionType, "服务层更新失败", deviceID)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.logger.Infof("%s: 设备更新成功, ID: %d", actionType, resp.ID)
|
logger.Infof("%s: 设备更新成功, ID: %d", actionType, resp.ID)
|
||||||
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "设备更新成功", resp, actionType, "设备更新成功", resp)
|
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "设备更新成功", resp, actionType, "设备更新成功", resp)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -163,31 +172,33 @@ func (c *Controller) UpdateDevice(ctx echo.Context) error {
|
|||||||
// @Success 200 {object} controller.Response
|
// @Success 200 {object} controller.Response
|
||||||
// @Router /api/v1/devices/{id} [delete]
|
// @Router /api/v1/devices/{id} [delete]
|
||||||
func (c *Controller) DeleteDevice(ctx echo.Context) error {
|
func (c *Controller) DeleteDevice(ctx echo.Context) error {
|
||||||
|
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "DeleteDevice")
|
||||||
|
|
||||||
const actionType = "删除设备"
|
const actionType = "删除设备"
|
||||||
deviceID := ctx.Param("id")
|
deviceID := ctx.Param("id")
|
||||||
|
|
||||||
id, err := strconv.ParseUint(deviceID, 10, 64)
|
id, err := strconv.ParseUint(deviceID, 10, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.logger.Errorf("%s: 无效的ID: %s", actionType, deviceID)
|
logger.Errorf("%s: 无效的ID: %s", actionType, deviceID)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的ID: "+deviceID, actionType, "无效的ID", deviceID)
|
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的ID: "+deviceID, actionType, "无效的ID", deviceID)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := c.deviceService.DeleteDevice(uint(id)); err != nil {
|
if err := c.deviceService.DeleteDevice(reqCtx, uint(id)); err != nil {
|
||||||
switch {
|
switch {
|
||||||
case errors.Is(err, gorm.ErrRecordNotFound):
|
case errors.Is(err, gorm.ErrRecordNotFound):
|
||||||
c.logger.Warnf("%s: 设备不存在, ID: %s", actionType, deviceID)
|
logger.Warnf("%s: 设备不存在, ID: %s", actionType, deviceID)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "设备未找到", actionType, "设备不存在", deviceID)
|
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "设备未找到", actionType, "设备不存在", deviceID)
|
||||||
case errors.Is(err, service.ErrDeviceInUse):
|
case errors.Is(err, service.ErrDeviceInUse):
|
||||||
c.logger.Warnf("%s: 尝试删除正在被使用的设备, ID: %s", actionType, deviceID)
|
logger.Warnf("%s: 尝试删除正在被使用的设备, ID: %s", actionType, deviceID)
|
||||||
// 返回 409 Conflict 状态码,表示请求与服务器当前状态冲突
|
// 返回 409 Conflict 状态码,表示请求与服务器当前状态冲突
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeConflict, err.Error(), actionType, "设备正在被使用", deviceID)
|
return controller.SendErrorWithAudit(ctx, controller.CodeConflict, err.Error(), actionType, "设备正在被使用", deviceID)
|
||||||
default:
|
default:
|
||||||
c.logger.Errorf("%s: 服务层删除失败: %v, ID: %s", actionType, err, deviceID)
|
logger.Errorf("%s: 服务层删除失败: %v, ID: %s", actionType, err, deviceID)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "删除设备失败: "+err.Error(), actionType, "服务层删除失败", deviceID)
|
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "删除设备失败: "+err.Error(), actionType, "服务层删除失败", deviceID)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
c.logger.Infof("%s: 设备删除成功, ID: %s", actionType, deviceID)
|
logger.Infof("%s: 设备删除成功, ID: %s", actionType, deviceID)
|
||||||
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "设备删除成功", nil, actionType, "设备删除成功", deviceID)
|
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "设备删除成功", nil, actionType, "设备删除成功", deviceID)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -203,27 +214,29 @@ func (c *Controller) DeleteDevice(ctx echo.Context) error {
|
|||||||
// @Success 200 {object} controller.Response
|
// @Success 200 {object} controller.Response
|
||||||
// @Router /api/v1/devices/manual-control/{id} [post]
|
// @Router /api/v1/devices/manual-control/{id} [post]
|
||||||
func (c *Controller) ManualControl(ctx echo.Context) error {
|
func (c *Controller) ManualControl(ctx echo.Context) error {
|
||||||
|
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "ManualControlDevice")
|
||||||
|
|
||||||
const actionType = "手动控制设备"
|
const actionType = "手动控制设备"
|
||||||
deviceID := ctx.Param("id")
|
deviceID := ctx.Param("id")
|
||||||
|
|
||||||
var req dto.ManualControlDeviceRequest
|
var req dto.ManualControlDeviceRequest
|
||||||
if err := ctx.Bind(&req); err != nil {
|
if err := ctx.Bind(&req); err != nil {
|
||||||
c.logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
|
logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req)
|
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req)
|
||||||
}
|
}
|
||||||
|
|
||||||
id, err := strconv.ParseUint(deviceID, 10, 64)
|
id, err := strconv.ParseUint(deviceID, 10, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.logger.Errorf("%s: 无效的ID: %s", actionType, deviceID)
|
logger.Errorf("%s: 无效的ID: %s", actionType, deviceID)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的ID: "+deviceID, actionType, "无效的ID", deviceID)
|
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的ID: "+deviceID, actionType, "无效的ID", deviceID)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := c.deviceService.ManualControl(uint(id), &req); err != nil {
|
if err := c.deviceService.ManualControl(reqCtx, uint(id), &req); err != nil {
|
||||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
c.logger.Warnf("%s: 设备不存在, ID: %s", actionType, deviceID)
|
logger.Warnf("%s: 设备不存在, ID: %s", actionType, deviceID)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "设备未找到", actionType, "设备不存在", deviceID)
|
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "设备未找到", actionType, "设备不存在", deviceID)
|
||||||
}
|
}
|
||||||
c.logger.Errorf("%s: 服务层手动控制失败: %v, ID: %s", actionType, err, deviceID)
|
logger.Errorf("%s: 服务层手动控制失败: %v, ID: %s", actionType, err, deviceID)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "手动控制失败: "+err.Error(), actionType, "服务层手动控制失败", deviceID)
|
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "手动控制失败: "+err.Error(), actionType, "服务层手动控制失败", deviceID)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -243,20 +256,22 @@ func (c *Controller) ManualControl(ctx echo.Context) error {
|
|||||||
// @Success 200 {object} controller.Response{data=dto.AreaControllerResponse}
|
// @Success 200 {object} controller.Response{data=dto.AreaControllerResponse}
|
||||||
// @Router /api/v1/area-controllers [post]
|
// @Router /api/v1/area-controllers [post]
|
||||||
func (c *Controller) CreateAreaController(ctx echo.Context) error {
|
func (c *Controller) CreateAreaController(ctx echo.Context) error {
|
||||||
|
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "CreateAreaController")
|
||||||
|
|
||||||
const actionType = "创建区域主控"
|
const actionType = "创建区域主控"
|
||||||
var req dto.CreateAreaControllerRequest
|
var req dto.CreateAreaControllerRequest
|
||||||
if err := ctx.Bind(&req); err != nil {
|
if err := ctx.Bind(&req); err != nil {
|
||||||
c.logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
|
logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req)
|
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req)
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := c.deviceService.CreateAreaController(&req)
|
resp, err := c.deviceService.CreateAreaController(reqCtx, &req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.logger.Errorf("%s: 服务层创建失败: %v", actionType, err)
|
logger.Errorf("%s: 服务层创建失败: %v", actionType, err)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "创建区域主控失败: "+err.Error(), actionType, "服务层创建失败", req)
|
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "创建区域主控失败: "+err.Error(), actionType, "服务层创建失败", req)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.logger.Infof("%s: 区域主控创建成功, ID: %d", actionType, resp.ID)
|
logger.Infof("%s: 区域主控创建成功, ID: %d", actionType, resp.ID)
|
||||||
return controller.SendSuccessWithAudit(ctx, controller.CodeCreated, "区域主控创建成功", resp, actionType, "区域主控创建成功", resp)
|
return controller.SendSuccessWithAudit(ctx, controller.CodeCreated, "区域主控创建成功", resp, actionType, "区域主控创建成功", resp)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -270,26 +285,28 @@ func (c *Controller) CreateAreaController(ctx echo.Context) error {
|
|||||||
// @Success 200 {object} controller.Response{data=dto.AreaControllerResponse}
|
// @Success 200 {object} controller.Response{data=dto.AreaControllerResponse}
|
||||||
// @Router /api/v1/area-controllers/{id} [get]
|
// @Router /api/v1/area-controllers/{id} [get]
|
||||||
func (c *Controller) GetAreaController(ctx echo.Context) error {
|
func (c *Controller) GetAreaController(ctx echo.Context) error {
|
||||||
|
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "GetAreaController")
|
||||||
|
|
||||||
const actionType = "获取区域主控"
|
const actionType = "获取区域主控"
|
||||||
acID := ctx.Param("id")
|
acID := ctx.Param("id")
|
||||||
|
|
||||||
id, err := strconv.ParseUint(acID, 10, 64)
|
id, err := strconv.ParseUint(acID, 10, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.logger.Errorf("%s: 无效的ID: %s", actionType, acID)
|
logger.Errorf("%s: 无效的ID: %s", actionType, acID)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的ID: "+acID, actionType, "无效的ID", acID)
|
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的ID: "+acID, actionType, "无效的ID", acID)
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := c.deviceService.GetAreaController(uint(id))
|
resp, err := c.deviceService.GetAreaController(reqCtx, uint(id))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
c.logger.Warnf("%s: 区域主控不存在, ID: %s", actionType, acID)
|
logger.Warnf("%s: 区域主控不存在, ID: %s", actionType, acID)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "区域主控未找到", actionType, "区域主控不存在", acID)
|
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "区域主控未找到", actionType, "区域主控不存在", acID)
|
||||||
}
|
}
|
||||||
c.logger.Errorf("%s: 服务层获取失败: %v, ID: %s", actionType, err, acID)
|
logger.Errorf("%s: 服务层获取失败: %v, ID: %s", actionType, err, acID)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取区域主控信息失败: "+err.Error(), actionType, "服务层获取失败", acID)
|
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取区域主控信息失败: "+err.Error(), actionType, "服务层获取失败", acID)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.logger.Infof("%s: 获取区域主控信息成功, ID: %d", actionType, resp.ID)
|
logger.Infof("%s: 获取区域主控信息成功, ID: %d", actionType, resp.ID)
|
||||||
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取区域主控信息成功", resp, actionType, "获取区域主控信息成功", resp)
|
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取区域主控信息成功", resp, actionType, "获取区域主控信息成功", resp)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -302,14 +319,16 @@ func (c *Controller) GetAreaController(ctx echo.Context) error {
|
|||||||
// @Success 200 {object} controller.Response{data=[]dto.AreaControllerResponse}
|
// @Success 200 {object} controller.Response{data=[]dto.AreaControllerResponse}
|
||||||
// @Router /api/v1/area-controllers [get]
|
// @Router /api/v1/area-controllers [get]
|
||||||
func (c *Controller) ListAreaControllers(ctx echo.Context) error {
|
func (c *Controller) ListAreaControllers(ctx echo.Context) error {
|
||||||
|
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "ListAreaControllers")
|
||||||
|
|
||||||
const actionType = "获取区域主控列表"
|
const actionType = "获取区域主控列表"
|
||||||
resp, err := c.deviceService.ListAreaControllers()
|
resp, err := c.deviceService.ListAreaControllers(reqCtx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.logger.Errorf("%s: 服务层获取列表失败: %v", actionType, err)
|
logger.Errorf("%s: 服务层获取列表失败: %v", actionType, err)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取区域主控列表失败: "+err.Error(), actionType, "服务层获取列表失败", nil)
|
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取区域主控列表失败: "+err.Error(), actionType, "服务层获取列表失败", nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.logger.Infof("%s: 获取区域主控列表成功, 数量: %d", actionType, len(resp))
|
logger.Infof("%s: 获取区域主控列表成功, 数量: %d", actionType, len(resp))
|
||||||
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取区域主控列表成功", resp, actionType, "获取区域主控列表成功", resp)
|
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取区域主控列表成功", resp, actionType, "获取区域主控列表成功", resp)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -325,31 +344,33 @@ func (c *Controller) ListAreaControllers(ctx echo.Context) error {
|
|||||||
// @Success 200 {object} controller.Response{data=dto.AreaControllerResponse}
|
// @Success 200 {object} controller.Response{data=dto.AreaControllerResponse}
|
||||||
// @Router /api/v1/area-controllers/{id} [put]
|
// @Router /api/v1/area-controllers/{id} [put]
|
||||||
func (c *Controller) UpdateAreaController(ctx echo.Context) error {
|
func (c *Controller) UpdateAreaController(ctx echo.Context) error {
|
||||||
|
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "UpdateAreaController")
|
||||||
|
|
||||||
const actionType = "更新区域主控"
|
const actionType = "更新区域主控"
|
||||||
acID := ctx.Param("id")
|
acID := ctx.Param("id")
|
||||||
|
|
||||||
var req dto.UpdateAreaControllerRequest
|
var req dto.UpdateAreaControllerRequest
|
||||||
if err := ctx.Bind(&req); err != nil {
|
if err := ctx.Bind(&req); err != nil {
|
||||||
c.logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
|
logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req)
|
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req)
|
||||||
}
|
}
|
||||||
id, err := strconv.ParseUint(acID, 10, 64)
|
id, err := strconv.ParseUint(acID, 10, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.logger.Errorf("%s: 无效的ID: %s", actionType, acID)
|
logger.Errorf("%s: 无效的ID: %s", actionType, acID)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的ID: "+acID, actionType, "无效的ID", acID)
|
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的ID: "+acID, actionType, "无效的ID", acID)
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := c.deviceService.UpdateAreaController(uint(id), &req)
|
resp, err := c.deviceService.UpdateAreaController(reqCtx, uint(id), &req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
c.logger.Warnf("%s: 区域主控不存在, ID: %s", actionType, acID)
|
logger.Warnf("%s: 区域主控不存在, ID: %s", actionType, acID)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "区域主控未找到", actionType, "区域主控不存在", acID)
|
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "区域主控未找到", actionType, "区域主控不存在", acID)
|
||||||
}
|
}
|
||||||
c.logger.Errorf("%s: 服务层更新失败: %v, ID: %s", actionType, err, acID)
|
logger.Errorf("%s: 服务层更新失败: %v, ID: %s", actionType, err, acID)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "更新区域主控失败: "+err.Error(), actionType, "服务层更新失败", acID)
|
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "更新区域主控失败: "+err.Error(), actionType, "服务层更新失败", acID)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.logger.Infof("%s: 区域主控更新成功, ID: %d", actionType, resp.ID)
|
logger.Infof("%s: 区域主控更新成功, ID: %d", actionType, resp.ID)
|
||||||
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "区域主控更新成功", resp, actionType, "区域主控更新成功", resp)
|
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "区域主控更新成功", resp, actionType, "区域主控更新成功", resp)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -363,30 +384,32 @@ func (c *Controller) UpdateAreaController(ctx echo.Context) error {
|
|||||||
// @Success 200 {object} controller.Response
|
// @Success 200 {object} controller.Response
|
||||||
// @Router /api/v1/area-controllers/{id} [delete]
|
// @Router /api/v1/area-controllers/{id} [delete]
|
||||||
func (c *Controller) DeleteAreaController(ctx echo.Context) error {
|
func (c *Controller) DeleteAreaController(ctx echo.Context) error {
|
||||||
|
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "DeleteAreaController")
|
||||||
|
|
||||||
const actionType = "删除区域主控"
|
const actionType = "删除区域主控"
|
||||||
acID := ctx.Param("id")
|
acID := ctx.Param("id")
|
||||||
|
|
||||||
id, err := strconv.ParseUint(acID, 10, 64)
|
id, err := strconv.ParseUint(acID, 10, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.logger.Errorf("%s: 无效的ID: %s", actionType, acID)
|
logger.Errorf("%s: 无效的ID: %s", actionType, acID)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的ID: "+acID, actionType, "无效的ID", acID)
|
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的ID: "+acID, actionType, "无效的ID", acID)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := c.deviceService.DeleteAreaController(uint(id)); err != nil {
|
if err := c.deviceService.DeleteAreaController(reqCtx, uint(id)); err != nil {
|
||||||
switch {
|
switch {
|
||||||
case errors.Is(err, gorm.ErrRecordNotFound):
|
case errors.Is(err, gorm.ErrRecordNotFound):
|
||||||
c.logger.Warnf("%s: 区域主控不存在, ID: %s", actionType, acID)
|
logger.Warnf("%s: 区域主控不存在, ID: %s", actionType, acID)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "区域主控未找到", actionType, "区域主控不存在", acID)
|
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "区域主控未找到", actionType, "区域主控不存在", acID)
|
||||||
case errors.Is(err, service.ErrAreaControllerInUse):
|
case errors.Is(err, service.ErrAreaControllerInUse):
|
||||||
c.logger.Warnf("%s: 尝试删除正在被使用的主控, ID: %s", actionType, acID)
|
logger.Warnf("%s: 尝试删除正在被使用的主控, ID: %s", actionType, acID)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeConflict, err.Error(), actionType, "主控正在被使用", acID)
|
return controller.SendErrorWithAudit(ctx, controller.CodeConflict, err.Error(), actionType, "主控正在被使用", acID)
|
||||||
default:
|
default:
|
||||||
c.logger.Errorf("%s: 服务层删除失败: %v, ID: %s", actionType, err, acID)
|
logger.Errorf("%s: 服务层删除失败: %v, ID: %s", actionType, err, acID)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "删除区域主控失败: "+err.Error(), actionType, "服务层删除失败", acID)
|
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "删除区域主控失败: "+err.Error(), actionType, "服务层删除失败", acID)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
c.logger.Infof("%s: 区域主控删除成功, ID: %s", actionType, acID)
|
logger.Infof("%s: 区域主控删除成功, ID: %s", actionType, acID)
|
||||||
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "区域主控删除成功", nil, actionType, "区域主控删除成功", acID)
|
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "区域主控删除成功", nil, actionType, "区域主控删除成功", acID)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -403,20 +426,22 @@ func (c *Controller) DeleteAreaController(ctx echo.Context) error {
|
|||||||
// @Success 200 {object} controller.Response{data=dto.DeviceTemplateResponse}
|
// @Success 200 {object} controller.Response{data=dto.DeviceTemplateResponse}
|
||||||
// @Router /api/v1/device-templates [post]
|
// @Router /api/v1/device-templates [post]
|
||||||
func (c *Controller) CreateDeviceTemplate(ctx echo.Context) error {
|
func (c *Controller) CreateDeviceTemplate(ctx echo.Context) error {
|
||||||
|
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "CreateDeviceTemplate")
|
||||||
|
|
||||||
const actionType = "创建设备模板"
|
const actionType = "创建设备模板"
|
||||||
var req dto.CreateDeviceTemplateRequest
|
var req dto.CreateDeviceTemplateRequest
|
||||||
if err := ctx.Bind(&req); err != nil {
|
if err := ctx.Bind(&req); err != nil {
|
||||||
c.logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
|
logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req)
|
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req)
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := c.deviceService.CreateDeviceTemplate(&req)
|
resp, err := c.deviceService.CreateDeviceTemplate(reqCtx, &req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.logger.Errorf("%s: 服务层创建失败: %v", actionType, err)
|
logger.Errorf("%s: 服务层创建失败: %v", actionType, err)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "创建设备模板失败: "+err.Error(), actionType, "服务层创建失败", req)
|
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "创建设备模板失败: "+err.Error(), actionType, "服务层创建失败", req)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.logger.Infof("%s: 设备模板创建成功, ID: %d", actionType, resp.ID)
|
logger.Infof("%s: 设备模板创建成功, ID: %d", actionType, resp.ID)
|
||||||
return controller.SendSuccessWithAudit(ctx, controller.CodeCreated, "设备模板创建成功", resp, actionType, "设备模板创建成功", resp)
|
return controller.SendSuccessWithAudit(ctx, controller.CodeCreated, "设备模板创建成功", resp, actionType, "设备模板创建成功", resp)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -430,26 +455,28 @@ func (c *Controller) CreateDeviceTemplate(ctx echo.Context) error {
|
|||||||
// @Success 200 {object} controller.Response{data=dto.DeviceTemplateResponse}
|
// @Success 200 {object} controller.Response{data=dto.DeviceTemplateResponse}
|
||||||
// @Router /api/v1/device-templates/{id} [get]
|
// @Router /api/v1/device-templates/{id} [get]
|
||||||
func (c *Controller) GetDeviceTemplate(ctx echo.Context) error {
|
func (c *Controller) GetDeviceTemplate(ctx echo.Context) error {
|
||||||
|
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "GetDeviceTemplate")
|
||||||
|
|
||||||
const actionType = "获取设备模板"
|
const actionType = "获取设备模板"
|
||||||
dtID := ctx.Param("id")
|
dtID := ctx.Param("id")
|
||||||
|
|
||||||
id, err := strconv.ParseUint(dtID, 10, 64)
|
id, err := strconv.ParseUint(dtID, 10, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.logger.Errorf("%s: 无效的ID: %s", actionType, dtID)
|
logger.Errorf("%s: 无效的ID: %s", actionType, dtID)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的ID: "+dtID, actionType, "无效的ID", dtID)
|
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的ID: "+dtID, actionType, "无效的ID", dtID)
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := c.deviceService.GetDeviceTemplate(uint(id))
|
resp, err := c.deviceService.GetDeviceTemplate(reqCtx, uint(id))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
c.logger.Warnf("%s: 设备模板不存在, ID: %s", actionType, dtID)
|
logger.Warnf("%s: 设备模板不存在, ID: %s", actionType, dtID)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "设备模板未找到", actionType, "设备模板不存在", dtID)
|
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "设备模板未找到", actionType, "设备模板不存在", dtID)
|
||||||
}
|
}
|
||||||
c.logger.Errorf("%s: 服务层获取失败: %v, ID: %s", actionType, err, dtID)
|
logger.Errorf("%s: 服务层获取失败: %v, ID: %s", actionType, err, dtID)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取设备模板信息失败: "+err.Error(), actionType, "服务层获取失败", dtID)
|
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取设备模板信息失败: "+err.Error(), actionType, "服务层获取失败", dtID)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.logger.Infof("%s: 获取设备模板信息成功, ID: %d", actionType, resp.ID)
|
logger.Infof("%s: 获取设备模板信息成功, ID: %d", actionType, resp.ID)
|
||||||
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取设备模板信息成功", resp, actionType, "获取设备模板信息成功", resp)
|
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取设备模板信息成功", resp, actionType, "获取设备模板信息成功", resp)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -462,14 +489,16 @@ func (c *Controller) GetDeviceTemplate(ctx echo.Context) error {
|
|||||||
// @Success 200 {object} controller.Response{data=[]dto.DeviceTemplateResponse}
|
// @Success 200 {object} controller.Response{data=[]dto.DeviceTemplateResponse}
|
||||||
// @Router /api/v1/device-templates [get]
|
// @Router /api/v1/device-templates [get]
|
||||||
func (c *Controller) ListDeviceTemplates(ctx echo.Context) error {
|
func (c *Controller) ListDeviceTemplates(ctx echo.Context) error {
|
||||||
|
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "ListDeviceTemplates")
|
||||||
|
|
||||||
const actionType = "获取设备模板列表"
|
const actionType = "获取设备模板列表"
|
||||||
resp, err := c.deviceService.ListDeviceTemplates()
|
resp, err := c.deviceService.ListDeviceTemplates(reqCtx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.logger.Errorf("%s: 服务层获取列表失败: %v", actionType, err)
|
logger.Errorf("%s: 服务层获取列表失败: %v", actionType, err)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取设备模板列表失败: "+err.Error(), actionType, "服务层获取列表失败", nil)
|
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取设备模板列表失败: "+err.Error(), actionType, "服务层获取列表失败", nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.logger.Infof("%s: 获取设备模板列表成功, 数量: %d", actionType, len(resp))
|
logger.Infof("%s: 获取设备模板列表成功, 数量: %d", actionType, len(resp))
|
||||||
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取设备模板列表成功", resp, actionType, "获取设备模板列表成功", resp)
|
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取设备模板列表成功", resp, actionType, "获取设备模板列表成功", resp)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -485,32 +514,34 @@ func (c *Controller) ListDeviceTemplates(ctx echo.Context) error {
|
|||||||
// @Success 200 {object} controller.Response{data=dto.DeviceTemplateResponse}
|
// @Success 200 {object} controller.Response{data=dto.DeviceTemplateResponse}
|
||||||
// @Router /api/v1/device-templates/{id} [put]
|
// @Router /api/v1/device-templates/{id} [put]
|
||||||
func (c *Controller) UpdateDeviceTemplate(ctx echo.Context) error {
|
func (c *Controller) UpdateDeviceTemplate(ctx echo.Context) error {
|
||||||
|
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "UpdateDeviceTemplate")
|
||||||
|
|
||||||
const actionType = "更新设备模板"
|
const actionType = "更新设备模板"
|
||||||
dtID := ctx.Param("id")
|
dtID := ctx.Param("id")
|
||||||
|
|
||||||
var req dto.UpdateDeviceTemplateRequest
|
var req dto.UpdateDeviceTemplateRequest
|
||||||
if err := ctx.Bind(&req); err != nil {
|
if err := ctx.Bind(&req); err != nil {
|
||||||
c.logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
|
logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req)
|
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req)
|
||||||
}
|
}
|
||||||
|
|
||||||
id, err := strconv.ParseUint(dtID, 10, 64)
|
id, err := strconv.ParseUint(dtID, 10, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.logger.Errorf("%s: 无效的ID: %s", actionType, dtID)
|
logger.Errorf("%s: 无效的ID: %s", actionType, dtID)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的ID: "+dtID, actionType, "无效的ID", dtID)
|
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的ID: "+dtID, actionType, "无效的ID", dtID)
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := c.deviceService.UpdateDeviceTemplate(uint(id), &req)
|
resp, err := c.deviceService.UpdateDeviceTemplate(reqCtx, uint(id), &req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
c.logger.Warnf("%s: 设备模板不存在, ID: %s", actionType, dtID)
|
logger.Warnf("%s: 设备模板不存在, ID: %s", actionType, dtID)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "设备模板未找到", actionType, "设备模板不存在", dtID)
|
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "设备模板未找到", actionType, "设备模板不存在", dtID)
|
||||||
}
|
}
|
||||||
c.logger.Errorf("%s: 服务层更新失败: %v, ID: %s", actionType, err, dtID)
|
logger.Errorf("%s: 服务层更新失败: %v, ID: %s", actionType, err, dtID)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "更新设备模板失败: "+err.Error(), actionType, "服务层更新失败", dtID)
|
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "更新设备模板失败: "+err.Error(), actionType, "服务层更新失败", dtID)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.logger.Infof("%s: 设备模板更新成功, ID: %d", actionType, resp.ID)
|
logger.Infof("%s: 设备模板更新成功, ID: %d", actionType, resp.ID)
|
||||||
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "设备模板更新成功", resp, actionType, "设备模板更新成功", resp)
|
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "设备模板更新成功", resp, actionType, "设备模板更新成功", resp)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -524,29 +555,31 @@ func (c *Controller) UpdateDeviceTemplate(ctx echo.Context) error {
|
|||||||
// @Success 200 {object} controller.Response
|
// @Success 200 {object} controller.Response
|
||||||
// @Router /api/v1/device-templates/{id} [delete]
|
// @Router /api/v1/device-templates/{id} [delete]
|
||||||
func (c *Controller) DeleteDeviceTemplate(ctx echo.Context) error {
|
func (c *Controller) DeleteDeviceTemplate(ctx echo.Context) error {
|
||||||
|
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "DeleteDeviceTemplate")
|
||||||
|
|
||||||
const actionType = "删除设备模板"
|
const actionType = "删除设备模板"
|
||||||
dtID := ctx.Param("id")
|
dtID := ctx.Param("id")
|
||||||
|
|
||||||
id, err := strconv.ParseUint(dtID, 10, 64)
|
id, err := strconv.ParseUint(dtID, 10, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.logger.Errorf("%s: 无效的ID: %s", actionType, dtID)
|
logger.Errorf("%s: 无效的ID: %s", actionType, dtID)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的ID: "+dtID, actionType, "无效的ID", dtID)
|
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的ID: "+dtID, actionType, "无效的ID", dtID)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := c.deviceService.DeleteDeviceTemplate(uint(id)); err != nil {
|
if err := c.deviceService.DeleteDeviceTemplate(reqCtx, uint(id)); err != nil {
|
||||||
switch {
|
switch {
|
||||||
case errors.Is(err, gorm.ErrRecordNotFound):
|
case errors.Is(err, gorm.ErrRecordNotFound):
|
||||||
c.logger.Warnf("%s: 设备模板不存在, ID: %s", actionType, dtID)
|
logger.Warnf("%s: 设备模板不存在, ID: %s", actionType, dtID)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "设备模板未找到", actionType, "设备模板不存在", dtID)
|
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "设备模板未找到", actionType, "设备模板不存在", dtID)
|
||||||
case errors.Is(err, service.ErrDeviceTemplateInUse):
|
case errors.Is(err, service.ErrDeviceTemplateInUse):
|
||||||
c.logger.Warnf("%s: 尝试删除正在被使用的模板, ID: %s", actionType, dtID)
|
logger.Warnf("%s: 尝试删除正在被使用的模板, ID: %s", actionType, dtID)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeConflict, err.Error(), actionType, "模板正在被使用", dtID)
|
return controller.SendErrorWithAudit(ctx, controller.CodeConflict, err.Error(), actionType, "模板正在被使用", dtID)
|
||||||
default:
|
default:
|
||||||
c.logger.Errorf("%s: 服务层删除失败: %v, ID: %s", actionType, err, dtID)
|
logger.Errorf("%s: 服务层删除失败: %v, ID: %s", actionType, err, dtID)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "删除设备模板失败: "+err.Error(), actionType, "服务层删除失败", dtID)
|
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "删除设备模板失败: "+err.Error(), actionType, "服务层删除失败", dtID)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
c.logger.Infof("%s: 设备模板删除成功, ID: %s", actionType, dtID)
|
logger.Infof("%s: 设备模板删除成功, ID: %s", actionType, dtID)
|
||||||
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "设备模板删除成功", nil, actionType, "设备模板删除成功", dtID)
|
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "设备模板删除成功", nil, actionType, "设备模板删除成功", dtID)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,25 +1,25 @@
|
|||||||
package management
|
package management
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"git.huangwc.com/pig/pig-farm-controller/internal/app/dto"
|
"git.huangwc.com/pig/pig-farm-controller/internal/app/dto"
|
||||||
"git.huangwc.com/pig/pig-farm-controller/internal/app/service"
|
"git.huangwc.com/pig/pig-farm-controller/internal/app/service"
|
||||||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/logs"
|
"git.huangwc.com/pig/pig-farm-controller/internal/infra/logs"
|
||||||
|
|
||||||
"github.com/labstack/echo/v4"
|
"github.com/labstack/echo/v4"
|
||||||
)
|
)
|
||||||
|
|
||||||
// PigBatchController 负责处理猪批次相关的API请求
|
// PigBatchController 负责处理猪批次相关的API请求
|
||||||
type PigBatchController struct {
|
type PigBatchController struct {
|
||||||
logger *logs.Logger
|
ctx context.Context
|
||||||
service service.PigBatchService
|
service service.PigBatchService
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewPigBatchController 创建一个新的 PigBatchController 实例
|
// NewPigBatchController 创建一个新的 PigBatchController 实例
|
||||||
func NewPigBatchController(logger *logs.Logger, service service.PigBatchService) *PigBatchController {
|
func NewPigBatchController(ctx context.Context, service service.PigBatchService) *PigBatchController {
|
||||||
return &PigBatchController{
|
return &PigBatchController{
|
||||||
logger: logger,
|
ctx: ctx,
|
||||||
service: service,
|
service: service,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -35,6 +35,8 @@ func NewPigBatchController(logger *logs.Logger, service service.PigBatchService)
|
|||||||
// @Success 201 {object} controller.Response{data=dto.PigBatchResponseDTO} "创建成功"
|
// @Success 201 {object} controller.Response{data=dto.PigBatchResponseDTO} "创建成功"
|
||||||
// @Router /api/v1/pig-batches [post]
|
// @Router /api/v1/pig-batches [post]
|
||||||
func (c *PigBatchController) CreatePigBatch(ctx echo.Context) error {
|
func (c *PigBatchController) CreatePigBatch(ctx echo.Context) error {
|
||||||
|
reqCtx := logs.AddFuncName(ctx.Request().Context(), c.ctx, "CreatePigBatch")
|
||||||
|
|
||||||
const action = "创建猪批次"
|
const action = "创建猪批次"
|
||||||
var req dto.PigBatchCreateDTO
|
var req dto.PigBatchCreateDTO
|
||||||
|
|
||||||
@@ -42,7 +44,7 @@ func (c *PigBatchController) CreatePigBatch(ctx echo.Context) error {
|
|||||||
c, ctx, action, &req,
|
c, ctx, action, &req,
|
||||||
func(ctx echo.Context, operatorID uint, primaryID uint, req *dto.PigBatchCreateDTO) (*dto.PigBatchResponseDTO, error) {
|
func(ctx echo.Context, operatorID uint, primaryID uint, req *dto.PigBatchCreateDTO) (*dto.PigBatchResponseDTO, error) {
|
||||||
// 对于创建操作,primaryID通常不从路径中获取,而是由服务层生成
|
// 对于创建操作,primaryID通常不从路径中获取,而是由服务层生成
|
||||||
return c.service.CreatePigBatch(operatorID, req)
|
return c.service.CreatePigBatch(reqCtx, operatorID, req)
|
||||||
},
|
},
|
||||||
"创建成功",
|
"创建成功",
|
||||||
nil, // 无需自定义ID提取器,primaryID将为0
|
nil, // 无需自定义ID提取器,primaryID将为0
|
||||||
@@ -59,12 +61,14 @@ func (c *PigBatchController) CreatePigBatch(ctx echo.Context) error {
|
|||||||
// @Success 200 {object} controller.Response{data=dto.PigBatchResponseDTO} "获取成功"
|
// @Success 200 {object} controller.Response{data=dto.PigBatchResponseDTO} "获取成功"
|
||||||
// @Router /api/v1/pig-batches/{id} [get]
|
// @Router /api/v1/pig-batches/{id} [get]
|
||||||
func (c *PigBatchController) GetPigBatch(ctx echo.Context) error {
|
func (c *PigBatchController) GetPigBatch(ctx echo.Context) error {
|
||||||
|
reqCtx := logs.AddFuncName(ctx.Request().Context(), c.ctx, "GetPigBatch")
|
||||||
|
|
||||||
const action = "获取猪批次"
|
const action = "获取猪批次"
|
||||||
|
|
||||||
return handleNoBodyAPIRequestWithResponse(
|
return handleNoBodyAPIRequestWithResponse(
|
||||||
c, ctx, action,
|
c, ctx, action,
|
||||||
func(ctx echo.Context, operatorID uint, primaryID uint) (*dto.PigBatchResponseDTO, error) {
|
func(ctx echo.Context, operatorID uint, primaryID uint) (*dto.PigBatchResponseDTO, error) {
|
||||||
return c.service.GetPigBatch(primaryID)
|
return c.service.GetPigBatch(reqCtx, primaryID)
|
||||||
},
|
},
|
||||||
"获取成功",
|
"获取成功",
|
||||||
nil, // 默认从 ":id" 路径参数提取ID
|
nil, // 默认从 ":id" 路径参数提取ID
|
||||||
@@ -83,13 +87,15 @@ func (c *PigBatchController) GetPigBatch(ctx echo.Context) error {
|
|||||||
// @Success 200 {object} controller.Response{data=dto.PigBatchResponseDTO} "更新成功"
|
// @Success 200 {object} controller.Response{data=dto.PigBatchResponseDTO} "更新成功"
|
||||||
// @Router /api/v1/pig-batches/{id} [put]
|
// @Router /api/v1/pig-batches/{id} [put]
|
||||||
func (c *PigBatchController) UpdatePigBatch(ctx echo.Context) error {
|
func (c *PigBatchController) UpdatePigBatch(ctx echo.Context) error {
|
||||||
|
reqCtx := logs.AddFuncName(ctx.Request().Context(), c.ctx, "UpdatePigBatch")
|
||||||
|
|
||||||
const action = "更新猪批次"
|
const action = "更新猪批次"
|
||||||
var req dto.PigBatchUpdateDTO
|
var req dto.PigBatchUpdateDTO
|
||||||
|
|
||||||
return handleAPIRequestWithResponse(
|
return handleAPIRequestWithResponse(
|
||||||
c, ctx, action, &req,
|
c, ctx, action, &req,
|
||||||
func(ctx echo.Context, operatorID uint, primaryID uint, req *dto.PigBatchUpdateDTO) (*dto.PigBatchResponseDTO, error) {
|
func(ctx echo.Context, operatorID uint, primaryID uint, req *dto.PigBatchUpdateDTO) (*dto.PigBatchResponseDTO, error) {
|
||||||
return c.service.UpdatePigBatch(primaryID, req)
|
return c.service.UpdatePigBatch(reqCtx, primaryID, req)
|
||||||
},
|
},
|
||||||
"更新成功",
|
"更新成功",
|
||||||
nil, // 默认从 ":id" 路径参数提取ID
|
nil, // 默认从 ":id" 路径参数提取ID
|
||||||
@@ -106,12 +112,14 @@ func (c *PigBatchController) UpdatePigBatch(ctx echo.Context) error {
|
|||||||
// @Success 200 {object} controller.Response "删除成功"
|
// @Success 200 {object} controller.Response "删除成功"
|
||||||
// @Router /api/v1/pig-batches/{id} [delete]
|
// @Router /api/v1/pig-batches/{id} [delete]
|
||||||
func (c *PigBatchController) DeletePigBatch(ctx echo.Context) error {
|
func (c *PigBatchController) DeletePigBatch(ctx echo.Context) error {
|
||||||
|
reqCtx := logs.AddFuncName(ctx.Request().Context(), c.ctx, "DeletePigBatch")
|
||||||
|
|
||||||
const action = "删除猪批次"
|
const action = "删除猪批次"
|
||||||
|
|
||||||
return handleNoBodyAPIRequest(
|
return handleNoBodyAPIRequest(
|
||||||
c, ctx, action,
|
c, ctx, action,
|
||||||
func(ctx echo.Context, operatorID uint, primaryID uint) error {
|
func(ctx echo.Context, operatorID uint, primaryID uint) error {
|
||||||
return c.service.DeletePigBatch(primaryID)
|
return c.service.DeletePigBatch(reqCtx, primaryID)
|
||||||
},
|
},
|
||||||
"删除成功",
|
"删除成功",
|
||||||
nil, // 默认从 ":id" 路径参数提取ID
|
nil, // 默认从 ":id" 路径参数提取ID
|
||||||
@@ -128,13 +136,15 @@ func (c *PigBatchController) DeletePigBatch(ctx echo.Context) error {
|
|||||||
// @Success 200 {object} controller.Response{data=[]dto.PigBatchResponseDTO} "获取成功"
|
// @Success 200 {object} controller.Response{data=[]dto.PigBatchResponseDTO} "获取成功"
|
||||||
// @Router /api/v1/pig-batches [get]
|
// @Router /api/v1/pig-batches [get]
|
||||||
func (c *PigBatchController) ListPigBatches(ctx echo.Context) error {
|
func (c *PigBatchController) ListPigBatches(ctx echo.Context) error {
|
||||||
|
reqCtx := logs.AddFuncName(ctx.Request().Context(), c.ctx, "ListPigBatches")
|
||||||
|
|
||||||
const action = "获取猪批次列表"
|
const action = "获取猪批次列表"
|
||||||
var query dto.PigBatchQueryDTO
|
var query dto.PigBatchQueryDTO
|
||||||
|
|
||||||
return handleQueryAPIRequestWithResponse(
|
return handleQueryAPIRequestWithResponse(
|
||||||
c, ctx, action, &query,
|
c, ctx, action, &query,
|
||||||
func(ctx echo.Context, operatorID uint, query *dto.PigBatchQueryDTO) ([]*dto.PigBatchResponseDTO, error) {
|
func(ctx echo.Context, operatorID uint, query *dto.PigBatchQueryDTO) ([]*dto.PigBatchResponseDTO, error) {
|
||||||
return c.service.ListPigBatches(query.IsActive)
|
return c.service.ListPigBatches(reqCtx, query.IsActive)
|
||||||
},
|
},
|
||||||
"获取成功",
|
"获取成功",
|
||||||
)
|
)
|
||||||
@@ -152,13 +162,15 @@ func (c *PigBatchController) ListPigBatches(ctx echo.Context) error {
|
|||||||
// @Success 200 {object} controller.Response "分配成功"
|
// @Success 200 {object} controller.Response "分配成功"
|
||||||
// @Router /api/v1/pig-batches/assign-pens/{id} [post]
|
// @Router /api/v1/pig-batches/assign-pens/{id} [post]
|
||||||
func (c *PigBatchController) AssignEmptyPensToBatch(ctx echo.Context) error {
|
func (c *PigBatchController) AssignEmptyPensToBatch(ctx echo.Context) error {
|
||||||
|
reqCtx := logs.AddFuncName(ctx.Request().Context(), c.ctx, "AssignEmptyPensToBatch")
|
||||||
|
|
||||||
const action = "为猪批次分配空栏"
|
const action = "为猪批次分配空栏"
|
||||||
var req dto.AssignEmptyPensToBatchRequest
|
var req dto.AssignEmptyPensToBatchRequest
|
||||||
|
|
||||||
return handleAPIRequest(
|
return handleAPIRequest(
|
||||||
c, ctx, action, &req,
|
c, ctx, action, &req,
|
||||||
func(ctx echo.Context, operatorID uint, primaryID uint, req *dto.AssignEmptyPensToBatchRequest) error {
|
func(ctx echo.Context, operatorID uint, primaryID uint, req *dto.AssignEmptyPensToBatchRequest) error {
|
||||||
return c.service.AssignEmptyPensToBatch(primaryID, req.PenIDs, operatorID)
|
return c.service.AssignEmptyPensToBatch(reqCtx, primaryID, req.PenIDs, operatorID)
|
||||||
},
|
},
|
||||||
"分配成功",
|
"分配成功",
|
||||||
nil, // 默认从 ":id" 路径参数提取ID
|
nil, // 默认从 ":id" 路径参数提取ID
|
||||||
@@ -177,6 +189,8 @@ func (c *PigBatchController) AssignEmptyPensToBatch(ctx echo.Context) error {
|
|||||||
// @Success 200 {object} controller.Response "划拨成功"
|
// @Success 200 {object} controller.Response "划拨成功"
|
||||||
// @Router /api/v1/pig-batches/reclassify-pen/{fromBatchID} [post]
|
// @Router /api/v1/pig-batches/reclassify-pen/{fromBatchID} [post]
|
||||||
func (c *PigBatchController) ReclassifyPenToNewBatch(ctx echo.Context) error {
|
func (c *PigBatchController) ReclassifyPenToNewBatch(ctx echo.Context) error {
|
||||||
|
reqCtx := logs.AddFuncName(ctx.Request().Context(), c.ctx, "ReclassifyPenToNewBatch")
|
||||||
|
|
||||||
const action = "划拨猪栏到新批次"
|
const action = "划拨猪栏到新批次"
|
||||||
var req dto.ReclassifyPenToNewBatchRequest
|
var req dto.ReclassifyPenToNewBatchRequest
|
||||||
|
|
||||||
@@ -184,7 +198,7 @@ func (c *PigBatchController) ReclassifyPenToNewBatch(ctx echo.Context) error {
|
|||||||
c, ctx, action, &req,
|
c, ctx, action, &req,
|
||||||
func(ctx echo.Context, operatorID uint, primaryID uint, req *dto.ReclassifyPenToNewBatchRequest) error {
|
func(ctx echo.Context, operatorID uint, primaryID uint, req *dto.ReclassifyPenToNewBatchRequest) error {
|
||||||
// primaryID 在这里是 fromBatchID
|
// primaryID 在这里是 fromBatchID
|
||||||
return c.service.ReclassifyPenToNewBatch(primaryID, req.ToBatchID, req.PenID, operatorID, req.Remarks)
|
return c.service.ReclassifyPenToNewBatch(reqCtx, primaryID, req.ToBatchID, req.PenID, operatorID, req.Remarks)
|
||||||
},
|
},
|
||||||
"划拨成功",
|
"划拨成功",
|
||||||
func(ctx echo.Context) (uint, error) { // 自定义ID提取器,从 ":fromBatchID" 路径参数提取
|
func(ctx echo.Context) (uint, error) { // 自定义ID提取器,从 ":fromBatchID" 路径参数提取
|
||||||
@@ -209,6 +223,8 @@ func (c *PigBatchController) ReclassifyPenToNewBatch(ctx echo.Context) error {
|
|||||||
// @Success 200 {object} controller.Response "移除成功"
|
// @Success 200 {object} controller.Response "移除成功"
|
||||||
// @Router /api/v1/pig-batches/remove-pen/{penID}/{batchID} [delete]
|
// @Router /api/v1/pig-batches/remove-pen/{penID}/{batchID} [delete]
|
||||||
func (c *PigBatchController) RemoveEmptyPenFromBatch(ctx echo.Context) error {
|
func (c *PigBatchController) RemoveEmptyPenFromBatch(ctx echo.Context) error {
|
||||||
|
reqCtx := logs.AddFuncName(ctx.Request().Context(), c.ctx, "RemoveEmptyPenFromBatch")
|
||||||
|
|
||||||
const action = "从猪批次移除空栏"
|
const action = "从猪批次移除空栏"
|
||||||
|
|
||||||
return handleNoBodyAPIRequest(
|
return handleNoBodyAPIRequest(
|
||||||
@@ -220,7 +236,7 @@ func (c *PigBatchController) RemoveEmptyPenFromBatch(ctx echo.Context) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err // 返回错误,因为 penID 格式无效
|
return err // 返回错误,因为 penID 格式无效
|
||||||
}
|
}
|
||||||
return c.service.RemoveEmptyPenFromBatch(primaryID, uint(parsedPenID))
|
return c.service.RemoveEmptyPenFromBatch(reqCtx, primaryID, uint(parsedPenID))
|
||||||
},
|
},
|
||||||
"移除成功",
|
"移除成功",
|
||||||
func(ctx echo.Context) (uint, error) { // 自定义ID提取器,从 ":batchID" 路径参数提取
|
func(ctx echo.Context) (uint, error) { // 自定义ID提取器,从 ":batchID" 路径参数提取
|
||||||
@@ -246,13 +262,15 @@ func (c *PigBatchController) RemoveEmptyPenFromBatch(ctx echo.Context) error {
|
|||||||
// @Success 200 {object} controller.Response "移入成功"
|
// @Success 200 {object} controller.Response "移入成功"
|
||||||
// @Router /api/v1/pig-batches/move-pigs-into-pen/{id} [post]
|
// @Router /api/v1/pig-batches/move-pigs-into-pen/{id} [post]
|
||||||
func (c *PigBatchController) MovePigsIntoPen(ctx echo.Context) error {
|
func (c *PigBatchController) MovePigsIntoPen(ctx echo.Context) error {
|
||||||
|
reqCtx := logs.AddFuncName(ctx.Request().Context(), c.ctx, "MovePigsIntoPen")
|
||||||
|
|
||||||
const action = "将猪只移入猪栏"
|
const action = "将猪只移入猪栏"
|
||||||
var req dto.MovePigsIntoPenRequest
|
var req dto.MovePigsIntoPenRequest
|
||||||
|
|
||||||
return handleAPIRequest(
|
return handleAPIRequest(
|
||||||
c, ctx, action, &req,
|
c, ctx, action, &req,
|
||||||
func(ctx echo.Context, operatorID uint, primaryID uint, req *dto.MovePigsIntoPenRequest) error {
|
func(ctx echo.Context, operatorID uint, primaryID uint, req *dto.MovePigsIntoPenRequest) error {
|
||||||
return c.service.MovePigsIntoPen(primaryID, req.ToPenID, req.Quantity, operatorID, req.Remarks)
|
return c.service.MovePigsIntoPen(reqCtx, primaryID, req.ToPenID, req.Quantity, operatorID, req.Remarks)
|
||||||
},
|
},
|
||||||
"移入成功",
|
"移入成功",
|
||||||
nil, // 默认从 ":id" 路径参数提取ID
|
nil, // 默认从 ":id" 路径参数提取ID
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package management
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"git.huangwc.com/pig/pig-farm-controller/internal/app/dto"
|
"git.huangwc.com/pig/pig-farm-controller/internal/app/dto"
|
||||||
|
"git.huangwc.com/pig/pig-farm-controller/internal/infra/logs"
|
||||||
"github.com/labstack/echo/v4"
|
"github.com/labstack/echo/v4"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -17,13 +18,15 @@ import (
|
|||||||
// @Success 200 {object} controller.Response "记录成功"
|
// @Success 200 {object} controller.Response "记录成功"
|
||||||
// @Router /api/v1/pig-batches/record-sick-pigs/{id} [post]
|
// @Router /api/v1/pig-batches/record-sick-pigs/{id} [post]
|
||||||
func (c *PigBatchController) RecordSickPigs(ctx echo.Context) error {
|
func (c *PigBatchController) RecordSickPigs(ctx echo.Context) error {
|
||||||
|
reqCtx := logs.AddFuncName(ctx.Request().Context(), c.ctx, "RecordSickPigs")
|
||||||
|
|
||||||
const action = "记录新增病猪事件"
|
const action = "记录新增病猪事件"
|
||||||
var req dto.RecordSickPigsRequest
|
var req dto.RecordSickPigsRequest
|
||||||
|
|
||||||
return handleAPIRequest(
|
return handleAPIRequest(
|
||||||
c, ctx, action, &req,
|
c, ctx, action, &req,
|
||||||
func(ctx echo.Context, operatorID uint, primaryID uint, req *dto.RecordSickPigsRequest) error {
|
func(ctx echo.Context, operatorID uint, primaryID uint, req *dto.RecordSickPigsRequest) error {
|
||||||
return c.service.RecordSickPigs(operatorID, primaryID, req.PenID, req.Quantity, req.TreatmentLocation, req.HappenedAt, req.Remarks)
|
return c.service.RecordSickPigs(reqCtx, primaryID, req.PenID, req.Quantity, req.TreatmentLocation, req.HappenedAt, req.Remarks)
|
||||||
},
|
},
|
||||||
"记录成功",
|
"记录成功",
|
||||||
nil, // 默认从 ":id" 路径参数提取ID
|
nil, // 默认从 ":id" 路径参数提取ID
|
||||||
@@ -42,13 +45,15 @@ func (c *PigBatchController) RecordSickPigs(ctx echo.Context) error {
|
|||||||
// @Success 200 {object} controller.Response "记录成功"
|
// @Success 200 {object} controller.Response "记录成功"
|
||||||
// @Router /api/v1/pig-batches/record-sick-pig-recovery/{id} [post]
|
// @Router /api/v1/pig-batches/record-sick-pig-recovery/{id} [post]
|
||||||
func (c *PigBatchController) RecordSickPigRecovery(ctx echo.Context) error {
|
func (c *PigBatchController) RecordSickPigRecovery(ctx echo.Context) error {
|
||||||
|
reqCtx := logs.AddFuncName(ctx.Request().Context(), c.ctx, "RecordSickPigRecovery")
|
||||||
|
|
||||||
const action = "记录病猪康复事件"
|
const action = "记录病猪康复事件"
|
||||||
var req dto.RecordSickPigRecoveryRequest
|
var req dto.RecordSickPigRecoveryRequest
|
||||||
|
|
||||||
return handleAPIRequest(
|
return handleAPIRequest(
|
||||||
c, ctx, action, &req,
|
c, ctx, action, &req,
|
||||||
func(ctx echo.Context, operatorID uint, primaryID uint, req *dto.RecordSickPigRecoveryRequest) error {
|
func(ctx echo.Context, operatorID uint, primaryID uint, req *dto.RecordSickPigRecoveryRequest) error {
|
||||||
return c.service.RecordSickPigRecovery(operatorID, primaryID, req.PenID, req.Quantity, req.TreatmentLocation, req.HappenedAt, req.Remarks)
|
return c.service.RecordSickPigRecovery(reqCtx, operatorID, primaryID, req.PenID, req.Quantity, req.TreatmentLocation, req.HappenedAt, req.Remarks)
|
||||||
},
|
},
|
||||||
"记录成功",
|
"记录成功",
|
||||||
nil, // 默认从 ":id" 路径参数提取ID
|
nil, // 默认从 ":id" 路径参数提取ID
|
||||||
@@ -67,13 +72,15 @@ func (c *PigBatchController) RecordSickPigRecovery(ctx echo.Context) error {
|
|||||||
// @Success 200 {object} controller.Response "记录成功"
|
// @Success 200 {object} controller.Response "记录成功"
|
||||||
// @Router /api/v1/pig-batches/record-sick-pig-death/{id} [post]
|
// @Router /api/v1/pig-batches/record-sick-pig-death/{id} [post]
|
||||||
func (c *PigBatchController) RecordSickPigDeath(ctx echo.Context) error {
|
func (c *PigBatchController) RecordSickPigDeath(ctx echo.Context) error {
|
||||||
|
reqCtx := logs.AddFuncName(ctx.Request().Context(), c.ctx, "RecordSickPigDeath")
|
||||||
|
|
||||||
const action = "记录病猪死亡事件"
|
const action = "记录病猪死亡事件"
|
||||||
var req dto.RecordSickPigDeathRequest
|
var req dto.RecordSickPigDeathRequest
|
||||||
|
|
||||||
return handleAPIRequest(
|
return handleAPIRequest(
|
||||||
c, ctx, action, &req,
|
c, ctx, action, &req,
|
||||||
func(ctx echo.Context, operatorID uint, primaryID uint, req *dto.RecordSickPigDeathRequest) error {
|
func(ctx echo.Context, operatorID uint, primaryID uint, req *dto.RecordSickPigDeathRequest) error {
|
||||||
return c.service.RecordSickPigDeath(operatorID, primaryID, req.PenID, req.Quantity, req.TreatmentLocation, req.HappenedAt, req.Remarks)
|
return c.service.RecordSickPigDeath(reqCtx, operatorID, primaryID, req.PenID, req.Quantity, req.TreatmentLocation, req.HappenedAt, req.Remarks)
|
||||||
},
|
},
|
||||||
"记录成功",
|
"记录成功",
|
||||||
nil, // 默认从 ":id" 路径参数提取ID
|
nil, // 默认从 ":id" 路径参数提取ID
|
||||||
@@ -92,13 +99,15 @@ func (c *PigBatchController) RecordSickPigDeath(ctx echo.Context) error {
|
|||||||
// @Success 200 {object} controller.Response "记录成功"
|
// @Success 200 {object} controller.Response "记录成功"
|
||||||
// @Router /api/v1/pig-batches/record-sick-pig-cull/{id} [post]
|
// @Router /api/v1/pig-batches/record-sick-pig-cull/{id} [post]
|
||||||
func (c *PigBatchController) RecordSickPigCull(ctx echo.Context) error {
|
func (c *PigBatchController) RecordSickPigCull(ctx echo.Context) error {
|
||||||
|
reqCtx := logs.AddFuncName(ctx.Request().Context(), c.ctx, "RecordSickPigCull")
|
||||||
|
|
||||||
const action = "记录病猪淘汰事件"
|
const action = "记录病猪淘汰事件"
|
||||||
var req dto.RecordSickPigCullRequest
|
var req dto.RecordSickPigCullRequest
|
||||||
|
|
||||||
return handleAPIRequest(
|
return handleAPIRequest(
|
||||||
c, ctx, action, &req,
|
c, ctx, action, &req,
|
||||||
func(ctx echo.Context, operatorID uint, primaryID uint, req *dto.RecordSickPigCullRequest) error {
|
func(ctx echo.Context, operatorID uint, primaryID uint, req *dto.RecordSickPigCullRequest) error {
|
||||||
return c.service.RecordSickPigCull(operatorID, primaryID, req.PenID, req.Quantity, req.TreatmentLocation, req.HappenedAt, req.Remarks)
|
return c.service.RecordSickPigCull(reqCtx, operatorID, primaryID, req.PenID, req.Quantity, req.TreatmentLocation, req.HappenedAt, req.Remarks)
|
||||||
},
|
},
|
||||||
"记录成功",
|
"记录成功",
|
||||||
nil, // 默认从 ":id" 路径参数提取ID
|
nil, // 默认从 ":id" 路径参数提取ID
|
||||||
@@ -117,13 +126,15 @@ func (c *PigBatchController) RecordSickPigCull(ctx echo.Context) error {
|
|||||||
// @Success 200 {object} controller.Response "记录成功"
|
// @Success 200 {object} controller.Response "记录成功"
|
||||||
// @Router /api/v1/pig-batches/record-death/{id} [post]
|
// @Router /api/v1/pig-batches/record-death/{id} [post]
|
||||||
func (c *PigBatchController) RecordDeath(ctx echo.Context) error {
|
func (c *PigBatchController) RecordDeath(ctx echo.Context) error {
|
||||||
|
reqCtx := logs.AddFuncName(ctx.Request().Context(), c.ctx, "RecordDeath")
|
||||||
|
|
||||||
const action = "记录正常猪只死亡事件"
|
const action = "记录正常猪只死亡事件"
|
||||||
var req dto.RecordDeathRequest
|
var req dto.RecordDeathRequest
|
||||||
|
|
||||||
return handleAPIRequest(
|
return handleAPIRequest(
|
||||||
c, ctx, action, &req,
|
c, ctx, action, &req,
|
||||||
func(ctx echo.Context, operatorID uint, primaryID uint, req *dto.RecordDeathRequest) error {
|
func(ctx echo.Context, operatorID uint, primaryID uint, req *dto.RecordDeathRequest) error {
|
||||||
return c.service.RecordDeath(operatorID, primaryID, req.PenID, req.Quantity, req.HappenedAt, req.Remarks)
|
return c.service.RecordDeath(reqCtx, operatorID, primaryID, req.PenID, req.Quantity, req.HappenedAt, req.Remarks)
|
||||||
},
|
},
|
||||||
"记录成功",
|
"记录成功",
|
||||||
nil, // 默认从 ":id" 路径参数提取ID
|
nil, // 默认从 ":id" 路径参数提取ID
|
||||||
@@ -142,13 +153,15 @@ func (c *PigBatchController) RecordDeath(ctx echo.Context) error {
|
|||||||
// @Success 200 {object} controller.Response "记录成功"
|
// @Success 200 {object} controller.Response "记录成功"
|
||||||
// @Router /api/v1/pig-batches/record-cull/{id} [post]
|
// @Router /api/v1/pig-batches/record-cull/{id} [post]
|
||||||
func (c *PigBatchController) RecordCull(ctx echo.Context) error {
|
func (c *PigBatchController) RecordCull(ctx echo.Context) error {
|
||||||
|
reqCtx := logs.AddFuncName(ctx.Request().Context(), c.ctx, "RecordCull")
|
||||||
|
|
||||||
const action = "记录正常猪只淘汰事件"
|
const action = "记录正常猪只淘汰事件"
|
||||||
var req dto.RecordCullRequest
|
var req dto.RecordCullRequest
|
||||||
|
|
||||||
return handleAPIRequest(
|
return handleAPIRequest(
|
||||||
c, ctx, action, &req,
|
c, ctx, action, &req,
|
||||||
func(ctx echo.Context, operatorID uint, primaryID uint, req *dto.RecordCullRequest) error {
|
func(ctx echo.Context, operatorID uint, primaryID uint, req *dto.RecordCullRequest) error {
|
||||||
return c.service.RecordCull(operatorID, primaryID, req.PenID, req.Quantity, req.HappenedAt, req.Remarks)
|
return c.service.RecordCull(reqCtx, operatorID, primaryID, req.PenID, req.Quantity, req.HappenedAt, req.Remarks)
|
||||||
},
|
},
|
||||||
"记录成功",
|
"记录成功",
|
||||||
nil, // 默认从 ":id" 路径参数提取ID
|
nil, // 默认从 ":id" 路径参数提取ID
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package management
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"git.huangwc.com/pig/pig-farm-controller/internal/app/dto"
|
"git.huangwc.com/pig/pig-farm-controller/internal/app/dto"
|
||||||
|
"git.huangwc.com/pig/pig-farm-controller/internal/infra/logs"
|
||||||
"github.com/labstack/echo/v4"
|
"github.com/labstack/echo/v4"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -17,13 +18,14 @@ import (
|
|||||||
// @Success 200 {object} controller.Response "卖猪成功"
|
// @Success 200 {object} controller.Response "卖猪成功"
|
||||||
// @Router /api/v1/pig-batches/sell-pigs/{id} [post]
|
// @Router /api/v1/pig-batches/sell-pigs/{id} [post]
|
||||||
func (c *PigBatchController) SellPigs(ctx echo.Context) error {
|
func (c *PigBatchController) SellPigs(ctx echo.Context) error {
|
||||||
|
reqCtx := logs.AddFuncName(ctx.Request().Context(), c.ctx, "SellPigs")
|
||||||
const action = "卖猪"
|
const action = "卖猪"
|
||||||
var req dto.SellPigsRequest
|
var req dto.SellPigsRequest
|
||||||
|
|
||||||
return handleAPIRequest(
|
return handleAPIRequest(
|
||||||
c, ctx, action, &req,
|
c, ctx, action, &req,
|
||||||
func(ctx echo.Context, operatorID uint, primaryID uint, req *dto.SellPigsRequest) error {
|
func(ctx echo.Context, operatorID uint, primaryID uint, req *dto.SellPigsRequest) error {
|
||||||
return c.service.SellPigs(primaryID, req.PenID, req.Quantity, req.UnitPrice, req.TotalPrice, req.TraderName, req.TradeDate, req.Remarks, operatorID)
|
return c.service.SellPigs(reqCtx, primaryID, req.PenID, req.Quantity, req.UnitPrice, req.TotalPrice, req.TraderName, req.TradeDate, req.Remarks, operatorID)
|
||||||
},
|
},
|
||||||
"卖猪成功",
|
"卖猪成功",
|
||||||
nil, // 默认从 ":id" 路径参数提取ID
|
nil, // 默认从 ":id" 路径参数提取ID
|
||||||
@@ -42,13 +44,14 @@ func (c *PigBatchController) SellPigs(ctx echo.Context) error {
|
|||||||
// @Success 200 {object} controller.Response "买猪成功"
|
// @Success 200 {object} controller.Response "买猪成功"
|
||||||
// @Router /api/v1/pig-batches/buy-pigs/{id} [post]
|
// @Router /api/v1/pig-batches/buy-pigs/{id} [post]
|
||||||
func (c *PigBatchController) BuyPigs(ctx echo.Context) error {
|
func (c *PigBatchController) BuyPigs(ctx echo.Context) error {
|
||||||
|
reqCtx := logs.AddFuncName(ctx.Request().Context(), c.ctx, "BuyPigs")
|
||||||
const action = "买猪"
|
const action = "买猪"
|
||||||
var req dto.BuyPigsRequest
|
var req dto.BuyPigsRequest
|
||||||
|
|
||||||
return handleAPIRequest(
|
return handleAPIRequest(
|
||||||
c, ctx, action, &req,
|
c, ctx, action, &req,
|
||||||
func(ctx echo.Context, operatorID uint, primaryID uint, req *dto.BuyPigsRequest) error {
|
func(ctx echo.Context, operatorID uint, primaryID uint, req *dto.BuyPigsRequest) error {
|
||||||
return c.service.BuyPigs(primaryID, req.PenID, req.Quantity, req.UnitPrice, req.TotalPrice, req.TraderName, req.TradeDate, req.Remarks, operatorID)
|
return c.service.BuyPigs(reqCtx, primaryID, req.PenID, req.Quantity, req.UnitPrice, req.TotalPrice, req.TraderName, req.TradeDate, req.Remarks, operatorID)
|
||||||
},
|
},
|
||||||
"买猪成功",
|
"买猪成功",
|
||||||
nil, // 默认从 ":id" 路径参数提取ID
|
nil, // 默认从 ":id" 路径参数提取ID
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"git.huangwc.com/pig/pig-farm-controller/internal/app/dto"
|
"git.huangwc.com/pig/pig-farm-controller/internal/app/dto"
|
||||||
|
"git.huangwc.com/pig/pig-farm-controller/internal/infra/logs"
|
||||||
"github.com/labstack/echo/v4"
|
"github.com/labstack/echo/v4"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -19,6 +20,8 @@ import (
|
|||||||
// @Success 200 {object} controller.Response "调栏成功"
|
// @Success 200 {object} controller.Response "调栏成功"
|
||||||
// @Router /api/v1/pig-batches/transfer-across-batches/{sourceBatchID} [post]
|
// @Router /api/v1/pig-batches/transfer-across-batches/{sourceBatchID} [post]
|
||||||
func (c *PigBatchController) TransferPigsAcrossBatches(ctx echo.Context) error {
|
func (c *PigBatchController) TransferPigsAcrossBatches(ctx echo.Context) error {
|
||||||
|
reqCtx := logs.AddFuncName(ctx.Request().Context(), c.ctx, "TransferPigsAcrossBatches")
|
||||||
|
|
||||||
const action = "跨猪群调栏"
|
const action = "跨猪群调栏"
|
||||||
var req dto.TransferPigsAcrossBatchesRequest
|
var req dto.TransferPigsAcrossBatchesRequest
|
||||||
|
|
||||||
@@ -26,7 +29,7 @@ func (c *PigBatchController) TransferPigsAcrossBatches(ctx echo.Context) error {
|
|||||||
c, ctx, action, &req,
|
c, ctx, action, &req,
|
||||||
func(ctx echo.Context, operatorID uint, primaryID uint, req *dto.TransferPigsAcrossBatchesRequest) error {
|
func(ctx echo.Context, operatorID uint, primaryID uint, req *dto.TransferPigsAcrossBatchesRequest) error {
|
||||||
// primaryID 在这里是 sourceBatchID
|
// primaryID 在这里是 sourceBatchID
|
||||||
return c.service.TransferPigsAcrossBatches(primaryID, req.DestBatchID, req.FromPenID, req.ToPenID, req.Quantity, operatorID, req.Remarks)
|
return c.service.TransferPigsAcrossBatches(reqCtx, primaryID, req.DestBatchID, req.FromPenID, req.ToPenID, req.Quantity, operatorID, req.Remarks)
|
||||||
},
|
},
|
||||||
"调栏成功",
|
"调栏成功",
|
||||||
func(ctx echo.Context) (uint, error) { // 自定义ID提取器,从 ":sourceBatchID" 路径参数提取
|
func(ctx echo.Context) (uint, error) { // 自定义ID提取器,从 ":sourceBatchID" 路径参数提取
|
||||||
@@ -52,6 +55,8 @@ func (c *PigBatchController) TransferPigsAcrossBatches(ctx echo.Context) error {
|
|||||||
// @Success 200 {object} controller.Response "调栏成功"
|
// @Success 200 {object} controller.Response "调栏成功"
|
||||||
// @Router /api/v1/pig-batches/transfer-within-batch/{id} [post]
|
// @Router /api/v1/pig-batches/transfer-within-batch/{id} [post]
|
||||||
func (c *PigBatchController) TransferPigsWithinBatch(ctx echo.Context) error {
|
func (c *PigBatchController) TransferPigsWithinBatch(ctx echo.Context) error {
|
||||||
|
reqCtx := logs.AddFuncName(ctx.Request().Context(), c.ctx, "TransferPigsWithinBatch")
|
||||||
|
|
||||||
const action = "群内调栏"
|
const action = "群内调栏"
|
||||||
var req dto.TransferPigsWithinBatchRequest
|
var req dto.TransferPigsWithinBatchRequest
|
||||||
|
|
||||||
@@ -59,7 +64,7 @@ func (c *PigBatchController) TransferPigsWithinBatch(ctx echo.Context) error {
|
|||||||
c, ctx, action, &req,
|
c, ctx, action, &req,
|
||||||
func(ctx echo.Context, operatorID uint, primaryID uint, req *dto.TransferPigsWithinBatchRequest) error {
|
func(ctx echo.Context, operatorID uint, primaryID uint, req *dto.TransferPigsWithinBatchRequest) error {
|
||||||
// primaryID 在这里是 batchID
|
// primaryID 在这里是 batchID
|
||||||
return c.service.TransferPigsWithinBatch(primaryID, req.FromPenID, req.ToPenID, req.Quantity, operatorID, req.Remarks)
|
return c.service.TransferPigsWithinBatch(reqCtx, primaryID, req.FromPenID, req.ToPenID, req.Quantity, operatorID, req.Remarks)
|
||||||
},
|
},
|
||||||
"调栏成功",
|
"调栏成功",
|
||||||
nil, // 默认从 ":id" 路径参数提取ID
|
nil, // 默认从 ":id" 路径参数提取ID
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package management
|
package management
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
@@ -15,14 +16,14 @@ import (
|
|||||||
|
|
||||||
// PigFarmController 负责处理猪舍和猪栏相关的API请求
|
// PigFarmController 负责处理猪舍和猪栏相关的API请求
|
||||||
type PigFarmController struct {
|
type PigFarmController struct {
|
||||||
logger *logs.Logger
|
ctx context.Context
|
||||||
service service.PigFarmService
|
service service.PigFarmService
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewPigFarmController 创建一个新的 PigFarmController 实例
|
// NewPigFarmController 创建一个新的 PigFarmController 实例
|
||||||
func NewPigFarmController(logger *logs.Logger, service service.PigFarmService) *PigFarmController {
|
func NewPigFarmController(ctx context.Context, service service.PigFarmService) *PigFarmController {
|
||||||
return &PigFarmController{
|
return &PigFarmController{
|
||||||
logger: logger,
|
ctx: ctx,
|
||||||
service: service,
|
service: service,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -40,16 +41,17 @@ func NewPigFarmController(logger *logs.Logger, service service.PigFarmService) *
|
|||||||
// @Success 201 {object} controller.Response{data=dto.PigHouseResponse} "创建成功"
|
// @Success 201 {object} controller.Response{data=dto.PigHouseResponse} "创建成功"
|
||||||
// @Router /api/v1/pig-houses [post]
|
// @Router /api/v1/pig-houses [post]
|
||||||
func (c *PigFarmController) CreatePigHouse(ctx echo.Context) error {
|
func (c *PigFarmController) CreatePigHouse(ctx echo.Context) error {
|
||||||
|
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "CreatePigHouse")
|
||||||
const action = "创建猪舍"
|
const action = "创建猪舍"
|
||||||
var req dto.CreatePigHouseRequest
|
var req dto.CreatePigHouseRequest
|
||||||
if err := ctx.Bind(&req); err != nil {
|
if err := ctx.Bind(&req); err != nil {
|
||||||
c.logger.Errorf("%s: 参数绑定失败: %v", action, err)
|
logger.Errorf("%s: 参数绑定失败: %v", action, err)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体", action, "请求体绑定失败", req)
|
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体", action, "请求体绑定失败", req)
|
||||||
}
|
}
|
||||||
|
|
||||||
house, err := c.service.CreatePigHouse(req.Name, req.Description)
|
house, err := c.service.CreatePigHouse(reqCtx, req.Name, req.Description)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.logger.Errorf("%s: 业务逻辑失败: %v", action, err)
|
logger.Errorf("%s: 业务逻辑失败: %v", action, err)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "创建猪舍失败", action, "业务逻辑失败", req)
|
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "创建猪舍失败", action, "业务逻辑失败", req)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -66,18 +68,19 @@ func (c *PigFarmController) CreatePigHouse(ctx echo.Context) error {
|
|||||||
// @Success 200 {object} controller.Response{data=dto.PigHouseResponse} "获取成功"
|
// @Success 200 {object} controller.Response{data=dto.PigHouseResponse} "获取成功"
|
||||||
// @Router /api/v1/pig-houses/{id} [get]
|
// @Router /api/v1/pig-houses/{id} [get]
|
||||||
func (c *PigFarmController) GetPigHouse(ctx echo.Context) error {
|
func (c *PigFarmController) GetPigHouse(ctx echo.Context) error {
|
||||||
|
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "GetPigHouse")
|
||||||
const action = "获取猪舍"
|
const action = "获取猪舍"
|
||||||
id, err := strconv.ParseUint(ctx.Param("id"), 10, 32)
|
id, err := strconv.ParseUint(ctx.Param("id"), 10, 32)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的ID格式", action, "ID格式错误", ctx.Param("id"))
|
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的ID格式", action, "ID格式错误", ctx.Param("id"))
|
||||||
}
|
}
|
||||||
|
|
||||||
house, err := c.service.GetPigHouseByID(uint(id))
|
house, err := c.service.GetPigHouseByID(reqCtx, uint(id))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, service.ErrHouseNotFound) {
|
if errors.Is(err, service.ErrHouseNotFound) {
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "猪舍不存在", action, "猪舍不存在", id)
|
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "猪舍不存在", action, "猪舍不存在", id)
|
||||||
}
|
}
|
||||||
c.logger.Errorf("%s: 业务逻辑失败: %v", action, err)
|
logger.Errorf("%s: 业务逻辑失败: %v", action, err)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取猪舍失败", action, "业务逻辑失败", id)
|
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取猪舍失败", action, "业务逻辑失败", id)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -93,10 +96,11 @@ func (c *PigFarmController) GetPigHouse(ctx echo.Context) error {
|
|||||||
// @Success 200 {object} controller.Response{data=[]dto.PigHouseResponse} "获取成功"
|
// @Success 200 {object} controller.Response{data=[]dto.PigHouseResponse} "获取成功"
|
||||||
// @Router /api/v1/pig-houses [get]
|
// @Router /api/v1/pig-houses [get]
|
||||||
func (c *PigFarmController) ListPigHouses(ctx echo.Context) error {
|
func (c *PigFarmController) ListPigHouses(ctx echo.Context) error {
|
||||||
|
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "ListPigHouses")
|
||||||
const action = "获取猪舍列表"
|
const action = "获取猪舍列表"
|
||||||
houses, err := c.service.ListPigHouses()
|
houses, err := c.service.ListPigHouses(reqCtx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.logger.Errorf("%s: 业务逻辑失败: %v", action, err)
|
logger.Errorf("%s: 业务逻辑失败: %v", action, err)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取列表失败", action, "业务逻辑失败", nil)
|
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取列表失败", action, "业务逻辑失败", nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -115,6 +119,7 @@ func (c *PigFarmController) ListPigHouses(ctx echo.Context) error {
|
|||||||
// @Success 200 {object} controller.Response{data=dto.PigHouseResponse} "更新成功"
|
// @Success 200 {object} controller.Response{data=dto.PigHouseResponse} "更新成功"
|
||||||
// @Router /api/v1/pig-houses/{id} [put]
|
// @Router /api/v1/pig-houses/{id} [put]
|
||||||
func (c *PigFarmController) UpdatePigHouse(ctx echo.Context) error {
|
func (c *PigFarmController) UpdatePigHouse(ctx echo.Context) error {
|
||||||
|
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "UpdatePigHouse")
|
||||||
const action = "更新猪舍"
|
const action = "更新猪舍"
|
||||||
id, err := strconv.ParseUint(ctx.Param("id"), 10, 32)
|
id, err := strconv.ParseUint(ctx.Param("id"), 10, 32)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -126,12 +131,12 @@ func (c *PigFarmController) UpdatePigHouse(ctx echo.Context) error {
|
|||||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体", action, "请求体绑定失败", req)
|
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体", action, "请求体绑定失败", req)
|
||||||
}
|
}
|
||||||
|
|
||||||
house, err := c.service.UpdatePigHouse(uint(id), req.Name, req.Description)
|
house, err := c.service.UpdatePigHouse(reqCtx, uint(id), req.Name, req.Description)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, service.ErrHouseNotFound) {
|
if errors.Is(err, service.ErrHouseNotFound) {
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "猪舍不存在", action, "猪舍不存在", id)
|
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "猪舍不存在", action, "猪舍不存在", id)
|
||||||
}
|
}
|
||||||
c.logger.Errorf("%s: 业务逻辑失败: %v", action, err)
|
logger.Errorf("%s: 业务逻辑失败: %v", action, err)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "更新失败", action, "业务逻辑失败", req)
|
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "更新失败", action, "业务逻辑失败", req)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -148,13 +153,14 @@ func (c *PigFarmController) UpdatePigHouse(ctx echo.Context) error {
|
|||||||
// @Success 200 {object} controller.Response "删除成功"
|
// @Success 200 {object} controller.Response "删除成功"
|
||||||
// @Router /api/v1/pig-houses/{id} [delete]
|
// @Router /api/v1/pig-houses/{id} [delete]
|
||||||
func (c *PigFarmController) DeletePigHouse(ctx echo.Context) error {
|
func (c *PigFarmController) DeletePigHouse(ctx echo.Context) error {
|
||||||
|
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "DeletePigHouse")
|
||||||
const action = "删除猪舍"
|
const action = "删除猪舍"
|
||||||
id, err := strconv.ParseUint(ctx.Param("id"), 10, 32)
|
id, err := strconv.ParseUint(ctx.Param("id"), 10, 32)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的ID格式", action, "ID格式错误", ctx.Param("id"))
|
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的ID格式", action, "ID格式错误", ctx.Param("id"))
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := c.service.DeletePigHouse(uint(id)); err != nil {
|
if err := c.service.DeletePigHouse(reqCtx, uint(id)); err != nil {
|
||||||
if errors.Is(err, service.ErrHouseNotFound) {
|
if errors.Is(err, service.ErrHouseNotFound) {
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "猪舍不存在", action, "猪舍不存在", id)
|
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "猪舍不存在", action, "猪舍不存在", id)
|
||||||
}
|
}
|
||||||
@@ -162,7 +168,7 @@ func (c *PigFarmController) DeletePigHouse(ctx echo.Context) error {
|
|||||||
if errors.Is(err, service.ErrHouseContainsPens) {
|
if errors.Is(err, service.ErrHouseContainsPens) {
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeConflict, err.Error(), action, err.Error(), id)
|
return controller.SendErrorWithAudit(ctx, controller.CodeConflict, err.Error(), action, err.Error(), id)
|
||||||
}
|
}
|
||||||
c.logger.Errorf("%s: 业务逻辑失败: %v", action, err)
|
logger.Errorf("%s: 业务逻辑失败: %v", action, err)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "删除失败", action, "业务逻辑失败", id)
|
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "删除失败", action, "业务逻辑失败", id)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -182,19 +188,20 @@ func (c *PigFarmController) DeletePigHouse(ctx echo.Context) error {
|
|||||||
// @Success 201 {object} controller.Response{data=dto.PenResponse} "创建成功"
|
// @Success 201 {object} controller.Response{data=dto.PenResponse} "创建成功"
|
||||||
// @Router /api/v1/pens [post]
|
// @Router /api/v1/pens [post]
|
||||||
func (c *PigFarmController) CreatePen(ctx echo.Context) error {
|
func (c *PigFarmController) CreatePen(ctx echo.Context) error {
|
||||||
|
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "CreatePen")
|
||||||
const action = "创建猪栏"
|
const action = "创建猪栏"
|
||||||
var req dto.CreatePenRequest
|
var req dto.CreatePenRequest
|
||||||
if err := ctx.Bind(&req); err != nil {
|
if err := ctx.Bind(&req); err != nil {
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体", action, "请求体绑定失败", req)
|
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体", action, "请求体绑定失败", req)
|
||||||
}
|
}
|
||||||
|
|
||||||
pen, err := c.service.CreatePen(req.PenNumber, req.HouseID, req.Capacity)
|
pen, err := c.service.CreatePen(reqCtx, req.PenNumber, req.HouseID, req.Capacity)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// 检查是否是业务逻辑错误
|
// 检查是否是业务逻辑错误
|
||||||
if errors.Is(err, service.ErrHouseNotFound) {
|
if errors.Is(err, service.ErrHouseNotFound) {
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, err.Error(), action, err.Error(), req)
|
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, err.Error(), action, err.Error(), req)
|
||||||
}
|
}
|
||||||
c.logger.Errorf("%s: 业务逻辑失败: %v", action, err)
|
logger.Errorf("%s: 业务逻辑失败: %v", action, err)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "创建猪栏失败", action, "业务逻辑失败", req)
|
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "创建猪栏失败", action, "业务逻辑失败", req)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -211,18 +218,19 @@ func (c *PigFarmController) CreatePen(ctx echo.Context) error {
|
|||||||
// @Success 200 {object} controller.Response{data=dto.PenResponse} "获取成功"
|
// @Success 200 {object} controller.Response{data=dto.PenResponse} "获取成功"
|
||||||
// @Router /api/v1/pens/{id} [get]
|
// @Router /api/v1/pens/{id} [get]
|
||||||
func (c *PigFarmController) GetPen(ctx echo.Context) error {
|
func (c *PigFarmController) GetPen(ctx echo.Context) error {
|
||||||
|
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "GetPen")
|
||||||
const action = "获取猪栏"
|
const action = "获取猪栏"
|
||||||
id, err := strconv.ParseUint(ctx.Param("id"), 10, 32)
|
id, err := strconv.ParseUint(ctx.Param("id"), 10, 32)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的ID格式", action, "ID格式错误", ctx.Param("id"))
|
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的ID格式", action, "ID格式错误", ctx.Param("id"))
|
||||||
}
|
}
|
||||||
|
|
||||||
pen, err := c.service.GetPenByID(uint(id))
|
pen, err := c.service.GetPenByID(reqCtx, uint(id))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, service.ErrPenNotFound) {
|
if errors.Is(err, service.ErrPenNotFound) {
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "猪栏不存在", action, "猪栏不存在", id)
|
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "猪栏不存在", action, "猪栏不存在", id)
|
||||||
}
|
}
|
||||||
c.logger.Errorf("%s: 业务逻辑失败: %v", action, err)
|
logger.Errorf("%s: 业务逻辑失败: %v", action, err)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取猪栏失败", action, "业务逻辑失败", id)
|
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取猪栏失败", action, "业务逻辑失败", id)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -238,10 +246,11 @@ func (c *PigFarmController) GetPen(ctx echo.Context) error {
|
|||||||
// @Success 200 {object} controller.Response{data=[]dto.PenResponse} "获取成功"
|
// @Success 200 {object} controller.Response{data=[]dto.PenResponse} "获取成功"
|
||||||
// @Router /api/v1/pens [get]
|
// @Router /api/v1/pens [get]
|
||||||
func (c *PigFarmController) ListPens(ctx echo.Context) error {
|
func (c *PigFarmController) ListPens(ctx echo.Context) error {
|
||||||
|
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "ListPens")
|
||||||
const action = "获取猪栏列表"
|
const action = "获取猪栏列表"
|
||||||
pens, err := c.service.ListPens()
|
pens, err := c.service.ListPens(reqCtx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.logger.Errorf("%s: 业务逻辑失败: %v", action, err)
|
logger.Errorf("%s: 业务逻辑失败: %v", action, err)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取列表失败", action, "业务逻辑失败", nil)
|
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取列表失败", action, "业务逻辑失败", nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -260,6 +269,7 @@ func (c *PigFarmController) ListPens(ctx echo.Context) error {
|
|||||||
// @Success 200 {object} controller.Response{data=dto.PenResponse} "更新成功"
|
// @Success 200 {object} controller.Response{data=dto.PenResponse} "更新成功"
|
||||||
// @Router /api/v1/pens/{id} [put]
|
// @Router /api/v1/pens/{id} [put]
|
||||||
func (c *PigFarmController) UpdatePen(ctx echo.Context) error {
|
func (c *PigFarmController) UpdatePen(ctx echo.Context) error {
|
||||||
|
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "UpdatePen")
|
||||||
const action = "更新猪栏"
|
const action = "更新猪栏"
|
||||||
id, err := strconv.ParseUint(ctx.Param("id"), 10, 32)
|
id, err := strconv.ParseUint(ctx.Param("id"), 10, 32)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -271,13 +281,13 @@ func (c *PigFarmController) UpdatePen(ctx echo.Context) error {
|
|||||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体", action, "请求体绑定失败", req)
|
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体", action, "请求体绑定失败", req)
|
||||||
}
|
}
|
||||||
|
|
||||||
pen, err := c.service.UpdatePen(uint(id), req.PenNumber, req.HouseID, req.Capacity, req.Status)
|
pen, err := c.service.UpdatePen(reqCtx, uint(id), req.PenNumber, req.HouseID, req.Capacity, req.Status)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, service.ErrPenNotFound) {
|
if errors.Is(err, service.ErrPenNotFound) {
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "猪栏不存在", action, "猪栏不存在", id)
|
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "猪栏不存在", action, "猪栏不存在", id)
|
||||||
}
|
}
|
||||||
// 其他业务逻辑错误可以在这里添加处理
|
// 其他业务逻辑错误可以在这里添加处理
|
||||||
c.logger.Errorf("%s: 业务逻辑失败: %v", action, err)
|
logger.Errorf("%s: 业务逻辑失败: %v", action, err)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "更新失败", action, "业务逻辑失败", req)
|
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "更新失败", action, "业务逻辑失败", req)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -294,13 +304,14 @@ func (c *PigFarmController) UpdatePen(ctx echo.Context) error {
|
|||||||
// @Success 200 {object} controller.Response "删除成功"
|
// @Success 200 {object} controller.Response "删除成功"
|
||||||
// @Router /api/v1/pens/{id} [delete]
|
// @Router /api/v1/pens/{id} [delete]
|
||||||
func (c *PigFarmController) DeletePen(ctx echo.Context) error {
|
func (c *PigFarmController) DeletePen(ctx echo.Context) error {
|
||||||
|
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "DeletePen")
|
||||||
const action = "删除猪栏"
|
const action = "删除猪栏"
|
||||||
id, err := strconv.ParseUint(ctx.Param("id"), 10, 32)
|
id, err := strconv.ParseUint(ctx.Param("id"), 10, 32)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的ID格式", action, "ID格式错误", ctx.Param("id"))
|
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的ID格式", action, "ID格式错误", ctx.Param("id"))
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := c.service.DeletePen(uint(id)); err != nil {
|
if err := c.service.DeletePen(reqCtx, uint(id)); err != nil {
|
||||||
if errors.Is(err, service.ErrPenNotFound) {
|
if errors.Is(err, service.ErrPenNotFound) {
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "猪栏不存在", action, "猪栏不存在", id)
|
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "猪栏不存在", action, "猪栏不存在", id)
|
||||||
}
|
}
|
||||||
@@ -308,7 +319,7 @@ func (c *PigFarmController) DeletePen(ctx echo.Context) error {
|
|||||||
if errors.Is(err, service.ErrPenInUse) {
|
if errors.Is(err, service.ErrPenInUse) {
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeConflict, err.Error(), action, err.Error(), id)
|
return controller.SendErrorWithAudit(ctx, controller.CodeConflict, err.Error(), action, err.Error(), id)
|
||||||
}
|
}
|
||||||
c.logger.Errorf("%s: 业务逻辑失败: %v", action, err)
|
logger.Errorf("%s: 业务逻辑失败: %v", action, err)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "删除失败", action, "业务逻辑失败", id)
|
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "删除失败", action, "业务逻辑失败", id)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -327,6 +338,7 @@ func (c *PigFarmController) DeletePen(ctx echo.Context) error {
|
|||||||
// @Success 200 {object} controller.Response{data=dto.PenResponse} "更新成功"
|
// @Success 200 {object} controller.Response{data=dto.PenResponse} "更新成功"
|
||||||
// @Router /api/v1/pens/{id}/status [put]
|
// @Router /api/v1/pens/{id}/status [put]
|
||||||
func (c *PigFarmController) UpdatePenStatus(ctx echo.Context) error {
|
func (c *PigFarmController) UpdatePenStatus(ctx echo.Context) error {
|
||||||
|
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "UpdatePenStatus")
|
||||||
const action = "更新猪栏状态"
|
const action = "更新猪栏状态"
|
||||||
id, err := strconv.ParseUint(ctx.Param("id"), 10, 32)
|
id, err := strconv.ParseUint(ctx.Param("id"), 10, 32)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -338,14 +350,14 @@ func (c *PigFarmController) UpdatePenStatus(ctx echo.Context) error {
|
|||||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体", action, "请求体绑定失败", req)
|
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体", action, "请求体绑定失败", req)
|
||||||
}
|
}
|
||||||
|
|
||||||
pen, err := c.service.UpdatePenStatus(uint(id), req.Status)
|
pen, err := c.service.UpdatePenStatus(reqCtx, uint(id), req.Status)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, service.ErrPenNotFound) {
|
if errors.Is(err, service.ErrPenNotFound) {
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, err.Error(), action, err.Error(), id)
|
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, err.Error(), action, err.Error(), id)
|
||||||
} else if errors.Is(err, service.ErrPenStatusInvalidForOccupiedPen) || errors.Is(err, service.ErrPenStatusInvalidForUnoccupiedPen) {
|
} else if errors.Is(err, service.ErrPenStatusInvalidForOccupiedPen) || errors.Is(err, service.ErrPenStatusInvalidForUnoccupiedPen) {
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeConflict, err.Error(), action, err.Error(), id)
|
return controller.SendErrorWithAudit(ctx, controller.CodeConflict, err.Error(), action, err.Error(), id)
|
||||||
}
|
}
|
||||||
c.logger.Errorf("%s: 业务逻辑失败: %v", action, err)
|
logger.Errorf("%s: 业务逻辑失败: %v", action, err)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "更新猪栏状态失败", action, err.Error(), id)
|
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "更新猪栏状态失败", action, err.Error(), id)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package monitor
|
package monitor
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
|
|
||||||
"git.huangwc.com/pig/pig-farm-controller/internal/app/controller"
|
"git.huangwc.com/pig/pig-farm-controller/internal/app/controller"
|
||||||
@@ -13,15 +14,15 @@ import (
|
|||||||
|
|
||||||
// Controller 监控控制器,封装了所有与数据监控相关的业务逻辑
|
// Controller 监控控制器,封装了所有与数据监控相关的业务逻辑
|
||||||
type Controller struct {
|
type Controller struct {
|
||||||
|
ctx context.Context
|
||||||
monitorService service.MonitorService
|
monitorService service.MonitorService
|
||||||
logger *logs.Logger
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewController 创建一个新的监控控制器实例
|
// NewController 创建一个新的监控控制器实例
|
||||||
func NewController(monitorService service.MonitorService, logger *logs.Logger) *Controller {
|
func NewController(ctx context.Context, monitorService service.MonitorService) *Controller {
|
||||||
return &Controller{
|
return &Controller{
|
||||||
|
ctx: ctx,
|
||||||
monitorService: monitorService,
|
monitorService: monitorService,
|
||||||
logger: logger,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -35,26 +36,27 @@ func NewController(monitorService service.MonitorService, logger *logs.Logger) *
|
|||||||
// @Success 200 {object} controller.Response{data=dto.ListSensorDataResponse}
|
// @Success 200 {object} controller.Response{data=dto.ListSensorDataResponse}
|
||||||
// @Router /api/v1/monitor/sensor-data [get]
|
// @Router /api/v1/monitor/sensor-data [get]
|
||||||
func (c *Controller) ListSensorData(ctx echo.Context) error {
|
func (c *Controller) ListSensorData(ctx echo.Context) error {
|
||||||
|
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "ListSensorData")
|
||||||
const actionType = "获取传感器数据列表"
|
const actionType = "获取传感器数据列表"
|
||||||
|
|
||||||
var req dto.ListSensorDataRequest
|
var req dto.ListSensorDataRequest
|
||||||
if err := ctx.Bind(&req); err != nil {
|
if err := ctx.Bind(&req); err != nil {
|
||||||
c.logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
|
logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的查询参数: "+err.Error(), actionType, "参数绑定失败", req)
|
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的查询参数: "+err.Error(), actionType, "参数绑定失败", req)
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := c.monitorService.ListSensorData(&req)
|
resp, err := c.monitorService.ListSensorData(reqCtx, &req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, repository.ErrInvalidPagination) {
|
if errors.Is(err, repository.ErrInvalidPagination) {
|
||||||
c.logger.Warnf("%s: 无效的分页参数: %v", actionType, err)
|
logger.Warnf("%s: 无效的分页参数: %v", actionType, err)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的分页参数: "+err.Error(), actionType, "无效分页参数", req)
|
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的分页参数: "+err.Error(), actionType, "无效分页参数", req)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.logger.Errorf("%s: 服务层查询失败: %v", actionType, err)
|
logger.Errorf("%s: 服务层查询失败: %v", actionType, err)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取传感器数据失败: "+err.Error(), actionType, "服务层查询失败", req)
|
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取传感器数据失败: "+err.Error(), actionType, "服务层查询失败", req)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.logger.Infof("%s: 成功, 获取到 %d 条记录, 总计 %d 条", actionType, len(resp.List), resp.Pagination.Total)
|
logger.Infof("%s: 成功, 获取到 %d 条记录, 总计 %d 条", actionType, len(resp.List), resp.Pagination.Total)
|
||||||
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取传感器数据成功", resp, actionType, "获取传感器数据成功", req)
|
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取传感器数据成功", resp, actionType, "获取传感器数据成功", req)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -68,26 +70,27 @@ func (c *Controller) ListSensorData(ctx echo.Context) error {
|
|||||||
// @Success 200 {object} controller.Response{data=dto.ListDeviceCommandLogResponse}
|
// @Success 200 {object} controller.Response{data=dto.ListDeviceCommandLogResponse}
|
||||||
// @Router /api/v1/monitor/device-command-logs [get]
|
// @Router /api/v1/monitor/device-command-logs [get]
|
||||||
func (c *Controller) ListDeviceCommandLogs(ctx echo.Context) error {
|
func (c *Controller) ListDeviceCommandLogs(ctx echo.Context) error {
|
||||||
|
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "ListDeviceCommandLogs")
|
||||||
const actionType = "获取设备命令日志列表"
|
const actionType = "获取设备命令日志列表"
|
||||||
|
|
||||||
var req dto.ListDeviceCommandLogRequest
|
var req dto.ListDeviceCommandLogRequest
|
||||||
if err := ctx.Bind(&req); err != nil {
|
if err := ctx.Bind(&req); err != nil {
|
||||||
c.logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
|
logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的查询参数: "+err.Error(), actionType, "参数绑定失败", req)
|
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的查询参数: "+err.Error(), actionType, "参数绑定失败", req)
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := c.monitorService.ListDeviceCommandLogs(&req)
|
resp, err := c.monitorService.ListDeviceCommandLogs(reqCtx, &req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, repository.ErrInvalidPagination) {
|
if errors.Is(err, repository.ErrInvalidPagination) {
|
||||||
c.logger.Warnf("%s: 无效的分页参数: %v", actionType, err)
|
logger.Warnf("%s: 无效的分页参数: %v", actionType, err)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的分页参数: "+err.Error(), actionType, "无效分页参数", req)
|
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的分页参数: "+err.Error(), actionType, "无效分页参数", req)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.logger.Errorf("%s: 服务层查询失败: %v", actionType, err)
|
logger.Errorf("%s: 服务层查询失败: %v", actionType, err)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取设备命令日志失败: "+err.Error(), actionType, "服务层查询失败", req)
|
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取设备命令日志失败: "+err.Error(), actionType, "服务层查询失败", req)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.logger.Infof("%s: 成功, 获取到 %d 条记录, 总计 %d 条", actionType, len(resp.List), resp.Pagination.Total)
|
logger.Infof("%s: 成功, 获取到 %d 条记录, 总计 %d 条", actionType, len(resp.List), resp.Pagination.Total)
|
||||||
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取设备命令日志成功", resp, actionType, "获取设备命令日志成功", req)
|
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取设备命令日志成功", resp, actionType, "获取设备命令日志成功", req)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -101,26 +104,27 @@ func (c *Controller) ListDeviceCommandLogs(ctx echo.Context) error {
|
|||||||
// @Success 200 {object} controller.Response{data=dto.ListPlanExecutionLogResponse}
|
// @Success 200 {object} controller.Response{data=dto.ListPlanExecutionLogResponse}
|
||||||
// @Router /api/v1/monitor/plan-execution-logs [get]
|
// @Router /api/v1/monitor/plan-execution-logs [get]
|
||||||
func (c *Controller) ListPlanExecutionLogs(ctx echo.Context) error {
|
func (c *Controller) ListPlanExecutionLogs(ctx echo.Context) error {
|
||||||
|
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "ListPlanExecutionLogs")
|
||||||
const actionType = "获取计划执行日志列表"
|
const actionType = "获取计划执行日志列表"
|
||||||
|
|
||||||
var req dto.ListPlanExecutionLogRequest
|
var req dto.ListPlanExecutionLogRequest
|
||||||
if err := ctx.Bind(&req); err != nil {
|
if err := ctx.Bind(&req); err != nil {
|
||||||
c.logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
|
logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的查询参数: "+err.Error(), actionType, "参数绑定失败", req)
|
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的查询参数: "+err.Error(), actionType, "参数绑定失败", req)
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := c.monitorService.ListPlanExecutionLogs(&req)
|
resp, err := c.monitorService.ListPlanExecutionLogs(reqCtx, &req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, repository.ErrInvalidPagination) {
|
if errors.Is(err, repository.ErrInvalidPagination) {
|
||||||
c.logger.Warnf("%s: 无效的分页参数: %v", actionType, err)
|
logger.Warnf("%s: 无效的分页参数: %v", actionType, err)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的分页参数: "+err.Error(), actionType, "无效分页参数", req)
|
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的分页参数: "+err.Error(), actionType, "无效分页参数", req)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.logger.Errorf("%s: 服务层查询失败: %v", actionType, err)
|
logger.Errorf("%s: 服务层查询失败: %v", actionType, err)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取计划执行日志失败: "+err.Error(), actionType, "服务层查询失败", req)
|
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取计划执行日志失败: "+err.Error(), actionType, "服务层查询失败", req)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.logger.Infof("%s: 成功, 获取到 %d 条记录, 总计 %d 条", actionType, len(resp.List), resp.Pagination.Total)
|
logger.Infof("%s: 成功, 获取到 %d 条记录, 总计 %d 条", actionType, len(resp.List), resp.Pagination.Total)
|
||||||
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取计划执行日志成功", resp, actionType, "获取计划执行日志成功", req)
|
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取计划执行日志成功", resp, actionType, "获取计划执行日志成功", req)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -134,26 +138,27 @@ func (c *Controller) ListPlanExecutionLogs(ctx echo.Context) error {
|
|||||||
// @Success 200 {object} controller.Response{data=dto.ListTaskExecutionLogResponse}
|
// @Success 200 {object} controller.Response{data=dto.ListTaskExecutionLogResponse}
|
||||||
// @Router /api/v1/monitor/task-execution-logs [get]
|
// @Router /api/v1/monitor/task-execution-logs [get]
|
||||||
func (c *Controller) ListTaskExecutionLogs(ctx echo.Context) error {
|
func (c *Controller) ListTaskExecutionLogs(ctx echo.Context) error {
|
||||||
|
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "ListTaskExecutionLogs")
|
||||||
const actionType = "获取任务执行日志列表"
|
const actionType = "获取任务执行日志列表"
|
||||||
|
|
||||||
var req dto.ListTaskExecutionLogRequest
|
var req dto.ListTaskExecutionLogRequest
|
||||||
if err := ctx.Bind(&req); err != nil {
|
if err := ctx.Bind(&req); err != nil {
|
||||||
c.logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
|
logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的查询参数: "+err.Error(), actionType, "参数绑定失败", req)
|
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的查询参数: "+err.Error(), actionType, "参数绑定失败", req)
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := c.monitorService.ListTaskExecutionLogs(&req)
|
resp, err := c.monitorService.ListTaskExecutionLogs(reqCtx, &req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, repository.ErrInvalidPagination) {
|
if errors.Is(err, repository.ErrInvalidPagination) {
|
||||||
c.logger.Warnf("%s: 无效的分页参数: %v", actionType, err)
|
logger.Warnf("%s: 无效的分页参数: %v", actionType, err)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的分页参数: "+err.Error(), actionType, "无效分页参数", req)
|
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的分页参数: "+err.Error(), actionType, "无效分页参数", req)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.logger.Errorf("%s: 服务层查询失败: %v", actionType, err)
|
logger.Errorf("%s: 服务层查询失败: %v", actionType, err)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取任务执行日志失败: "+err.Error(), actionType, "服务层查询失败", req)
|
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取任务执行日志失败: "+err.Error(), actionType, "服务层查询失败", req)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.logger.Infof("%s: 成功, 获取到 %d 条记录, 总计 %d 条", actionType, len(resp.List), resp.Pagination.Total)
|
logger.Infof("%s: 成功, 获取到 %d 条记录, 总计 %d 条", actionType, len(resp.List), resp.Pagination.Total)
|
||||||
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取任务执行日志成功", resp, actionType, "获取任务执行日志成功", req)
|
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取任务执行日志成功", resp, actionType, "获取任务执行日志成功", req)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -167,26 +172,27 @@ func (c *Controller) ListTaskExecutionLogs(ctx echo.Context) error {
|
|||||||
// @Success 200 {object} controller.Response{data=dto.ListPendingCollectionResponse}
|
// @Success 200 {object} controller.Response{data=dto.ListPendingCollectionResponse}
|
||||||
// @Router /api/v1/monitor/pending-collections [get]
|
// @Router /api/v1/monitor/pending-collections [get]
|
||||||
func (c *Controller) ListPendingCollections(ctx echo.Context) error {
|
func (c *Controller) ListPendingCollections(ctx echo.Context) error {
|
||||||
|
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "ListPendingCollections")
|
||||||
const actionType = "获取待采集请求列表"
|
const actionType = "获取待采集请求列表"
|
||||||
|
|
||||||
var req dto.ListPendingCollectionRequest
|
var req dto.ListPendingCollectionRequest
|
||||||
if err := ctx.Bind(&req); err != nil {
|
if err := ctx.Bind(&req); err != nil {
|
||||||
c.logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
|
logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的查询参数: "+err.Error(), actionType, "参数绑定失败", req)
|
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的查询参数: "+err.Error(), actionType, "参数绑定失败", req)
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := c.monitorService.ListPendingCollections(&req)
|
resp, err := c.monitorService.ListPendingCollections(reqCtx, &req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, repository.ErrInvalidPagination) {
|
if errors.Is(err, repository.ErrInvalidPagination) {
|
||||||
c.logger.Warnf("%s: 无效的分页参数: %v", actionType, err)
|
logger.Warnf("%s: 无效的分页参数: %v", actionType, err)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的分页参数: "+err.Error(), actionType, "无效分页参数", req)
|
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的分页参数: "+err.Error(), actionType, "无效分页参数", req)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.logger.Errorf("%s: 服务层查询失败: %v", actionType, err)
|
logger.Errorf("%s: 服务层查询失败: %v", actionType, err)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取待采集请求失败: "+err.Error(), actionType, "服务层查询失败", req)
|
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取待采集请求失败: "+err.Error(), actionType, "服务层查询失败", req)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.logger.Infof("%s: 成功, 获取到 %d 条记录, 总计 %d 条", actionType, len(resp.List), resp.Pagination.Total)
|
logger.Infof("%s: 成功, 获取到 %d 条记录, 总计 %d 条", actionType, len(resp.List), resp.Pagination.Total)
|
||||||
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取待采集请求成功", resp, actionType, "获取待采集请求成功", req)
|
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取待采集请求成功", resp, actionType, "获取待采集请求成功", req)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -200,26 +206,27 @@ func (c *Controller) ListPendingCollections(ctx echo.Context) error {
|
|||||||
// @Success 200 {object} controller.Response{data=dto.ListUserActionLogResponse}
|
// @Success 200 {object} controller.Response{data=dto.ListUserActionLogResponse}
|
||||||
// @Router /api/v1/monitor/user-action-logs [get]
|
// @Router /api/v1/monitor/user-action-logs [get]
|
||||||
func (c *Controller) ListUserActionLogs(ctx echo.Context) error {
|
func (c *Controller) ListUserActionLogs(ctx echo.Context) error {
|
||||||
|
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "ListUserActionLogs")
|
||||||
const actionType = "获取用户操作日志列表"
|
const actionType = "获取用户操作日志列表"
|
||||||
|
|
||||||
var req dto.ListUserActionLogRequest
|
var req dto.ListUserActionLogRequest
|
||||||
if err := ctx.Bind(&req); err != nil {
|
if err := ctx.Bind(&req); err != nil {
|
||||||
c.logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
|
logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的查询参数: "+err.Error(), actionType, "参数绑定失败", req)
|
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的查询参数: "+err.Error(), actionType, "参数绑定失败", req)
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := c.monitorService.ListUserActionLogs(&req)
|
resp, err := c.monitorService.ListUserActionLogs(reqCtx, &req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, repository.ErrInvalidPagination) {
|
if errors.Is(err, repository.ErrInvalidPagination) {
|
||||||
c.logger.Warnf("%s: 无效的分页参数: %v", actionType, err)
|
logger.Warnf("%s: 无效的分页参数: %v", actionType, err)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的分页参数: "+err.Error(), actionType, "无效分页参数", req)
|
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的分页参数: "+err.Error(), actionType, "无效分页参数", req)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.logger.Errorf("%s: 服务层查询失败: %v", actionType, err)
|
logger.Errorf("%s: 服务层查询失败: %v", actionType, err)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取用户操作日志失败: "+err.Error(), actionType, "服务层查询失败", req)
|
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取用户操作日志失败: "+err.Error(), actionType, "服务层查询失败", req)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.logger.Infof("%s: 成功, 获取到 %d 条记录, 总计 %d 条", actionType, len(resp.List), resp.Pagination.Total)
|
logger.Infof("%s: 成功, 获取到 %d 条记录, 总计 %d 条", actionType, len(resp.List), resp.Pagination.Total)
|
||||||
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取用户操作日志成功", resp, actionType, "获取用户操作日志成功", req)
|
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取用户操作日志成功", resp, actionType, "获取用户操作日志成功", req)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -233,26 +240,27 @@ func (c *Controller) ListUserActionLogs(ctx echo.Context) error {
|
|||||||
// @Success 200 {object} controller.Response{data=dto.ListRawMaterialPurchaseResponse}
|
// @Success 200 {object} controller.Response{data=dto.ListRawMaterialPurchaseResponse}
|
||||||
// @Router /api/v1/monitor/raw-material-purchases [get]
|
// @Router /api/v1/monitor/raw-material-purchases [get]
|
||||||
func (c *Controller) ListRawMaterialPurchases(ctx echo.Context) error {
|
func (c *Controller) ListRawMaterialPurchases(ctx echo.Context) error {
|
||||||
|
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "ListRawMaterialPurchases")
|
||||||
const actionType = "获取原料采购记录列表"
|
const actionType = "获取原料采购记录列表"
|
||||||
|
|
||||||
var req dto.ListRawMaterialPurchaseRequest
|
var req dto.ListRawMaterialPurchaseRequest
|
||||||
if err := ctx.Bind(&req); err != nil {
|
if err := ctx.Bind(&req); err != nil {
|
||||||
c.logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
|
logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的查询参数: "+err.Error(), actionType, "参数绑定失败", req)
|
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的查询参数: "+err.Error(), actionType, "参数绑定失败", req)
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := c.monitorService.ListRawMaterialPurchases(&req)
|
resp, err := c.monitorService.ListRawMaterialPurchases(reqCtx, &req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, repository.ErrInvalidPagination) {
|
if errors.Is(err, repository.ErrInvalidPagination) {
|
||||||
c.logger.Warnf("%s: 无效的分页参数: %v", actionType, err)
|
logger.Warnf("%s: 无效的分页参数: %v", actionType, err)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的分页参数: "+err.Error(), actionType, "无效分页参数", req)
|
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的分页参数: "+err.Error(), actionType, "无效分页参数", req)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.logger.Errorf("%s: 服务层查询失败: %v", actionType, err)
|
logger.Errorf("%s: 服务层查询失败: %v", actionType, err)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取原料采购记录失败: "+err.Error(), actionType, "服务层查询失败", req)
|
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取原料采购记录失败: "+err.Error(), actionType, "服务层查询失败", req)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.logger.Infof("%s: 成功, 获取到 %d 条记录, 总计 %d 条", actionType, len(resp.List), resp.Pagination.Total)
|
logger.Infof("%s: 成功, 获取到 %d 条记录, 总计 %d 条", actionType, len(resp.List), resp.Pagination.Total)
|
||||||
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取原料采购记录成功", resp, actionType, "获取原料采购记录成功", req)
|
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取原料采购记录成功", resp, actionType, "获取原料采购记录成功", req)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -266,26 +274,27 @@ func (c *Controller) ListRawMaterialPurchases(ctx echo.Context) error {
|
|||||||
// @Success 200 {object} controller.Response{data=dto.ListRawMaterialStockLogResponse}
|
// @Success 200 {object} controller.Response{data=dto.ListRawMaterialStockLogResponse}
|
||||||
// @Router /api/v1/monitor/raw-material-stock-logs [get]
|
// @Router /api/v1/monitor/raw-material-stock-logs [get]
|
||||||
func (c *Controller) ListRawMaterialStockLogs(ctx echo.Context) error {
|
func (c *Controller) ListRawMaterialStockLogs(ctx echo.Context) error {
|
||||||
|
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "ListRawMaterialStockLogs")
|
||||||
const actionType = "获取原料库存日志列表"
|
const actionType = "获取原料库存日志列表"
|
||||||
|
|
||||||
var req dto.ListRawMaterialStockLogRequest
|
var req dto.ListRawMaterialStockLogRequest
|
||||||
if err := ctx.Bind(&req); err != nil {
|
if err := ctx.Bind(&req); err != nil {
|
||||||
c.logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
|
logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的查询参数: "+err.Error(), actionType, "参数绑定失败", req)
|
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的查询参数: "+err.Error(), actionType, "参数绑定失败", req)
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := c.monitorService.ListRawMaterialStockLogs(&req)
|
resp, err := c.monitorService.ListRawMaterialStockLogs(reqCtx, &req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, repository.ErrInvalidPagination) {
|
if errors.Is(err, repository.ErrInvalidPagination) {
|
||||||
c.logger.Warnf("%s: 无效的分页参数: %v", actionType, err)
|
logger.Warnf("%s: 无效的分页参数: %v", actionType, err)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的分页参数: "+err.Error(), actionType, "无效分页参数", req)
|
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的分页参数: "+err.Error(), actionType, "无效分页参数", req)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.logger.Errorf("%s: 服务层查询失败: %v", actionType, err)
|
logger.Errorf("%s: 服务层查询失败: %v", actionType, err)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取原料库存日志失败: "+err.Error(), actionType, "服务层查询失败", req)
|
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取原料库存日志失败: "+err.Error(), actionType, "服务层查询失败", req)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.logger.Infof("%s: 成功, 获取到 %d 条记录, 总计 %d 条", actionType, len(resp.List), resp.Pagination.Total)
|
logger.Infof("%s: 成功, 获取到 %d 条记录, 总计 %d 条", actionType, len(resp.List), resp.Pagination.Total)
|
||||||
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取原料库存日志成功", resp, actionType, "获取原料库存日志成功", req)
|
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取原料库存日志成功", resp, actionType, "获取原料库存日志成功", req)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -299,26 +308,27 @@ func (c *Controller) ListRawMaterialStockLogs(ctx echo.Context) error {
|
|||||||
// @Success 200 {object} controller.Response{data=dto.ListFeedUsageRecordResponse}
|
// @Success 200 {object} controller.Response{data=dto.ListFeedUsageRecordResponse}
|
||||||
// @Router /api/v1/monitor/feed-usage-records [get]
|
// @Router /api/v1/monitor/feed-usage-records [get]
|
||||||
func (c *Controller) ListFeedUsageRecords(ctx echo.Context) error {
|
func (c *Controller) ListFeedUsageRecords(ctx echo.Context) error {
|
||||||
|
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "ListFeedUsageRecords")
|
||||||
const actionType = "获取饲料使用记录列表"
|
const actionType = "获取饲料使用记录列表"
|
||||||
|
|
||||||
var req dto.ListFeedUsageRecordRequest
|
var req dto.ListFeedUsageRecordRequest
|
||||||
if err := ctx.Bind(&req); err != nil {
|
if err := ctx.Bind(&req); err != nil {
|
||||||
c.logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
|
logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的查询参数: "+err.Error(), actionType, "参数绑定失败", req)
|
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的查询参数: "+err.Error(), actionType, "参数绑定失败", req)
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := c.monitorService.ListFeedUsageRecords(&req)
|
resp, err := c.monitorService.ListFeedUsageRecords(reqCtx, &req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, repository.ErrInvalidPagination) {
|
if errors.Is(err, repository.ErrInvalidPagination) {
|
||||||
c.logger.Warnf("%s: 无效的分页参数: %v", actionType, err)
|
logger.Warnf("%s: 无效的分页参数: %v", actionType, err)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的分页参数: "+err.Error(), actionType, "无效分页参数", req)
|
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的分页参数: "+err.Error(), actionType, "无效分页参数", req)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.logger.Errorf("%s: 服务层查询失败: %v", actionType, err)
|
logger.Errorf("%s: 服务层查询失败: %v", actionType, err)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取饲料使用记录失败: "+err.Error(), actionType, "服务层查询失败", req)
|
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取饲料使用记录失败: "+err.Error(), actionType, "服务层查询失败", req)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.logger.Infof("%s: 成功, 获取到 %d 条记录, 总计 %d 条", actionType, len(resp.List), resp.Pagination.Total)
|
logger.Infof("%s: 成功, 获取到 %d 条记录, 总计 %d 条", actionType, len(resp.List), resp.Pagination.Total)
|
||||||
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取饲料使用记录成功", resp, actionType, "获取饲料使用记录成功", req)
|
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取饲料使用记录成功", resp, actionType, "获取饲料使用记录成功", req)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -332,26 +342,27 @@ func (c *Controller) ListFeedUsageRecords(ctx echo.Context) error {
|
|||||||
// @Success 200 {object} controller.Response{data=dto.ListMedicationLogResponse}
|
// @Success 200 {object} controller.Response{data=dto.ListMedicationLogResponse}
|
||||||
// @Router /api/v1/monitor/medication-logs [get]
|
// @Router /api/v1/monitor/medication-logs [get]
|
||||||
func (c *Controller) ListMedicationLogs(ctx echo.Context) error {
|
func (c *Controller) ListMedicationLogs(ctx echo.Context) error {
|
||||||
|
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "ListMedicationLogs")
|
||||||
const actionType = "获取用药记录列表"
|
const actionType = "获取用药记录列表"
|
||||||
|
|
||||||
var req dto.ListMedicationLogRequest
|
var req dto.ListMedicationLogRequest
|
||||||
if err := ctx.Bind(&req); err != nil {
|
if err := ctx.Bind(&req); err != nil {
|
||||||
c.logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
|
logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的查询参数: "+err.Error(), actionType, "参数绑定失败", req)
|
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的查询参数: "+err.Error(), actionType, "参数绑定失败", req)
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := c.monitorService.ListMedicationLogs(&req)
|
resp, err := c.monitorService.ListMedicationLogs(reqCtx, &req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, repository.ErrInvalidPagination) {
|
if errors.Is(err, repository.ErrInvalidPagination) {
|
||||||
c.logger.Warnf("%s: 无效的分页参数: %v", actionType, err)
|
logger.Warnf("%s: 无效的分页参数: %v", actionType, err)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的分页参数: "+err.Error(), actionType, "无效分页参数", req)
|
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的分页参数: "+err.Error(), actionType, "无效分页参数", req)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.logger.Errorf("%s: 服务层查询失败: %v", actionType, err)
|
logger.Errorf("%s: 服务层查询失败: %v", actionType, err)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取用药记录失败: "+err.Error(), actionType, "服务层查询失败", req)
|
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取用药记录失败: "+err.Error(), actionType, "服务层查询失败", req)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.logger.Infof("%s: 成功, 获取到 %d 条记录, 总计 %d 条", actionType, len(resp.List), resp.Pagination.Total)
|
logger.Infof("%s: 成功, 获取到 %d 条记录, 总计 %d 条", actionType, len(resp.List), resp.Pagination.Total)
|
||||||
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取用药记录成功", resp, actionType, "获取用药记录成功", req)
|
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取用药记录成功", resp, actionType, "获取用药记录成功", req)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -365,26 +376,27 @@ func (c *Controller) ListMedicationLogs(ctx echo.Context) error {
|
|||||||
// @Success 200 {object} controller.Response{data=dto.ListPigBatchLogResponse}
|
// @Success 200 {object} controller.Response{data=dto.ListPigBatchLogResponse}
|
||||||
// @Router /api/v1/monitor/pig-batch-logs [get]
|
// @Router /api/v1/monitor/pig-batch-logs [get]
|
||||||
func (c *Controller) ListPigBatchLogs(ctx echo.Context) error {
|
func (c *Controller) ListPigBatchLogs(ctx echo.Context) error {
|
||||||
|
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "ListPigBatchLogs")
|
||||||
const actionType = "获取猪批次日志列表"
|
const actionType = "获取猪批次日志列表"
|
||||||
|
|
||||||
var req dto.ListPigBatchLogRequest
|
var req dto.ListPigBatchLogRequest
|
||||||
if err := ctx.Bind(&req); err != nil {
|
if err := ctx.Bind(&req); err != nil {
|
||||||
c.logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
|
logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的查询参数: "+err.Error(), actionType, "参数绑定失败", req)
|
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的查询参数: "+err.Error(), actionType, "参数绑定失败", req)
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := c.monitorService.ListPigBatchLogs(&req)
|
resp, err := c.monitorService.ListPigBatchLogs(reqCtx, &req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, repository.ErrInvalidPagination) {
|
if errors.Is(err, repository.ErrInvalidPagination) {
|
||||||
c.logger.Warnf("%s: 无效的分页参数: %v", actionType, err)
|
logger.Warnf("%s: 无效的分页参数: %v", actionType, err)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的分页参数: "+err.Error(), actionType, "无效分页参数", req)
|
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的分页参数: "+err.Error(), actionType, "无效分页参数", req)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.logger.Errorf("%s: 服务层查询失败: %v", actionType, err)
|
logger.Errorf("%s: 服务层查询失败: %v", actionType, err)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取猪批次日志失败: "+err.Error(), actionType, "服务层查询失败", req)
|
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取猪批次日志失败: "+err.Error(), actionType, "服务层查询失败", req)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.logger.Infof("%s: 成功, 获取到 %d 条记录, 总计 %d 条", actionType, len(resp.List), resp.Pagination.Total)
|
logger.Infof("%s: 成功, 获取到 %d 条记录, 总计 %d 条", actionType, len(resp.List), resp.Pagination.Total)
|
||||||
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取猪批次日志成功", resp, actionType, "获取猪批次日志成功", req)
|
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取猪批次日志成功", resp, actionType, "获取猪批次日志成功", req)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -398,26 +410,27 @@ func (c *Controller) ListPigBatchLogs(ctx echo.Context) error {
|
|||||||
// @Success 200 {object} controller.Response{data=dto.ListWeighingBatchResponse}
|
// @Success 200 {object} controller.Response{data=dto.ListWeighingBatchResponse}
|
||||||
// @Router /api/v1/monitor/weighing-batches [get]
|
// @Router /api/v1/monitor/weighing-batches [get]
|
||||||
func (c *Controller) ListWeighingBatches(ctx echo.Context) error {
|
func (c *Controller) ListWeighingBatches(ctx echo.Context) error {
|
||||||
|
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "ListWeighingBatches")
|
||||||
const actionType = "获取批次称重记录列表"
|
const actionType = "获取批次称重记录列表"
|
||||||
|
|
||||||
var req dto.ListWeighingBatchRequest
|
var req dto.ListWeighingBatchRequest
|
||||||
if err := ctx.Bind(&req); err != nil {
|
if err := ctx.Bind(&req); err != nil {
|
||||||
c.logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
|
logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的查询参数: "+err.Error(), actionType, "参数绑定失败", req)
|
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的查询参数: "+err.Error(), actionType, "参数绑定失败", req)
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := c.monitorService.ListWeighingBatches(&req)
|
resp, err := c.monitorService.ListWeighingBatches(reqCtx, &req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, repository.ErrInvalidPagination) {
|
if errors.Is(err, repository.ErrInvalidPagination) {
|
||||||
c.logger.Warnf("%s: 无效的分页参数: %v", actionType, err)
|
logger.Warnf("%s: 无效的分页参数: %v", actionType, err)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的分页参数: "+err.Error(), actionType, "无效分页参数", req)
|
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的分页参数: "+err.Error(), actionType, "无效分页参数", req)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.logger.Errorf("%s: 服务层查询失败: %v", actionType, err)
|
logger.Errorf("%s: 服务层查询失败: %v", actionType, err)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取批次称重记录失败: "+err.Error(), actionType, "服务层查询失败", req)
|
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取批次称重记录失败: "+err.Error(), actionType, "服务层查询失败", req)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.logger.Infof("%s: 成功, 获取到 %d 条记录, 总计 %d 条", actionType, len(resp.List), resp.Pagination.Total)
|
logger.Infof("%s: 成功, 获取到 %d 条记录, 总计 %d 条", actionType, len(resp.List), resp.Pagination.Total)
|
||||||
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取批次称重记录成功", resp, actionType, "获取批次称重记录成功", req)
|
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取批次称重记录成功", resp, actionType, "获取批次称重记录成功", req)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -431,26 +444,27 @@ func (c *Controller) ListWeighingBatches(ctx echo.Context) error {
|
|||||||
// @Success 200 {object} controller.Response{data=dto.ListWeighingRecordResponse}
|
// @Success 200 {object} controller.Response{data=dto.ListWeighingRecordResponse}
|
||||||
// @Router /api/v1/monitor/weighing-records [get]
|
// @Router /api/v1/monitor/weighing-records [get]
|
||||||
func (c *Controller) ListWeighingRecords(ctx echo.Context) error {
|
func (c *Controller) ListWeighingRecords(ctx echo.Context) error {
|
||||||
|
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "ListWeighingRecords")
|
||||||
const actionType = "获取单次称重记录列表"
|
const actionType = "获取单次称重记录列表"
|
||||||
|
|
||||||
var req dto.ListWeighingRecordRequest
|
var req dto.ListWeighingRecordRequest
|
||||||
if err := ctx.Bind(&req); err != nil {
|
if err := ctx.Bind(&req); err != nil {
|
||||||
c.logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
|
logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的查询参数: "+err.Error(), actionType, "参数绑定失败", req)
|
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的查询参数: "+err.Error(), actionType, "参数绑定失败", req)
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := c.monitorService.ListWeighingRecords(&req)
|
resp, err := c.monitorService.ListWeighingRecords(reqCtx, &req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, repository.ErrInvalidPagination) {
|
if errors.Is(err, repository.ErrInvalidPagination) {
|
||||||
c.logger.Warnf("%s: 无效的分页参数: %v", actionType, err)
|
logger.Warnf("%s: 无效的分页参数: %v", actionType, err)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的分页参数: "+err.Error(), actionType, "无效分页参数", req)
|
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的分页参数: "+err.Error(), actionType, "无效分页参数", req)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.logger.Errorf("%s: 服务层查询失败: %v", actionType, err)
|
logger.Errorf("%s: 服务层查询失败: %v", actionType, err)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取单次称重记录失败: "+err.Error(), actionType, "服务层查询失败", req)
|
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取单次称重记录失败: "+err.Error(), actionType, "服务层查询失败", req)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.logger.Infof("%s: 成功, 获取到 %d 条记录, 总计 %d 条", actionType, len(resp.List), resp.Pagination.Total)
|
logger.Infof("%s: 成功, 获取到 %d 条记录, 总计 %d 条", actionType, len(resp.List), resp.Pagination.Total)
|
||||||
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取单次称重记录成功", resp, actionType, "获取单次称重记录成功", req)
|
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取单次称重记录成功", resp, actionType, "获取单次称重记录成功", req)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -464,26 +478,27 @@ func (c *Controller) ListWeighingRecords(ctx echo.Context) error {
|
|||||||
// @Success 200 {object} controller.Response{data=dto.ListPigTransferLogResponse}
|
// @Success 200 {object} controller.Response{data=dto.ListPigTransferLogResponse}
|
||||||
// @Router /api/v1/monitor/pig-transfer-logs [get]
|
// @Router /api/v1/monitor/pig-transfer-logs [get]
|
||||||
func (c *Controller) ListPigTransferLogs(ctx echo.Context) error {
|
func (c *Controller) ListPigTransferLogs(ctx echo.Context) error {
|
||||||
|
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "ListPigTransferLogs")
|
||||||
const actionType = "获取猪只迁移日志列表"
|
const actionType = "获取猪只迁移日志列表"
|
||||||
|
|
||||||
var req dto.ListPigTransferLogRequest
|
var req dto.ListPigTransferLogRequest
|
||||||
if err := ctx.Bind(&req); err != nil {
|
if err := ctx.Bind(&req); err != nil {
|
||||||
c.logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
|
logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的查询参数: "+err.Error(), actionType, "参数绑定失败", req)
|
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的查询参数: "+err.Error(), actionType, "参数绑定失败", req)
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := c.monitorService.ListPigTransferLogs(&req)
|
resp, err := c.monitorService.ListPigTransferLogs(reqCtx, &req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, repository.ErrInvalidPagination) {
|
if errors.Is(err, repository.ErrInvalidPagination) {
|
||||||
c.logger.Warnf("%s: 无效的分页参数: %v", actionType, err)
|
logger.Warnf("%s: 无效的分页参数: %v", actionType, err)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的分页参数: "+err.Error(), actionType, "无效分页参数", req)
|
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的分页参数: "+err.Error(), actionType, "无效分页参数", req)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.logger.Errorf("%s: 服务层查询失败: %v", actionType, err)
|
logger.Errorf("%s: 服务层查询失败: %v", actionType, err)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取猪只迁移日志失败: "+err.Error(), actionType, "服务层查询失败", req)
|
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取猪只迁移日志失败: "+err.Error(), actionType, "服务层查询失败", req)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.logger.Infof("%s: 成功, 获取到 %d 条记录, 总计 %d 条", actionType, len(resp.List), resp.Pagination.Total)
|
logger.Infof("%s: 成功, 获取到 %d 条记录, 总计 %d 条", actionType, len(resp.List), resp.Pagination.Total)
|
||||||
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取猪只迁移日志成功", resp, actionType, "获取猪只迁移日志成功", req)
|
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取猪只迁移日志成功", resp, actionType, "获取猪只迁移日志成功", req)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -497,26 +512,27 @@ func (c *Controller) ListPigTransferLogs(ctx echo.Context) error {
|
|||||||
// @Success 200 {object} controller.Response{data=dto.ListPigSickLogResponse}
|
// @Success 200 {object} controller.Response{data=dto.ListPigSickLogResponse}
|
||||||
// @Router /api/v1/monitor/pig-sick-logs [get]
|
// @Router /api/v1/monitor/pig-sick-logs [get]
|
||||||
func (c *Controller) ListPigSickLogs(ctx echo.Context) error {
|
func (c *Controller) ListPigSickLogs(ctx echo.Context) error {
|
||||||
|
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "ListPigSickLogs")
|
||||||
const actionType = "获取病猪日志列表"
|
const actionType = "获取病猪日志列表"
|
||||||
|
|
||||||
var req dto.ListPigSickLogRequest
|
var req dto.ListPigSickLogRequest
|
||||||
if err := ctx.Bind(&req); err != nil {
|
if err := ctx.Bind(&req); err != nil {
|
||||||
c.logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
|
logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的查询参数: "+err.Error(), actionType, "参数绑定失败", req)
|
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的查询参数: "+err.Error(), actionType, "参数绑定失败", req)
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := c.monitorService.ListPigSickLogs(&req)
|
resp, err := c.monitorService.ListPigSickLogs(reqCtx, &req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, repository.ErrInvalidPagination) {
|
if errors.Is(err, repository.ErrInvalidPagination) {
|
||||||
c.logger.Warnf("%s: 无效的分页参数: %v", actionType, err)
|
logger.Warnf("%s: 无效的分页参数: %v", actionType, err)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的分页参数: "+err.Error(), actionType, "无效分页参数", req)
|
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的分页参数: "+err.Error(), actionType, "无效分页参数", req)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.logger.Errorf("%s: 服务层查询失败: %v", actionType, err)
|
logger.Errorf("%s: 服务层查询失败: %v", actionType, err)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取病猪日志失败: "+err.Error(), actionType, "服务层查询失败", req)
|
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取病猪日志失败: "+err.Error(), actionType, "服务层查询失败", req)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.logger.Infof("%s: 成功, 获取到 %d 条记录, 总计 %d 条", actionType, len(resp.List), resp.Pagination.Total)
|
logger.Infof("%s: 成功, 获取到 %d 条记录, 总计 %d 条", actionType, len(resp.List), resp.Pagination.Total)
|
||||||
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取病猪日志成功", resp, actionType, "获取病猪日志成功", req)
|
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取病猪日志成功", resp, actionType, "获取病猪日志成功", req)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -530,26 +546,27 @@ func (c *Controller) ListPigSickLogs(ctx echo.Context) error {
|
|||||||
// @Success 200 {object} controller.Response{data=dto.ListPigPurchaseResponse}
|
// @Success 200 {object} controller.Response{data=dto.ListPigPurchaseResponse}
|
||||||
// @Router /api/v1/monitor/pig-purchases [get]
|
// @Router /api/v1/monitor/pig-purchases [get]
|
||||||
func (c *Controller) ListPigPurchases(ctx echo.Context) error {
|
func (c *Controller) ListPigPurchases(ctx echo.Context) error {
|
||||||
|
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "ListPigPurchases")
|
||||||
const actionType = "获取猪只采购记录列表"
|
const actionType = "获取猪只采购记录列表"
|
||||||
|
|
||||||
var req dto.ListPigPurchaseRequest
|
var req dto.ListPigPurchaseRequest
|
||||||
if err := ctx.Bind(&req); err != nil {
|
if err := ctx.Bind(&req); err != nil {
|
||||||
c.logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
|
logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的查询参数: "+err.Error(), actionType, "参数绑定失败", req)
|
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的查询参数: "+err.Error(), actionType, "参数绑定失败", req)
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := c.monitorService.ListPigPurchases(&req)
|
resp, err := c.monitorService.ListPigPurchases(reqCtx, &req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, repository.ErrInvalidPagination) {
|
if errors.Is(err, repository.ErrInvalidPagination) {
|
||||||
c.logger.Warnf("%s: 无效的分页参数: %v", actionType, err)
|
logger.Warnf("%s: 无效的分页参数: %v", actionType, err)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的分页参数: "+err.Error(), actionType, "无效分页参数", req)
|
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的分页参数: "+err.Error(), actionType, "无效分页参数", req)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.logger.Errorf("%s: 服务层查询失败: %v", actionType, err)
|
logger.Errorf("%s: 服务层查询失败: %v", actionType, err)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取猪只采购记录失败: "+err.Error(), actionType, "服务层查询失败", req)
|
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取猪只采购记录失败: "+err.Error(), actionType, "服务层查询失败", req)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.logger.Infof("%s: 成功, 获取到 %d 条记录, 总计 %d 条", actionType, len(resp.List), resp.Pagination.Total)
|
logger.Infof("%s: 成功, 获取到 %d 条记录, 总计 %d 条", actionType, len(resp.List), resp.Pagination.Total)
|
||||||
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取猪只采购记录成功", resp, actionType, "获取猪只采购记录成功", req)
|
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取猪只采购记录成功", resp, actionType, "获取猪只采购记录成功", req)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -563,26 +580,27 @@ func (c *Controller) ListPigPurchases(ctx echo.Context) error {
|
|||||||
// @Success 200 {object} controller.Response{data=dto.ListPigSaleResponse}
|
// @Success 200 {object} controller.Response{data=dto.ListPigSaleResponse}
|
||||||
// @Router /api/v1/monitor/pig-sales [get]
|
// @Router /api/v1/monitor/pig-sales [get]
|
||||||
func (c *Controller) ListPigSales(ctx echo.Context) error {
|
func (c *Controller) ListPigSales(ctx echo.Context) error {
|
||||||
|
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "ListPigSales")
|
||||||
const actionType = "获取猪只售卖记录列表"
|
const actionType = "获取猪只售卖记录列表"
|
||||||
|
|
||||||
var req dto.ListPigSaleRequest
|
var req dto.ListPigSaleRequest
|
||||||
if err := ctx.Bind(&req); err != nil {
|
if err := ctx.Bind(&req); err != nil {
|
||||||
c.logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
|
logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的查询参数: "+err.Error(), actionType, "参数绑定失败", req)
|
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的查询参数: "+err.Error(), actionType, "参数绑定失败", req)
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := c.monitorService.ListPigSales(&req)
|
resp, err := c.monitorService.ListPigSales(reqCtx, &req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, repository.ErrInvalidPagination) {
|
if errors.Is(err, repository.ErrInvalidPagination) {
|
||||||
c.logger.Warnf("%s: 无效的分页参数: %v", actionType, err)
|
logger.Warnf("%s: 无效的分页参数: %v", actionType, err)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的分页参数: "+err.Error(), actionType, "无效分页参数", req)
|
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的分页参数: "+err.Error(), actionType, "无效分页参数", req)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.logger.Errorf("%s: 服务层查询失败: %v", actionType, err)
|
logger.Errorf("%s: 服务层查询失败: %v", actionType, err)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取猪只售卖记录失败: "+err.Error(), actionType, "服务层查询失败", req)
|
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取猪只售卖记录失败: "+err.Error(), actionType, "服务层查询失败", req)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.logger.Infof("%s: 成功, 获取到 %d 条记录, 总计 %d 条", actionType, len(resp.List), resp.Pagination.Total)
|
logger.Infof("%s: 成功, 获取到 %d 条记录, 总计 %d 条", actionType, len(resp.List), resp.Pagination.Total)
|
||||||
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取猪只售卖记录成功", resp, actionType, "获取猪只售卖记录成功", req)
|
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取猪只售卖记录成功", resp, actionType, "获取猪只售卖记录成功", req)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -596,25 +614,26 @@ func (c *Controller) ListPigSales(ctx echo.Context) error {
|
|||||||
// @Success 200 {object} controller.Response{data=dto.ListNotificationResponse}
|
// @Success 200 {object} controller.Response{data=dto.ListNotificationResponse}
|
||||||
// @Router /api/v1/monitor/notifications [get]
|
// @Router /api/v1/monitor/notifications [get]
|
||||||
func (c *Controller) ListNotifications(ctx echo.Context) error {
|
func (c *Controller) ListNotifications(ctx echo.Context) error {
|
||||||
|
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "ListNotifications")
|
||||||
const actionType = "批量查询通知"
|
const actionType = "批量查询通知"
|
||||||
|
|
||||||
var req dto.ListNotificationRequest
|
var req dto.ListNotificationRequest
|
||||||
if err := ctx.Bind(&req); err != nil {
|
if err := ctx.Bind(&req); err != nil {
|
||||||
c.logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
|
logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的查询参数: "+err.Error(), actionType, "参数绑定失败", req)
|
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的查询参数: "+err.Error(), actionType, "参数绑定失败", req)
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := c.monitorService.ListNotifications(&req)
|
resp, err := c.monitorService.ListNotifications(reqCtx, &req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, repository.ErrInvalidPagination) {
|
if errors.Is(err, repository.ErrInvalidPagination) {
|
||||||
c.logger.Warnf("%s: 无效的分页参数: %v", actionType, err)
|
logger.Warnf("%s: 无效的分页参数: %v", actionType, err)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的分页参数: "+err.Error(), actionType, "无效分页参数", req)
|
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的分页参数: "+err.Error(), actionType, "无效分页参数", req)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.logger.Errorf("%s: 服务层查询失败: %v", actionType, err)
|
logger.Errorf("%s: 服务层查询失败: %v", actionType, err)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "批量查询通知失败: "+err.Error(), actionType, "服务层查询失败", req)
|
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "批量查询通知失败: "+err.Error(), actionType, "服务层查询失败", req)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.logger.Infof("%s: 成功, 获取到 %d 条记录, 总计 %d 条", actionType, len(resp.List), resp.Pagination.Total)
|
logger.Infof("%s: 成功, 获取到 %d 条记录, 总计 %d 条", actionType, len(resp.List), resp.Pagination.Total)
|
||||||
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "批量查询通知成功", resp, actionType, "批量查询通知成功", req)
|
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "批量查询通知成功", resp, actionType, "批量查询通知成功", req)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package plan
|
package plan
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
@@ -16,14 +17,14 @@ import (
|
|||||||
|
|
||||||
// Controller 定义了计划相关的控制器
|
// Controller 定义了计划相关的控制器
|
||||||
type Controller struct {
|
type Controller struct {
|
||||||
logger *logs.Logger
|
ctx context.Context
|
||||||
planService service.PlanService
|
planService service.PlanService
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewController 创建一个新的 Controller 实例
|
// NewController 创建一个新的 Controller 实例
|
||||||
func NewController(logger *logs.Logger, planService service.PlanService) *Controller {
|
func NewController(ctx context.Context, planService service.PlanService) *Controller {
|
||||||
return &Controller{
|
return &Controller{
|
||||||
logger: logger,
|
ctx: ctx,
|
||||||
planService: planService,
|
planService: planService,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -41,17 +42,18 @@ func NewController(logger *logs.Logger, planService service.PlanService) *Contro
|
|||||||
// @Success 200 {object} controller.Response{data=dto.PlanResponse} "业务码为201代表创建成功"
|
// @Success 200 {object} controller.Response{data=dto.PlanResponse} "业务码为201代表创建成功"
|
||||||
// @Router /api/v1/plans [post]
|
// @Router /api/v1/plans [post]
|
||||||
func (c *Controller) CreatePlan(ctx echo.Context) error {
|
func (c *Controller) CreatePlan(ctx echo.Context) error {
|
||||||
|
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "CreatePlan")
|
||||||
var req dto.CreatePlanRequest
|
var req dto.CreatePlanRequest
|
||||||
const actionType = "创建计划"
|
const actionType = "创建计划"
|
||||||
if err := ctx.Bind(&req); err != nil {
|
if err := ctx.Bind(&req); err != nil {
|
||||||
c.logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
|
logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req)
|
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 调用服务层创建计划
|
// 调用服务层创建计划
|
||||||
resp, err := c.planService.CreatePlan(&req)
|
resp, err := c.planService.CreatePlan(reqCtx, &req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.logger.Errorf("%s: 服务层创建计划失败: %v", actionType, err)
|
logger.Errorf("%s: 服务层创建计划失败: %v", actionType, err)
|
||||||
// 根据服务层返回的错误类型,转换为相应的HTTP状态码
|
// 根据服务层返回的错误类型,转换为相应的HTTP状态码
|
||||||
if errors.Is(err, plan.ErrPlanNotFound) { // 修改为 plan.ErrPlanNotFound
|
if errors.Is(err, plan.ErrPlanNotFound) { // 修改为 plan.ErrPlanNotFound
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, err.Error(), actionType, "计划数据校验失败或关联计划不存在", req)
|
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, err.Error(), actionType, "计划数据校验失败或关联计划不存在", req)
|
||||||
@@ -60,7 +62,7 @@ func (c *Controller) CreatePlan(ctx echo.Context) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 使用统一的成功响应函数
|
// 使用统一的成功响应函数
|
||||||
c.logger.Infof("%s: 计划创建成功, ID: %d", actionType, resp.ID)
|
logger.Infof("%s: 计划创建成功, ID: %d", actionType, resp.ID)
|
||||||
return controller.SendSuccessWithAudit(ctx, controller.CodeCreated, "计划创建成功", resp, actionType, "计划创建成功", resp)
|
return controller.SendSuccessWithAudit(ctx, controller.CodeCreated, "计划创建成功", resp, actionType, "计划创建成功", resp)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -74,19 +76,20 @@ func (c *Controller) CreatePlan(ctx echo.Context) error {
|
|||||||
// @Success 200 {object} controller.Response{data=dto.PlanResponse} "业务码为200代表成功获取"
|
// @Success 200 {object} controller.Response{data=dto.PlanResponse} "业务码为200代表成功获取"
|
||||||
// @Router /api/v1/plans/{id} [get]
|
// @Router /api/v1/plans/{id} [get]
|
||||||
func (c *Controller) GetPlan(ctx echo.Context) error {
|
func (c *Controller) GetPlan(ctx echo.Context) error {
|
||||||
|
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "GetPlan")
|
||||||
const actionType = "获取计划详情"
|
const actionType = "获取计划详情"
|
||||||
// 1. 从 URL 路径中获取 ID
|
// 1. 从 URL 路径中获取 ID
|
||||||
idStr := ctx.Param("id")
|
idStr := ctx.Param("id")
|
||||||
id, err := strconv.ParseUint(idStr, 10, 32)
|
id, err := strconv.ParseUint(idStr, 10, 32)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.logger.Errorf("%s: 计划ID格式错误: %v, ID: %s", actionType, err, idStr)
|
logger.Errorf("%s: 计划ID格式错误: %v, ID: %s", actionType, err, idStr)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的计划ID格式", actionType, "计划ID格式错误", idStr)
|
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的计划ID格式", actionType, "计划ID格式错误", idStr)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 调用服务层获取计划详情
|
// 调用服务层获取计划详情
|
||||||
resp, err := c.planService.GetPlanByID(uint(id))
|
resp, err := c.planService.GetPlanByID(reqCtx, uint(id))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.logger.Errorf("%s: 服务层获取计划详情失败: %v, ID: %d", actionType, err, id)
|
logger.Errorf("%s: 服务层获取计划详情失败: %v, ID: %d", actionType, err, id)
|
||||||
if errors.Is(err, plan.ErrPlanNotFound) { // 修改为 plan.ErrPlanNotFound
|
if errors.Is(err, plan.ErrPlanNotFound) { // 修改为 plan.ErrPlanNotFound
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, err.Error(), actionType, "计划不存在", id)
|
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, err.Error(), actionType, "计划不存在", id)
|
||||||
}
|
}
|
||||||
@@ -94,7 +97,7 @@ func (c *Controller) GetPlan(ctx echo.Context) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 4. 发送成功响应
|
// 4. 发送成功响应
|
||||||
c.logger.Infof("%s: 获取计划详情成功, ID: %d", actionType, id)
|
logger.Infof("%s: 获取计划详情成功, ID: %d", actionType, id)
|
||||||
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取计划详情成功", resp, actionType, "获取计划详情成功", resp)
|
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取计划详情成功", resp, actionType, "获取计划详情成功", resp)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -108,21 +111,22 @@ func (c *Controller) GetPlan(ctx echo.Context) error {
|
|||||||
// @Success 200 {object} controller.Response{data=dto.ListPlansResponse} "业务码为200代表成功获取列表"
|
// @Success 200 {object} controller.Response{data=dto.ListPlansResponse} "业务码为200代表成功获取列表"
|
||||||
// @Router /api/v1/plans [get]
|
// @Router /api/v1/plans [get]
|
||||||
func (c *Controller) ListPlans(ctx echo.Context) error {
|
func (c *Controller) ListPlans(ctx echo.Context) error {
|
||||||
|
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "ListPlans")
|
||||||
const actionType = "获取计划列表"
|
const actionType = "获取计划列表"
|
||||||
var query dto.ListPlansQuery
|
var query dto.ListPlansQuery
|
||||||
if err := ctx.Bind(&query); err != nil {
|
if err := ctx.Bind(&query); err != nil {
|
||||||
c.logger.Errorf("%s: 查询参数绑定失败: %v", actionType, err)
|
logger.Errorf("%s: 查询参数绑定失败: %v", actionType, err)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的查询参数: "+err.Error(), actionType, "查询参数绑定失败", query)
|
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的查询参数: "+err.Error(), actionType, "查询参数绑定失败", query)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 调用服务层获取计划列表
|
// 调用服务层获取计划列表
|
||||||
resp, err := c.planService.ListPlans(&query)
|
resp, err := c.planService.ListPlans(reqCtx, &query)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.logger.Errorf("%s: 服务层获取计划列表失败: %v", actionType, err)
|
logger.Errorf("%s: 服务层获取计划列表失败: %v", actionType, err)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取计划列表失败: "+err.Error(), actionType, "服务层获取计划列表失败", nil)
|
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取计划列表失败: "+err.Error(), actionType, "服务层获取计划列表失败", nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.logger.Infof("%s: 获取计划列表成功, 数量: %d", actionType, len(resp.Plans))
|
logger.Infof("%s: 获取计划列表成功, 数量: %d", actionType, len(resp.Plans))
|
||||||
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取计划列表成功", resp, actionType, "获取计划列表成功", resp)
|
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取计划列表成功", resp, actionType, "获取计划列表成功", resp)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -138,26 +142,27 @@ func (c *Controller) ListPlans(ctx echo.Context) error {
|
|||||||
// @Success 200 {object} controller.Response{data=dto.PlanResponse} "业务码为200代表更新成功"
|
// @Success 200 {object} controller.Response{data=dto.PlanResponse} "业务码为200代表更新成功"
|
||||||
// @Router /api/v1/plans/{id} [put]
|
// @Router /api/v1/plans/{id} [put]
|
||||||
func (c *Controller) UpdatePlan(ctx echo.Context) error {
|
func (c *Controller) UpdatePlan(ctx echo.Context) error {
|
||||||
|
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "UpdatePlan")
|
||||||
const actionType = "更新计划"
|
const actionType = "更新计划"
|
||||||
// 1. 从 URL 路径中获取 ID
|
// 1. 从 URL 路径中获取 ID
|
||||||
idStr := ctx.Param("id")
|
idStr := ctx.Param("id")
|
||||||
id, err := strconv.ParseUint(idStr, 10, 32)
|
id, err := strconv.ParseUint(idStr, 10, 32)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.logger.Errorf("%s: 计划ID格式错误: %v, ID: %s", actionType, err, idStr)
|
logger.Errorf("%s: 计划ID格式错误: %v, ID: %s", actionType, err, idStr)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的计划ID格式", actionType, "计划ID格式错误", idStr)
|
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的计划ID格式", actionType, "计划ID格式错误", idStr)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2. 绑定请求体
|
// 2. 绑定请求体
|
||||||
var req dto.UpdatePlanRequest
|
var req dto.UpdatePlanRequest
|
||||||
if err := ctx.Bind(&req); err != nil {
|
if err := ctx.Bind(&req); err != nil {
|
||||||
c.logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
|
logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req)
|
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 调用服务层更新计划
|
// 调用服务层更新计划
|
||||||
resp, err := c.planService.UpdatePlan(uint(id), &req)
|
resp, err := c.planService.UpdatePlan(reqCtx, uint(id), &req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.logger.Errorf("%s: 服务层更新计划失败: %v, ID: %d", actionType, err, id)
|
logger.Errorf("%s: 服务层更新计划失败: %v, ID: %d", actionType, err, id)
|
||||||
if errors.Is(err, plan.ErrPlanNotFound) { // 修改为 plan.ErrPlanNotFound
|
if errors.Is(err, plan.ErrPlanNotFound) { // 修改为 plan.ErrPlanNotFound
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, err.Error(), actionType, "计划不存在", id)
|
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, err.Error(), actionType, "计划不存在", id)
|
||||||
} else if errors.Is(err, plan.ErrPlanCannotBeModified) { // 修改为 plan.ErrPlanCannotBeModified
|
} else if errors.Is(err, plan.ErrPlanCannotBeModified) { // 修改为 plan.ErrPlanCannotBeModified
|
||||||
@@ -167,7 +172,7 @@ func (c *Controller) UpdatePlan(ctx echo.Context) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 9. 发送成功响应
|
// 9. 发送成功响应
|
||||||
c.logger.Infof("%s: 计划更新成功, ID: %d", actionType, resp.ID)
|
logger.Infof("%s: 计划更新成功, ID: %d", actionType, resp.ID)
|
||||||
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "计划更新成功", resp, actionType, "计划更新成功", resp)
|
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "计划更新成功", resp, actionType, "计划更新成功", resp)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -181,19 +186,20 @@ func (c *Controller) UpdatePlan(ctx echo.Context) error {
|
|||||||
// @Success 200 {object} controller.Response "业务码为200代表删除成功"
|
// @Success 200 {object} controller.Response "业务码为200代表删除成功"
|
||||||
// @Router /api/v1/plans/{id} [delete]
|
// @Router /api/v1/plans/{id} [delete]
|
||||||
func (c *Controller) DeletePlan(ctx echo.Context) error {
|
func (c *Controller) DeletePlan(ctx echo.Context) error {
|
||||||
|
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "DeletePlan")
|
||||||
const actionType = "删除计划"
|
const actionType = "删除计划"
|
||||||
// 1. 从 URL 路径中获取 ID
|
// 1. 从 URL 路径中获取 ID
|
||||||
idStr := ctx.Param("id")
|
idStr := ctx.Param("id")
|
||||||
id, err := strconv.ParseUint(idStr, 10, 32)
|
id, err := strconv.ParseUint(idStr, 10, 32)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.logger.Errorf("%s: 计划ID格式错误: %v, ID: %s", actionType, err, idStr)
|
logger.Errorf("%s: 计划ID格式错误: %v, ID: %s", actionType, err, idStr)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的计划ID格式", actionType, "计划ID格式错误", idStr)
|
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的计划ID格式", actionType, "计划ID格式错误", idStr)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 调用服务层删除计划
|
// 调用服务层删除计划
|
||||||
err = c.planService.DeletePlan(uint(id))
|
err = c.planService.DeletePlan(reqCtx, uint(id))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.logger.Errorf("%s: 服务层删除计划失败: %v, ID: %d", actionType, err, id)
|
logger.Errorf("%s: 服务层删除计划失败: %v, ID: %d", actionType, err, id)
|
||||||
if errors.Is(err, plan.ErrPlanNotFound) { // 修改为 plan.ErrPlanNotFound
|
if errors.Is(err, plan.ErrPlanNotFound) { // 修改为 plan.ErrPlanNotFound
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, err.Error(), actionType, "计划不存在", id)
|
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, err.Error(), actionType, "计划不存在", id)
|
||||||
} else if errors.Is(err, plan.ErrPlanCannotBeDeleted) { // 修改为 plan.ErrPlanCannotBeDeleted
|
} else if errors.Is(err, plan.ErrPlanCannotBeDeleted) { // 修改为 plan.ErrPlanCannotBeDeleted
|
||||||
@@ -203,7 +209,7 @@ func (c *Controller) DeletePlan(ctx echo.Context) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 6. 发送成功响应
|
// 6. 发送成功响应
|
||||||
c.logger.Infof("%s: 计划删除成功, ID: %d", actionType, id)
|
logger.Infof("%s: 计划删除成功, ID: %d", actionType, id)
|
||||||
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "计划删除成功", nil, actionType, "计划删除成功", id)
|
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "计划删除成功", nil, actionType, "计划删除成功", id)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -217,19 +223,20 @@ func (c *Controller) DeletePlan(ctx echo.Context) error {
|
|||||||
// @Success 200 {object} controller.Response "业务码为200代表成功启动计划"
|
// @Success 200 {object} controller.Response "业务码为200代表成功启动计划"
|
||||||
// @Router /api/v1/plans/{id}/start [post]
|
// @Router /api/v1/plans/{id}/start [post]
|
||||||
func (c *Controller) StartPlan(ctx echo.Context) error {
|
func (c *Controller) StartPlan(ctx echo.Context) error {
|
||||||
|
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "StartPlan")
|
||||||
const actionType = "启动计划"
|
const actionType = "启动计划"
|
||||||
// 1. 从 URL 路径中获取 ID
|
// 1. 从 URL 路径中获取 ID
|
||||||
idStr := ctx.Param("id")
|
idStr := ctx.Param("id")
|
||||||
id, err := strconv.ParseUint(idStr, 10, 32)
|
id, err := strconv.ParseUint(idStr, 10, 32)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.logger.Errorf("%s: 计划ID格式错误: %v, ID: %s", actionType, err, idStr)
|
logger.Errorf("%s: 计划ID格式错误: %v, ID: %s", actionType, err, idStr)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的计划ID格式", actionType, "计划ID格式错误", idStr)
|
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的计划ID格式", actionType, "计划ID格式错误", idStr)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 调用服务层启动计划
|
// 调用服务层启动计划
|
||||||
err = c.planService.StartPlan(uint(id))
|
err = c.planService.StartPlan(reqCtx, uint(id))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.logger.Errorf("%s: 服务层启动计划失败: %v, ID: %d", actionType, err, id)
|
logger.Errorf("%s: 服务层启动计划失败: %v, ID: %d", actionType, err, id)
|
||||||
if errors.Is(err, plan.ErrPlanNotFound) { // 修改为 plan.ErrPlanNotFound
|
if errors.Is(err, plan.ErrPlanNotFound) { // 修改为 plan.ErrPlanNotFound
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, err.Error(), actionType, "计划不存在", id)
|
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, err.Error(), actionType, "计划不存在", id)
|
||||||
} else if errors.Is(err, plan.ErrPlanCannotBeStarted) { // 修改为 plan.ErrPlanCannotBeStarted
|
} else if errors.Is(err, plan.ErrPlanCannotBeStarted) { // 修改为 plan.ErrPlanCannotBeStarted
|
||||||
@@ -241,7 +248,7 @@ func (c *Controller) StartPlan(ctx echo.Context) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 6. 发送成功响应
|
// 6. 发送成功响应
|
||||||
c.logger.Infof("%s: 计划已成功启动, ID: %d", actionType, id)
|
logger.Infof("%s: 计划已成功启动, ID: %d", actionType, id)
|
||||||
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "计划已成功启动", nil, actionType, "计划已成功启动", id)
|
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "计划已成功启动", nil, actionType, "计划已成功启动", id)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -255,19 +262,20 @@ func (c *Controller) StartPlan(ctx echo.Context) error {
|
|||||||
// @Success 200 {object} controller.Response "业务码为200代表成功停止计划"
|
// @Success 200 {object} controller.Response "业务码为200代表成功停止计划"
|
||||||
// @Router /api/v1/plans/{id}/stop [post]
|
// @Router /api/v1/plans/{id}/stop [post]
|
||||||
func (c *Controller) StopPlan(ctx echo.Context) error {
|
func (c *Controller) StopPlan(ctx echo.Context) error {
|
||||||
|
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "StopPlan")
|
||||||
const actionType = "停止计划"
|
const actionType = "停止计划"
|
||||||
// 1. 从 URL 路径中获取 ID
|
// 1. 从 URL 路径中获取 ID
|
||||||
idStr := ctx.Param("id")
|
idStr := ctx.Param("id")
|
||||||
id, err := strconv.ParseUint(idStr, 10, 32)
|
id, err := strconv.ParseUint(idStr, 10, 32)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.logger.Errorf("%s: 计划ID格式错误: %v, ID: %s", actionType, err, idStr)
|
logger.Errorf("%s: 计划ID格式错误: %v, ID: %s", actionType, err, idStr)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的计划ID格式", actionType, "计划ID格式错误", idStr)
|
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的计划ID格式", actionType, "计划ID格式错误", idStr)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 调用服务层停止计划
|
// 调用服务层停止计划
|
||||||
err = c.planService.StopPlan(uint(id))
|
err = c.planService.StopPlan(reqCtx, uint(id))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.logger.Errorf("%s: 服务层停止计划失败: %v, ID: %d", actionType, err, id)
|
logger.Errorf("%s: 服务层停止计划失败: %v, ID: %d", actionType, err, id)
|
||||||
if errors.Is(err, plan.ErrPlanNotFound) { // 修改为 plan.ErrPlanNotFound
|
if errors.Is(err, plan.ErrPlanNotFound) { // 修改为 plan.ErrPlanNotFound
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, err.Error(), actionType, "计划不存在", id)
|
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, err.Error(), actionType, "计划不存在", id)
|
||||||
} else if errors.Is(err, plan.ErrPlanCannotBeStopped) { // 修改为 plan.ErrPlanCannotBeStopped
|
} else if errors.Is(err, plan.ErrPlanCannotBeStopped) { // 修改为 plan.ErrPlanCannotBeStopped
|
||||||
@@ -279,6 +287,6 @@ func (c *Controller) StopPlan(ctx echo.Context) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 6. 发送成功响应
|
// 6. 发送成功响应
|
||||||
c.logger.Infof("%s: 计划已成功停止, ID: %d", actionType, id)
|
logger.Infof("%s: 计划已成功停止, ID: %d", actionType, id)
|
||||||
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "计划已成功停止", nil, actionType, "计划已成功停止", id)
|
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "计划已成功停止", nil, actionType, "计划已成功停止", id)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package user
|
package user
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"git.huangwc.com/pig/pig-farm-controller/internal/app/controller"
|
"git.huangwc.com/pig/pig-farm-controller/internal/app/controller"
|
||||||
@@ -12,18 +13,18 @@ import (
|
|||||||
|
|
||||||
// Controller 用户控制器
|
// Controller 用户控制器
|
||||||
type Controller struct {
|
type Controller struct {
|
||||||
|
ctx context.Context
|
||||||
userService service.UserService
|
userService service.UserService
|
||||||
logger *logs.Logger
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewController 创建用户控制器实例
|
// NewController 创建用户控制器实例
|
||||||
func NewController(
|
func NewController(
|
||||||
|
ctx context.Context,
|
||||||
userService service.UserService,
|
userService service.UserService,
|
||||||
logger *logs.Logger,
|
|
||||||
) *Controller {
|
) *Controller {
|
||||||
return &Controller{
|
return &Controller{
|
||||||
|
ctx: ctx,
|
||||||
userService: userService,
|
userService: userService,
|
||||||
logger: logger,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -39,15 +40,17 @@ func NewController(
|
|||||||
// @Success 200 {object} controller.Response{data=dto.CreateUserResponse} "业务码为201代表创建成功"
|
// @Success 200 {object} controller.Response{data=dto.CreateUserResponse} "业务码为201代表创建成功"
|
||||||
// @Router /api/v1/users [post]
|
// @Router /api/v1/users [post]
|
||||||
func (c *Controller) CreateUser(ctx echo.Context) error {
|
func (c *Controller) CreateUser(ctx echo.Context) error {
|
||||||
|
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "CreateUser")
|
||||||
|
|
||||||
var req dto.CreateUserRequest
|
var req dto.CreateUserRequest
|
||||||
if err := ctx.Bind(&req); err != nil {
|
if err := ctx.Bind(&req); err != nil {
|
||||||
c.logger.Errorf("创建用户: 参数绑定失败: %v", err)
|
logger.Errorf("创建用户: 参数绑定失败: %v", err)
|
||||||
return controller.SendErrorResponse(ctx, controller.CodeBadRequest, err.Error())
|
return controller.SendErrorResponse(ctx, controller.CodeBadRequest, err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := c.userService.CreateUser(&req)
|
resp, err := c.userService.CreateUser(reqCtx, &req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.logger.Errorf("创建用户: 服务层调用失败: %v", err)
|
logger.Errorf("创建用户: 服务层调用失败: %v", err)
|
||||||
return controller.SendErrorResponse(ctx, controller.CodeInternalError, err.Error())
|
return controller.SendErrorResponse(ctx, controller.CodeInternalError, err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -64,15 +67,17 @@ func (c *Controller) CreateUser(ctx echo.Context) error {
|
|||||||
// @Success 200 {object} controller.Response{data=dto.LoginResponse} "业务码为200代表登录成功"
|
// @Success 200 {object} controller.Response{data=dto.LoginResponse} "业务码为200代表登录成功"
|
||||||
// @Router /api/v1/users/login [post]
|
// @Router /api/v1/users/login [post]
|
||||||
func (c *Controller) Login(ctx echo.Context) error {
|
func (c *Controller) Login(ctx echo.Context) error {
|
||||||
|
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "Login")
|
||||||
|
|
||||||
var req dto.LoginRequest
|
var req dto.LoginRequest
|
||||||
if err := ctx.Bind(&req); err != nil {
|
if err := ctx.Bind(&req); err != nil {
|
||||||
c.logger.Errorf("登录: 参数绑定失败: %v", err)
|
logger.Errorf("登录: 参数绑定失败: %v", err)
|
||||||
return controller.SendErrorResponse(ctx, controller.CodeBadRequest, err.Error())
|
return controller.SendErrorResponse(ctx, controller.CodeBadRequest, err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := c.userService.Login(&req)
|
resp, err := c.userService.Login(reqCtx, &req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.logger.Errorf("登录: 服务层调用失败: %v", err)
|
logger.Errorf("登录: 服务层调用失败: %v", err)
|
||||||
return controller.SendErrorResponse(ctx, controller.CodeUnauthorized, err.Error())
|
return controller.SendErrorResponse(ctx, controller.CodeUnauthorized, err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -91,30 +96,31 @@ func (c *Controller) Login(ctx echo.Context) error {
|
|||||||
// @Success 200 {object} controller.Response{data=string} "成功响应"
|
// @Success 200 {object} controller.Response{data=string} "成功响应"
|
||||||
// @Router /api/v1/users/{id}/notifications/test [post]
|
// @Router /api/v1/users/{id}/notifications/test [post]
|
||||||
func (c *Controller) SendTestNotification(ctx echo.Context) error {
|
func (c *Controller) SendTestNotification(ctx echo.Context) error {
|
||||||
|
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "SendTestNotification")
|
||||||
const actionType = "发送测试通知"
|
const actionType = "发送测试通知"
|
||||||
|
|
||||||
// 1. 从 URL 中获取用户 ID
|
// 1. 从 URL 中获取用户 ID
|
||||||
userID, err := strconv.ParseUint(ctx.Param("id"), 10, 32)
|
userID, err := strconv.ParseUint(ctx.Param("id"), 10, 32)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.logger.Errorf("%s: 无效的用户ID格式: %v", actionType, err)
|
logger.Errorf("%s: 无效的用户ID格式: %v", actionType, err)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的用户ID格式", actionType, "无效的用户ID格式", ctx.Param("id"))
|
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的用户ID格式", actionType, "无效的用户ID格式", ctx.Param("id"))
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2. 从请求体 (JSON Body) 中获取要测试的通知类型
|
// 2. 从请求体 (JSON Body) 中获取要测试的通知类型
|
||||||
var req dto.SendTestNotificationRequest
|
var req dto.SendTestNotificationRequest
|
||||||
if err := ctx.Bind(&req); err != nil {
|
if err := ctx.Bind(&req); err != nil {
|
||||||
c.logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
|
logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "请求体格式错误或缺少 'type' 字段: "+err.Error(), actionType, "请求体绑定失败", req)
|
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "请求体格式错误或缺少 'type' 字段: "+err.Error(), actionType, "请求体绑定失败", req)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 3. 调用服务层
|
// 3. 调用服务层
|
||||||
err = c.userService.SendTestNotification(uint(userID), &req)
|
err = c.userService.SendTestNotification(reqCtx, uint(userID), &req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.logger.Errorf("%s: 服务层调用失败: %v", actionType, err)
|
logger.Errorf("%s: 服务层调用失败: %v", actionType, err)
|
||||||
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "发送测试消息失败: "+err.Error(), actionType, "服务层调用失败", map[string]interface{}{"userID": userID, "type": req.Type})
|
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "发送测试消息失败: "+err.Error(), actionType, "服务层调用失败", map[string]interface{}{"userID": userID, "type": req.Type})
|
||||||
}
|
}
|
||||||
|
|
||||||
// 4. 返回成功响应
|
// 4. 返回成功响应
|
||||||
c.logger.Infof("%s: 成功为用户 %d 发送类型为 %s 的测试消息", actionType, userID, req.Type)
|
logger.Infof("%s: 成功为用户 %d 发送类型为 %s 的测试消息", actionType, userID, req.Type)
|
||||||
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "测试消息已发送,请检查您的接收端。", nil, actionType, "测试消息发送成功", map[string]interface{}{"userID": userID, "type": req.Type})
|
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "测试消息已发送,请检查您的接收端。", nil, actionType, "测试消息发送成功", map[string]interface{}{"userID": userID, "type": req.Type})
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user