1. 增加新类型中继设备
2. 优化代码
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
"strconv"
|
||||
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/api/middleware"
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/controller"
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/logs"
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/model"
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/storage/repository"
|
||||
@@ -41,24 +42,24 @@ func (c *Controller) Create(ctx *gin.Context) {
|
||||
// 从上下文中获取用户信息
|
||||
userValue, exists := ctx.Get("user")
|
||||
if !exists {
|
||||
ctx.JSON(http.StatusUnauthorized, gin.H{"error": "无法获取用户信息"})
|
||||
controller.SendErrorResponse(ctx, http.StatusUnauthorized, "无法获取用户信息")
|
||||
return
|
||||
}
|
||||
|
||||
user, ok := userValue.(*middleware.AuthUser)
|
||||
if !ok {
|
||||
ctx.JSON(http.StatusInternalServerError, gin.H{"error": "用户信息格式错误"})
|
||||
controller.SendErrorResponse(ctx, http.StatusInternalServerError, "用户信息格式错误")
|
||||
return
|
||||
}
|
||||
|
||||
var req CreateRequest
|
||||
if err := ctx.ShouldBindJSON(&req); err != nil {
|
||||
ctx.JSON(http.StatusBadRequest, gin.H{"error": "请求参数错误"})
|
||||
controller.SendErrorResponse(ctx, http.StatusBadRequest, "请求参数错误")
|
||||
return
|
||||
}
|
||||
|
||||
// 创建操作历史记录
|
||||
history := &model.OperationHistory{
|
||||
operation := &model.OperationHistory{
|
||||
UserID: user.ID,
|
||||
Action: req.Action,
|
||||
Target: req.Target,
|
||||
@@ -67,30 +68,13 @@ func (c *Controller) Create(ctx *gin.Context) {
|
||||
Result: req.Result,
|
||||
}
|
||||
|
||||
if err := c.operationHistoryRepo.Create(history); err != nil {
|
||||
if err := c.operationHistoryRepo.Create(operation); err != nil {
|
||||
c.logger.Error("创建操作历史记录失败: " + err.Error())
|
||||
ctx.JSON(http.StatusInternalServerError, gin.H{"error": "创建操作历史记录失败"})
|
||||
controller.SendErrorResponse(ctx, http.StatusInternalServerError, "创建操作历史记录失败")
|
||||
return
|
||||
}
|
||||
|
||||
ctx.JSON(http.StatusOK, gin.H{
|
||||
"message": "操作历史记录创建成功",
|
||||
"data": map[string]interface{}{
|
||||
"id": history.ID,
|
||||
"action": history.Action,
|
||||
"target": history.Target,
|
||||
"parameters": history.Parameters,
|
||||
"status": history.Status,
|
||||
"result": history.Result,
|
||||
"created_at": history.CreatedAt,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
// ListByUserRequest 按用户查询操作历史请求结构体
|
||||
type ListByUserRequest struct {
|
||||
Page int `form:"page" binding:"required"`
|
||||
Limit int `form:"limit" binding:"required"`
|
||||
controller.SendSuccessResponse(ctx, "操作历史记录创建成功", nil)
|
||||
}
|
||||
|
||||
// ListByUser 获取当前用户的所有操作历史记录
|
||||
@@ -98,115 +82,77 @@ func (c *Controller) ListByUser(ctx *gin.Context) {
|
||||
// 从上下文中获取用户信息
|
||||
userValue, exists := ctx.Get("user")
|
||||
if !exists {
|
||||
ctx.JSON(http.StatusUnauthorized, gin.H{"error": "无法获取用户信息"})
|
||||
controller.SendErrorResponse(ctx, http.StatusUnauthorized, "无法获取用户信息")
|
||||
return
|
||||
}
|
||||
|
||||
user, ok := userValue.(*middleware.AuthUser)
|
||||
if !ok {
|
||||
ctx.JSON(http.StatusInternalServerError, gin.H{"error": "用户信息格式错误"})
|
||||
controller.SendErrorResponse(ctx, http.StatusInternalServerError, "用户信息格式错误")
|
||||
return
|
||||
}
|
||||
|
||||
// 解析查询参数
|
||||
var req ListByUserRequest
|
||||
if err := ctx.ShouldBindQuery(&req); err != nil {
|
||||
ctx.JSON(http.StatusBadRequest, gin.H{"error": "查询参数错误"})
|
||||
return
|
||||
}
|
||||
// 获取分页参数
|
||||
page, _ := strconv.Atoi(ctx.DefaultQuery("page", "1"))
|
||||
limit, _ := strconv.Atoi(ctx.DefaultQuery("limit", "10"))
|
||||
|
||||
if req.Page <= 0 {
|
||||
req.Page = 1
|
||||
// 确保分页参数有效
|
||||
if page < 1 {
|
||||
page = 1
|
||||
}
|
||||
|
||||
if req.Limit <= 0 || req.Limit > 100 {
|
||||
req.Limit = 10
|
||||
if limit < 1 || limit > 100 {
|
||||
limit = 10
|
||||
}
|
||||
|
||||
// 计算偏移量
|
||||
offset := (req.Page - 1) * req.Limit
|
||||
offset := (page - 1) * limit
|
||||
|
||||
// 查询用户操作历史记录
|
||||
histories, err := c.operationHistoryRepo.FindByUserID(user.ID)
|
||||
// 查询操作历史记录
|
||||
operations, err := c.operationHistoryRepo.ListByUserID(user.ID, offset, limit)
|
||||
if err != nil {
|
||||
c.logger.Error("查询操作历史记录失败: " + err.Error())
|
||||
ctx.JSON(http.StatusInternalServerError, gin.H{"error": "查询操作历史记录失败"})
|
||||
c.logger.Error("获取操作历史记录失败: " + err.Error())
|
||||
controller.SendErrorResponse(ctx, http.StatusInternalServerError, "获取操作历史记录失败")
|
||||
return
|
||||
}
|
||||
|
||||
// 分页处理
|
||||
start := offset
|
||||
end := start + req.Limit
|
||||
if start > len(histories) {
|
||||
start = len(histories)
|
||||
}
|
||||
if end > len(histories) {
|
||||
end = len(histories)
|
||||
}
|
||||
|
||||
pagedHistories := histories[start:end]
|
||||
|
||||
ctx.JSON(http.StatusOK, gin.H{
|
||||
"message": "查询成功",
|
||||
"data": map[string]interface{}{
|
||||
"histories": pagedHistories,
|
||||
"page": req.Page,
|
||||
"limit": req.Limit,
|
||||
"total": len(histories),
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
// GetRequest 获取单个操作历史记录请求结构体
|
||||
type GetRequest struct {
|
||||
ID string `uri:"id" binding:"required"`
|
||||
controller.SendSuccessResponse(ctx, "获取操作历史记录成功", operations)
|
||||
}
|
||||
|
||||
// Get 获取单个操作历史记录
|
||||
func (c *Controller) Get(ctx *gin.Context) {
|
||||
// 获取操作历史记录ID
|
||||
id, err := strconv.ParseUint(ctx.Param("id"), 10, 64)
|
||||
if err != nil {
|
||||
controller.SendErrorResponse(ctx, http.StatusBadRequest, "无效的操作历史记录ID")
|
||||
return
|
||||
}
|
||||
|
||||
// 查询操作历史记录
|
||||
operation, err := c.operationHistoryRepo.FindByID(uint(id))
|
||||
if err != nil {
|
||||
c.logger.Error("获取操作历史记录失败: " + err.Error())
|
||||
controller.SendErrorResponse(ctx, http.StatusInternalServerError, "获取操作历史记录失败")
|
||||
return
|
||||
}
|
||||
|
||||
// 从上下文中获取用户信息
|
||||
userValue, exists := ctx.Get("user")
|
||||
if !exists {
|
||||
ctx.JSON(http.StatusUnauthorized, gin.H{"error": "无法获取用户信息"})
|
||||
controller.SendErrorResponse(ctx, http.StatusUnauthorized, "无法获取用户信息")
|
||||
return
|
||||
}
|
||||
|
||||
user, ok := userValue.(*middleware.AuthUser)
|
||||
if !ok {
|
||||
ctx.JSON(http.StatusInternalServerError, gin.H{"error": "用户信息格式错误"})
|
||||
controller.SendErrorResponse(ctx, http.StatusInternalServerError, "用户信息格式错误")
|
||||
return
|
||||
}
|
||||
|
||||
// 解析路径参数
|
||||
var req GetRequest
|
||||
if err := ctx.ShouldBindUri(&req); err != nil {
|
||||
ctx.JSON(http.StatusBadRequest, gin.H{"error": "路径参数错误"})
|
||||
// 检查操作历史记录是否属于当前用户
|
||||
if operation.UserID != user.ID {
|
||||
controller.SendErrorResponse(ctx, http.StatusForbidden, "无权访问该操作历史记录")
|
||||
return
|
||||
}
|
||||
|
||||
// 将ID转换为整数
|
||||
id, err := strconv.ParseUint(req.ID, 10, 32)
|
||||
if err != nil {
|
||||
ctx.JSON(http.StatusBadRequest, gin.H{"error": "ID格式错误"})
|
||||
return
|
||||
}
|
||||
|
||||
// 查询操作历史记录
|
||||
history, err := c.operationHistoryRepo.FindByID(uint(id))
|
||||
if err != nil {
|
||||
c.logger.Error("查询操作历史记录失败: " + err.Error())
|
||||
ctx.JSON(http.StatusNotFound, gin.H{"error": "操作历史记录不存在"})
|
||||
return
|
||||
}
|
||||
|
||||
// 检查是否是当前用户的记录
|
||||
if history.UserID != user.ID {
|
||||
ctx.JSON(http.StatusForbidden, gin.H{"error": "无权访问该记录"})
|
||||
return
|
||||
}
|
||||
|
||||
ctx.JSON(http.StatusOK, gin.H{
|
||||
"message": "查询成功",
|
||||
"data": history,
|
||||
})
|
||||
controller.SendSuccessResponse(ctx, "获取操作历史记录成功", operation)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user