prepareparam-node.ts 13.7 KB
Newer Older
1
import { IPSDEUILogicNode, IPSDEUILogicNodeParam, IPSDEUILogicParam } from '@ibiz/dynamic-model-api';
2
import { AppServiceBase } from 'ibiz-core';
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
import { UILogicParamValueType } from '../const/logic-param-value-type';
import { UIActionContext } from '../uiaction-context';
import { AppUILogicNodeBase } from './logic-node-base';
/**
 * 准备处理参数节点
 *
 * @export
 * @class AppUILogicPrepareParamNode
 */
export class AppUILogicPrepareParamNode extends AppUILogicNodeBase {

    constructor() {
        super();
    }

    /**
     * 执行节点
     *
     * @param {IPSDEUILogicNode} logicNode 逻辑节点模型数据
     * @param {UIActionContext} actionContext 界面逻辑上下文
     * @memberof AppUILogicPrepareParamNode
     */
    public async executeNode(logicNode: IPSDEUILogicNode, actionContext: UIActionContext) {
        try {
            this.handleParam(logicNode, actionContext);
            return this.computeNextNodes(logicNode, actionContext);
        } catch (error: any) {
30
            throw new Error(`逻辑节点 ${logicNode.name}${error?.message ? error?.message : '发生未知错误!'}`);
31 32 33 34 35 36 37 38 39 40 41 42 43 44
        }
    }

    /**
     * 处理参数
     *
     * @param {IPSDELogicNode} logicNode 节点模型数据
     * @param {ActionContext} actionContext  逻辑上下文
     * @memberof AppUILogicPrepareParamNode
     */
    public handleParam(logicNode: IPSDEUILogicNode, actionContext: UIActionContext) {
        if (!logicNode || !logicNode.getPSDEUILogicNodeParams()) {
            return;
        }
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
        try {
          for (let logicNodeParam of (logicNode.getPSDEUILogicNodeParams() as IPSDEUILogicNodeParam[])) {
              // 设置变量
              if (Object.is(logicNodeParam.paramAction, UILogicParamValueType.SETPARAMVALUE)) {
                  this.onSetParamValue(logicNodeParam, actionContext);
              }
              // 重置变量
              if (Object.is(logicNodeParam.paramAction, UILogicParamValueType.RESETPARAM)) {
                  this.onResetParam(logicNodeParam, actionContext);
              }
              // 拷贝变量
              if (Object.is(logicNodeParam.paramAction, UILogicParamValueType.COPYPARAM)) {
                  this.onCopyParam(logicNodeParam, actionContext);
              }
              // 绑定参数
              if (Object.is(logicNodeParam.paramAction, UILogicParamValueType.BINDPARAM)) {
                  this.onBindParam(logicNodeParam, actionContext);
              }
              // 重新建立变量
              if (Object.is(logicNodeParam.paramAction, UILogicParamValueType.RENEWPARAM)) {
                  this.onRenewParam(logicNodeParam, actionContext);
              }
              // 附加到数组变量
              if (Object.is(logicNodeParam.paramAction, UILogicParamValueType.APPENDPARAM)) {
                  this.onAppendParam(logicNodeParam, actionContext);
              }
              // 排序数组变量
              if (Object.is(logicNodeParam.paramAction, UILogicParamValueType.SORTPARAM)) {
                  this.onSortParam(logicNodeParam, actionContext);
              }
          }
        } catch (error: any) {
          throw new Error(`${error?.message ? error?.message : '发生未知错误!'}`);
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
        }
    }

    /**
     * 设置参数(根据配置把源逻辑参数的值赋给目标逻辑参数)
     *
     * @param {IPSDEUILogicNodeParam} logicNodeParam 逻辑节点模型数据
     * @param {UIActionContext} actionContext 界面逻辑上下文
     * @memberof AppUILogicPrepareParamNode
     */
    public onSetParamValue(logicNodeParam: IPSDEUILogicNodeParam, actionContext: UIActionContext) {
        try {
            // 源类型参数/目标逻辑参数/目标属性缺一跳过不做处理
            if (!logicNodeParam.getDstPSDEUILogicParam() || !logicNodeParam.srcValueType) {
                throw new Error(`逻辑参数${logicNodeParam.name}源类型参数或者目标逻辑参数缺失`);
            }
            // 目标数据
            const dstParam: any = actionContext.getParam((logicNodeParam.getDstPSDEUILogicParam() as IPSDEUILogicParam)?.codeName);
            // 无值类型
            if (Object.is(logicNodeParam.srcValueType, 'NONEVALUE')) {
                if(logicNodeParam.dstFieldName){
                    dstParam.reset(logicNodeParam.dstFieldName.toLowerCase());
                }else{
                    dstParam.bind(undefined);
                }
            } else {
                const result = this.computeTargetParam(logicNodeParam, actionContext);
                if(logicNodeParam.dstFieldName){
                    dstParam.set(logicNodeParam.dstFieldName.toLowerCase(), result);
                }else{
                    dstParam.bind(result);
                }
                
            }
        } catch (error: any) {
            throw new Error(`逻辑参数${logicNodeParam.name}${error?.message ? error?.message : '发生未知错误!'}`);
        }
    }

