chore(init): 初始化解忧商行项目并完成 Phase 0 基座

This commit is contained in:
yml2213
2026-10-30 11:46:10 +08:00
commit 8d9481c251
689 changed files with 138703 additions and 0 deletions
@@ -0,0 +1,37 @@
package adminaudit
import (
"time"
"gorm.io/datatypes"
)
type Query struct {
ActorID uint64
Action string
BizType string
Page int
PageSize int
}
type PaginatedResult struct {
Items interface{} `json:"items"`
Total int64 `json:"total"`
Page int `json:"page"`
PageSize int `json:"page_size"`
}
type LogDTO struct {
ID uint64 `json:"id"`
ActorType string `json:"actor_type"`
ActorID uint64 `json:"actor_id"`
ActorUsername string `json:"actor_username"`
ActorNickname string `json:"actor_nickname"`
Action string `json:"action"`
BizType string `json:"biz_type"`
BizID *uint64 `json:"biz_id"`
IP string `json:"ip"`
UserAgent string `json:"user_agent"`
Detail datatypes.JSON `json:"detail"`
CreatedAt time.Time `json:"created_at"`
}
@@ -0,0 +1,75 @@
package adminaudit
import (
"errors"
"net/http"
"strconv"
"hfb_sys/backend/pkg/response"
"github.com/gin-gonic/gin"
)
type Handler struct {
service *Service
}
func NewHandler(service *Service) *Handler {
return &Handler{service: service}
}
func parsePagination(c *gin.Context) (int, int) {
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
pageSize, _ := strconv.Atoi(c.DefaultQuery("page_size", "20"))
if page < 1 {
page = 1
}
if pageSize < 1 {
pageSize = 20
}
if pageSize > 100 {
pageSize = 100
}
return page, pageSize
}
func (h *Handler) List(c *gin.Context) {
query, ok := parseQuery(c)
if !ok {
return
}
result, err := h.service.List(c.Request.Context(), query)
if err != nil {
writeAuditError(c, err)
return
}
response.OK(c, result)
}
func parseQuery(c *gin.Context) (Query, bool) {
var query Query
if raw := c.Query("actor_id"); raw != "" {
value, err := strconv.ParseUint(raw, 10, 64)
if err != nil || value == 0 {
response.BadRequest(c, "管理员 ID 不正确")
return query, false
}
query.ActorID = value
}
query.Action = c.Query("action")
query.BizType = c.Query("biz_type")
page, pageSize := parsePagination(c)
query.Page = page
query.PageSize = pageSize
return query, true
}
func writeAuditError(c *gin.Context, err error) {
response.RecordError(c, err)
switch {
case errors.Is(err, ErrDependencyUnavailable):
response.ServiceUnavailable(c, "数据库未连接")
default:
response.Error(c, http.StatusInternalServerError, "internal_error", "审计日志服务暂时不可用")
}
}
@@ -0,0 +1,89 @@
package adminaudit
import (
"context"
"time"
"hfb_sys/backend/internal/model"
"gorm.io/datatypes"
"gorm.io/gorm"
)
type Repository struct {
db *gorm.DB
}
func NewRepository(db *gorm.DB) *Repository {
return &Repository{db: db}
}
func (r *Repository) List(ctx context.Context, query Query) (*PaginatedResult, error) {
db := r.db.WithContext(ctx).Table("audit_logs AS al").
Select(`al.id, al.actor_type, al.actor_id, COALESCE(au.username, '') AS actor_username,
COALESCE(au.nickname, '') AS actor_nickname, al.action, al.biz_type, al.biz_id,
al.ip, al.user_agent, al.detail, al.created_at`).
Joins("LEFT JOIN admin_users AS au ON au.id = al.actor_id AND al.actor_type = ?", "admin")
countDB := r.db.WithContext(ctx).Model(&model.AuditLog{})
if query.ActorID > 0 {
db = db.Where("al.actor_id = ?", query.ActorID)
countDB = countDB.Where("actor_id = ?", query.ActorID)
}
if query.Action != "" {
db = db.Where("al.action = ?", query.Action)
countDB = countDB.Where("action = ?", query.Action)
}
if query.BizType != "" {
db = db.Where("al.biz_type = ?", query.BizType)
countDB = countDB.Where("biz_type = ?", query.BizType)
}
var total int64
if err := countDB.Count(&total).Error; err != nil {
return nil, err
}
offset := (query.Page - 1) * query.PageSize
var rows []auditLogRow
if err := db.Order("al.id DESC").Offset(offset).Limit(query.PageSize).Scan(&rows).Error; err != nil {
return nil, err
}
items := make([]LogDTO, 0, len(rows))
for _, row := range rows {
items = append(items, row.toDTO())
}
return &PaginatedResult{Items: items, Total: total, Page: query.Page, PageSize: query.PageSize}, nil
}
type auditLogRow struct {
ID uint64
ActorType string
ActorID uint64
ActorUsername string
ActorNickname string
Action string
BizType string
BizID *uint64
IP string
UserAgent string
Detail datatypes.JSON
CreatedAt time.Time
}
func (row auditLogRow) toDTO() LogDTO {
return LogDTO{
ID: row.ID,
ActorType: row.ActorType,
ActorID: row.ActorID,
ActorUsername: row.ActorUsername,
ActorNickname: row.ActorNickname,
Action: row.Action,
BizType: row.BizType,
BizID: row.BizID,
IP: row.IP,
UserAgent: row.UserAgent,
Detail: row.Detail,
CreatedAt: row.CreatedAt,
}
}
@@ -0,0 +1,23 @@
package adminaudit
import (
"context"
"errors"
)
var ErrDependencyUnavailable = errors.New("dependency unavailable")
type Service struct {
repo *Repository
}
func NewService(repo *Repository) *Service {
return &Service{repo: repo}
}
func (s *Service) List(ctx context.Context, query Query) (*PaginatedResult, error) {
if s.repo == nil {
return nil, ErrDependencyUnavailable
}
return s.repo.List(ctx, query)
}