Files
pig-farm-controller/internal/controller/operation/operation.go
2025-09-07 21:18:28 +08:00

213 lines
5.5 KiB
Go

// Package operation 提供操作历史相关功能的控制器
// 实现操作历史记录、查询等操作
package operation
import (
"net/http"
"strconv"
"git.huangwc.com/pig/pig-farm-controller/internal/api/middleware"
"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"
"github.com/gin-gonic/gin"
)
// Controller 操作历史控制器
type Controller struct {
operationHistoryRepo repository.OperationHistoryRepo
logger *logs.Logger
}
// NewController 创建操作历史控制器实例
func NewController(operationHistoryRepo repository.OperationHistoryRepo) *Controller {
return &Controller{
operationHistoryRepo: operationHistoryRepo,
logger: logs.NewLogger(),
}
}
// CreateRequest 创建操作历史请求结构体
type CreateRequest struct {
Action string `json:"action" binding:"required"`
Target string `json:"target"`
Parameters string `json:"parameters"`
Status string `json:"status" binding:"required"`
Result string `json:"result"`
}
// Create 创建操作历史记录
func (c *Controller) Create(ctx *gin.Context) {
// 从上下文中获取用户信息
userValue, exists := ctx.Get("user")
if !exists {
ctx.JSON(http.StatusUnauthorized, gin.H{"error": "无法获取用户信息"})
return
}
user, ok := userValue.(*middleware.AuthUser)
if !ok {
ctx.JSON(http.StatusInternalServerError, gin.H{"error": "用户信息格式错误"})
return
}
var req CreateRequest
if err := ctx.ShouldBindJSON(&req); err != nil {
ctx.JSON(http.StatusBadRequest, gin.H{"error": "请求参数错误"})
return
}
// 创建操作历史记录
history := &model.OperationHistory{
UserID: user.ID,
Action: req.Action,
Target: req.Target,
Parameters: req.Parameters,
Status: req.Status,
Result: req.Result,
}
if err := c.operationHistoryRepo.Create(history); err != nil {
c.logger.Error("创建操作历史记录失败: " + err.Error())
ctx.JSON(http.StatusInternalServerError, gin.H{"error": "创建操作历史记录失败"})
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"`
}
// ListByUser 获取当前用户的所有操作历史记录
func (c *Controller) ListByUser(ctx *gin.Context) {
// 从上下文中获取用户信息
userValue, exists := ctx.Get("user")
if !exists {
ctx.JSON(http.StatusUnauthorized, gin.H{"error": "无法获取用户信息"})
return
}
user, ok := userValue.(*middleware.AuthUser)
if !ok {
ctx.JSON(http.StatusInternalServerError, gin.H{"error": "用户信息格式错误"})
return
}
// 解析查询参数
var req ListByUserRequest
if err := ctx.ShouldBindQuery(&req); err != nil {
ctx.JSON(http.StatusBadRequest, gin.H{"error": "查询参数错误"})
return
}
if req.Page <= 0 {
req.Page = 1
}
if req.Limit <= 0 || req.Limit > 100 {
req.Limit = 10
}
// 计算偏移量
offset := (req.Page - 1) * req.Limit
// 查询用户操作历史记录
histories, err := c.operationHistoryRepo.FindByUserID(user.ID)
if err != nil {
c.logger.Error("查询操作历史记录失败: " + err.Error())
ctx.JSON(http.StatusInternalServerError, gin.H{"error": "查询操作历史记录失败"})
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"`
}
// Get 获取单个操作历史记录
func (c *Controller) Get(ctx *gin.Context) {
// 从上下文中获取用户信息
userValue, exists := ctx.Get("user")
if !exists {
ctx.JSON(http.StatusUnauthorized, gin.H{"error": "无法获取用户信息"})
return
}
user, ok := userValue.(*middleware.AuthUser)
if !ok {
ctx.JSON(http.StatusInternalServerError, gin.H{"error": "用户信息格式错误"})
return
}
// 解析路径参数
var req GetRequest
if err := ctx.ShouldBindUri(&req); err != nil {
ctx.JSON(http.StatusBadRequest, gin.H{"error": "路径参数错误"})
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,
})
}