    /**
     * 重置变量
     *
     * @param {IPSDEUILogicNodeParam} logicNodeParam
     * @param {ActionContext} actionContext
     * @memberof AppUILogicPrepareParamNode
     */
    public onResetParam(logicNodeParam: IPSDEUILogicNodeParam, actionContext: UIActionContext) {
        try {
            // 目标数据
            const dstParam: any = actionContext.getParam((logicNodeParam.getDstPSDEUILogicParam() as IPSDEUILogicParam)?.codeName);
            dstParam.resetAll();
        } catch (error: any) {
            throw new Error(`逻辑参数${logicNodeParam.name}${error?.message ? error?.message : '发生未知错误!'}`);
        }
    }

    /**
     * 拷贝变量
     *
     * @param {IPSDEUILogicNodeParam} logicNodeParam
     * @param {ActionContext} actionContext
     * @memberof AppUILogicPrepareParamNode
     */
    public onCopyParam(logicNodeParam: IPSDEUILogicNodeParam, actionContext: UIActionContext) {
        try {
            // 源数据
            const srcParam: any = actionContext.getParam((logicNodeParam.getSrcPSDEUILogicParam() as IPSDEUILogicParam)?.codeName);
            // 目标数据
            const dstParam: any = actionContext.getParam((logicNodeParam.getDstPSDEUILogicParam() as IPSDEUILogicParam)?.codeName);
            srcParam.copyTo(dstParam);
        } catch (error: any) {
            throw new Error(`逻辑参数${logicNodeParam.name}${error?.message ? error?.message : '发生未知错误!'}`);
        }
    }

    /**
     * 绑定参数
     *
     * @param {IPSDEUILogicNodeParam} logicNodeParam
     * @param {ActionContext} actionContext
     * @memberof AppUILogicPrepareParamNode
     */
    public onBindParam(logicNodeParam: IPSDEUILogicNodeParam, actionContext: UIActionContext) {
        try {
            // 源数据
            const srcParam: any = actionContext.getParam((logicNodeParam.getSrcPSDEUILogicParam() as IPSDEUILogicParam)?.codeName);
            // 目标数据
            const dstParam: any = actionContext.getParam((logicNodeParam.getDstPSDEUILogicParam() as IPSDEUILogicParam)?.codeName);
            // 源属性
            const srcFieldName: string = logicNodeParam.srcFieldName?.toLowerCase?.();
            if (srcFieldName) {
                dstParam.bind(srcParam.get(srcFieldName));
            } else {
                dstParam.bind(srcParam.getReal());
            }
        } catch (error: any) {
            throw new Error(`逻辑参数${logicNodeParam.name}${error?.message ? error?.message : '发生未知错误!'}`);
        }
    }

    /**
     * 重新建立变量
     *
     * @param {IPSDEUILogicNodeParam} logicNodeParam
     * @param {UIActionContext} actionContext
     * @memberof AppUILogicPrepareParamNode
     */
    public onRenewParam(logicNodeParam: IPSDEUILogicNodeParam, actionContext: UIActionContext) {
186
      try {
187 188 189
        // 目标参数
        const dstParam: any = actionContext.getParam((logicNodeParam.getDstPSDEUILogicParam() as IPSDEUILogicParam)?.codeName);
        dstParam.renew();
190 191 192
      } catch (error: any) {
        throw new Error(`逻辑参数${logicNodeParam.name}${error?.message ? error?.message : '发生未知错误!'}`);
      }
193 194 195 196 197 198 199 200 201 202
    }

    /**
     * 附加到数组变量
     *
     * @param {IPSDEUILogicNodeParam} logicNodeParam
     * @param {UIActionContext} actionContext
     * @memberof AppUILogicPrepareParamNode
     */
    public onAppendParam(logicNodeParam: IPSDEUILogicNodeParam, actionContext: UIActionContext) {
203
      try {
204 205 206 207 208 209 210 211 212 213 214 215 216
        // 源数据
        const srcParam: any = actionContext.getParam((logicNodeParam.getSrcPSDEUILogicParam() as IPSDEUILogicParam)?.codeName);
        // 目标数据
        const dstParam: any = actionContext.getParam((logicNodeParam.getDstPSDEUILogicParam() as IPSDEUILogicParam)?.codeName);
        // 源属性
        const srcFieldName: string = logicNodeParam.srcFieldName?.toLowerCase?.();
        let objParam: any;
        if (srcFieldName) {
            objParam = srcParam.get(srcFieldName);
        } else {
            objParam = srcParam.getReal();
        }
        dstParam.append(logicNodeParam.dstIndex, objParam, logicNodeParam.srcIndex, logicNodeParam.srcSize);
217 218 219
      } catch (error: any) {
        throw new Error(`逻辑参数${logicNodeParam.name}${error?.message ? error?.message : '发生未知错误!'}`);
      }
220 221 222 223 224 225 226 227 228 229
    }

