import { Component, OnInit, Input, ViewChild, OnChanges } from '@angular/core'; import { MessagerService } from '@farris/ui-messager'; import { NotifyService } from '@farris/ui-notify'; import { ExpressioneditorComponent } from '../expressioneditor/expressioneditor.component'; import { EntityType, NavigationType, Schema } from '../common/Schema'; import { NewEntityType, NewNavigationType, NewSchema } from '../common/NewSchema'; import { ExprEditMode, ExpressionEntity, FunctionEntity, InjectedEntity, InjectedType, SmartHelpEntity, VarEntity } from '../common/CommonEntity'; import { AllOperator, StringOperator, NumberOperator, RelationOperator, LeftBracket, RightBracket, ColumnSize } from "../common/ComputeCommonResource"; import { ComputeExpressUtils, ComputeTreeNode, ConditionRule, ExpressionResult } from '../common/ComputeCommonEntity'; import { ComputeLocalePipe } from '../utils/compute-local.pipe'; @Component({ selector: 'app-compute-rule-editor', templateUrl: './compute-rule-editor.component.html', styleUrls: ['./compute-rule-editor.component.css'] }) export class ComputeRuleEditorComponent implements OnInit, OnChanges { @ViewChild("Advanced") Advanced: ExpressioneditorComponent; @Input() type: ExprEditMode; //表达式类型:Conditional/Advanced。无参数默认为null @Input() text: string; //表达式文本 @Input() injectName: string; //注入实体:自定义名称 @Input() injectSchemaName: string; //map中key值 @Input() injectNameInSchema: string; //数据模型根节点 @Input() injectedType: InjectedType = InjectedType.EntityType; //注入实体类型 @Input() newSchema: NewSchema; //数据模型(新) @Input() newSchemaName: string; //数据模型key(新) @Input() schema: Schema; //数据模型 @Input() schemaName: string; //数据模型key @Input() advancedSessions: VarEntity[] = []; //会话变量(高级表达式) @Input() advancedContexts: VarEntity[] = []; //上下文变量(高级表达式) @Input() advancedFunctions: FunctionEntity[] = []; //扩展函数(高级表达式) @Input() advancedSmartHelps: SmartHelpEntity[] = []; //帮助(高级表达式) expressionEntity: ExpressionEntity = { expr: "", sexpr: "" }; //表达式实体 isNewSchema: boolean; //是否NewSchema private oldId = 10001; //为属性赋值id使用 private newId = 20001; //国际化设置 private localeId: string; private local: ComputeLocalePipe; //简易表达式变量 ColumnSize = ColumnSize; //列长度 LeftBracket = LeftBracket; //左括号 RightBracket = RightBracket; //右括号 relaOpers = []; //逻辑运算符 allOpers = []; //全部操作符 strOpers = []; //字符操作符 numOpers = []; //数字操作符 notifies = {}; //提示信息 headers = []; headButtons = []; //表头按钮 entities = []; //实体列表 propertyList: any; //Farris组件需要实体 operators = []; //所有计算条件的操作符集合 currentCondition: { index: number, condition: ConditionRule } = null; conditions: ConditionRule[] = [{ computeField: '', fieldName: '', value: '', lbracket: '(', rbracket: ')', relation: "&&", operator: "" }]; //高级表达式变量 expressionText: string; isPanel: boolean = true; readOnly: boolean = false; ContextVariables: VarEntity[]; SessionVariables: VarEntity[]; ExtendFunctions: FunctionEntity[]; InjectName: string; InjectSchemaName: string; InjectNameInSchema: string; InjectedType: InjectedType = InjectedType.EntityType; Schema: Schema; SchemaName: string; NewSchema: NewSchema; NewSchemaName: string; constructor(private msgSer: MessagerService, private notifyService: NotifyService) { this.localeId = localStorage.getItem('languageCode'); this.local = new ComputeLocalePipe(this.localeId); this.notifies = { 1: { type: 'success', msg: this.local.transform("Notice_Success") }, 2: { type: 'warning', msg: this.local.transform("Notice_NotComplate") }, 3: { type: 'warning', msg: this.local.transform("Notice_CannotDeleteFirst") }, }; RelationOperator.forEach(x => { if (x.lang == this.localeId) { this.relaOpers = x.resource; } }); AllOperator.forEach(x => { if (x.lang == this.localeId) { this.allOpers = x.resource; } }); StringOperator.forEach(x => { if (x.lang == this.localeId) { this.strOpers = x.resource; } }); NumberOperator.forEach(x => { if (x.lang == this.localeId) { this.numOpers = x.resource; } }); this.headers[0] = this.local.transform("Header_LeftBracket"); this.headers[1] = this.local.transform("Headrt_Data"); this.headers[2] = this.local.transform("Headrt_Operator"); this.headers[3] = this.local.transform("Headrt_Value"); this.headers[4] = this.local.transform("Headrt_RightBracket"); this.headers[5] = this.local.transform("Headrt_Relation"); this.headButtons[0] = this.local.transform("HeadButton_Up"); this.headButtons[1] = this.local.transform("HeadButton_Down"); this.headButtons[2] = this.local.transform("HeadButton_Top"); this.headButtons[3] = this.local.transform("HeadButton_Bottom"); } private conditionStr: string = null; //临时存储条件表达式文本,切换时回显 private expressionStr: string = null; //临时存储高级表达式文本,切换时回显 ngOnInit() { this.change(); } ngOnChanges() { this.change(); } /** * 切换表达式:预置数据等 */ private change() { if (this.type == null) { //不传递type,通过text识别是哪种表达式 //通过text判断是哪种表达式 try { this.expressionEntity = JSON.parse(this.text) as ExpressionEntity; } catch (e3) { this.expressionEntity.expr = ""; this.expressionEntity.sexpr = JSON.stringify([{ computeField: '', fieldName: '', value: '', lbracket: '(', rbracket: ')', relation: "&&", operator: "" }]); } //根据sexpr识别表达式类型 if (this.expressionEntity.sexpr == null || this.expressionEntity.sexpr == "") { //如果sexpr为null或"",为高级表达式 this.type = ExprEditMode.Advanced; //init高级表达式数据 this.initAdvancedExpress(); } else { //sexpr不为空则为条件表达式 this.type = ExprEditMode.Conditional; try { this.conditions = JSON.parse(this.expressionEntity.sexpr); //空数据,预置第一行 if (this.conditions.length == 0) { this.conditions = [{ computeField: '', fieldName: '', value: '', lbracket: '(', rbracket: ')', relation: "&&", operator: "" }]; } } catch (e2) { this.conditions = [{ computeField: '', fieldName: '', value: '', lbracket: '(', rbracket: ')', relation: "&&", operator: "" }]; } //init条件表达式数据 this.initComputeExpress(); } } else if (this.type == ExprEditMode.Conditional) { //条件表达式 //首次打开传入类型/切换记录类型 if (this.conditionStr == null) { //首次打开/切换 条件表达式 try { this.expressionEntity = JSON.parse(this.text); //反序列化表达式实体 try { //反序列化条件表达式文本sexpr let oldExpr = JSON.parse(this.expressionEntity.sexpr) as ConditionRule[]; if (oldExpr.length != 0) { this.conditions = oldExpr; } } catch (err) { //此处出现异常为:高级切换条件,异常吞掉即可 this.conditions = [{ computeField: '', fieldName: '', value: '', lbracket: '(', rbracket: ')', relation: "&&", operator: "" }]; } } catch (e) { //捕获异常,赋默认值 this.expressionEntity = { expr: "", sexpr: JSON.stringify([{ computeField: '', fieldName: '', value: '', lbracket: '(', rbracket: ')', relation: "&&", operator: "" }]) }; } } else { //之前已经切换过条件,conditionStr有缓存值 this.expressionEntity = JSON.parse(this.conditionStr); this.conditions = JSON.parse(this.expressionEntity.sexpr); } //init条件表达式数据 this.initComputeExpress(); } else if (this.type == ExprEditMode.Advanced) { //高级表达式 //首次打开传入类型/切换记录类型 if (this.expressionStr == null) { //首次切换高级表达式 if (this.conditionStr != null) { //切换导致条件表达式有值 try { this.expressionEntity = JSON.parse(this.conditionStr); this.expressionEntity.sexpr = ""; } catch (e1) { this.expressionEntity = { expr: "", sexpr: "" } } } else { //为切换,条件表达式无值 try { this.expressionEntity = JSON.parse(this.text); this.expressionEntity.sexpr = ""; } catch (e) { //捕获异常,第一次切换高级,赋默认值 this.expressionEntity = { expr: "", sexpr: "" } } } } else { this.expressionEntity = JSON.parse(this.expressionStr); } //init高级表达式数据 this.initAdvancedExpress(); } } //init高级表达式数据 private initAdvancedExpress() { //文本+变量+扩展函数 this.expressionText = JSON.stringify(this.expressionEntity); //序列化ExpressionEntity this.ContextVariables = this.advancedContexts; this.SessionVariables = this.advancedSessions; this.ExtendFunctions = this.advancedFunctions; //注入实体 if (this.injectSchemaName != null && this.injectNameInSchema != null) { //注入实体 this.InjectName = this.injectName ? this.injectName : this.injectNameInSchema; this.InjectSchemaName = this.injectSchemaName; this.InjectNameInSchema = this.injectNameInSchema; this.InjectedType = this.injectedType; //数据模型 if (this.newSchema != null && this.newSchemaName != null) { //新版 this.isNewSchema = true; this.NewSchemaName = this.newSchemaName; this.NewSchema = this.newSchema; } else if (this.schema != null && this.schemaName != null) { //旧版 this.isNewSchema = false; this.SchemaName = this.schemaName; this.Schema = this.schema; } } } //init条件表达式数据 private initComputeExpress() { //注入实体&&数据模型 if (this.injectSchemaName != null && this.injectNameInSchema != null) { //注入实体 let injectedEntity = new InjectedEntity(); injectedEntity.name = this.injectName ? this.injectName : this.injectNameInSchema; injectedEntity.schemaName = this.injectSchemaName; injectedEntity.injectedName = this.injectNameInSchema; injectedEntity.injectedType = this.injectedType; //数据模型 if (this.newSchema != null && this.newSchemaName != null) { //新版 let map = new Map(); map.set(this.newSchemaName, this.newSchema); let tempLists: InjectedEntity[] = []; tempLists.push(injectedEntity) this.initNewSchema(tempLists, map); } else if (this.schema != null && this.schemaName != null) { //旧版 let map = new Map(); map.set(this.schemaName, this.schema); let tempLists: InjectedEntity[] = []; tempLists.push(injectedEntity) this.initSchema(tempLists, map); } } //初始化操作符集合 if (this.entities.length > 0) { for (let index in this.conditions) { this.operators[index] = this.allOpers; for (let entity of this.entities) { if (entity.chainCode == this.conditions[index].computeField) { if (entity.type == "String" || entity.type == "Date" || entity.type == "DateTime") this.operators[index] = this.strOpers; else if (entity.type == "Decimal" || entity.type == "Integer" || entity.type == "Byte" || entity.type == "Binary") this.operators[index] = this.numOpers; else this.operators[index] = this.allOpers; break; } } } } this.operators = [...this.operators]; //propertyList初始化 this.propertyList = { columns: [ { "field": "code", "title": this.local.transform("Data_Code"), "width": 130, "hAlign": "left", "align": "left", "formatter": null }, { "field": "name", "title": this.local.transform("Data_Name"), "width": 130, "hAlign": "left", "align": "left", "formatter": null } ], items: this.entities, treeInfo: { "loadDataType": "all", "layerType": "pathcode", "dataField": "treeInfo", "pathField": "path", "layerField": "layer", "isDetailField": "isDetail", "onlySelectLeaf": false, "parentField": "parentElement", "treeDataIsInit": false }, displayType: "TreeList" } } //外部调用接口 /** * 切换表达式类型(简易表达式<--->高级表达式) * @date 2021-01-11 * @author Vincent */ public changeType() { if (this.type == ExprEditMode.Conditional) { //存储条件数据 this.conditionStr = this.getExprOfConditional().text; //切换为高级 this.type = ExprEditMode.Advanced; } else if (this.type == ExprEditMode.Advanced) { //存储高级数据 this.expressionStr = this.getExprOfAdvanced().text; //切换为条件 this.type = ExprEditMode.Conditional; } //模型初始化 this.change(); } /** * 获取保存后表达式结果 * @returns ExpressionResult * @date 2021-01-11 * @author Vincent */ public getExpr(): ExpressionResult { //保存后,置为null this.conditionStr = null; this.expressionStr = null; if (this.type == ExprEditMode.Conditional) { return this.getExprOfConditional(); } else if (this.type == ExprEditMode.Advanced) { return this.getExprOfAdvanced(); } } //高级表达式获取结果 private getExprOfAdvanced(): ExpressionResult { return { type: this.type, text: this.Advanced.getExpr() }; } //简单表达式获取结果 private getExprOfConditional(): ExpressionResult { let result = new ExpressionEntity(); result.expr = ComputeExpressUtils.convertExpr(this.conditions); if (result.expr == "") { //无条件,返回null return { type: this.type, text: null }; } result.sexpr = JSON.stringify(this.conditions); return { type: this.type, text: JSON.stringify(result) }; } /** * 高级表达式语法校验 * @returns Promise 语法错误信息,无语法错误返回null */ public checkAdvanced(): Promise { if (this.type == ExprEditMode.Advanced) return this.Advanced.checkExpression(); else return new Promise(function (resolve) { resolve(null) }); } //新增和删除条件 insert(index: number) { this.conditions.splice(index + 1, 0, { computeField: '', fieldName: '', value: '', lbracket: '(', rbracket: ')', relation: "&&", operator: "" }); this.operators.splice(index + 1, 0, this.allOpers); //操作符集合调整 this.operators = [...this.operators]; } remove(index: number) { let num = index + 1; if (num == 1) { this.notifyService[this.notifies['3'].type]({ msg: this.notifies['3'].msg }); } else { this.msgSer.confirm('确认删除第' + num + '行数据?').subscribe((result) => { if (result) { if (index != 0) { this.conditions.splice(index, 1); this.operators.splice(index, 1); //操作符集合调整 } else if (this.conditions.length > 1) { this.conditions.splice(index, 1); this.operators.splice(index, 1); //操作符集合调整 } else { this.conditions = [{ computeField: '', fieldName: '', value: '', lbracket: '(', rbracket: ')', relation: "&&", operator: "" }]; } if (this.currentCondition && index === this.currentCondition.index) { this.currentCondition = null; } } }); } } //条件选中事件 selected($event) { this.currentCondition = $event; } isSelected($event: number) { return this.currentCondition != null ? this.currentCondition.index === $event : false; } //farris控件变量变更事件 selectChange($event) { if ($event.$event.data.type == "String" || $event.$event.data.type == "Date" || $event.$event.data.type == "DateTime") { this.operators[$event.index] = this.strOpers; this.conditions[$event.index].operator = this.strOpers[0].value; //由于控件默认会展示第一个,所以此处设置第一个。如果此处不赋值的话,页面看起来没有错。但实际是没有值的 } else if ($event.$event.data.type == "Decimal" || $event.$event.data.type == "Integer" || $event.$event.data.type == "Byte" || $event.$event.data.type == "Binary") { this.operators[$event.index] = this.numOpers; this.conditions[$event.index].operator = this.numOpers[0].value; } else { this.operators[$event.index] = this.allOpers; this.conditions[$event.index].operator = this.allOpers[0].value; } this.operators = [...this.operators]; } //条件上下移动 toTop() { if (this.currentCondition) { this.conditions.unshift(this.currentCondition.condition); this.conditions.splice(this.currentCondition.index + 1, 1); this.operators.unshift(this.operators[this.currentCondition.index]); this.operators.splice(this.currentCondition.index + 1, 1); this.currentCondition.index = 0; this.operators = [...this.operators]; } } toUp() { if (this.currentCondition.index == 0) return; else if (this.currentCondition) { let index = this.currentCondition.index; let tempArr = this.conditions.splice(index, 1); let tempOper = this.operators.splice(index, 1); this.conditions.splice(index - 1, 0, ...tempArr); this.operators.splice(index - 1, 0, ...tempOper); this.currentCondition.index = index - 1; this.operators = [...this.operators]; } } toDown() { if (this.currentCondition.index >= this.conditions.length - 1) return; else if (this.currentCondition) { let index = this.currentCondition.index; let tempArr = this.conditions.splice(index, 1); let tempOper = this.operators.splice(index, 1); this.conditions.splice(index + 1, 0, ...tempArr); this.operators.splice(index + 1, 0, ...tempOper); this.currentCondition.index = index + 1; this.operators = [...this.operators]; } } toBottom() { if (this.currentCondition) { this.conditions.push(this.currentCondition.condition); this.conditions.splice(this.currentCondition.index, 1); this.operators.push(this.operators[this.currentCondition.index]); this.operators.splice(this.currentCondition.index, 1); this.currentCondition.index = this.conditions.length - 1; this.operators = [...this.operators]; } } //Schema功能方法 //旧Schema转化 private initSchema(injectedEntities: InjectedEntity[], schema: Map) { if (injectedEntities && injectedEntities.length > 0) { this.entities = []; //实体显示禁忌表 let navigatorMap = new Map(); let numcount = 1; //解析每一个注入实体,将其转化为TreeNode injectedEntities.forEach(injectEntity => { //根据schema名称找到对应的schema let currentSchema = schema.get(injectEntity.schemaName); switch (injectEntity.injectedType) { //注入的若为实体集 case InjectedType.EntitySet: { }; break; //注入的若为实体类型 case InjectedType.EntityType: { let entityType = currentSchema.entityTypes.find(entityType => entityType.name == injectEntity.injectedName); this.initEntityType(entityType, null, currentSchema, navigatorMap, numcount); }; break; } numcount++; }); } } //旧EntityType转化 private initEntityType(entityType: EntityType, parent: ComputeTreeNode, schema: Schema, navigatorMap: any, pathNum: any) { if (entityType) { let child = new ComputeTreeNode; //子节点 child.id = this.oldId++; child.code = entityType.name; child.name = entityType.description; child.chainCode = entityType.name; child.chainName = entityType.description; child.type = "Type"; if (parent) { child.parentId = parent.id; child.chainCode = parent.chainCode + "." + entityType.name; child.chainName = parent.chainName + "." + entityType.description; child.treeInfo.layer = parent.treeInfo.layer + 1; child.treeInfo.path = parent.treeInfo.path + ComputeExpressUtils.getPath(pathNum); } else { child.parentId = null; child.treeInfo.layer = 1; child.treeInfo.path = ComputeExpressUtils.getPath(1); //设置自定义根节点命名 if (this.injectName != null && this.injectName.length != 0) { child.chainCode = this.injectName; child.code = this.injectName; } } this.entities.push(child); //将转化的entitytype添加进禁忌表 navigatorMap.set(entityType.name + entityType.id, InjectedType.EntityType); //将每个属性转化为每个TreeNode let propertyNum = 1; entityType.properties.forEach(property => { let leaf = new ComputeTreeNode; //叶子节点 leaf.id = this.oldId++; leaf.code = property.name; leaf.name = property.description; leaf.chainCode = child.chainCode + "." + property.name; leaf.chainName = child.chainName + "." + property.description; leaf.type = property.type; leaf.treeInfo.layer = child.treeInfo.layer + 1; leaf.treeInfo.path = child.treeInfo.path + ComputeExpressUtils.getPath(propertyNum); propertyNum++; leaf.parentId = child.id; this.entities.push(leaf); }); entityType.navigationProperties.forEach(navigationProperty => { //判断是否导航属性对应的名称在禁忌表中 if (!(navigatorMap.get(navigationProperty.name + navigationProperty.navigationId))) { if (navigationProperty.type == NavigationType.EntitySet) { }//若为实体类型 else if (navigationProperty.type == NavigationType.EntityType) { let entityType = schema.entityTypes.find(entitytype => entitytype.id == navigationProperty.navigationId && entitytype.name == navigationProperty.name); this.initEntityType(entityType, child, schema, navigatorMap, propertyNum + 1) } } else { //若导航属性在禁忌表中 let navigatortree = new ComputeTreeNode; navigatortree.id = this.oldId++; navigatortree.code = navigationProperty.name; navigatortree.name = navigationProperty.description; navigatortree.chainCode = child.chainCode + "." + navigationProperty.name; navigatortree.chainName = child.chainName + "." + navigationProperty.description; navigatortree.type = navigationProperty.type.toString(); navigatortree.treeInfo.layer = child.treeInfo.layer + 1;; navigatortree.treeInfo.path = child.treeInfo.path + ComputeExpressUtils.getPath(propertyNum); navigatortree.parentId = child.id; this.entities.push(navigatortree); } propertyNum++; }); } //若entitytype为空或未定义 else return null; } //新NewSchema转化 private initNewSchema(injectedEntities: InjectedEntity[], schemaMap: Map) { if (injectedEntities && injectedEntities.length > 0) { this.entities = []; //实体显示禁忌表 let navigatorMap = new Map(); let numcount = 1; //解析每一个注入实体,将其转化为TreeNode injectedEntities.forEach(injectEntity => { //根据schema名称找到对应的schema let currentSchema = schemaMap.get(injectEntity.schemaName); switch (injectEntity.injectedType) { //注入的若为实体集 case InjectedType.EntitySet: { }; break; //注入的若为实体类型 case InjectedType.EntityType: { let entityType = currentSchema.entityTypes.find(type => type.code == injectEntity.injectedName); this.initNewEntityType(entityType, null, currentSchema, navigatorMap, numcount); }; break; } numcount++; }); } } //新NewEntityType转化 private initNewEntityType(entityType: NewEntityType, parent: ComputeTreeNode, schema: NewSchema, navigatorMap: any, pathNum: any) { if (entityType) { let child = new ComputeTreeNode; //子节点 child.id = this.newId++; child.code = entityType.code; child.name = entityType.name; child.chainCode = entityType.code; child.chainName = entityType.name; child.type = "Type"; if (parent) { child.parentId = parent.id; child.chainCode = parent.chainCode + "." + entityType.code; child.chainName = parent.chainName + "." + entityType.name; child.treeInfo.layer = parent.treeInfo.layer + 1; child.treeInfo.path = parent.treeInfo.path + ComputeExpressUtils.getPath(pathNum); } else { child.parentId = null; child.treeInfo.layer = 1; child.treeInfo.path = ComputeExpressUtils.getPath(1); //设置自定义根节点命名 if (this.injectName != null && this.injectName.length != 0) { child.chainCode = this.injectName; child.code = this.injectName; } } this.entities.push(child); //将转化的entitytype添加进禁忌表 navigatorMap.set(entityType.code + entityType.name, InjectedType.EntityType); //将每个属性转化为每个TreeNode let propertyNum = 1; entityType.properties.forEach(property => { let leaf = new ComputeTreeNode; //叶子节点 leaf.id = this.newId++; leaf.code = property.code; leaf.name = property.name; leaf.chainCode = child.chainCode + "." + property.code; leaf.chainName = child.chainName + "." + property.name; leaf.type = property.dataType; leaf.treeInfo.layer = child.treeInfo.layer + 1; leaf.treeInfo.path = child.treeInfo.path + ComputeExpressUtils.getPath(propertyNum); propertyNum++; leaf.parentId = child.id; this.entities.push(leaf); }); entityType.navigationProperties.forEach(navigationProperty => { //判断是否导航属性对应的名称在禁忌表中 if (!(navigatorMap.get(navigationProperty.code + navigationProperty.navigationName))) { if (navigationProperty.navigationType == NewNavigationType.EntitySet) { } else if (navigationProperty.navigationType == NewNavigationType.EntityType) { //若为实体类型 let entityType = schema.entityTypes.find(type => type.code == navigationProperty.navigationName); this.initNewEntityType(entityType, child, schema, navigatorMap, propertyNum + 1) } } else { //若导航属性在禁忌表中 let navigatortree = new ComputeTreeNode; navigatortree.id = this.newId++; navigatortree.code = navigationProperty.code; navigatortree.name = navigationProperty.name; navigatortree.chainCode = child.chainCode + "." + navigationProperty.code; navigatortree.chainName = child.chainName + "." + navigationProperty.name; navigatortree.type = navigationProperty.navigationType; navigatortree.treeInfo.layer = child.treeInfo.layer + 1;; navigatortree.treeInfo.path = child.treeInfo.path + ComputeExpressUtils.getPath(propertyNum); navigatortree.parentId = child.id; this.entities.push(navigatortree); } propertyNum++; }); } //若entitytype为空或未定义 else return null; } }