273 lines
8.0 KiB
Go
273 lines
8.0 KiB
Go
package handler
|
|
|
|
import (
|
|
"errors"
|
|
"strconv"
|
|
"time"
|
|
|
|
"kra/internal/biz"
|
|
"kra/internal/server/httpx"
|
|
"kra/internal/service"
|
|
"kra/internal/service/dto"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type Audit struct{ service *service.AuditService }
|
|
|
|
func NewAudit(service *service.AuditService) *Audit { return &Audit{service: service} }
|
|
|
|
func page(c *gin.Context) (int, int) {
|
|
value, _ := strconv.Atoi(c.Query("page"))
|
|
size, _ := strconv.Atoi(c.Query("pageSize"))
|
|
return value, size
|
|
}
|
|
func IDsFromQuery(c *gin.Context) []uint {
|
|
values := c.QueryArray("IDs[]")
|
|
if len(values) == 0 {
|
|
values = c.QueryArray("ids[]")
|
|
}
|
|
ids := make([]uint, 0, len(values))
|
|
for _, value := range values {
|
|
id, _ := strconv.ParseUint(value, 10, 64)
|
|
if id > 0 {
|
|
ids = append(ids, uint(id))
|
|
}
|
|
}
|
|
return ids
|
|
}
|
|
|
|
func (h *Audit) Operations(c *gin.Context) {
|
|
p, size := page(c)
|
|
status, _ := strconv.Atoi(c.Query("status"))
|
|
items, total, err := h.service.OperationsFilter(c.Request.Context(), p, size, c.Query("path"), c.Query("method"), status)
|
|
if err != nil {
|
|
httpx.Fail(c, "获取失败")
|
|
return
|
|
}
|
|
httpx.Write(c, httpx.CodeSuccess, httpx.PageResult{List: items, Total: total, Page: p, PageSize: size}, "获取成功")
|
|
}
|
|
func (h *Audit) Operation(c *gin.Context) {
|
|
id, _ := strconv.ParseUint(c.Query("ID"), 10, 64)
|
|
item, err := h.service.Operation(c.Request.Context(), uint(id))
|
|
if err != nil {
|
|
httpx.Fail(c, "查询失败")
|
|
return
|
|
}
|
|
httpx.Write(c, httpx.CodeSuccess, gin.H{"reSysOperationRecord": item}, "查询成功")
|
|
}
|
|
func (h *Audit) DeleteOperation(c *gin.Context) {
|
|
var req dto.IDRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
httpx.Fail(c, err.Error())
|
|
return
|
|
}
|
|
if err := h.service.DeleteOperations(c.Request.Context(), []uint{req.ID}); err != nil {
|
|
httpx.Fail(c, "删除失败")
|
|
return
|
|
}
|
|
httpx.Write(c, httpx.CodeSuccess, gin.H{}, "删除成功")
|
|
}
|
|
func (h *Audit) DeleteOperations(c *gin.Context) {
|
|
var req dto.IDsRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
httpx.Fail(c, err.Error())
|
|
return
|
|
}
|
|
if err := h.service.DeleteOperations(c.Request.Context(), req.IDs); err != nil {
|
|
httpx.Fail(c, "批量删除失败")
|
|
return
|
|
}
|
|
httpx.Write(c, httpx.CodeSuccess, gin.H{}, "批量删除成功")
|
|
}
|
|
|
|
func (h *Audit) Logins(c *gin.Context) {
|
|
p, size := page(c)
|
|
status, _ := strconv.ParseBool(c.Query("status"))
|
|
items, total, err := h.service.LoginsFilter(c.Request.Context(), p, size, c.Query("username"), status)
|
|
if err != nil {
|
|
httpx.Fail(c, "获取失败")
|
|
return
|
|
}
|
|
httpx.Write(c, httpx.CodeSuccess, httpx.PageResult{List: items, Total: total, Page: p, PageSize: size}, "获取成功")
|
|
}
|
|
func (h *Audit) Login(c *gin.Context) {
|
|
id, _ := strconv.ParseUint(c.Query("ID"), 10, 64)
|
|
item, err := h.service.Login(c.Request.Context(), uint(id))
|
|
if err != nil {
|
|
httpx.Fail(c, "查询失败")
|
|
return
|
|
}
|
|
httpx.Write(c, httpx.CodeSuccess, item, "查询成功")
|
|
}
|
|
func (h *Audit) DeleteLogin(c *gin.Context) {
|
|
var req dto.IDRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
httpx.Fail(c, err.Error())
|
|
return
|
|
}
|
|
if err := h.service.DeleteLogins(c.Request.Context(), []uint{req.ID}); err != nil {
|
|
httpx.Fail(c, "删除失败")
|
|
return
|
|
}
|
|
httpx.Write(c, httpx.CodeSuccess, gin.H{}, "删除成功")
|
|
}
|
|
func (h *Audit) DeleteLogins(c *gin.Context) {
|
|
var req dto.IDsRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
httpx.Fail(c, err.Error())
|
|
return
|
|
}
|
|
if err := h.service.DeleteLogins(c.Request.Context(), req.IDs); err != nil {
|
|
httpx.Fail(c, "批量删除失败")
|
|
return
|
|
}
|
|
httpx.Write(c, httpx.CodeSuccess, gin.H{}, "批量删除成功")
|
|
}
|
|
|
|
func (h *Audit) DataAccess(c *gin.Context) {
|
|
var req dto.DataAccessListRequest
|
|
if c.ShouldBindJSON(&req) != nil {
|
|
httpx.Fail(c, "参数错误")
|
|
return
|
|
}
|
|
items, total, err := h.service.DataAccessRequest(c.Request.Context(), &req)
|
|
if err != nil {
|
|
httpx.Fail(c, "获取失败:"+err.Error())
|
|
return
|
|
}
|
|
httpx.Write(c, httpx.CodeSuccess, httpx.PageResult{List: items, Total: total, Page: req.Page, PageSize: req.PageSize}, "获取成功")
|
|
}
|
|
func (h *Audit) DeleteDataAccess(c *gin.Context) {
|
|
var req dto.IDsRequest
|
|
if c.ShouldBindJSON(&req) != nil {
|
|
httpx.Fail(c, "参数错误")
|
|
return
|
|
}
|
|
if err := h.service.DeleteDataAccess(c.Request.Context(), req.IDs); err != nil {
|
|
httpx.Fail(c, "删除失败:"+err.Error())
|
|
return
|
|
}
|
|
httpx.Write(c, httpx.CodeSuccess, gin.H{}, "删除成功")
|
|
}
|
|
|
|
func (h *Audit) LogDates(c *gin.Context) {
|
|
data, err := h.service.LogDates(c.Request.Context(), c.Query("month"))
|
|
if err != nil {
|
|
failLogViewer(c, err)
|
|
return
|
|
}
|
|
httpx.Write(c, httpx.CodeSuccess, data, "获取成功")
|
|
}
|
|
func (h *Audit) LogFiles(c *gin.Context) {
|
|
data, err := h.service.LogFiles(c.Request.Context(), c.Query("date"))
|
|
if err != nil {
|
|
failLogViewer(c, err)
|
|
return
|
|
}
|
|
httpx.Write(c, httpx.CodeSuccess, data, "获取成功")
|
|
}
|
|
func (h *Audit) LogContent(c *gin.Context) {
|
|
var cursor *int64
|
|
if raw := c.Query("cursor"); raw != "" {
|
|
value, err := strconv.ParseInt(raw, 10, 64)
|
|
if err != nil || value < 0 {
|
|
httpx.Fail(c, "参数错误")
|
|
return
|
|
}
|
|
cursor = &value
|
|
}
|
|
data, err := h.service.LogContent(c.Request.Context(), c.Query("date"), c.Query("path"), cursor)
|
|
if err != nil {
|
|
failLogViewer(c, err)
|
|
return
|
|
}
|
|
httpx.Write(c, httpx.CodeSuccess, data, "获取成功")
|
|
}
|
|
|
|
func failLogViewer(c *gin.Context, err error) {
|
|
message := "读取日志失败"
|
|
switch {
|
|
case errors.Is(err, biz.ErrInvalidLogMonth):
|
|
message = "日志月份格式不正确"
|
|
case errors.Is(err, biz.ErrInvalidLogDate):
|
|
message = "日志日期格式不正确"
|
|
case errors.Is(err, biz.ErrInvalidLogPath):
|
|
message = "日志文件路径不合法"
|
|
case errors.Is(err, biz.ErrLogFileNotFound):
|
|
message = "日志文件不存在"
|
|
case errors.Is(err, biz.ErrLogFileUnreadable):
|
|
message = "日志文件不可读取"
|
|
case errors.Is(err, biz.ErrLogRootUnavailable):
|
|
message = "日志目录不可读取"
|
|
}
|
|
httpx.Fail(c, message)
|
|
}
|
|
|
|
func (h *Audit) DeleteError(c *gin.Context) {
|
|
id, _ := strconv.ParseUint(c.Query("ID"), 10, 64)
|
|
if err := h.service.DeleteErrors(c.Request.Context(), []uint{uint(id)}); err != nil {
|
|
httpx.Fail(c, "删除失败:"+err.Error())
|
|
return
|
|
}
|
|
httpx.Write(c, httpx.CodeSuccess, gin.H{}, "删除成功")
|
|
}
|
|
func (h *Audit) DeleteErrors(c *gin.Context) {
|
|
if err := h.service.DeleteErrors(c.Request.Context(), IDsFromQuery(c)); err != nil {
|
|
httpx.Fail(c, "批量删除失败:"+err.Error())
|
|
return
|
|
}
|
|
httpx.Write(c, httpx.CodeSuccess, gin.H{}, "批量删除成功")
|
|
}
|
|
func (h *Audit) UpdateError(c *gin.Context) {
|
|
var req dto.ErrorRecordRequest
|
|
if c.ShouldBindJSON(&req) != nil {
|
|
httpx.Fail(c, "参数错误")
|
|
return
|
|
}
|
|
if err := h.service.UpdateErrorRequest(c.Request.Context(), &req); err != nil {
|
|
httpx.Fail(c, "更新失败:"+err.Error())
|
|
return
|
|
}
|
|
httpx.Write(c, httpx.CodeSuccess, gin.H{}, "更新成功")
|
|
}
|
|
func (h *Audit) Error(c *gin.Context) {
|
|
id, _ := strconv.ParseUint(c.Query("ID"), 10, 64)
|
|
item, err := h.service.Error(c.Request.Context(), uint(id))
|
|
if err != nil {
|
|
httpx.Fail(c, "查询失败:"+err.Error())
|
|
return
|
|
}
|
|
httpx.OKWithData(c, item)
|
|
}
|
|
func (h *Audit) Errors(c *gin.Context) {
|
|
p, size := page(c)
|
|
createdAtRange := make([]time.Time, 0, 2)
|
|
for _, raw := range c.QueryArray("createdAtRange[]") {
|
|
value, parseErr := time.Parse(time.RFC3339, raw)
|
|
if parseErr != nil {
|
|
httpx.Fail(c, parseErr.Error())
|
|
return
|
|
}
|
|
createdAtRange = append(createdAtRange, value)
|
|
}
|
|
items, total, err := h.service.ErrorsFilter(c.Request.Context(), p, size, c.Query("form"), c.Query("info"), createdAtRange)
|
|
if err != nil {
|
|
httpx.Fail(c, "获取失败:"+err.Error())
|
|
return
|
|
}
|
|
httpx.Write(c, httpx.CodeSuccess, httpx.PageResult{List: items, Total: total, Page: p, PageSize: size}, "获取成功")
|
|
}
|
|
func (h *Audit) CreateError(c *gin.Context) {
|
|
var req dto.ErrorRecordRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
httpx.Fail(c, err.Error())
|
|
return
|
|
}
|
|
if err := h.service.CreateErrorRequest(c.Request.Context(), &req); err != nil {
|
|
httpx.Fail(c, "创建失败:"+err.Error())
|
|
return
|
|
}
|
|
httpx.Write(c, httpx.CodeSuccess, gin.H{}, "创建成功")
|
|
}
|