优化
This commit is contained in:
@@ -26,7 +26,7 @@ type departmentPO struct {
|
||||
Ancestors string
|
||||
Sort int
|
||||
LeaderID uint
|
||||
Status bool `gorm:"default:true"`
|
||||
Status *bool `gorm:"default:true"`
|
||||
}
|
||||
|
||||
func (departmentPO) TableName() string { return "sys_departments" }
|
||||
|
||||
@@ -35,7 +35,23 @@ func (r *auditRecorderRepo) CreateError(ctx context.Context, v *biz.ErrorRecord)
|
||||
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 *auditQueryRepo) 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
|
||||
updates := make(map[string]any, 5)
|
||||
if v.Form != "" {
|
||||
updates["form"] = v.Form
|
||||
}
|
||||
if v.Info != "" {
|
||||
updates["info"] = v.Info
|
||||
}
|
||||
if v.Level != "" {
|
||||
updates["level"] = v.Level
|
||||
}
|
||||
if v.Solution != "" {
|
||||
updates["solution"] = v.Solution
|
||||
}
|
||||
if v.Status != "" {
|
||||
updates["status"] = v.Status
|
||||
}
|
||||
return r.data.gormDB.WithContext(ctx).Model(&errorRecordPO{}).Where("id = ?", v.ID).Updates(updates).Error
|
||||
}
|
||||
func (r *auditQueryRepo) DeleteErrors(ctx context.Context, ids []uint) error {
|
||||
return r.data.gormDB.WithContext(ctx).Delete(&errorRecordPO{}, ids).Error
|
||||
|
||||
@@ -2,6 +2,7 @@ package data
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"kra/internal/biz"
|
||||
@@ -47,6 +48,9 @@ func uploadFromPO(v uploadSessionPO) *biz.UploadSession {
|
||||
func (r *mediaRepo) FindCompletedSession(ctx context.Context, userID uint, hash string) (*biz.UploadSession, error) {
|
||||
var po uploadSessionPO
|
||||
if err := r.data.gormDB.WithContext(ctx).Where("user_id = ? AND file_hash = ? AND status = ?", userID, hash, "completed").First(&po).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, biz.ErrUploadSessionNotFound
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return uploadFromPO(po), nil
|
||||
@@ -54,6 +58,9 @@ func (r *mediaRepo) FindCompletedSession(ctx context.Context, userID uint, hash
|
||||
func (r *mediaRepo) FindUploadingSession(ctx context.Context, userID uint, hash string) (*biz.UploadSession, error) {
|
||||
var po uploadSessionPO
|
||||
if err := r.data.gormDB.WithContext(ctx).Where("user_id = ? AND file_hash = ? AND status = ?", userID, hash, "uploading").First(&po).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, biz.ErrUploadSessionNotFound
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return uploadFromPO(po), nil
|
||||
|
||||
@@ -35,9 +35,8 @@ func migrateAll(db *gorm.DB) error {
|
||||
return reconcileReferenceIndexes(db)
|
||||
}
|
||||
|
||||
// Older Kra builds used a status label that is not part of the GVA error-log
|
||||
// contract and is therefore rendered as an unknown state by the compatible
|
||||
// administration page.
|
||||
// Older builds used a status label outside the administration page's supported
|
||||
// state set, so normalize existing rows during migration.
|
||||
func normalizeErrorRecordStatuses(db *gorm.DB) error {
|
||||
return db.Model(&errorRecordPO{}).Where("status = ?", "未解决").Update("status", "未处理").Error
|
||||
}
|
||||
|
||||
@@ -22,7 +22,7 @@ type positionPO struct {
|
||||
Name string `gorm:"index"`
|
||||
Code string
|
||||
Sort int
|
||||
Status bool `gorm:"default:true"`
|
||||
Status *bool `gorm:"default:true"`
|
||||
Remark string
|
||||
}
|
||||
|
||||
|
||||
@@ -174,11 +174,12 @@ func (r *initializationRepo) Initialize(ctx context.Context, input *biz.Database
|
||||
return err
|
||||
}
|
||||
}
|
||||
department := departmentPO{Name: "总公司", ParentID: 0, Ancestors: "0", Sort: 0, Status: true}
|
||||
enabled := true
|
||||
department := departmentPO{Name: "总公司", ParentID: 0, Ancestors: "0", Sort: 0, Status: &enabled}
|
||||
if err := tx.Where("name = ?", department.Name).FirstOrCreate(&department).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
for _, position := range []positionPO{{Name: "总经理", Code: "CEO", Sort: 1, Status: true}, {Name: "普通员工", Code: "STAFF", Sort: 2, Status: true}} {
|
||||
for _, position := range []positionPO{{Name: "总经理", Code: "CEO", Sort: 1, Status: &enabled}, {Name: "普通员工", Code: "STAFF", Sort: 2, Status: &enabled}} {
|
||||
if err := tx.Where("code = ?", position.Code).FirstOrCreate(&position).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -63,6 +63,25 @@ func TestUserAuthorityWritesAreAtomic(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetUserAuthoritiesRejectsMissingUser(t *testing.T) {
|
||||
data := newTransactionTestData(t)
|
||||
ctx := context.Background()
|
||||
if err := data.gormDB.WithContext(ctx).Create(&authorityPO{AuthorityID: 888, AuthorityName: "admin"}).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
repo := &userRepo{data: data}
|
||||
if err := repo.SetUserAuthorities(ctx, 999999, []uint{888}); err == nil || err.Error() != "查询用户数据失败" {
|
||||
t.Fatalf("expected the compatible missing-user error, got %v", err)
|
||||
}
|
||||
var links int64
|
||||
if err := data.gormDB.WithContext(ctx).Model(&userAuthorityPO{}).Where("sys_user_id = ?", 999999).Count(&links).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if links != 0 {
|
||||
t.Fatalf("unexpected authority links for missing user: %d", links)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDictionaryImportKeepsHierarchyInOneTransaction(t *testing.T) {
|
||||
data := newTransactionTestData(t)
|
||||
ctx := context.Background()
|
||||
|
||||
@@ -486,6 +486,10 @@ func (r *userRepo) ListAuthorities(ctx context.Context) ([]*biz.Authority, error
|
||||
|
||||
func (r *userRepo) SetUserAuthorities(ctx context.Context, id uint, authorityIDs []uint) error {
|
||||
return r.data.gormDB.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
var user userPO
|
||||
if err := tx.Where("id = ?", id).First(&user).Error; err != nil {
|
||||
return errors.New("查询用户数据失败")
|
||||
}
|
||||
access := &authorityAccessRepo{data: r.data}
|
||||
for _, authorityID := range authorityIDs {
|
||||
if err := access.checkAuthorityIDAuth(ctx, authorityID); err != nil {
|
||||
|
||||
Reference in New Issue
Block a user