From 0c35e2ce7dbcd57fcaf28bab2c0f5494c8cfb5e7 Mon Sep 17 00:00:00 2001 From: huang <1724659546@qq.com> Date: Fri, 31 Oct 2025 15:38:10 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E4=BB=BB=E5=8A=A12.2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/device/device_controller.go | 504 +++--------------- internal/app/service/device_service.go | 373 +++++++++++++ .../design.md | 129 ++++- .../refactor-business-logic-layering/tasks.md | 51 +- 4 files changed, 594 insertions(+), 463 deletions(-) create mode 100644 internal/app/service/device_service.go diff --git a/internal/app/controller/device/device_controller.go b/internal/app/controller/device/device_controller.go index ba6510a..26fa53e 100644 --- a/internal/app/controller/device/device_controller.go +++ b/internal/app/controller/device/device_controller.go @@ -1,44 +1,30 @@ package device import ( - "encoding/json" "errors" - "strconv" - "strings" "git.huangwc.com/pig/pig-farm-controller/internal/app/controller" "git.huangwc.com/pig/pig-farm-controller/internal/app/dto" - "git.huangwc.com/pig/pig-farm-controller/internal/domain/device" + "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/models" - "git.huangwc.com/pig/pig-farm-controller/internal/infra/repository" "github.com/labstack/echo/v4" "gorm.io/gorm" ) // Controller 设备控制器,封装了所有与设备和区域主控相关的业务逻辑 type Controller struct { - deviceRepo repository.DeviceRepository - areaControllerRepo repository.AreaControllerRepository - deviceTemplateRepo repository.DeviceTemplateRepository - deviceService device.Service - logger *logs.Logger + deviceService service.DeviceService + logger *logs.Logger } // NewController 创建一个新的设备控制器实例 func NewController( - deviceRepo repository.DeviceRepository, - areaControllerRepo repository.AreaControllerRepository, - deviceTemplateRepo repository.DeviceTemplateRepository, - deviceService device.Service, + deviceService service.DeviceService, logger *logs.Logger, ) *Controller { return &Controller{ - deviceRepo: deviceRepo, - areaControllerRepo: areaControllerRepo, - deviceTemplateRepo: deviceTemplateRepo, - deviceService: deviceService, - logger: logger, + deviceService: deviceService, + logger: logger, } } @@ -62,43 +48,13 @@ func (c *Controller) CreateDevice(ctx echo.Context) error { return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req) } - propertiesJSON, err := json.Marshal(req.Properties) + resp, err := c.deviceService.CreateDevice(&req) if err != nil { - c.logger.Errorf("%s: 序列化属性失败: %v", actionType, err) - return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "属性字段格式错误", actionType, "属性序列化失败", req.Properties) + c.logger.Errorf("%s: 服务层创建失败: %v", actionType, err) + return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "创建设备失败: "+err.Error(), actionType, "服务层创建失败", req) } - device := &models.Device{ - Name: req.Name, - DeviceTemplateID: req.DeviceTemplateID, - AreaControllerID: req.AreaControllerID, - Location: req.Location, - Properties: propertiesJSON, - } - - if err := device.SelfCheck(); err != nil { - c.logger.Errorf("%s: 设备属性自检失败: %v", actionType, err) - return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "设备属性不符合要求: "+err.Error(), actionType, "设备属性自检失败", device) - } - - if err := c.deviceRepo.Create(device); err != nil { - c.logger.Errorf("%s: 数据库操作失败: %v", actionType, err) - return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "创建设备失败: "+err.Error(), actionType, "数据库创建失败", device) - } - - createdDevice, err := c.deviceRepo.FindByID(device.ID) - if err != nil { - c.logger.Errorf("%s: 重新加载创建的设备失败: %v", actionType, err) - return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "设备创建成功,但重新加载设备失败", actionType, "重新加载设备失败", device) - } - - resp, err := dto.NewDeviceResponse(createdDevice) - if err != nil { - c.logger.Errorf("%s: 序列化响应失败: %v, Device: %+v", actionType, err, createdDevice) - return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "设备创建成功,但响应生成失败", actionType, "响应序列化失败", createdDevice) - } - - c.logger.Infof("%s: 设备创建成功, ID: %d", actionType, device.ID) + c.logger.Infof("%s: 设备创建成功, ID: %d", actionType, resp.ID) return controller.SendSuccessWithAudit(ctx, controller.CodeCreated, "设备创建成功", resp, actionType, "设备创建成功", resp) } @@ -115,32 +71,17 @@ func (c *Controller) GetDevice(ctx echo.Context) error { const actionType = "获取设备" deviceID := ctx.Param("id") - if deviceID == "" { - c.logger.Errorf("%s: 设备ID为空", actionType) - return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "设备ID不能为空", actionType, "设备ID为空", nil) - } - - device, err := c.deviceRepo.FindByIDString(deviceID) + resp, err := c.deviceService.GetDevice(deviceID) if err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { c.logger.Warnf("%s: 设备不存在, ID: %s", actionType, deviceID) return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "设备未找到", actionType, "设备不存在", deviceID) } - if strings.Contains(err.Error(), "无效的设备ID格式") { - c.logger.Errorf("%s: 设备ID格式错误: %v, ID: %s", actionType, err, deviceID) - return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, err.Error(), actionType, "设备ID格式错误", deviceID) - } - c.logger.Errorf("%s: 数据库查询失败: %v, ID: %s", actionType, err, deviceID) - return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取设备信息失败: "+err.Error(), actionType, "数据库查询失败", deviceID) + c.logger.Errorf("%s: 服务层获取失败: %v, ID: %s", actionType, err, deviceID) + return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取设备信息失败: "+err.Error(), actionType, "服务层获取失败", deviceID) } - resp, err := dto.NewDeviceResponse(device) - if err != nil { - c.logger.Errorf("%s: 序列化响应失败: %v, Device: %+v", actionType, err, device) - return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取设备信息失败: 内部数据格式错误", actionType, "响应序列化失败", device) - } - - c.logger.Infof("%s: 获取设备信息成功, ID: %d", actionType, device.ID) + c.logger.Infof("%s: 获取设备信息成功, ID: %d", actionType, resp.ID) return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取设备信息成功", resp, actionType, "获取设备信息成功", resp) } @@ -154,19 +95,13 @@ func (c *Controller) GetDevice(ctx echo.Context) error { // @Router /api/v1/devices [get] func (c *Controller) ListDevices(ctx echo.Context) error { const actionType = "获取设备列表" - devices, err := c.deviceRepo.ListAll() + resp, err := c.deviceService.ListDevices() if err != nil { - c.logger.Errorf("%s: 数据库查询失败: %v", actionType, err) - return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取设备列表失败: "+err.Error(), actionType, "数据库查询失败", nil) + c.logger.Errorf("%s: 服务层获取列表失败: %v", actionType, err) + return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取设备列表失败: "+err.Error(), actionType, "服务层获取列表失败", nil) } - resp, err := dto.NewListDeviceResponse(devices) - if err != nil { - c.logger.Errorf("%s: 序列化响应失败: %v, Devices: %+v", actionType, err, devices) - return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取设备列表失败: 内部数据格式错误", actionType, "响应序列化失败", devices) - } - - c.logger.Infof("%s: 获取设备列表成功, 数量: %d", actionType, len(devices)) + c.logger.Infof("%s: 获取设备列表成功, 数量: %d", actionType, len(resp)) return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取设备列表成功", resp, actionType, "获取设备列表成功", resp) } @@ -185,61 +120,23 @@ func (c *Controller) UpdateDevice(ctx echo.Context) error { const actionType = "更新设备" deviceID := ctx.Param("id") - existingDevice, err := c.deviceRepo.FindByIDString(deviceID) - if err != nil { - if errors.Is(err, gorm.ErrRecordNotFound) { - c.logger.Warnf("%s: 设备不存在, ID: %s", actionType, deviceID) - return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "设备未找到", actionType, "设备不存在", deviceID) - } - if strings.Contains(err.Error(), "无效的设备ID格式") { - c.logger.Errorf("%s: 设备ID格式错误: %v, ID: %s", actionType, err, deviceID) - return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, err.Error(), actionType, "设备ID格式错误", deviceID) - } - c.logger.Errorf("%s: 数据库查询失败: %v, ID: %s", actionType, err, deviceID) - return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "更新设备失败: "+err.Error(), actionType, "数据库查询失败", deviceID) - } - var req dto.UpdateDeviceRequest if err := ctx.Bind(&req); err != nil { c.logger.Errorf("%s: 参数绑定失败: %v", actionType, err) return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req) } - propertiesJSON, err := json.Marshal(req.Properties) + resp, err := c.deviceService.UpdateDevice(deviceID, &req) if err != nil { - c.logger.Errorf("%s: 序列化属性失败: %v", actionType, err) - return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "属性字段格式错误", actionType, "属性序列化失败", req.Properties) + if errors.Is(err, gorm.ErrRecordNotFound) { + c.logger.Warnf("%s: 设备不存在, ID: %s", actionType, deviceID) + return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "设备未找到", actionType, "设备不存在", deviceID) + } + c.logger.Errorf("%s: 服务层更新失败: %v, ID: %s", actionType, err, deviceID) + return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "更新设备失败: "+err.Error(), actionType, "服务层更新失败", deviceID) } - existingDevice.Name = req.Name - existingDevice.DeviceTemplateID = req.DeviceTemplateID - existingDevice.AreaControllerID = req.AreaControllerID - existingDevice.Location = req.Location - existingDevice.Properties = propertiesJSON - - if err := existingDevice.SelfCheck(); err != nil { - c.logger.Errorf("%s: 设备属性自检失败: %v", actionType, err) - return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "设备属性不符合要求: "+err.Error(), actionType, "设备属性自检失败", existingDevice) - } - - if err := c.deviceRepo.Update(existingDevice); err != nil { - c.logger.Errorf("%s: 数据库更新失败: %v, Device: %+v", actionType, err, existingDevice) - return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "更新设备失败: "+err.Error(), actionType, "数据库更新失败", deviceID) - } - - updatedDevice, err := c.deviceRepo.FindByID(existingDevice.ID) - if err != nil { - c.logger.Errorf("%s: 重新加载更新的设备失败: %v", actionType, err) - return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "设备更新成功,但重新加载设备失败", actionType, "重新加载设备失败", existingDevice) - } - - resp, err := dto.NewDeviceResponse(updatedDevice) - if err != nil { - c.logger.Errorf("%s: 序列化响应失败: %v, Device: %+v", actionType, err, updatedDevice) - return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "设备更新成功,但响应生成失败", actionType, "响应序列化失败", updatedDevice) - } - - c.logger.Infof("%s: 设备更新成功, ID: %d", actionType, existingDevice.ID) + c.logger.Infof("%s: 设备更新成功, ID: %d", actionType, resp.ID) return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "设备更新成功", resp, actionType, "设备更新成功", resp) } @@ -256,28 +153,16 @@ func (c *Controller) DeleteDevice(ctx echo.Context) error { const actionType = "删除设备" deviceID := ctx.Param("id") - idUint, err := strconv.ParseUint(deviceID, 10, 64) - if err != nil { - c.logger.Errorf("%s: 设备ID格式错误: %v, ID: %s", actionType, err, deviceID) - return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的设备ID格式", actionType, "设备ID格式错误", deviceID) - } - - _, err = c.deviceRepo.FindByIDString(deviceID) - if err != nil { + if err := c.deviceService.DeleteDevice(deviceID); err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { c.logger.Warnf("%s: 设备不存在, ID: %s", actionType, deviceID) return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "设备未找到", actionType, "设备不存在", deviceID) } - c.logger.Errorf("%s: 查找设备失败: %v, ID: %s", actionType, err, deviceID) - return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "删除设备失败: 查找设备时发生内部错误", actionType, "数据库查询失败", deviceID) + c.logger.Errorf("%s: 服务层删除失败: %v, ID: %s", actionType, err, deviceID) + return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "删除设备失败: "+err.Error(), actionType, "服务层删除失败", deviceID) } - if err := c.deviceRepo.Delete(uint(idUint)); err != nil { - c.logger.Errorf("%s: 数据库删除失败: %v, ID: %d", actionType, err, idUint) - return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "删除设备失败: "+err.Error(), actionType, "数据库删除失败", deviceID) - } - - c.logger.Infof("%s: 设备删除成功, ID: %d", actionType, idUint) + c.logger.Infof("%s: 设备删除成功, ID: %s", actionType, deviceID) return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "设备删除成功", nil, actionType, "设备删除成功", deviceID) } @@ -302,45 +187,16 @@ func (c *Controller) ManualControl(ctx echo.Context) error { return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req) } - dev, err := c.deviceRepo.FindByIDString(deviceID) - if err != nil { + if err := c.deviceService.ManualControl(deviceID, &req); err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { c.logger.Warnf("%s: 设备不存在, ID: %s", actionType, deviceID) return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "设备未找到", actionType, "设备不存在", deviceID) } - if strings.Contains(err.Error(), "无效的设备ID格式") { - c.logger.Errorf("%s: 设备ID格式错误: %v, ID: %s", actionType, err, deviceID) - return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, err.Error(), actionType, "设备ID格式错误", deviceID) - } - c.logger.Errorf("%s: 数据库查询失败: %v, ID: %s", actionType, err, deviceID) - return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "手动控制失败: "+err.Error(), actionType, "数据库查询失败", deviceID) + c.logger.Errorf("%s: 服务层手动控制失败: %v, ID: %s", actionType, err, deviceID) + return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "手动控制失败: "+err.Error(), actionType, "服务层手动控制失败", deviceID) } - c.logger.Infof("%s: 接收到指令, 设备ID: %s, 动作: %s", actionType, deviceID, req.Action) - if req.Action == nil { - err = c.deviceService.Collect(dev.AreaControllerID, []*models.Device{dev}) - if err != nil { - c.logger.Errorf("%s: 获取设备状态失败: %v, 设备ID: %s", actionType, err, deviceID) - return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取设备状态失败: "+err.Error(), actionType, "获取设备状态失败", deviceID) - } - } else { - action := device.DeviceActionStart - switch *req.Action { - case "off": - action = device.DeviceActionStop - case "on": - default: - c.logger.Errorf("%s: 无效的动作: %s, 设备ID: %s", actionType, *req.Action, deviceID) - return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的动作: "+*req.Action, actionType, "无效的动作", req.Action) - } - err = c.deviceService.Switch(dev, action) - if err != nil { - c.logger.Errorf("%s: 设备控制失败: %v, 设备ID: %s", actionType, err, deviceID) - return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "设备控制失败: "+err.Error(), actionType, "设备控制失败", deviceID) - } - } - - return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "指令已发送", map[string]interface{}{"device_id": deviceID}, actionType, "指令发送成功", map[string]interface{}{"device_id": deviceID, "action": req.Action}) + return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "指令已发送", nil, actionType, "指令发送成功", nil) } // --- Controller Methods: Area Controllers --- @@ -363,36 +219,13 @@ func (c *Controller) CreateAreaController(ctx echo.Context) error { return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req) } - propertiesJSON, err := json.Marshal(req.Properties) + resp, err := c.deviceService.CreateAreaController(&req) if err != nil { - c.logger.Errorf("%s: 序列化属性失败: %v", actionType, err) - return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "属性字段格式错误", actionType, "属性序列化失败", req.Properties) + c.logger.Errorf("%s: 服务层创建失败: %v", actionType, err) + return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "创建区域主控失败: "+err.Error(), actionType, "服务层创建失败", req) } - ac := &models.AreaController{ - Name: req.Name, - NetworkID: req.NetworkID, - Location: req.Location, - Properties: propertiesJSON, - } - - if err := ac.SelfCheck(); err != nil { - c.logger.Errorf("%s: 区域主控自检失败: %v", actionType, err) - return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "区域主控参数不符合要求: "+err.Error(), actionType, "区域主控自检失败", ac) - } - - if err := c.areaControllerRepo.Create(ac); err != nil { - c.logger.Errorf("%s: 数据库操作失败: %v", actionType, err) - return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "创建区域主控失败: "+err.Error(), actionType, "数据库创建失败", ac) - } - - resp, err := dto.NewAreaControllerResponse(ac) - if err != nil { - c.logger.Errorf("%s: 序列化响应失败: %v", actionType, err) - return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "区域主控创建成功,但响应生成失败", actionType, "响应序列化失败", ac) - } - - c.logger.Infof("%s: 区域主控创建成功, ID: %d", actionType, ac.ID) + c.logger.Infof("%s: 区域主控创建成功, ID: %d", actionType, resp.ID) return controller.SendSuccessWithAudit(ctx, controller.CodeCreated, "区域主控创建成功", resp, actionType, "区域主控创建成功", resp) } @@ -409,29 +242,17 @@ func (c *Controller) GetAreaController(ctx echo.Context) error { const actionType = "获取区域主控" acID := ctx.Param("id") - idUint, err := strconv.ParseUint(acID, 10, 64) - if err != nil { - c.logger.Errorf("%s: 区域主控ID格式错误: %v, ID: %s", actionType, err, acID) - return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的区域主控ID格式", actionType, "ID格式错误", acID) - } - - ac, err := c.areaControllerRepo.FindByID(uint(idUint)) + resp, err := c.deviceService.GetAreaController(acID) if err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { c.logger.Warnf("%s: 区域主控不存在, ID: %s", actionType, acID) return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "区域主控未找到", actionType, "区域主控不存在", acID) } - c.logger.Errorf("%s: 数据库查询失败: %v, ID: %s", actionType, err, acID) - return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取区域主控信息失败: "+err.Error(), actionType, "数据库查询失败", acID) + c.logger.Errorf("%s: 服务层获取失败: %v, ID: %s", actionType, err, acID) + return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取区域主控信息失败: "+err.Error(), actionType, "服务层获取失败", acID) } - resp, err := dto.NewAreaControllerResponse(ac) - if err != nil { - c.logger.Errorf("%s: 序列化响应失败: %v, AreaController: %+v", actionType, err, ac) - return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取区域主控信息失败: 内部数据格式错误", actionType, "响应序列化失败", ac) - } - - c.logger.Infof("%s: 获取区域主控信息成功, ID: %d", actionType, ac.ID) + c.logger.Infof("%s: 获取区域主控信息成功, ID: %d", actionType, resp.ID) return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取区域主控信息成功", resp, actionType, "获取区域主控信息成功", resp) } @@ -445,19 +266,13 @@ func (c *Controller) GetAreaController(ctx echo.Context) error { // @Router /api/v1/area-controllers [get] func (c *Controller) ListAreaControllers(ctx echo.Context) error { const actionType = "获取区域主控列表" - acs, err := c.areaControllerRepo.ListAll() + resp, err := c.deviceService.ListAreaControllers() if err != nil { - c.logger.Errorf("%s: 数据库查询失败: %v", actionType, err) - return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取区域主控列表失败: "+err.Error(), actionType, "数据库查询失败", nil) + c.logger.Errorf("%s: 服务层获取列表失败: %v", actionType, err) + return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取区域主控列表失败: "+err.Error(), actionType, "服务层获取列表失败", nil) } - resp, err := dto.NewListAreaControllerResponse(acs) - if err != nil { - c.logger.Errorf("%s: 序列化响应失败: %v, AreaControllers: %+v", actionType, err, acs) - return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取区域主控列表失败: 内部数据格式错误", actionType, "响应序列化失败", acs) - } - - c.logger.Infof("%s: 获取区域主控列表成功, 数量: %d", actionType, len(acs)) + c.logger.Infof("%s: 获取区域主控列表成功, 数量: %d", actionType, len(resp)) return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取区域主控列表成功", resp, actionType, "获取区域主控列表成功", resp) } @@ -476,56 +291,23 @@ func (c *Controller) UpdateAreaController(ctx echo.Context) error { const actionType = "更新区域主控" acID := ctx.Param("id") - idUint, err := strconv.ParseUint(acID, 10, 64) - if err != nil { - c.logger.Errorf("%s: 区域主控ID格式错误: %v, ID: %s", actionType, err, acID) - return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的区域主控ID格式", actionType, "ID格式错误", acID) - } - - existingAC, err := c.areaControllerRepo.FindByID(uint(idUint)) - if err != nil { - if errors.Is(err, gorm.ErrRecordNotFound) { - c.logger.Warnf("%s: 区域主控不存在, ID: %s", actionType, acID) - return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "区域主控未找到", actionType, "区域主控不存在", acID) - } - c.logger.Errorf("%s: 数据库查询失败: %v, ID: %s", actionType, err, acID) - return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "更新区域主控失败: "+err.Error(), actionType, "数据库查询失败", acID) - } - var req dto.UpdateAreaControllerRequest if err := ctx.Bind(&req); err != nil { c.logger.Errorf("%s: 参数绑定失败: %v", actionType, err) return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req) } - propertiesJSON, err := json.Marshal(req.Properties) + resp, err := c.deviceService.UpdateAreaController(acID, &req) if err != nil { - c.logger.Errorf("%s: 序列化属性失败: %v", actionType, err) - return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "属性字段格式错误", actionType, "属性序列化失败", req.Properties) + if errors.Is(err, gorm.ErrRecordNotFound) { + c.logger.Warnf("%s: 区域主控不存在, ID: %s", actionType, acID) + return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "区域主控未找到", actionType, "区域主控不存在", acID) + } + c.logger.Errorf("%s: 服务层更新失败: %v, ID: %s", actionType, err, acID) + return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "更新区域主控失败: "+err.Error(), actionType, "服务层更新失败", acID) } - existingAC.Name = req.Name - existingAC.NetworkID = req.NetworkID - existingAC.Location = req.Location - existingAC.Properties = propertiesJSON - - if err := existingAC.SelfCheck(); err != nil { - c.logger.Errorf("%s: 区域主控自检失败: %v", actionType, err) - return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "区域主控参数不符合要求: "+err.Error(), actionType, "区域主控自检失败", existingAC) - } - - if err := c.areaControllerRepo.Update(existingAC); err != nil { - c.logger.Errorf("%s: 数据库更新失败: %v, AreaController: %+v", actionType, err, existingAC) - return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "更新区域主控失败: "+err.Error(), actionType, "数据库更新失败", acID) - } - - resp, err := dto.NewAreaControllerResponse(existingAC) - if err != nil { - c.logger.Errorf("%s: 序列化响应失败: %v, AreaController: %+v", actionType, err, existingAC) - return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "区域主控更新成功,但响应生成失败", actionType, "响应序列化失败", existingAC) - } - - c.logger.Infof("%s: 区域主控更新成功, ID: %d", actionType, existingAC.ID) + c.logger.Infof("%s: 区域主控更新成功, ID: %d", actionType, resp.ID) return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "区域主控更新成功", resp, actionType, "区域主控更新成功", resp) } @@ -542,28 +324,16 @@ func (c *Controller) DeleteAreaController(ctx echo.Context) error { const actionType = "删除区域主控" acID := ctx.Param("id") - idUint, err := strconv.ParseUint(acID, 10, 64) - if err != nil { - c.logger.Errorf("%s: 区域主控ID格式错误: %v, ID: %s", actionType, err, acID) - return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的区域主控ID格式", actionType, "ID格式错误", acID) - } - - _, err = c.areaControllerRepo.FindByID(uint(idUint)) - if err != nil { + if err := c.deviceService.DeleteAreaController(acID); err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { c.logger.Warnf("%s: 区域主控不存在, ID: %s", actionType, acID) return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "区域主控未找到", actionType, "区域主控不存在", acID) } - c.logger.Errorf("%s: 查找区域主控失败: %v, ID: %s", actionType, err, acID) - return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "删除区域主控失败: 查找时发生内部错误", actionType, "数据库查询失败", acID) + c.logger.Errorf("%s: 服务层删除失败: %v, ID: %s", actionType, err, acID) + return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "删除区域主控失败: "+err.Error(), actionType, "服务层删除失败", acID) } - if err := c.areaControllerRepo.Delete(uint(idUint)); err != nil { - c.logger.Errorf("%s: 数据库删除失败: %v, ID: %d", actionType, err, idUint) - return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "删除区域主控失败: "+err.Error(), actionType, "数据库删除失败", acID) - } - - c.logger.Infof("%s: 区域主控删除成功, ID: %d", actionType, idUint) + c.logger.Infof("%s: 区域主控删除成功, ID: %s", actionType, acID) return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "区域主控删除成功", nil, actionType, "区域主控删除成功", acID) } @@ -587,44 +357,13 @@ func (c *Controller) CreateDeviceTemplate(ctx echo.Context) error { return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req) } - commandsJSON, err := json.Marshal(req.Commands) + resp, err := c.deviceService.CreateDeviceTemplate(&req) if err != nil { - c.logger.Errorf("%s: 序列化命令失败: %v", actionType, err) - return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "命令字段格式错误", actionType, "命令序列化失败", req.Commands) + c.logger.Errorf("%s: 服务层创建失败: %v", actionType, err) + return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "创建设备模板失败: "+err.Error(), actionType, "服务层创建失败", req) } - valuesJSON, err := json.Marshal(req.Values) - if err != nil { - c.logger.Errorf("%s: 序列化值描述符失败: %v", actionType, err) - return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "值描述符字段格式错误", actionType, "值描述符序列化失败", req.Values) - } - - deviceTemplate := &models.DeviceTemplate{ - Name: req.Name, - Manufacturer: req.Manufacturer, - Description: req.Description, - Category: req.Category, - Commands: commandsJSON, - Values: valuesJSON, - } - - if err := deviceTemplate.SelfCheck(); err != nil { - c.logger.Errorf("%s: 设备模板自检失败: %v", actionType, err) - return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "设备模板参数不符合要求: "+err.Error(), actionType, "设备模板自检失败", deviceTemplate) - } - - if err := c.deviceTemplateRepo.Create(deviceTemplate); err != nil { - c.logger.Errorf("%s: 数据库操作失败: %v", actionType, err) - return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "创建设备模板失败: "+err.Error(), actionType, "数据库创建失败", deviceTemplate) - } - - resp, err := dto.NewDeviceTemplateResponse(deviceTemplate) - if err != nil { - c.logger.Errorf("%s: 序列化响应失败: %v", actionType, err) - return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "设备模板创建成功,但响应生成失败", actionType, "响应序列化失败", deviceTemplate) - } - - c.logger.Infof("%s: 设备模板创建成功, ID: %d", actionType, deviceTemplate.ID) + c.logger.Infof("%s: 设备模板创建成功, ID: %d", actionType, resp.ID) return controller.SendSuccessWithAudit(ctx, controller.CodeCreated, "设备模板创建成功", resp, actionType, "设备模板创建成功", resp) } @@ -641,29 +380,17 @@ func (c *Controller) GetDeviceTemplate(ctx echo.Context) error { const actionType = "获取设备模板" dtID := ctx.Param("id") - idUint, err := strconv.ParseUint(dtID, 10, 64) - if err != nil { - c.logger.Errorf("%s: 设备模板ID格式错误: %v, ID: %s", actionType, err, dtID) - return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的设备模板ID格式", actionType, "ID格式错误", dtID) - } - - deviceTemplate, err := c.deviceTemplateRepo.FindByID(uint(idUint)) + resp, err := c.deviceService.GetDeviceTemplate(dtID) if err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { c.logger.Warnf("%s: 设备模板不存在, ID: %s", actionType, dtID) return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "设备模板未找到", actionType, "设备模板不存在", dtID) } - c.logger.Errorf("%s: 数据库查询失败: %v, ID: %s", actionType, err, dtID) - return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取设备模板信息失败: "+err.Error(), actionType, "数据库查询失败", dtID) + c.logger.Errorf("%s: 服务层获取失败: %v, ID: %s", actionType, err, dtID) + return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取设备模板信息失败: "+err.Error(), actionType, "服务层获取失败", dtID) } - resp, err := dto.NewDeviceTemplateResponse(deviceTemplate) - if err != nil { - c.logger.Errorf("%s: 序列化响应失败: %v, DeviceTemplate: %+v", actionType, err, deviceTemplate) - return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取设备模板信息失败: 内部数据格式错误", actionType, "响应序列化失败", deviceTemplate) - } - - c.logger.Infof("%s: 获取设备模板信息成功, ID: %d", actionType, deviceTemplate.ID) + c.logger.Infof("%s: 获取设备模板信息成功, ID: %d", actionType, resp.ID) return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取设备模板信息成功", resp, actionType, "获取设备模板信息成功", resp) } @@ -677,19 +404,13 @@ func (c *Controller) GetDeviceTemplate(ctx echo.Context) error { // @Router /api/v1/device-templates [get] func (c *Controller) ListDeviceTemplates(ctx echo.Context) error { const actionType = "获取设备模板列表" - deviceTemplates, err := c.deviceTemplateRepo.ListAll() + resp, err := c.deviceService.ListDeviceTemplates() if err != nil { - c.logger.Errorf("%s: 数据库查询失败: %v", actionType, err) - return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取设备模板列表失败: "+err.Error(), actionType, "数据库查询失败", nil) + c.logger.Errorf("%s: 服务层获取列表失败: %v", actionType, err) + return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取设备模板列表失败: "+err.Error(), actionType, "服务层获取列表失败", nil) } - resp, err := dto.NewListDeviceTemplateResponse(deviceTemplates) - if err != nil { - c.logger.Errorf("%s: 序列化响应失败: %v, DeviceTemplates: %+v", actionType, err, deviceTemplates) - return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取设备模板列表失败: 内部数据格式错误", actionType, "响应序列化失败", deviceTemplates) - } - - c.logger.Infof("%s: 获取设备模板列表成功, 数量: %d", actionType, len(deviceTemplates)) + c.logger.Infof("%s: 获取设备模板列表成功, 数量: %d", actionType, len(resp)) return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取设备模板列表成功", resp, actionType, "获取设备模板列表成功", resp) } @@ -708,64 +429,23 @@ func (c *Controller) UpdateDeviceTemplate(ctx echo.Context) error { const actionType = "更新设备模板" dtID := ctx.Param("id") - idUint, err := strconv.ParseUint(dtID, 10, 64) - if err != nil { - c.logger.Errorf("%s: 设备模板ID格式错误: %v, ID: %s", actionType, err, dtID) - return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的设备模板ID格式", actionType, "ID格式错误", dtID) - } - - existingDeviceTemplate, err := c.deviceTemplateRepo.FindByID(uint(idUint)) - if err != nil { - if errors.Is(err, gorm.ErrRecordNotFound) { - c.logger.Warnf("%s: 设备模板不存在, ID: %s", actionType, dtID) - return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "设备模板未找到", actionType, "设备模板不存在", dtID) - } - c.logger.Errorf("%s: 数据库查询失败: %v, ID: %s", actionType, err, dtID) - return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "更新设备模板失败: "+err.Error(), actionType, "数据库查询失败", dtID) - } - var req dto.UpdateDeviceTemplateRequest if err := ctx.Bind(&req); err != nil { c.logger.Errorf("%s: 参数绑定失败: %v", actionType, err) return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req) } - commandsJSON, err := json.Marshal(req.Commands) + resp, err := c.deviceService.UpdateDeviceTemplate(dtID, &req) if err != nil { - c.logger.Errorf("%s: 序列化命令失败: %v", actionType, err) - return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "命令字段格式错误", actionType, "命令序列化失败", req.Commands) + if errors.Is(err, gorm.ErrRecordNotFound) { + c.logger.Warnf("%s: 设备模板不存在, ID: %s", actionType, dtID) + return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "设备模板未找到", actionType, "设备模板不存在", dtID) + } + c.logger.Errorf("%s: 服务层更新失败: %v, ID: %s", actionType, err, dtID) + return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "更新设备模板失败: "+err.Error(), actionType, "服务层更新失败", dtID) } - valuesJSON, err := json.Marshal(req.Values) - if err != nil { - c.logger.Errorf("%s: 序列化值描述符失败: %v", actionType, err) - return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "值描述符字段格式错误", actionType, "值描述符序列化失败", req.Values) - } - - existingDeviceTemplate.Name = req.Name - existingDeviceTemplate.Manufacturer = req.Manufacturer - existingDeviceTemplate.Description = req.Description - existingDeviceTemplate.Category = req.Category - existingDeviceTemplate.Commands = commandsJSON - existingDeviceTemplate.Values = valuesJSON - - if err := existingDeviceTemplate.SelfCheck(); err != nil { - c.logger.Errorf("%s: 设备模板自检失败: %v", actionType, err) - return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "设备模板参数不符合要求: "+err.Error(), actionType, "设备模板自检失败", existingDeviceTemplate) - } - - if err := c.deviceTemplateRepo.Update(existingDeviceTemplate); err != nil { - c.logger.Errorf("%s: 数据库更新失败: %v, DeviceTemplate: %+v", actionType, err, existingDeviceTemplate) - return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "更新设备模板失败: "+err.Error(), actionType, "数据库更新失败", dtID) - } - - resp, err := dto.NewDeviceTemplateResponse(existingDeviceTemplate) - if err != nil { - c.logger.Errorf("%s: 序列化响应失败: %v, DeviceTemplate: %+v", actionType, err, existingDeviceTemplate) - return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "设备模板更新成功,但响应生成失败", actionType, "响应序列化失败", existingDeviceTemplate) - } - - c.logger.Infof("%s: 设备模板更新成功, ID: %d", actionType, existingDeviceTemplate.ID) + c.logger.Infof("%s: 设备模板更新成功, ID: %d", actionType, resp.ID) return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "设备模板更新成功", resp, actionType, "设备模板更新成功", resp) } @@ -782,35 +462,15 @@ func (c *Controller) DeleteDeviceTemplate(ctx echo.Context) error { const actionType = "删除设备模板" dtID := ctx.Param("id") - idUint, err := strconv.ParseUint(dtID, 10, 64) - if err != nil { - c.logger.Errorf("%s: 设备模板ID格式错误: %v, ID: %s", actionType, err, dtID) - return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的设备模板ID格式", actionType, "ID格式错误", dtID) - } - - // 在尝试删除之前,先检查设备模板是否存在 - _, err = c.deviceTemplateRepo.FindByID(uint(idUint)) - if err != nil { + if err := c.deviceService.DeleteDeviceTemplate(dtID); err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { c.logger.Warnf("%s: 设备模板不存在, ID: %s", actionType, dtID) return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "设备模板未找到", actionType, "设备模板不存在", dtID) } - c.logger.Errorf("%s: 查找设备模板失败: %v, ID: %s", actionType, err, dtID) - return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "删除设备模板失败: 查找时发生内部错误", actionType, "数据库查询失败", dtID) + c.logger.Errorf("%s: 服务层删除失败: %v, ID: %s", actionType, err, dtID) + return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "删除设备模板失败: "+err.Error(), actionType, "服务层删除失败", dtID) } - // 调用仓库层的删除方法,该方法会检查模板是否被使用 - if err := c.deviceTemplateRepo.Delete(uint(idUint)); err != nil { - c.logger.Errorf("%s: 数据库删除失败: %v, ID: %d", actionType, err, idUint) - // 如果错误信息包含“设备模板正在被设备使用,无法删除”,则返回特定的错误码 - if strings.Contains(err.Error(), "设备模板正在被设备使用,无法删除") { - return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, err.Error(), actionType, "设备模板正在使用", dtID) - } else { - // 其他数据库错误 - return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "删除设备模板失败: "+err.Error(), actionType, "数据库删除失败", dtID) - } - } - - c.logger.Infof("%s: 设备模板删除成功, ID: %d", actionType, idUint) + c.logger.Infof("%s: 设备模板删除成功, ID: %s", actionType, dtID) return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "设备模板删除成功", nil, actionType, "设备模板删除成功", dtID) } diff --git a/internal/app/service/device_service.go b/internal/app/service/device_service.go new file mode 100644 index 0000000..3589d1f --- /dev/null +++ b/internal/app/service/device_service.go @@ -0,0 +1,373 @@ +package service + +import ( + "encoding/json" + "errors" + "strconv" + + "git.huangwc.com/pig/pig-farm-controller/internal/app/dto" + "git.huangwc.com/pig/pig-farm-controller/internal/domain/device" + "git.huangwc.com/pig/pig-farm-controller/internal/infra/models" + "git.huangwc.com/pig/pig-farm-controller/internal/infra/repository" +) + +// DeviceService 定义了应用层的设备服务接口,用于协调设备相关的业务逻辑。 +type DeviceService interface { + CreateDevice(req *dto.CreateDeviceRequest) (*dto.DeviceResponse, error) + GetDevice(id string) (*dto.DeviceResponse, error) + ListDevices() ([]*dto.DeviceResponse, error) + UpdateDevice(id string, req *dto.UpdateDeviceRequest) (*dto.DeviceResponse, error) + DeleteDevice(id string) error + ManualControl(id string, req *dto.ManualControlDeviceRequest) error + + CreateAreaController(req *dto.CreateAreaControllerRequest) (*dto.AreaControllerResponse, error) + GetAreaController(id string) (*dto.AreaControllerResponse, error) + ListAreaControllers() ([]*dto.AreaControllerResponse, error) + UpdateAreaController(id string, req *dto.UpdateAreaControllerRequest) (*dto.AreaControllerResponse, error) + DeleteAreaController(id string) error + + CreateDeviceTemplate(req *dto.CreateDeviceTemplateRequest) (*dto.DeviceTemplateResponse, error) + GetDeviceTemplate(id string) (*dto.DeviceTemplateResponse, error) + ListDeviceTemplates() ([]*dto.DeviceTemplateResponse, error) + UpdateDeviceTemplate(id string, req *dto.UpdateDeviceTemplateRequest) (*dto.DeviceTemplateResponse, error) + DeleteDeviceTemplate(id string) error +} + +// deviceService 是 DeviceService 接口的具体实现。 +type deviceService struct { + deviceRepo repository.DeviceRepository + areaControllerRepo repository.AreaControllerRepository + deviceTemplateRepo repository.DeviceTemplateRepository + deviceDomainSvc device.Service // 依赖领域服务 +} + +// NewDeviceService 创建一个新的 DeviceService 实例。 +func NewDeviceService( + deviceRepo repository.DeviceRepository, + areaControllerRepo repository.AreaControllerRepository, + deviceTemplateRepo repository.DeviceTemplateRepository, + deviceDomainSvc device.Service, +) DeviceService { + return &deviceService{ + deviceRepo: deviceRepo, + areaControllerRepo: areaControllerRepo, + deviceTemplateRepo: deviceTemplateRepo, + deviceDomainSvc: deviceDomainSvc, + } +} + +// --- Devices --- + +func (s *deviceService) CreateDevice(req *dto.CreateDeviceRequest) (*dto.DeviceResponse, error) { + propertiesJSON, err := json.Marshal(req.Properties) + if err != nil { + return nil, err // Consider wrapping this error for better context + } + + device := &models.Device{ + Name: req.Name, + DeviceTemplateID: req.DeviceTemplateID, + AreaControllerID: req.AreaControllerID, + Location: req.Location, + Properties: propertiesJSON, + } + + if err := device.SelfCheck(); err != nil { + return nil, err + } + + if err := s.deviceRepo.Create(device); err != nil { + return nil, err + } + + createdDevice, err := s.deviceRepo.FindByID(device.ID) + if err != nil { + return nil, err + } + + return dto.NewDeviceResponse(createdDevice) +} + +func (s *deviceService) GetDevice(id string) (*dto.DeviceResponse, error) { + device, err := s.deviceRepo.FindByIDString(id) + if err != nil { + return nil, err + } + return dto.NewDeviceResponse(device) +} + +func (s *deviceService) ListDevices() ([]*dto.DeviceResponse, error) { + devices, err := s.deviceRepo.ListAll() + if err != nil { + return nil, err + } + return dto.NewListDeviceResponse(devices) +} + +func (s *deviceService) UpdateDevice(id string, req *dto.UpdateDeviceRequest) (*dto.DeviceResponse, error) { + existingDevice, err := s.deviceRepo.FindByIDString(id) + if err != nil { + return nil, err + } + + propertiesJSON, err := json.Marshal(req.Properties) + if err != nil { + return nil, err + } + + existingDevice.Name = req.Name + existingDevice.DeviceTemplateID = req.DeviceTemplateID + existingDevice.AreaControllerID = req.AreaControllerID + existingDevice.Location = req.Location + existingDevice.Properties = propertiesJSON + + if err := existingDevice.SelfCheck(); err != nil { + return nil, err + } + + if err := s.deviceRepo.Update(existingDevice); err != nil { + return nil, err + } + + updatedDevice, err := s.deviceRepo.FindByID(existingDevice.ID) + if err != nil { + return nil, err + } + + return dto.NewDeviceResponse(updatedDevice) +} + +func (s *deviceService) DeleteDevice(id string) error { + idUint, err := strconv.ParseUint(id, 10, 64) + if err != nil { + return err + } + + // Check if device exists before deleting + _, err = s.deviceRepo.FindByID(uint(idUint)) + if err != nil { + return err + } + + return s.deviceRepo.Delete(uint(idUint)) +} + +func (s *deviceService) ManualControl(id string, req *dto.ManualControlDeviceRequest) error { + dev, err := s.deviceRepo.FindByIDString(id) + if err != nil { + return err + } + + if req.Action == nil { + return s.deviceDomainSvc.Collect(dev.AreaControllerID, []*models.Device{dev}) + } else { + action := device.DeviceActionStart + switch *req.Action { + case "off": + action = device.DeviceActionStop + case "on": + action = device.DeviceActionStart + default: + return errors.New("invalid action") + } + return s.deviceDomainSvc.Switch(dev, action) + } +} + +// --- Area Controllers --- + +func (s *deviceService) CreateAreaController(req *dto.CreateAreaControllerRequest) (*dto.AreaControllerResponse, error) { + propertiesJSON, err := json.Marshal(req.Properties) + if err != nil { + return nil, err + } + + ac := &models.AreaController{ + Name: req.Name, + NetworkID: req.NetworkID, + Location: req.Location, + Properties: propertiesJSON, + } + + if err := ac.SelfCheck(); err != nil { + return nil, err + } + + if err := s.areaControllerRepo.Create(ac); err != nil { + return nil, err + } + + return dto.NewAreaControllerResponse(ac) +} + +func (s *deviceService) GetAreaController(id string) (*dto.AreaControllerResponse, error) { + idUint, err := strconv.ParseUint(id, 10, 64) + if err != nil { + return nil, err + } + ac, err := s.areaControllerRepo.FindByID(uint(idUint)) + if err != nil { + return nil, err + } + return dto.NewAreaControllerResponse(ac) +} + +func (s *deviceService) ListAreaControllers() ([]*dto.AreaControllerResponse, error) { + acs, err := s.areaControllerRepo.ListAll() + if err != nil { + return nil, err + } + return dto.NewListAreaControllerResponse(acs) +} + +func (s *deviceService) UpdateAreaController(id string, req *dto.UpdateAreaControllerRequest) (*dto.AreaControllerResponse, error) { + idUint, err := strconv.ParseUint(id, 10, 64) + if err != nil { + return nil, err + } + + existingAC, err := s.areaControllerRepo.FindByID(uint(idUint)) + if err != nil { + return nil, err + } + + propertiesJSON, err := json.Marshal(req.Properties) + if err != nil { + return nil, err + } + + existingAC.Name = req.Name + existingAC.NetworkID = req.NetworkID + existingAC.Location = req.Location + existingAC.Properties = propertiesJSON + + if err := existingAC.SelfCheck(); err != nil { + return nil, err + } + + if err := s.areaControllerRepo.Update(existingAC); err != nil { + return nil, err + } + + return dto.NewAreaControllerResponse(existingAC) +} + +func (s *deviceService) DeleteAreaController(id string) error { + idUint, err := strconv.ParseUint(id, 10, 64) + if err != nil { + return err + } + + _, err = s.areaControllerRepo.FindByID(uint(idUint)) + if err != nil { + return err + } + + return s.areaControllerRepo.Delete(uint(idUint)) +} + +// --- Device Templates --- + +func (s *deviceService) CreateDeviceTemplate(req *dto.CreateDeviceTemplateRequest) (*dto.DeviceTemplateResponse, error) { + commandsJSON, err := json.Marshal(req.Commands) + if err != nil { + return nil, err + } + + valuesJSON, err := json.Marshal(req.Values) + if err != nil { + return nil, err + } + + deviceTemplate := &models.DeviceTemplate{ + Name: req.Name, + Manufacturer: req.Manufacturer, + Description: req.Description, + Category: req.Category, + Commands: commandsJSON, + Values: valuesJSON, + } + + if err := deviceTemplate.SelfCheck(); err != nil { + return nil, err + } + + if err := s.deviceTemplateRepo.Create(deviceTemplate); err != nil { + return nil, err + } + + return dto.NewDeviceTemplateResponse(deviceTemplate) +} + +func (s *deviceService) GetDeviceTemplate(id string) (*dto.DeviceTemplateResponse, error) { + idUint, err := strconv.ParseUint(id, 10, 64) + if err != nil { + return nil, err + } + deviceTemplate, err := s.deviceTemplateRepo.FindByID(uint(idUint)) + if err != nil { + return nil, err + } + return dto.NewDeviceTemplateResponse(deviceTemplate) +} + +func (s *deviceService) ListDeviceTemplates() ([]*dto.DeviceTemplateResponse, error) { + deviceTemplates, err := s.deviceTemplateRepo.ListAll() + if err != nil { + return nil, err + } + return dto.NewListDeviceTemplateResponse(deviceTemplates) +} + +func (s *deviceService) UpdateDeviceTemplate(id string, req *dto.UpdateDeviceTemplateRequest) (*dto.DeviceTemplateResponse, error) { + idUint, err := strconv.ParseUint(id, 10, 64) + if err != nil { + return nil, err + } + + existingDeviceTemplate, err := s.deviceTemplateRepo.FindByID(uint(idUint)) + if err != nil { + return nil, err + } + + commandsJSON, err := json.Marshal(req.Commands) + if err != nil { + return nil, err + } + + valuesJSON, err := json.Marshal(req.Values) + if err != nil { + return nil, err + } + + existingDeviceTemplate.Name = req.Name + existingDeviceTemplate.Manufacturer = req.Manufacturer + existingDeviceTemplate.Description = req.Description + existingDeviceTemplate.Category = req.Category + existingDeviceTemplate.Commands = commandsJSON + existingDeviceTemplate.Values = valuesJSON + + if err := existingDeviceTemplate.SelfCheck(); err != nil { + return nil, err + } + + if err := s.deviceTemplateRepo.Update(existingDeviceTemplate); err != nil { + return nil, err + } + + return dto.NewDeviceTemplateResponse(existingDeviceTemplate) +} + +func (s *deviceService) DeleteDeviceTemplate(id string) error { + idUint, err := strconv.ParseUint(id, 10, 64) + if err != nil { + return err + } + + _, err = s.deviceTemplateRepo.FindByID(uint(idUint)) + if err != nil { + return err + } + + return s.deviceTemplateRepo.Delete(uint(idUint)) +} diff --git a/openspec/changes/refactor-business-logic-layering/design.md b/openspec/changes/refactor-business-logic-layering/design.md index e7a2a1b..eddd012 100644 --- a/openspec/changes/refactor-business-logic-layering/design.md +++ b/openspec/changes/refactor-business-logic-layering/design.md @@ -1,10 +1,14 @@ +# `monitor` 模块重构设计 + ## Context -当前, `monitor` 模块的数据转换逻辑(例如, 将 `repository` 层返回的 `models` 实体转换为 `dto` 对象)主要存在于 `internal/app/controller/monitor/monitor_controller.go` 文件中。 +当前, `monitor` 模块的数据转换逻辑(例如, 将 `repository` 层返回的 `models` 实体转换为 `dto` 对象)主要存在于 +`internal/app/controller/monitor/monitor_controller.go` 文件中。 这种设计导致了以下问题: -- **职责不清**:控制器层承担了过多的数据处理任务, 违反了“关注点分离”原则。控制器应主要负责处理 HTTP 请求、参数绑定和调用服务, 而非执行业务或数据转换逻辑。 +- **职责不清**:控制器层承担了过多的数据处理任务, 违反了“关注点分离”原则。控制器应主要负责处理 HTTP 请求、参数绑定和调用服务, + 而非执行业务或数据转换逻辑。 - **代码重复**:如果未来有其他服务需要类似的数据转换, 可能会导致代码重复。 - **可测试性差**:由于转换逻辑与 `echo.Context` 紧密耦合, 对其进行单元测试变得更加复杂。 @@ -12,7 +16,8 @@ ### Goals -- **迁移数据转换逻辑**:将 `monitor` 模块中所有的数据转换逻辑从控制器层 (`monitor_controller.go`) 迁移到服务层 (`monitor_service.go`)。 +- **迁移数据转换逻辑**:将 `monitor` 模块中所有的数据转换逻辑从控制器层 (`monitor_controller.go`) 迁移到服务层 ( + `monitor_service.go`)。 - **统一服务层接口**:使服务层的方法直接接收请求 DTO, 并返回响应 DTO, 从而使服务本身成为一个完整的、自包含的业务逻辑单元。 - **简化控制器**:精简控制器中的代码, 使其只关注其核心职责:请求处理和响应发送。 @@ -25,39 +30,131 @@ ## Decisions - **决策:在服务层完成 DTO 转换** - - **理由**:服务层是封装业务逻辑的核心, 将数据从领域模型 (`models`) 转换为外部表示 (`dto`) 是业务服务的一部分。这样做可以确保任何调用该服务的客户端(无论是控制器、gRPC 服务还是其他服务)都能获得一致的、随时可用的数据结构。 - - **替代方案**:曾考虑在 `dto` 包中创建一个独立的转换层。但最终认为, 将转换逻辑内聚到服务层更能体现其业务属性, 因为服务层最清楚需要暴露哪些数据以及如何组织这些数据。 + - **理由**:服务层是封装业务逻辑的核心, 将数据从领域模型 (`models`) 转换为外部表示 (`dto`) + 是业务服务的一部分。这样做可以确保任何调用该服务的客户端(无论是控制器、gRPC 服务还是其他服务)都能获得一致的、随时可用的数据结构。 + - **替代方案**:曾考虑在 `dto` 包中创建一个独立的转换层。但最终认为, 将转换逻辑内聚到服务层更能体现其业务属性, + 因为服务层最清楚需要暴露哪些数据以及如何组织这些数据。 - **决策:修改服务层接口以直接处理 DTO** - - **具体实现**:计划将 `MonitorService` 接口中的所有 `List...` 方法签名从 `ListSomething(opts repository.ListOptions, page, pageSize int) ([]models.Something, int64, error)` 修改为 `ListSomething(req *dto.ListSomethingRequest) (*dto.ListSomethingResponse, error)`。 - - **理由**:这种设计将极大地简化控制器与服务之间的交互。控制器将不再需要手动构建 `repository.ListOptions` 或在调用服务后手动组装响应 DTO。它只需传递请求 DTO, 然后直接使用服务返回的响应 DTO, 从而实现彻底的解耦。 + - **具体实现**:计划将 `MonitorService` 接口中的所有 `List...` 方法签名从 + `ListSomething(opts repository.ListOptions, page, pageSize int) ([]models.Something, int64, error)` 修改为 + `ListSomething(req *dto.ListSomethingRequest) (*dto.ListSomethingResponse, error)`。 + - **理由**:这种设计将极大地简化控制器与服务之间的交互。控制器将不再需要手动构建 `repository.ListOptions` + 或在调用服务后手动组装响应 DTO。它只需传递请求 DTO, 然后直接使用服务返回的响应 DTO, 从而实现彻底的解耦。 ## Risks / Trade-offs - **风险:意外修改或丢失现有业务逻辑** - - **描述**:在移动代码的过程中, 尤其是像 `ListPlanExecutionLogs` 这样包含特定业务逻辑(获取关联 `plans`)的方法, 存在逻辑被无意中删除或修改的风险。 - - **缓解措施**: - 1. **代码审查**:在重构前后仔细比对原有逻辑, 确保其被完整地迁移到了新的服务层方法中。 - 2. **保留原有实现**:在新的服务层方法中, 将严格按照控制器中原有的顺序——先构建查询选项, 再调用仓库, 最后进行数据转换——来组织代码, 确保逻辑的等效性。 - 3. **测试**:在完成重构后, 必须进行完整的回归测试, 确保所有受影响的 API 端点的行为与重构前完全一致。 + - **描述**:在移动代码的过程中, 尤其是像 `ListPlanExecutionLogs` 这样包含特定业务逻辑(获取关联 `plans`)的方法, + 存在逻辑被无意中删除或修改的风险。 + - **缓解措施**: + 1. **代码审查**:在重构前后仔细比对原有逻辑, 确保其被完整地迁移到了新的服务层方法中。 + 2. **保留原有实现**:在新的服务层方法中, 将严格按照控制器中原有的顺序——先构建查询选项, 再调用仓库, + 最后进行数据转换——来组织代码, 确保逻辑的等效性。 + 3. **测试**:在完成重构后, 必须进行完整的回归测试, 确保所有受影响的 API 端点的行为与重构前完全一致。 ## Migration Plan 本次重构将按以下步骤进行: -1. **修改服务层 (`internal/app/service/monitor_service.go`)** +1. **修改服务层 (`internal/app/service/monitor_service.go`)** - **更新接口**:修改 `MonitorService` 接口中所有 `List...` 方法的签名, 使其接收请求 DTO 并返回响应 DTO。 - - **实现数据转换**:在每个 `List...` 方法的实现中, 添加从请求 DTO 到 `repository.ListOptions` 的转换逻辑, 以及从业仓库返回的 `models` 到响应 DTO 的转换逻辑。对于 `ListPlanExecutionLogs` 等方法, 确保原有的附加业务逻辑(如查询关联 `Plan` 信息)被完整保留。 + - **实现数据转换**:在每个 `List...` 方法的实现中, 添加从请求 DTO 到 `repository.ListOptions` 的转换逻辑, 以及从业仓库返回的 + `models` 到响应 DTO 的转换逻辑。对于 `ListPlanExecutionLogs` 等方法, 确保原有的附加业务逻辑(如查询关联 `Plan` + 信息)被完整保留。 -2. **修改控制器层 (`internal/app/controller/monitor/monitor_controller.go`)** +2. **修改控制器层 (`internal/app/controller/monitor/monitor_controller.go`)** - **移除转换逻辑**:删除所有手动构建 `repository.ListOptions` 和调用 `dto.NewList...Response` 的代码。 - **更新服务调用**:修改对 `monitorService` 的调用, 使其传递完整的请求 DTO, 并直接处理返回的响应 DTO。 - **简化日志**:调整日志记录, 以便从服务层返回的 DTO 中获取列表长度和总记录数。 -3. **验证** +3. **验证** - 通过静态代码分析和审查, 确认代码风格和逻辑的正确性。 - 进行完整的单元测试和集成测试, 以确保重构没有引入任何回归问题。 ## Open Questions - 暂无。 + +--- + +## `device` 模块重构设计 + +### Context + +`device_controller.go` 当前直接依赖多个 `repository` 和 `domain.Service`,并在其方法内部执行了大量本应属于应用服务层的逻辑,包括: + +- **直接的数据库操作**:调用 `repository` 的 `Create`, `Update`, `Delete`, `Find` 等方法。 +- **领域模型实例化**:通过 `&models.Device{...}` 直接创建数据库模型。 +- **内部字段序列化**:对 `Properties`, `Commands`, `Values` 等字段执行 `json.Marshal`。 +- **业务规则验证**:调用 `model.SelfCheck()`。 +- **复杂的错误处理**:通过 `errors.Is` 和 `strings.Contains` 解析底层数据库错误。 +- **DTO 转换**:在方法末尾调用 `dto.New...Response`。 + +这种设计导致控制器与基础设施层和领域层紧密耦合,违反了分层架构的原则。 + +### Goals / Non-Goals + +#### Goals + +- **创建应用服务层**:引入一个新的 `internal/app/service/device_service.go` 来封装业务逻辑。 +- **迁移业务逻辑**:将上述所有在控制器中识别出的业务逻辑和数据处理任务,全部迁移到新的 `DeviceService` 中。 +- **简化控制器**:使 `device_controller.go` 只负责 HTTP 请求处理和对新 `DeviceService` 的调用。 +- **保持领域服务纯粹**:确保 `internal/domain/device/device_service.go` 继续专注于核心领域逻辑,不与 DTO 发生耦合。 + +#### Non-Goals + +- **不改变领域服务**:不对 `domain.device.Service` 的接口和实现进行任何修改。 +- **不改变 API 契约**:对外暴露的 API 接口、请求和响应格式保持不变。 + +### Decisions + +- **决策:引入新的应用服务 `DeviceService`** + - **理由**:这是解决控制器职责过重和分层不清问题的标准做法。该服务将作为应用层门面,协调 `repository` 和 + `domain.Service`,并为控制器提供一个清晰、稳定的接口。 + - **结构**:`DeviceService` 将依赖于 `DeviceRepository`, `AreaControllerRepository`, `DeviceTemplateRepository` 和 + `domain.device.Service`。 + +- **决策:`DeviceService` 接口全面采用 DTO** + - **具体实现**:接口方法将接收 `dto.Create...Request` 等请求 DTO,并返回 `*dto....Response` 响应 DTO。 + - **理由**:这与 `monitor` 模块的重构决策一致,可以确保应用服务层的接口统一、清晰,并与上层(控制器)和下层(领域/仓库)完全解耦。 + +### Migration Plan + +1. **创建 `internal/app/service/device_service.go` 文件** + - 定义 `DeviceService` 接口,为控制器中的每个处理器方法(`CreateDevice`, `UpdateDevice`, `GetDevice`, `ListDevices`, + `DeleteDevice`, `ManualControl` 等)创建相应的方法。 + - 定义 `deviceService` 结构体,并实现 `DeviceService` 接口。 + - **`Create/Update` 方法实现**: + 1. 接收请求 DTO。 + 2. 执行 `json.Marshal` 转换 `Properties` 等字段。 + 3. 创建 `models.Xxx` 实例。 + 4. 调用 `model.SelfCheck()`。 + 5. 调用 `repository.Create/Update`。 + 6. 调用 `repository.FindByID` 重新加载模型(确保关联数据完整)。 + 7. 调用 `dto.New...Response` 将模型转换为响应 DTO 并返回。 + - **`Get/List` 方法实现**: + 1. 调用 `repository.Find/List`。 + 2. 调用 `dto.New...Response` 转换并返回。 + - **`Delete` 方法实现**: + 1. 调用 `repository.Delete`。 + 2. 捕获并转换特定的“资源被使用”错误。 + - **`ManualControl` 方法实现**: + 1. 调用 `repository.FindByIDString` 加载模型。 + 2. 实现 `action` 字符串到 `device.DeviceAction` 的映射。 + 3. 调用 `domain.device.Service.Switch/Collect`。 + +2. **修改 `internal/app/controller/device/device_controller.go`** + - **更新依赖**:将 `Controller` 的依赖从多个 `repository` 和 `domain.Service` 替换为唯一的 + `app/service.DeviceService`。 + - **简化所有处理器方法**: + 1. 移除所有业务逻辑(`json.Marshal`, `SelfCheck`, `repository` 调用, `dto` 转换等)。 + 2. 每个方法仅保留:参数绑定、调用 `c.deviceService.Method(req)`、错误处理和成功响应。 + +3. **更新依赖注入** + - 在 `cmd/server/wire.go` (或项目中的依赖注入配置处) 更新 `DeviceController` 的创建逻辑,为其注入新创建的 + `DeviceService`。 + +### Open Questions + +- 暂无。 diff --git a/openspec/changes/refactor-business-logic-layering/tasks.md b/openspec/changes/refactor-business-logic-layering/tasks.md index 14b80a4..6948035 100644 --- a/openspec/changes/refactor-business-logic-layering/tasks.md +++ b/openspec/changes/refactor-business-logic-layering/tasks.md @@ -1,7 +1,8 @@ ## 1. 准备工作 - [ ] 1.1 阅读并理解 `openspec/changes/refactor-business-logic-layering/proposal.md`。 -- [ ] 1.2 阅读并理解 'AGENTS.md' +- [ ] 1.2 阅读并理解 `openspec/changes/refactor-business-logic-layering/design.md`。 +- [ ] 1.3 阅读并理解 'AGENTS.md' ## 2. 统一服务层接口输入输出为 DTO @@ -20,36 +21,36 @@ ### 2.2 `device` 模块 -- [ ] 2.2.1 **创建并修改 `internal/app/service/device_service.go`:** - - [ ] 定义 `DeviceService` 接口,包含 `CreateDevice`, `UpdateDevice`, `CreateAreaController`, `UpdateAreaController`, +- [x] 2.2.1 **创建并修改 `internal/app/service/device_service.go`:** + - [x] 定义 `DeviceService` 接口,包含 `CreateDevice`, `UpdateDevice`, `CreateAreaController`, `UpdateAreaController`, `CreateDeviceTemplate`, `UpdateDeviceTemplate`, `GetDevice`, `ListDevices`, `GetAreaController`, `ListAreaControllers`, `GetDeviceTemplate`, `ListDeviceTemplates`, `ManualControl` 等方法。 - - [ ] 为 `CreateDevice`, `UpdateDevice`, `CreateAreaController`, `UpdateAreaController`, `CreateDeviceTemplate`, + - [x] 为 `CreateDevice`, `UpdateDevice`, `CreateAreaController`, `UpdateAreaController`, `CreateDeviceTemplate`, `UpdateDeviceTemplate`, `ManualControl` 方法定义并接收 DTO 作为输入。 - - [ ] 将 `GetDevice`, `ListDevices`, `GetAreaController`, `ListAreaControllers`, `GetDeviceTemplate`, + - [x] 将 `GetDevice`, `ListDevices`, `GetAreaController`, `ListAreaControllers`, `GetDeviceTemplate`, `ListDeviceTemplates` 方法的返回值 `models.Xxx` 或 `[]models.Xxx` 替换为 `dto.XxxResponse` 或 `[]dto.XxxResponse`。 - - [ ] 实现 `DeviceService` 接口。 - - [ ] 在此服务层内部将输入 DTO 转换为 `models` 对象。 - - [ ] 在此服务层内部将 `repository` 或 `domain` 层返回的 `models` 对象转换为 `dto.XxxResponse`。 - - [ ] 将控制器中 `SelfCheck()` 验证逻辑移入此服务层。 - - [ ] 将控制器中 `Properties`, `Commands`, `Values` 的 JSON 序列化逻辑移入此服务层。 - - [ ] 将控制器中 `ManualControl` 的业务逻辑(如动作映射)移入此服务层。 - - [ ] 将控制器中直接调用 `repository` 方法的逻辑移入此服务层。 - - [ ] 将控制器中通过检查 `repository` 错误信息处理业务规则的逻辑移入此服务层。 - - [ ] 调整此服务层对 `internal/domain/device.Service` 的调用,确保传递的是 `models` 或领域对象,而不是 DTO。 -- [ ] 2.2.2 **修改 `internal/app/controller/device/device_controller.go`:** - - [ ] 引入并使用新创建的 `internal/app/service.DeviceService`。 - - [ ] 移除控制器中直接创建 `models.Device`, `models.AreaController`, `models.DeviceTemplate` 对象的逻辑。 - - [ ] 移除控制器中直接调用 `SelfCheck()` 的逻辑。 - - [ ] 移除控制器中直接调用 `repository` 方法的逻辑。 - - [ ] 移除控制器中通过检查 `repository` 错误信息处理业务规则的逻辑。 - - [ ] 移除控制器中 `Properties`, `Commands`, `Values` 的 JSON 序列化逻辑。 - - [ ] 调整服务层方法的调用,使其接收新的服务层输入 DTO 或基本参数,并直接处理服务层返回的 `dto.XxxResponse`。 -- [ ] 2.2.3 **保持 `internal/domain/device/device_service.go` 和 `internal/domain/device/general_device_service.go` + - [x] 实现 `DeviceService` 接口。 + - [x] 在此服务层内部将输入 DTO 转换为 `models` 对象。 + - [x] 在此服务层内部将 `repository` 或 `domain` 层返回的 `models` 对象转换为 `dto.XxxResponse`。 + - [x] 将控制器中 `SelfCheck()` 验证逻辑移入此服务层。 + - [x] 将控制器中 `Properties`, `Commands`, `Values` 的 JSON 序列化逻辑移入此服务层。 + - [x] 将控制器中 `ManualControl` 的业务逻辑(如动作映射)移入此服务层。 + - [x] 将控制器中直接调用 `repository` 方法的逻辑移入此服务层。 + - [x] 将控制器中通过检查 `repository` 错误信息处理业务规则的逻辑移入此服务层。 + - [x] 调整此服务层对 `internal/domain/device.Service` 的调用,确保传递的是 `models` 或领域对象,而不是 DTO。 +- [x] 2.2.2 **修改 `internal/app/controller/device/device_controller.go`:** + - [x] 引入并使用新创建的 `internal/app/service.DeviceService`。 + - [x] 移除控制器中直接创建 `models.Device`, `models.AreaController`, `models.DeviceTemplate` 对象的逻辑。 + - [x] 移除控制器中直接调用 `SelfCheck()` 的逻辑。 + - [x] 移除控制器中直接调用 `repository` 方法的逻辑。 + - [x] 移除控制器中通过检查 `repository` 错误信息处理业务规则的逻辑。 + - [x] 移除控制器中 `Properties`, `Commands`, `Values` 的 JSON 序列化逻辑。 + - [x] 调整服务层方法的调用,使其接收新的服务层输入 DTO 或基本参数,并直接处理服务层返回的 `dto.XxxResponse`。 +- [x] 2.2.3 **保持 `internal/domain/device/device_service.go` 和 `internal/domain/device/general_device_service.go` 专注于领域逻辑:** - - [ ] 确保 `internal/domain/device/device_service.go` 接口方法和 `internal/domain/device/general_device_service.go` + - [x] 确保 `internal/domain/device/device_service.go` 接口方法和 `internal/domain/device/general_device_service.go` 实现方法不直接接收或返回 DTO。 - - [ ] 调整 `internal/domain/device/general_device_service.go` 的方法签名和内部逻辑,以适应其调用方(新的 + - [x] 调整 `internal/domain/device/general_device_service.go` 的方法签名和内部逻辑,以适应其调用方(新的 `internal/app/service.DeviceService`)的调整,如果需要的话。 ### 2.3 `pig-farm` 模块