201 lines
9.2 KiB
Go
201 lines
9.2 KiB
Go
|
|
package device
|
|||
|
|
|
|||
|
|
import (
|
|||
|
|
"context"
|
|||
|
|
"errors"
|
|||
|
|
"strconv"
|
|||
|
|
|
|||
|
|
"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/app/service"
|
|||
|
|
"git.huangwc.com/pig/pig-farm-controller/internal/infra/logs"
|
|||
|
|
|
|||
|
|
"github.com/labstack/echo/v4"
|
|||
|
|
"gorm.io/gorm"
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
// AreaControllerController 区域主控控制器
|
|||
|
|
type AreaControllerController struct {
|
|||
|
|
ctx context.Context
|
|||
|
|
areaControllerService service.AreaControllerService
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// NewAreaControllerController 创建一个新的区域主控控制器实例
|
|||
|
|
func NewAreaControllerController(
|
|||
|
|
ctx context.Context,
|
|||
|
|
areaControllerService service.AreaControllerService,
|
|||
|
|
) *AreaControllerController {
|
|||
|
|
return &AreaControllerController{
|
|||
|
|
ctx: ctx,
|
|||
|
|
areaControllerService: areaControllerService,
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// CreateAreaController godoc
|
|||
|
|
// @Summary 创建新区域主控
|
|||
|
|
// @Description 根据提供的信息创建一个新区域主控
|
|||
|
|
// @Tags 区域主控管理
|
|||
|
|
// @Security BearerAuth
|
|||
|
|
// @Accept json
|
|||
|
|
// @Produce json
|
|||
|
|
// @Param areaController body dto.CreateAreaControllerRequest true "区域主控信息"
|
|||
|
|
// @Success 200 {object} controller.Response{data=dto.AreaControllerResponse}
|
|||
|
|
// @Router /api/v1/area-controllers [post]
|
|||
|
|
func (c *AreaControllerController) CreateAreaController(ctx echo.Context) error {
|
|||
|
|
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "CreateAreaController")
|
|||
|
|
|
|||
|
|
const actionType = "创建区域主控"
|
|||
|
|
var req dto.CreateAreaControllerRequest
|
|||
|
|
if err := ctx.Bind(&req); err != nil {
|
|||
|
|
logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
|
|||
|
|
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
resp, err := c.areaControllerService.CreateAreaController(reqCtx, &req)
|
|||
|
|
if err != nil {
|
|||
|
|
logger.Errorf("%s: 服务层创建失败: %v", actionType, err)
|
|||
|
|
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "创建区域主控失败: "+err.Error(), actionType, "服务层创建失败", req)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
logger.Infof("%s: 区域主控创建成功, ID: %d", actionType, resp.ID)
|
|||
|
|
return controller.SendSuccessWithAudit(ctx, controller.CodeCreated, "区域主控创建成功", resp, actionType, "区域主控创建成功", resp)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// GetAreaController godoc
|
|||
|
|
// @Summary 获取区域主控信息
|
|||
|
|
// @Description 根据ID获取单个区域主控的详细信息
|
|||
|
|
// @Tags 区域主控管理
|
|||
|
|
// @Security BearerAuth
|
|||
|
|
// @Produce json
|
|||
|
|
// @Param id path string true "区域主控ID"
|
|||
|
|
// @Success 200 {object} controller.Response{data=dto.AreaControllerResponse}
|
|||
|
|
// @Router /api/v1/area-controllers/{id} [get]
|
|||
|
|
func (c *AreaControllerController) GetAreaController(ctx echo.Context) error {
|
|||
|
|
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "GetAreaController")
|
|||
|
|
|
|||
|
|
const actionType = "获取区域主控"
|
|||
|
|
acID := ctx.Param("id")
|
|||
|
|
|
|||
|
|
id, err := strconv.ParseUint(acID, 10, 64)
|
|||
|
|
if err != nil {
|
|||
|
|
logger.Errorf("%s: 无效的ID: %s", actionType, acID)
|
|||
|
|
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的ID: "+acID, actionType, "无效的ID", acID)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
resp, err := c.areaControllerService.GetAreaController(reqCtx, uint32(id))
|
|||
|
|
if err != nil {
|
|||
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|||
|
|
logger.Warnf("%s: 区域主控不存在, ID: %s", actionType, acID)
|
|||
|
|
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "区域主控未找到", actionType, "区域主控不存在", acID)
|
|||
|
|
}
|
|||
|
|
logger.Errorf("%s: 服务层获取失败: %v, ID: %s", actionType, err, acID)
|
|||
|
|
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取区域主控信息失败: "+err.Error(), actionType, "服务层获取失败", acID)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
logger.Infof("%s: 获取区域主控信息成功, ID: %d", actionType, resp.ID)
|
|||
|
|
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取区域主控信息成功", resp, actionType, "获取区域主控信息成功", resp)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// ListAreaControllers godoc
|
|||
|
|
// @Summary 获取所有区域主控列表
|
|||
|
|
// @Description 获取系统中所有区域主控的列表
|
|||
|
|
// @Tags 区域主控管理
|
|||
|
|
// @Security BearerAuth
|
|||
|
|
// @Produce json
|
|||
|
|
// @Success 200 {object} controller.Response{data=[]dto.AreaControllerResponse}
|
|||
|
|
// @Router /api/v1/area-controllers [get]
|
|||
|
|
func (c *AreaControllerController) ListAreaControllers(ctx echo.Context) error {
|
|||
|
|
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "ListAreaControllers")
|
|||
|
|
|
|||
|
|
const actionType = "获取区域主控列表"
|
|||
|
|
resp, err := c.areaControllerService.ListAreaControllers(reqCtx)
|
|||
|
|
if err != nil {
|
|||
|
|
logger.Errorf("%s: 服务层获取列表失败: %v", actionType, err)
|
|||
|
|
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取区域主控列表失败: "+err.Error(), actionType, "服务层获取列表失败", nil)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
logger.Infof("%s: 获取区域主控列表成功, 数量: %d", actionType, len(resp))
|
|||
|
|
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取区域主控列表成功", resp, actionType, "获取区域主控列表成功", resp)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// UpdateAreaController godoc
|
|||
|
|
// @Summary 更新区域主控信息
|
|||
|
|
// @Description 根据ID更新一个已存在的区域主控信息
|
|||
|
|
// @Tags 区域主控管理
|
|||
|
|
// @Security BearerAuth
|
|||
|
|
// @Accept json
|
|||
|
|
// @Produce json
|
|||
|
|
// @Param id path string true "区域主控ID"
|
|||
|
|
// @Param areaController body dto.UpdateAreaControllerRequest true "要更新的区域主控信息"
|
|||
|
|
// @Success 200 {object} controller.Response{data=dto.AreaControllerResponse}
|
|||
|
|
// @Router /api/v1/area-controllers/{id} [put]
|
|||
|
|
func (c *AreaControllerController) UpdateAreaController(ctx echo.Context) error {
|
|||
|
|
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "UpdateAreaController")
|
|||
|
|
|
|||
|
|
const actionType = "更新区域主控"
|
|||
|
|
acID := ctx.Param("id")
|
|||
|
|
|
|||
|
|
var req dto.UpdateAreaControllerRequest
|
|||
|
|
if err := ctx.Bind(&req); err != nil {
|
|||
|
|
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 {
|
|||
|
|
logger.Errorf("%s: 无效的ID: %s", actionType, acID)
|
|||
|
|
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的ID: "+acID, actionType, "无效的ID", acID)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
resp, err := c.areaControllerService.UpdateAreaController(reqCtx, uint32(id), &req)
|
|||
|
|
if err != nil {
|
|||
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|||
|
|
logger.Warnf("%s: 区域主控不存在, ID: %s", actionType, acID)
|
|||
|
|
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "区域主控未找到", actionType, "区域主控不存在", acID)
|
|||
|
|
}
|
|||
|
|
logger.Errorf("%s: 服务层更新失败: %v, ID: %s", actionType, err, acID)
|
|||
|
|
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "更新区域主控失败: "+err.Error(), actionType, "服务层更新失败", acID)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
logger.Infof("%s: 区域主控更新成功, ID: %d", actionType, resp.ID)
|
|||
|
|
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "区域主控更新成功", resp, actionType, "区域主控更新成功", resp)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// DeleteAreaController godoc
|
|||
|
|
// @Summary 删除区域主控
|
|||
|
|
// @Description 根据ID删除一个区域主控(软删除)
|
|||
|
|
// @Tags 区域主控管理
|
|||
|
|
// @Security BearerAuth
|
|||
|
|
// @Produce json
|
|||
|
|
// @Param id path string true "区域主控ID"
|
|||
|
|
// @Success 200 {object} controller.Response
|
|||
|
|
// @Router /api/v1/area-controllers/{id} [delete]
|
|||
|
|
func (c *AreaControllerController) DeleteAreaController(ctx echo.Context) error {
|
|||
|
|
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "DeleteAreaController")
|
|||
|
|
|
|||
|
|
const actionType = "删除区域主控"
|
|||
|
|
acID := ctx.Param("id")
|
|||
|
|
|
|||
|
|
id, err := strconv.ParseUint(acID, 10, 64)
|
|||
|
|
if err != nil {
|
|||
|
|
logger.Errorf("%s: 无效的ID: %s", actionType, acID)
|
|||
|
|
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的ID: "+acID, actionType, "无效的ID", acID)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if err := c.areaControllerService.DeleteAreaController(reqCtx, uint32(id)); err != nil {
|
|||
|
|
switch {
|
|||
|
|
case errors.Is(err, gorm.ErrRecordNotFound):
|
|||
|
|
logger.Warnf("%s: 区域主控不存在, ID: %s", actionType, acID)
|
|||
|
|
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "区域主控未找到", actionType, "区域主控不存在", acID)
|
|||
|
|
case errors.Is(err, service.ErrAreaControllerInUse):
|
|||
|
|
logger.Warnf("%s: 尝试删除正在被使用的主控, ID: %s", actionType, acID)
|
|||
|
|
return controller.SendErrorWithAudit(ctx, controller.CodeConflict, err.Error(), actionType, "主控正在被使用", acID)
|
|||
|
|
default:
|
|||
|
|
logger.Errorf("%s: 服务层删除失败: %v, ID: %s", actionType, err, acID)
|
|||
|
|
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "删除区域主控失败: "+err.Error(), actionType, "服务层删除失败", acID)
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
logger.Infof("%s: 区域主控删除成功, ID: %s", actionType, acID)
|
|||
|
|
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "区域主控删除成功", nil, actionType, "区域主控删除成功", acID)
|
|||
|
|
}
|