import { Injectable, Injector, ApplicationRef, ComponentFactoryResolver } from '@angular/core'; import { HttpClient } from "@angular/common/http"; import { BsModalService } from '@farris/ui-modal'; import { GspCommonDataType, GSPCommonObject } from '@gsp-bef/gsp-cm-metadata'; import { ExpressioneditorComponent } from './expressioneditor/expressioneditor.component'; import { ComputeRuleComponent } from './compute-rule/compute-rule.component'; import { Schema, EntitySet } from './common/Schema'; import { NewSchema, NewEntitySet } from './common/NewSchema'; import { CurrLanguageVariable, ComputeEditorSetting } from "./common/CommonResource"; import { ExpressionContextEntity, ExpressionEntity, ExprEditMode, VarEntity, FunctionEntity, SmartHelpEntity, InjectedEntity, InjectedType, CustomizedSetting, DataArea, ExtendFunction, FuncType } from './common/CommonEntity'; import { I18nFunctions } from './i18n/I18nFunctions'; import { Adapter } from './utils/adapter'; import { CommonUtils } from './utils/CommonUtils'; import { ExpressionEditorLocalePipe } from './utils/expression-editor-locale.pipe'; import { ComputeLocalePipe } from './utils/compute-local.pipe'; /** * 表达式对外提供服务类 * 功能:提供打开、预置等接口 */ @Injectable() export class ExpressionService { //定义部分国际化资源 private localeId: string; private localePipe: ExpressionEditorLocalePipe; private computePipe: ComputeLocalePipe; private ExpressionTitle: string; private ExpressionSave: string; private ExpressionCancel: string; //定义函数前缀,用于打开时去除函数前缀(比如"DefaultFunction") private functionSuffixs = ["DefaultFunction", "Math", "DefaultExtendFunctionProxy"]; exprEditMode: ExprEditMode = ExprEditMode.Advanced; exprEntity: ExpressionContextEntity = new ExpressionContextEntity(); setting: CustomizedSetting = new CustomizedSetting(); //表达式页面定制设置 adapter: Adapter = new Adapter(); private modalService: BsModalService; private functions: I18nFunctions = new I18nFunctions(); constructor(private injector: Injector, private applicationRef: ApplicationRef, private compFactRes: ComponentFactoryResolver, private http: HttpClient) { this.modalService = injector.get(BsModalService); //初始国际化信息 this.localeId = localStorage.getItem('languageCode'); this.localePipe = new ExpressionEditorLocalePipe(this.localeId); this.computePipe = new ComputeLocalePipe(this.localeId); this.exprEntity.functionEntities = this.functions.get(this.localeId) || this.functions.get("zh-CHS"); //所有函数(包含不规范) this.ExpressionTitle = this.localePipe.transform("ExpressionTitle"); this.ExpressionSave = this.localePipe.transform("ExpressionSave"); this.ExpressionCancel = this.localePipe.transform("ExpressionCancel"); //初始化预置上下文变量(当前语言) if (!this.exprEntity.contextEntities.includes(CurrLanguageVariable)) this.exprEntity.contextEntities.push(CurrLanguageVariable); } /** * 设置表达式模式(Conditional:简易表达式 Advanced:高级表达式) * @param exprEditMode ExprEditMode */ public setEditPattern(exprEditMode: ExprEditMode) { this.exprEditMode = exprEditMode; } /** * 打开表达式窗口 * @param readOnly 高级表达式是否只读模式,默认false可编辑 * @param hideExpressButton 条件表达式是否隐藏切换高级表达式按钮,默认false不隐藏 * @returns Promise对象,使用.then()获取保存后结果 */ public buildExpression(readOnly: boolean = false, hideExpressButton: boolean = false) { let that = this; return new Promise( function (resolve) { if ((that.exprEditMode == ExprEditMode.Conditional && ((that.exprEntity.expr != null && that.exprEntity.expr != "") && (that.exprEntity.sexpr == null || that.exprEntity.sexpr == ""))) || that.exprEditMode == ExprEditMode.Advanced) { //模式为高级 || 模式简易但内容高级 const compFactory = that.compFactRes.resolveComponentFactory(ExpressioneditorComponent); const compRef = compFactory.create(that.injector); compRef.instance.expressionEntity = that.exprEntity; compRef.instance.setting = that.setting; compRef.instance.readOnly = readOnly; let dialog = that.modalService.show(compRef, { title: that.ExpressionTitle, width: 1325.5, height: 728, buttons: [ { text: that.ExpressionSave, cls: 'btn btn-primary', handle: async () => { compRef.instance.getExprWithCheck().then(res => { if (res.closeable) { compRef.instance.result.emit(res.expression); dialog.close(); } }); } }, { text: that.ExpressionCancel, cls: 'btn btn-secondary', handle: () => { dialog.close(); } } ], showButtons: true }); compRef.instance.result.subscribe((expressionContent) => { return resolve(expressionContent); }); } else { const compFactory = that.compFactRes.resolveComponentFactory(ComputeRuleComponent); const compRef = compFactory.create(that.injector); compRef.instance.contextEntity = that.exprEntity; compRef.instance.hideExpressButton = hideExpressButton; ComputeEditorSetting.title = that.computePipe.transform("ComputeExpression"); const dialog = that.modalService.show(compRef, ComputeEditorSetting); compRef.instance.closeModalEmitter.subscribe($event => dialog.close()); compRef.instance.result.subscribe((expressionContent) => { return resolve(expressionContent); }); } }) } /** * 预置表达式展示文本 * @param text 表达式文本 */ public addExpressionText(text: string) { try { //反序列化获取表达式文本对象 let obj = JSON.parse(text) as ExpressionEntity; if (!obj) { this.exprEntity.expr = text; this.exprEntity.sexpr = "[]"; } else if (obj.expr) { //Antlr4分词获取tokens let tokens = CommonUtils.getTokens(obj.expr); //合并上下文变量、会话变量 let varMap = CommonUtils.getVariableMap(this.exprEntity.contextEntities, this.exprEntity.sessionEntities, "Key2Name"); //tokens解析为语义化文本 this.exprEntity.expr = CommonUtils.toSemantic(tokens, this.exprEntity.functionEntities, varMap, this.functionSuffixs); //sexpr赋值 obj.sexpr != null ? this.exprEntity.sexpr = obj.sexpr : this.exprEntity.sexpr = ""; } else { this.GetUnCamelParse(obj); this.exprEntity.expr = obj.expr; //sexpr赋值 obj.sexpr != null ? this.exprEntity.sexpr = obj.sexpr : this.exprEntity.sexpr = ""; } } catch { this.exprEntity.expr = text; this.exprEntity.sexpr = "[]"; } } /** * 添加上下文变量 * @param contextEntity 上下文变量 */ public addContext(contextEntity: VarEntity) { this.exprEntity.contextEntities = this.exprEntity.contextEntities.filter(item => item.key !== contextEntity.key); this.exprEntity.contextEntities.push(contextEntity); } /** * 添加会话变量 * @param sessionEntity 会话变量 */ public addSession(sessionEntity: VarEntity) { this.exprEntity.sessionEntities = this.exprEntity.sessionEntities.filter(item => item.key !== sessionEntity.key); this.exprEntity.sessionEntities.push(sessionEntity); } /** * 添加扩展函数 * @deprecated * @param FunctionEntity 函数实体 */ public addFunction(functionEntity: FunctionEntity) { //过滤掉key相同的函数 this.exprEntity.functionEntities = this.exprEntity.functionEntities.filter(item => item.key !== functionEntity.key); if (!this.functionSuffixs.includes(functionEntity.keyFront)) this.functionSuffixs.push(functionEntity.keyFront); this.exprEntity.functionEntities.push(functionEntity); } /** * 批量添加扩展函数 * @deprecated * @param FunctionEntity[] 函数列表序列化后的字符串 */ public addFunctions(funcList: string) { let functions = JSON.parse(funcList); functions.forEach(func => { this.addFunction(func); }); } /** * 添加扩展函数 * @param func 函数实体 */ public addExtendFunction(func: ExtendFunction) { if (func != null) { func.key = func.token; func.isLatestExtendFunc = true; func.type = FuncType.Extend; this.exprEntity.functionEntities = this.exprEntity.functionEntities.filter(item => item.key !== func.token); this.exprEntity.functionEntities.push(func); } } /** * 展示数据模型信息,决定展示哪个数据模型 * @param name 自定义根节点名称。可不填,不填则使用第三个参数typeNameInSchema。 * @param schemaName 展示哪个数据模型,与addNewSchema或addSchema的第一个参数对应,默认为"CommonObject" * @param typeNameInSchema 数据模型根节点EntityType的信息,使用Schema为根节点EntityType的name,使用NewSchema为根节点EntityType的code * @param injectedType 数据模型的类型,默认为EntityType */ public addInjectedEntity(name: string, schemaName: string = "CommonObject", typeNameInSchema: string, injectedType: InjectedType = InjectedType.EntityType) { //构造注入实体对象 let injectedEntity: InjectedEntity = { name: name ? name : typeNameInSchema, schemaName: schemaName, injectedName: typeNameInSchema, injectedType: injectedType } this.exprEntity.injectedEntities = this.exprEntity.injectedEntities.filter(item => item.name !== injectedEntity.name); this.exprEntity.injectedEntities.push(injectedEntity); } /** * 添加NewSchema,NewSchema为数据模型描述信息 * @param name 表达式可存储多个数据模型,添加数据模型需要为模型定义一个name,用于确定展示哪个模型 * @param newSchema NewSchema */ public addNewSchema(name: string, newSchema: NewSchema) { if (!name) alert("Please Input NewSchema Name!"); else this.exprEntity.newSchemaMap.set(name, newSchema); } /** * 预置CommonObject(NewSchema) * @param commonObject GSPCommonObject */ public addCommonObjectOfNewSchema(commonObject: GSPCommonObject) { let newSchema = this.exprEntity.newSchemaMap.get("CommonObject") || new NewSchema(); //将每一个CO转化为schema内部结构中的entityType this.adapter.convertCOOfNewSchema(commonObject).forEach(entityType => { newSchema.entityTypes.push(entityType); let entitySet: NewEntitySet = { code: entityType.name + "s", name: '', entityType: entityType.code }; newSchema.entitySets.push(entitySet); }); //将合并后的newSchema放入内部上下文实体中 this.addNewSchema("CommonObject", newSchema); } /** * 预置GspCommonDataType(NewSchema) * @param gspCDTs GspCommonDataType[] */ public addCDTOfNewSchema(gspCDTs: GspCommonDataType[]) { let newSchema = this.exprEntity.newSchemaMap.get("CDT") || new NewSchema(); gspCDTs.forEach(gspCDT => { //将每一个CDT转化为schema内部结构中的entityType newSchema.entityTypes.push(this.adapter.convertCDTOfNewSchema(gspCDT)); }); //将合并后的schema放入内部上下文实体中 this.addNewSchema("CDT", newSchema); } /** * 清空注入实体 */ public clearInjectedEntity() { this.exprEntity.injectedEntities = []; } /** * 清空NewSchemaMap内容 */ public clearNewSchema() { this.exprEntity.newSchemaMap.clear(); } /** * 获取Injector对象 * @returns Injector */ public getInjector() { return this.injector; } //##########################一些不建议使用的方法################## /** * 预置帮助 * @param shEntity 帮助描述实体 * @deprecated */ public addSmartHelpEntity(shEntity: SmartHelpEntity) { this.exprEntity.smartHelpEntities = this.exprEntity.smartHelpEntities.filter(item => item.code !== shEntity.code); this.exprEntity.smartHelpEntities.push(shEntity); } /** * 添加Schema,Schema为数据模型描述信息 * @param name 表达式可存储多个数据模型,添加数据模型需要为模型定义一个name,用于确定展示哪个模型 * @param schema schema实体类 * @deprecated */ public addSchema(name: string, schema: Schema) { if (!name) alert("Please Input Schema Name!"); else this.exprEntity.schemaMap.set(name, schema); } /** * 预置CommonObject * @param commonObject GSPCommonObject * @deprecated */ public addCommonObject(commonObject: GSPCommonObject) { let schema = this.exprEntity.schemaMap.get("CommonObject") || new Schema(); //将每一个CO转化为schema内部结构中的entityType this.adapter.convertCO(commonObject).forEach(entityType => { schema.entityTypes.push(entityType); let set: EntitySet = { name: entityType.name + "s", entityType: entityType.name, description: entityType.description }; schema.entitySets.push(set); }); //将合并后的schema放入内部上下文实体中 this.addSchema("CommonObject", schema); } /** * 预置GspCommonDataType * @param gspCDTs GspCommonDataType[] * @deprecated */ public addCDT(gspCDTs: GspCommonDataType[]) { let schema = this.exprEntity.schemaMap.get("CDT") || new Schema(); gspCDTs.forEach(gspCDT => { //将每一个CDT转化为schema内部结构中的entityType schema.entityTypes.push(this.adapter.convertCDT(gspCDT)); }); //将合并后的schema放入内部上下文实体中 this.addSchema("CDT", schema); } /** * * @param windowWidth * @deprecated */ public addWindowWidth(windowWidth: number) { } /** * 是否展示子表的[0] * @param show 是否展示[0],默认展示 */ public showChildTableSymbol(show: boolean = true) { if (show != null) this.setting.DataModel_ShowChildSymbol = show; } /** * 设置数据区域的默认选中tab页 * @param defaultSelect 选中tab页,默认数据模型 */ public selectDataArea(defaultSelect: DataArea = DataArea.DataModel) { if (defaultSelect != null) this.setting.DataArea_DefaultSelect = defaultSelect; } //########工具方法######## private GetUnCamelParse(jsonObj: any) { for (let key in jsonObj) { if (typeof (jsonObj[key]) === "object") { this.GetUnCamelParse(jsonObj[key]); } let temp = jsonObj[key]; delete (jsonObj[key]); jsonObj[this.ConvertToUncamel(key)] = temp; } } //字符串反转换Camel private ConvertToUncamel(input: string): string { if (input.length > 0) { let len = input.length; var strTemp = ""; //新字符串 for (let i = 0; i < len; i++) { if (i == 0) { strTemp += input[i].toLowerCase(); //第一个 } else { strTemp += input[i]; } } return strTemp; } } }