import { Component, OnInit, Input, Output, EventEmitter, Injector, ComponentFactoryResolver } from '@angular/core'; import { MessagerService } from '@farris/ui-messager'; import { NotifyService } from '@farris/ui-notify'; import { BsModalService } from '@farris/ui-modal'; import { ExpressioneditorComponent } from '../expressioneditor/expressioneditor.component'; import { EntityType, NavigationType, Schema } from '../common/Schema'; import { NewEntityType, NewNavigationType, NewSchema } from '../common/NewSchema'; import { ExpressionContextEntity, ExpressionEntity, InjectedEntity, InjectedType } from '../common/CommonEntity'; import { AllOperator, StringOperator, NumberOperator, RelationOperator, LeftBracket, RightBracket, ColumnSize } from "../common/ComputeCommonResource"; import { ComputeExpressUtils, ComputeTreeNode, ConditionRule } from '../common/ComputeCommonEntity'; import { ComputeLocalePipe } from '../utils/compute-local.pipe'; @Component({ selector: 'svc-compute-rule', templateUrl: './compute-rule.component.html', styleUrls: ['./compute-rule.component.css'], }) export class ComputeRuleComponent implements OnInit { @Output() result = new EventEmitter(); @Output() closeModalEmitter = new EventEmitter(); @Input() contextEntity: ExpressionContextEntity = new ExpressionContextEntity;//表达式内部上下文 @Input() conditions: ConditionRule[] = [{ computeField: '', fieldName: '', value: '', lbracket: '(', rbracket: ')', relation: "&&", operator: "" }]; @Input() hideExpressButton: boolean = false; //是否隐藏底部切换按钮 constructor(private msgSer: MessagerService, private notifyService: NotifyService, private injector: Injector, private modalService: BsModalService, private compFactRes: ComponentFactoryResolver) { 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.buttons[0] = this.local.transform("Button_Advaned"); this.buttons[1] = this.local.transform("Button_Save"); this.buttons[2] = this.local.transform("Button_Reset"); this.buttons[3] = this.local.transform("Button_Cancel"); 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 localeId: string; private local: ComputeLocalePipe; private oldId = 10001; //为属性赋值id使用 private newId = 20001; ColumnSize = ColumnSize; //列长度 LeftBracket = LeftBracket; //左括号 RightBracket = RightBracket; //右括号 relaOpers = []; //逻辑运算符 allOpers = []; //全部操作符 strOpers = []; //字符操作符 numOpers = []; //数字操作符 notifies = {}; //提示信息 headers = []; //表头 buttons = []; //按钮 headButtons = []; //表头按钮 hideExpress: any; //通过样式来隐藏/展示切换高级表达式按钮 entities = []; //实体列表 propertyList: any; //Farris组件需要实体 operators = []; //所有计算条件的操作符集合 currentCondition: { index: number, condition: ConditionRule } = null; ngOnInit() { try { if (this.contextEntity.sexpr) { this.conditions = JSON.parse(this.contextEntity.sexpr) as ConditionRule[]; if (this.conditions.length == 0) { this.conditions = [{ computeField: '', fieldName: '', value: '', lbracket: '(', rbracket: ')', relation: "&&", operator: "" }]; } } } catch { this.conditions = [{ computeField: '', fieldName: '', value: '', lbracket: '(', rbracket: ')', relation: "&&", operator: "" }]; } //转换Schema结构 if (this.contextEntity.newSchemaMap != null && this.contextEntity.newSchemaMap.size > 0) { this.initNewSchema(this.contextEntity.injectedEntities, this.contextEntity.newSchemaMap); } else if (this.contextEntity.injectedEntities && this.contextEntity.injectedEntities.length > 0) { this.initSchema(this.contextEntity.injectedEntities, this.contextEntity.schemaMap); } //初始化操作符集合 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]; //字段选择 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" }; //切换高级表达式按钮(展示/隐藏) if (this.hideExpressButton) this.hideExpress = { display: 'none' }; else this.hideExpress = { display: 'block' }; } //新增和删除条件 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]; } } /**确认 */ confirm() { let expression = new ExpressionEntity(); expression.expr = ComputeExpressUtils.convertExpr(this.conditions); if (expression.expr == "") { //处理条件为空 this.result.emit(null); } expression.sexpr = JSON.stringify(this.conditions); this.result.emit(JSON.stringify(expression)); this.closeModalEmitter.emit("close"); } /**重置 */ reset() { this.conditions = [ { computeField: '', fieldName: '', value: '', lbracket: '(', rbracket: ')', relation: "&&", operator: "" } ]; } /**取消 */ cancle() { this.closeModalEmitter.emit("close"); } /**打开高级表达式 */ openAdvancedExpression() { let that = this; this.openAdvancedExpr().then( expre => { that.result.emit(expre as string); } ); this.closeModalEmitter.emit("close"); } openAdvancedExpr() { let that = this; return new Promise( function (resolve) { const compFactory = that.compFactRes.resolveComponentFactory(ExpressioneditorComponent); const compRef = compFactory.create(that.injector); compRef.instance.expressionEntity = that.contextEntity; compRef.instance.expressionEntity.expr = ComputeExpressUtils.convertExpr(that.conditions); compRef.instance.expressionEntity.sexpr = ""; let dialog = that.modalService.show(compRef, { title: that.local.transform("AdvancedExpression"), width: 1325.5, height: 728, buttons: [ { text: that.local.transform("Advanced_Save"), cls: 'btn btn-primary', handle: () => { compRef.instance.getExprWithCheck().then(res => { if (res.closeable) { compRef.instance.result.emit(res.expression); dialog.close(); } }); } }, { text: that.local.transform("Advanced_Cancel"), cls: 'btn btn-secondary', handle: () => { dialog.close(); } } ], showButtons: true }); compRef.instance.result.subscribe((expressionContent) => { return resolve(expressionContent); }); }) } //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); } 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); } 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; } }