Rule.ts•1.85 kB
import { v4 as uuidv4 } from 'uuid';
/**
* 规则基本信息接口
*/
export interface IRule {
id: string;
name: string;
content: string;
projectId?: string; // 如果为空,则为全局规则
createdAt: Date;
updatedAt: Date;
isGlobal: boolean;
description?: string;
}
/**
* 规则类,用于管理全局规则和项目规则
*/
export class Rule implements IRule {
id: string;
name: string;
content: string;
projectId?: string;
createdAt: Date;
updatedAt: Date;
isGlobal: boolean;
description?: string;
constructor(
name: string,
content: string,
isGlobal: boolean = false,
projectId?: string,
description?: string
) {
this.id = uuidv4();
this.name = name;
this.content = content;
this.projectId = isGlobal ? undefined : projectId;
this.isGlobal = isGlobal;
this.createdAt = new Date();
this.updatedAt = new Date();
this.description = description;
}
/**
* 从JSON对象创建规则实例
*/
static fromJSON(json: any): Rule {
const rule = new Rule(
json.name,
json.content,
json.isGlobal,
json.projectId,
json.description
);
rule.id = json.id;
rule.createdAt = new Date(json.createdAt);
rule.updatedAt = new Date(json.updatedAt);
return rule;
}
/**
* 转换为JSON对象
*/
toJSON(): IRule {
return {
id: this.id,
name: this.name,
content: this.content,
projectId: this.projectId,
createdAt: this.createdAt,
updatedAt: this.updatedAt,
isGlobal: this.isGlobal,
description: this.description
};
}
/**
* 更新规则内容
*/
update(content: string, description?: string): void {
this.content = content;
if (description) this.description = description;
this.updatedAt = new Date();
}
}