2
0
mirror of https://github.com/acepanel/panel.git synced 2026-02-04 06:47:20 +08:00
Files
panel/internal/http/rule/not_exists.go
2025-01-17 03:45:19 +08:00

44 lines
1.0 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package rule
import (
"fmt"
"gorm.io/gorm"
)
// NotExists 验证一个值在某个表中的字段中不存在,支持同时判断多个字段
// NotExists verify a value does not exist in a table field, support judging multiple fields at the same time
// 用法notExists:表名称,字段名称,字段名称,字段名称
// Usage: notExists:table_name,field_name,field_name,field_name
// 例子notExists:users,phone,email
// Example: notExists:users,phone,email
type NotExists struct {
db *gorm.DB
}
func NewNotExists(db *gorm.DB) *NotExists {
return &NotExists{db: db}
}
func (r *NotExists) Passes(val any, options ...any) bool {
if len(options) < 2 {
return false
}
tableName := options[0].(string)
fieldNames := options[1:]
query := r.db.Table(tableName).Where(fmt.Sprintf("%s = ?", fieldNames[0]), val)
for _, fieldName := range fieldNames[1:] {
query = query.Or(fmt.Sprintf("%s = ?", fieldName), val)
}
var count int64
err := query.Count(&count).Error
if err != nil {
return false
}
return count == 0
}