优化设备服务方法的入参

This commit is contained in:
2025-11-03 17:27:29 +08:00
parent f0b71b47a0
commit 9127eeaf31
5 changed files with 220 additions and 81 deletions

View File

@@ -2,6 +2,7 @@ package device
import (
"errors"
"strconv"
"git.huangwc.com/pig/pig-farm-controller/internal/app/controller"
"git.huangwc.com/pig/pig-farm-controller/internal/app/dto"
@@ -71,7 +72,13 @@ func (c *Controller) GetDevice(ctx echo.Context) error {
const actionType = "获取设备"
deviceID := ctx.Param("id")
resp, err := c.deviceService.GetDevice(deviceID)
id, err := strconv.ParseUint(deviceID, 10, 64)
if err != nil {
c.logger.Errorf("%s: 无效的ID: %s", actionType, deviceID)
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的ID: "+deviceID, actionType, "无效的ID", deviceID)
}
resp, err := c.deviceService.GetDevice(uint(id))
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
c.logger.Warnf("%s: 设备不存在, ID: %s", actionType, deviceID)
@@ -126,7 +133,13 @@ func (c *Controller) UpdateDevice(ctx echo.Context) error {
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req)
}
resp, err := c.deviceService.UpdateDevice(deviceID, &req)
id, err := strconv.ParseUint(deviceID, 10, 64)
if err != nil {
c.logger.Errorf("%s: 无效的ID: %s", actionType, deviceID)
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的ID: "+deviceID, actionType, "无效的ID", deviceID)
}
resp, err := c.deviceService.UpdateDevice(uint(id), &req)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
c.logger.Warnf("%s: 设备不存在, ID: %s", actionType, deviceID)
@@ -153,7 +166,13 @@ func (c *Controller) DeleteDevice(ctx echo.Context) error {
const actionType = "删除设备"
deviceID := ctx.Param("id")
if err := c.deviceService.DeleteDevice(deviceID); err != nil {
id, err := strconv.ParseUint(deviceID, 10, 64)
if err != nil {
c.logger.Errorf("%s: 无效的ID: %s", actionType, deviceID)
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的ID: "+deviceID, actionType, "无效的ID", deviceID)
}
if err := c.deviceService.DeleteDevice(uint(id)); err != nil {
switch {
case errors.Is(err, gorm.ErrRecordNotFound):
c.logger.Warnf("%s: 设备不存在, ID: %s", actionType, deviceID)
@@ -193,7 +212,13 @@ func (c *Controller) ManualControl(ctx echo.Context) error {
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req)
}
if err := c.deviceService.ManualControl(deviceID, &req); err != nil {
id, err := strconv.ParseUint(deviceID, 10, 64)
if err != nil {
c.logger.Errorf("%s: 无效的ID: %s", actionType, deviceID)
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的ID: "+deviceID, actionType, "无效的ID", deviceID)
}
if err := c.deviceService.ManualControl(uint(id), &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)
@@ -248,7 +273,13 @@ func (c *Controller) GetAreaController(ctx echo.Context) error {
const actionType = "获取区域主控"
acID := ctx.Param("id")
resp, err := c.deviceService.GetAreaController(acID)
id, err := strconv.ParseUint(acID, 10, 64)
if err != nil {
c.logger.Errorf("%s: 无效的ID: %s", actionType, acID)
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的ID: "+acID, actionType, "无效的ID", acID)
}
resp, err := c.deviceService.GetAreaController(uint(id))
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
c.logger.Warnf("%s: 区域主控不存在, ID: %s", actionType, acID)
@@ -302,8 +333,13 @@ func (c *Controller) UpdateAreaController(ctx echo.Context) error {
c.logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req)
}
id, err := strconv.ParseUint(acID, 10, 64)
if err != nil {
c.logger.Errorf("%s: 无效的ID: %s", actionType, acID)
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的ID: "+acID, actionType, "无效的ID", acID)
}
resp, err := c.deviceService.UpdateAreaController(acID, &req)
resp, err := c.deviceService.UpdateAreaController(uint(id), &req)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
c.logger.Warnf("%s: 区域主控不存在, ID: %s", actionType, acID)
@@ -330,7 +366,13 @@ func (c *Controller) DeleteAreaController(ctx echo.Context) error {
const actionType = "删除区域主控"
acID := ctx.Param("id")
if err := c.deviceService.DeleteAreaController(acID); err != nil {
id, err := strconv.ParseUint(acID, 10, 64)
if err != nil {
c.logger.Errorf("%s: 无效的ID: %s", actionType, acID)
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的ID: "+acID, actionType, "无效的ID", acID)
}
if err := c.deviceService.DeleteAreaController(uint(id)); err != nil {
switch {
case errors.Is(err, gorm.ErrRecordNotFound):
c.logger.Warnf("%s: 区域主控不存在, ID: %s", actionType, acID)
@@ -391,7 +433,13 @@ func (c *Controller) GetDeviceTemplate(ctx echo.Context) error {
const actionType = "获取设备模板"
dtID := ctx.Param("id")
resp, err := c.deviceService.GetDeviceTemplate(dtID)
id, err := strconv.ParseUint(dtID, 10, 64)
if err != nil {
c.logger.Errorf("%s: 无效的ID: %s", actionType, dtID)
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的ID: "+dtID, actionType, "无效的ID", dtID)
}
resp, err := c.deviceService.GetDeviceTemplate(uint(id))
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
c.logger.Warnf("%s: 设备模板不存在, ID: %s", actionType, dtID)
@@ -446,7 +494,13 @@ func (c *Controller) UpdateDeviceTemplate(ctx echo.Context) error {
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req)
}
resp, err := c.deviceService.UpdateDeviceTemplate(dtID, &req)
id, err := strconv.ParseUint(dtID, 10, 64)
if err != nil {
c.logger.Errorf("%s: 无效的ID: %s", actionType, dtID)
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的ID: "+dtID, actionType, "无效的ID", dtID)
}
resp, err := c.deviceService.UpdateDeviceTemplate(uint(id), &req)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
c.logger.Warnf("%s: 设备模板不存在, ID: %s", actionType, dtID)
@@ -473,7 +527,13 @@ func (c *Controller) DeleteDeviceTemplate(ctx echo.Context) error {
const actionType = "删除设备模板"
dtID := ctx.Param("id")
if err := c.deviceService.DeleteDeviceTemplate(dtID); err != nil {
id, err := strconv.ParseUint(dtID, 10, 64)
if err != nil {
c.logger.Errorf("%s: 无效的ID: %s", actionType, dtID)
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的ID: "+dtID, actionType, "无效的ID", dtID)
}
if err := c.deviceService.DeleteDeviceTemplate(uint(id)); err != nil {
switch {
case errors.Is(err, gorm.ErrRecordNotFound):
c.logger.Warnf("%s: 设备模板不存在, ID: %s", actionType, dtID)