import { $t, Tools, MarkupTableUtil } from 'platform-core'; import { Processor } from '../Processor'; import { PlaceHolder } from '@/utils/PlaceHolder'; class SingleRule extends Processor { constructor(targetType: string, context?: any) { super(targetType, context); this.PROCESSOR_TYPE = 'SINGLE_RULE'; this.EDITOR_DIALOG_WIDTH = 1024; } public getToolbarAction(): any { return { extend: 'add', name: 'singleRule', label: $t('io.sc.engine.rule.core.enums.ProcessorType.' + this.PROCESSOR_TYPE), icon: 'bi-noise-reduction', enableIf: (args: any) => { const valueType = this.context.target.valueType; return valueType === 'io.sc.engine.rule.core.SingleRuleResult'; }, afterClick: (args: any) => { args.grid.getEditorForm().setFieldValue('type', this.PROCESSOR_TYPE); }, }; } public format(row: any): any { const objs: any[] = Tools.json2Object(row.singleRule); if (objs && Tools.isArray(objs) && objs.length > 0) { let header: string = ''; header += ''; header += ' ' + $t('level') + ''; header += ' ' + $t('category') + ''; header += ' ' + $t('condition') + ''; header += ' ' + $t('value') + ''; header += ' ' + $t('message') + ''; header += ''; let body: string = ''; objs.forEach((obj) => { body += ''; body += ' ' + obj.level + ''; body += ' ' + obj.category + ''; body += ' ' + PlaceHolder.replace(obj.condition) + ''; body += ' ' + PlaceHolder.replace(obj.value) + ''; body += ' ' + PlaceHolder.replace(obj.message) + ''; body += ''; }); return MarkupTableUtil.markupTable(header, body, Tools.mergeObject({}, Processor.FORMAT_TABLE_STYLE)); } return row.singleRule; } public getEditorFields(): any { return [ { colSpan: 12, name: 'singleRule', label: $t('re.processor.grid.entity.singleRule'), showIf: (args: any) => { return args.form.getFieldValue('type') === this.PROCESSOR_TYPE; }, type: 'w-grid', height: 300, localMode: true, dbClickOperation: 'edit', autoFetchData: false, denseBody: false, dndMode: 'local', pageable: false, configButton: false, toolbarConfigure: { noIcon: false }, toolbarActions: [ 'add', 'clone', 'edit', 'remove', 'separator', { name: 'example', label: $t('example'), icon: 'bi-balloon', click: (args: any) => { const sampleData = [ { level: 1, category: '财务预警', condition: '${资产负债率}>0.3', value: '${资产负债率}', message: '资产负债率: ${资产负债率}, 大于 0.3' }, { level: 1, category: '财务预警', condition: '${净利润率}>0.3', value: '${净利润率}', message: '净利润率: ${净利润率}, 大于 0.3' }, ]; args.grid.setLocalData(sampleData); }, }, ], columns: [ { width: 60, name: 'level', label: $t('level') }, { width: 100, name: 'category', label: $t('category') }, { width: 250, name: 'condition', label: $t('condition') }, { width: 200, name: 'value', label: $t('value') }, { width: '100%', name: 'message', label: $t('message') }, ], editor: { cellDialog: { width: '1024px', }, form: { colsNum: 1, fields: [ { name: 'level', label: $t('level'), type: 'w-integer' }, { name: 'category', label: $t('category'), type: 'w-text' }, { name: 'condition', label: $t('condition'), type: 'w-code-mirror', lang: 'java', rows: 4, lineWrap: true, lineBreak: false, placeholder: true, autoCompletion: this.autoCompletionManager.autoCompletion(), userDefinedFunctions: this.userDefinedFunctionsManager.userDefinedFunctions(), }, { name: 'value', label: $t('value'), type: 'w-code-mirror', lang: 'java', rows: 4, lineWrap: true, lineBreak: false, placeholder: true, autoCompletion: this.autoCompletionManager.autoCompletion(), userDefinedFunctions: this.userDefinedFunctionsManager.userDefinedFunctions(), }, { name: 'message', label: $t('message'), type: 'w-code-mirror', lang: 'java', rows: 4, lineWrap: true, lineBreak: false, placeholder: true, autoCompletion: this.autoCompletionManager.autoCompletion(), userDefinedFunctions: this.userDefinedFunctionsManager.userDefinedFunctions(), }, ], }, }, onAfterEditorOpen: (args: any) => { this.initAutoCompletionManager(); this.initUserDefinedFunctionsManager(); }, }, ]; } public getViewerFields(): any { return [{ name: 'singleRule', label: $t('re.processor.grid.entity.singleRule') }]; } public beforeEditorDataSubmit(args: any): void { const grid = args.grid.getEditorForm().getFieldComponent('singleRule'); const localData: any[] = grid.getRows(); const rules: any[] = []; localData.forEach((item) => { rules.push({ level: item.level, category: item.category, condition: item.condition, value: item.value, message: item.message, }); }); args.data.singleRule = Tools.object2Json(rules); } public afterEditorOpen(args: any): void { args.grid.getEditorDialog().setWidth(this.EDITOR_DIALOG_WIDTH); this.initAutoCompletionManager(); this.initUserDefinedFunctionsManager(); const form = args.grid.getEditorForm(); const grid = args.grid.getEditorForm().getFieldComponent('singleRule'); switch (form.getStatus()) { case 'add': // 新增时清空表格内容 grid.setLocalData([]); break; case 'edit': // 编辑/复制时填充表格内容 case 'clone': const data = Tools.json2Object(args.data.singleRule); grid.setLocalData(data); break; } } } export { SingleRule };