159 lines
4.6 KiB
Go
159 lines
4.6 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/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"
|
|
"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 {
|
|
controller.SendErrorResponse(ctx, http.StatusUnauthorized, "无法获取用户信息")
|
|
return
|
|
}
|
|
|
|
user, ok := userValue.(*middleware.AuthUser)
|
|
if !ok {
|
|
controller.SendErrorResponse(ctx, http.StatusInternalServerError, "用户信息格式错误")
|
|
return
|
|
}
|
|
|
|
var req CreateRequest
|
|
if err := ctx.ShouldBindJSON(&req); err != nil {
|
|
controller.SendErrorResponse(ctx, http.StatusBadRequest, "请求参数错误")
|
|
return
|
|
}
|
|
|
|
// 创建操作历史记录
|
|
operation := &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(operation); err != nil {
|
|
c.logger.Error("创建操作历史记录失败: " + err.Error())
|
|
controller.SendErrorResponse(ctx, http.StatusInternalServerError, "创建操作历史记录失败")
|
|
return
|
|
}
|
|
|
|
controller.SendSuccessResponse(ctx, "操作历史记录创建成功", nil)
|
|
}
|
|
|
|
// ListByUser 获取当前用户的所有操作历史记录
|
|
func (c *Controller) ListByUser(ctx *gin.Context) {
|
|
// 从上下文中获取用户信息
|
|
userValue, exists := ctx.Get("user")
|
|
if !exists {
|
|
controller.SendErrorResponse(ctx, http.StatusUnauthorized, "无法获取用户信息")
|
|
return
|
|
}
|
|
|
|
user, ok := userValue.(*middleware.AuthUser)
|
|
if !ok {
|
|
controller.SendErrorResponse(ctx, http.StatusInternalServerError, "用户信息格式错误")
|
|
return
|
|
}
|
|
|
|
// 获取分页参数
|
|
page, _ := strconv.Atoi(ctx.DefaultQuery("page", "1"))
|
|
limit, _ := strconv.Atoi(ctx.DefaultQuery("limit", "10"))
|
|
|
|
// 确保分页参数有效
|
|
if page < 1 {
|
|
page = 1
|
|
}
|
|
if limit < 1 || limit > 100 {
|
|
limit = 10
|
|
}
|
|
|
|
// 计算偏移量
|
|
offset := (page - 1) * limit
|
|
|
|
// 查询操作历史记录
|
|
operations, err := c.operationHistoryRepo.ListByUserID(user.ID, offset, limit)
|
|
if err != nil {
|
|
c.logger.Error("获取操作历史记录失败: " + err.Error())
|
|
controller.SendErrorResponse(ctx, http.StatusInternalServerError, "获取操作历史记录失败")
|
|
return
|
|
}
|
|
|
|
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 {
|
|
controller.SendErrorResponse(ctx, http.StatusUnauthorized, "无法获取用户信息")
|
|
return
|
|
}
|
|
|
|
user, ok := userValue.(*middleware.AuthUser)
|
|
if !ok {
|
|
controller.SendErrorResponse(ctx, http.StatusInternalServerError, "用户信息格式错误")
|
|
return
|
|
}
|
|
|
|
// 检查操作历史记录是否属于当前用户
|
|
if operation.UserID != user.ID {
|
|
controller.SendErrorResponse(ctx, http.StatusForbidden, "无权访问该操作历史记录")
|
|
return
|
|
}
|
|
|
|
controller.SendSuccessResponse(ctx, "获取操作历史记录成功", operation)
|
|
}
|