refactor: separate backend domains and storage adapters
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
package data
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"kra/internal/biz"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type errorRecordPO struct {
|
||||
ID uint `gorm:"primaryKey"`
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
DeletedAt gorm.DeletedAt `gorm:"index"`
|
||||
Form string `gorm:"type:text"`
|
||||
Info string `gorm:"type:text"`
|
||||
Level string
|
||||
RequestID string `gorm:"index"`
|
||||
TraceID string `gorm:"index"`
|
||||
Solution string `gorm:"type:text"`
|
||||
Status string `gorm:"default:未处理"`
|
||||
}
|
||||
|
||||
func (errorRecordPO) TableName() string { return "sys_error" }
|
||||
|
||||
func errorFromPO(v errorRecordPO) *biz.ErrorRecord {
|
||||
return &biz.ErrorRecord{ID: v.ID, CreatedAt: v.CreatedAt, Form: v.Form, Info: v.Info, Level: v.Level, RequestID: v.RequestID, TraceID: v.TraceID, Solution: v.Solution, Status: v.Status}
|
||||
}
|
||||
func (r *auditRepo) CreateError(ctx context.Context, v *biz.ErrorRecord) error {
|
||||
if v.Status == "" {
|
||||
v.Status = "未处理"
|
||||
}
|
||||
return r.data.gormDB.WithContext(ctx).Create(&errorRecordPO{Form: v.Form, Info: v.Info, Level: v.Level, RequestID: v.RequestID, TraceID: v.TraceID, Solution: v.Solution, Status: v.Status}).Error
|
||||
}
|
||||
func (r *auditRepo) UpdateError(ctx context.Context, v *biz.ErrorRecord) error {
|
||||
return r.data.gormDB.WithContext(ctx).Model(&errorRecordPO{}).Where("id = ?", v.ID).Updates(map[string]any{"form": v.Form, "info": v.Info, "level": v.Level, "solution": v.Solution, "status": v.Status}).Error
|
||||
}
|
||||
func (r *auditRepo) DeleteErrors(ctx context.Context, ids []uint) error {
|
||||
return r.data.gormDB.WithContext(ctx).Delete(&errorRecordPO{}, ids).Error
|
||||
}
|
||||
func (r *auditRepo) FindError(ctx context.Context, id uint) (*biz.ErrorRecord, error) {
|
||||
var po errorRecordPO
|
||||
if err := r.data.gormDB.WithContext(ctx).First(&po, id).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return errorFromPO(po), nil
|
||||
}
|
||||
func (r *auditRepo) ListErrors(ctx context.Context, page, size int, q *biz.ErrorRecord) ([]*biz.ErrorRecord, int64, error) {
|
||||
if page < 1 {
|
||||
page = 1
|
||||
}
|
||||
if size < 1 {
|
||||
size = 10
|
||||
}
|
||||
db := r.data.gormDB.WithContext(ctx).Model(&errorRecordPO{})
|
||||
if q != nil {
|
||||
if q.Form != "" {
|
||||
db = db.Where("form LIKE ?", "%"+q.Form+"%")
|
||||
}
|
||||
if q.Level != "" {
|
||||
db = db.Where("level = ?", q.Level)
|
||||
}
|
||||
if q.Status != "" {
|
||||
db = db.Where("status = ?", q.Status)
|
||||
}
|
||||
}
|
||||
var total int64
|
||||
if err := db.Count(&total).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
var pos []errorRecordPO
|
||||
if err := db.Order("id desc").Offset((page - 1) * size).Limit(size).Find(&pos).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
out := make([]*biz.ErrorRecord, 0, len(pos))
|
||||
for _, po := range pos {
|
||||
out = append(out, errorFromPO(po))
|
||||
}
|
||||
return out, total, nil
|
||||
}
|
||||
Reference in New Issue
Block a user