187 lines
5.7 KiB
Go
187 lines
5.7 KiB
Go
package system
|
|
|
|
import (
|
|
"context"
|
|
|
|
"kra/internal/biz/system"
|
|
|
|
"github.com/go-kratos/kratos/v2/errors"
|
|
"github.com/go-kratos/kratos/v2/transport/http"
|
|
)
|
|
|
|
// OperationRecordService 操作记录服务
|
|
type OperationRecordService struct {
|
|
uc *system.OperationRecordUsecase
|
|
}
|
|
|
|
// NewOperationRecordService 创建操作记录服务
|
|
func NewOperationRecordService(uc *system.OperationRecordUsecase) *OperationRecordService {
|
|
return &OperationRecordService{uc: uc}
|
|
}
|
|
|
|
// OperationRecordInfo 操作记录信息
|
|
type OperationRecordInfo struct {
|
|
ID uint `json:"ID"`
|
|
IP string `json:"ip"`
|
|
Method string `json:"method"`
|
|
Path string `json:"path"`
|
|
Status int `json:"status"`
|
|
Latency int64 `json:"latency"`
|
|
Agent string `json:"agent"`
|
|
ErrorMessage string `json:"error_message"`
|
|
Body string `json:"body"`
|
|
Resp string `json:"resp"`
|
|
UserID uint `json:"user_id"`
|
|
User *UserInfo `json:"user,omitempty"`
|
|
}
|
|
|
|
// DeleteOperationRecord 删除操作记录
|
|
func (s *OperationRecordService) DeleteOperationRecord(ctx context.Context, id uint) error {
|
|
return s.uc.DeleteOperationRecord(ctx, id)
|
|
}
|
|
|
|
// DeleteOperationRecordByIdsRequest 批量删除请求
|
|
type DeleteOperationRecordByIdsRequest struct {
|
|
Ids []uint `json:"ids"`
|
|
}
|
|
|
|
// DeleteOperationRecordByIds 批量删除操作记录
|
|
func (s *OperationRecordService) DeleteOperationRecordByIds(ctx context.Context, req *DeleteOperationRecordByIdsRequest) error {
|
|
return s.uc.DeleteOperationRecordByIds(ctx, req.Ids)
|
|
}
|
|
|
|
// GetOperationRecord 获取操作记录
|
|
func (s *OperationRecordService) GetOperationRecord(ctx context.Context, id uint) (*OperationRecordInfo, error) {
|
|
record, err := s.uc.GetOperationRecord(ctx, id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return toOperationRecordInfo(record), nil
|
|
}
|
|
|
|
// GetOperationRecordListRequest 获取操作记录列表请求
|
|
type GetOperationRecordListRequest struct {
|
|
Page int `json:"page"`
|
|
PageSize int `json:"pageSize"`
|
|
Method string `json:"method"`
|
|
Path string `json:"path"`
|
|
Status int `json:"status"`
|
|
}
|
|
|
|
// GetOperationRecordListResponse 获取操作记录列表响应
|
|
type GetOperationRecordListResponse struct {
|
|
List []*OperationRecordInfo `json:"list"`
|
|
Total int64 `json:"total"`
|
|
Page int `json:"page"`
|
|
PageSize int `json:"pageSize"`
|
|
}
|
|
|
|
// GetOperationRecordList 获取操作记录列表
|
|
func (s *OperationRecordService) GetOperationRecordList(ctx context.Context, req *GetOperationRecordListRequest) (*GetOperationRecordListResponse, error) {
|
|
filters := make(map[string]interface{})
|
|
if req.Method != "" {
|
|
filters["method"] = req.Method
|
|
}
|
|
if req.Path != "" {
|
|
filters["path"] = req.Path
|
|
}
|
|
if req.Status != 0 {
|
|
filters["status"] = req.Status
|
|
}
|
|
|
|
list, total, err := s.uc.GetOperationRecordList(ctx, req.Page, req.PageSize, filters)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
result := make([]*OperationRecordInfo, len(list))
|
|
for i, r := range list {
|
|
result[i] = toOperationRecordInfo(r)
|
|
}
|
|
|
|
return &GetOperationRecordListResponse{
|
|
List: result,
|
|
Total: total,
|
|
Page: req.Page,
|
|
PageSize: req.PageSize,
|
|
}, nil
|
|
}
|
|
|
|
// 转换函数
|
|
func toOperationRecordInfo(r *system.OperationRecord) *OperationRecordInfo {
|
|
info := &OperationRecordInfo{
|
|
ID: r.ID,
|
|
IP: r.IP,
|
|
Method: r.Method,
|
|
Path: r.Path,
|
|
Status: r.Status,
|
|
Latency: r.Latency,
|
|
Agent: r.Agent,
|
|
ErrorMessage: r.ErrorMessage,
|
|
Body: r.Body,
|
|
Resp: r.Resp,
|
|
UserID: r.UserID,
|
|
}
|
|
if r.User != nil {
|
|
info.User = toUserInfo(r.User)
|
|
}
|
|
return info
|
|
}
|
|
|
|
// RegisterRoutes 注册路由
|
|
func (s *OperationRecordService) RegisterRoutes(srv *http.Server) {
|
|
r := srv.Route("/")
|
|
|
|
r.DELETE("/sysOperationRecord/deleteSysOperationRecord", s.handleDeleteOperationRecord)
|
|
r.DELETE("/sysOperationRecord/deleteSysOperationRecordByIds", s.handleDeleteOperationRecordByIds)
|
|
r.GET("/sysOperationRecord/findSysOperationRecord", s.handleGetOperationRecord)
|
|
r.GET("/sysOperationRecord/getSysOperationRecordList", s.handleGetOperationRecordList)
|
|
}
|
|
|
|
// HTTP Handlers
|
|
func (s *OperationRecordService) handleDeleteOperationRecord(ctx http.Context) error {
|
|
var req struct {
|
|
ID uint `json:"ID"`
|
|
}
|
|
if err := ctx.Bind(&req); err != nil {
|
|
return err
|
|
}
|
|
if err := s.DeleteOperationRecord(ctx, req.ID); err != nil {
|
|
return errors.BadRequest("DELETE_FAILED", err.Error())
|
|
}
|
|
return ctx.Result(200, map[string]any{"code": 0, "msg": "删除成功"})
|
|
}
|
|
|
|
func (s *OperationRecordService) handleDeleteOperationRecordByIds(ctx http.Context) error {
|
|
var req DeleteOperationRecordByIdsRequest
|
|
if err := ctx.Bind(&req); err != nil {
|
|
return err
|
|
}
|
|
if err := s.DeleteOperationRecordByIds(ctx, &req); err != nil {
|
|
return errors.BadRequest("DELETE_FAILED", err.Error())
|
|
}
|
|
return ctx.Result(200, map[string]any{"code": 0, "msg": "删除成功"})
|
|
}
|
|
|
|
func (s *OperationRecordService) handleGetOperationRecord(ctx http.Context) error {
|
|
idStr := ctx.Query().Get("ID")
|
|
id, _ := parseUint(idStr)
|
|
resp, err := s.GetOperationRecord(ctx, id)
|
|
if err != nil {
|
|
return errors.NotFound("NOT_FOUND", err.Error())
|
|
}
|
|
return ctx.Result(200, map[string]any{"code": 0, "msg": "获取成功", "data": map[string]any{"reSysOperationRecord": resp}})
|
|
}
|
|
|
|
func (s *OperationRecordService) handleGetOperationRecordList(ctx http.Context) error {
|
|
var req GetOperationRecordListRequest
|
|
if err := ctx.BindQuery(&req); err != nil {
|
|
return err
|
|
}
|
|
resp, err := s.GetOperationRecordList(ctx, &req)
|
|
if err != nil {
|
|
return errors.InternalServer("LIST_ERROR", err.Error())
|
|
}
|
|
return ctx.Result(200, map[string]any{"code": 0, "msg": "获取成功", "data": resp})
|
|
}
|