    /**
     * 排序数组变量
     *
     * @param {IPSDEUILogicNodeParam} logicNodeParam
     * @param {UIActionContext} actionContext
     * @memberof AppUILogicPrepareParamNode
     */
    public onSortParam(logicNodeParam: IPSDEUILogicNodeParam, actionContext: UIActionContext) {
230
      try {
231 232 233 234 235 236 237 238
        // 目标数据
        const dstParam: any = actionContext.getParam((logicNodeParam.getDstPSDEUILogicParam() as IPSDEUILogicParam)?.codeName);
        // 目标属性
        const dstFieldName: string = logicNodeParam.dstFieldName?.toLowerCase?.();
        if (!dstFieldName) {
            throw new Error(`逻辑参数${logicNodeParam.name}未指定设置排序属性`);
        }
        dstParam.sort(dstFieldName, logicNodeParam.dstSortDir);
239 240 241
      } catch (error: any) {
        throw new Error(`逻辑参数${logicNodeParam.name}${error?.message ? error?.message : '发生未知错误!'}`);
      }
242 243 244 245 246 247 248 249 250 251 252 253 254 255
    }

    /**
     * 计算目标值
     *
     * @param {IPSDEUILogicNodeParam} logicNodeParam 节点参数
     * @param {ActionContext} actionContext  逻辑上下文
     * @memberof AppUILogicPrepareParamNode
     */
    public computeTargetParam(logicNodeParam: IPSDEUILogicNodeParam, actionContext: UIActionContext) {
        let targetValue: any;
        // 源数据
        const srcParam: any = actionContext.getParam((logicNodeParam.getSrcPSDEUILogicParam() as IPSDEUILogicParam)?.codeName);
        // 源属性
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
        try {
          const srcFieldName: string = logicNodeParam?.srcFieldName?.toLowerCase();
          switch (logicNodeParam.srcValueType) {
              case "SRCDLPARAM":       // 源逻辑参数
              case 'WEBCONTEXT':       // 网页请求上下文
              case 'VIEWPARAM':        // 当前视图参数
                  targetValue = srcParam.get(srcFieldName);
                  break;
              case 'APPLICATION':      // 系统全局对象          
              case 'SESSION':          // 用户全局对象 
              case "APPDATA":          // 应用上下文
              case "DATACONTEXT":      // 数据上下文
                  const { context } = actionContext;
                  targetValue = context[srcFieldName];
                  break;
              case 'ENVPARAM':         // 当前环境参数
                  const Environment = AppServiceBase.getInstance().getAppEnvironment();
                  targetValue = Environment[srcFieldName];
                  break;
              case 'EXPRESSION':       // 计算式
                  targetValue = this.computeExpRessionValue(logicNodeParam, actionContext);
                  break;
              case "SRCVALUE":         // 直接值
                  targetValue = logicNodeParam?.srcValue;
                  break;
              case 'NULLVALUE':        // 空值(NULL)
                  targetValue = null;
                  break;
              default:
                  throw new Error(`源值类型${logicNodeParam.srcValueType}暂未支持`);
          }
          return targetValue;
        } catch (error: any) {
          throw new Error(`${error?.message ? error?.message : '发生未知错误!'}`);
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
        }
    }
    /**
     * 计算表达式值
     *
     * @param {IPSDEUILogicNodeParam} logicNodeParam 节点参数
     * @param {ActionContext} actionContext  逻辑上下文
     * @memberof AppUILogicPrepareParamNode
     */
    private computeExpRessionValue(logicNodeParam: IPSDEUILogicNodeParam, actionContext: UIActionContext) {
        let expression: string = logicNodeParam?.expression;
        let data: any = actionContext.defaultParam.getReal();
        let { context } = actionContext;
        if (!expression) {
            throw new Error(`表达式不能为空`);
        }
        try {
            expression = this.translateExpression(expression);
            return eval(expression);
        } catch (error) {
            throw new Error(`表达式计算异常: ${error}`);
        }
    }

    /**
     * 解析表达式
     *
     * @param {string} expression 表达式
     * @memberof AppUILogicPrepareParamNode
     */
    private translateExpression(expression: string): string {
        if ((expression.indexOf('${') != -1) && (expression.indexOf('}') != -1)) {
            const start: number = expression.indexOf('${');
            const end: number = expression.indexOf('}');
            const contentStr: string = expression.slice(start + 2, end);
            expression = expression.replace(expression.slice(start, end + 1), `data.${contentStr}`);
            return this.translateExpression(expression);
        }
        return expression;
    }

}