action-context.ts 3.6 KB
Newer Older
1 2 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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 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 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

/**
 * 实体处理逻辑上下文参数对象
 *
 * @export
 * @class ActionContext
 */
export class ActionContext {

    /**
     * 实体行为服务context
     *
     * @type {IContext}
     * @memberof ActionContext
     */
    public appContext: any;

    /**
     * 逻辑处理参数集合
     *
     * @type {Map<string, any>}
     * @memberof ActionContext
     */
    public paramsMap: Map<string, any> = new Map();

    /**
     * 默认逻辑处理参数名称
     *
     * @type {string}
     * @memberof ActionContext
     */
    private defaultParamName: string = '';

    /**
     * 默认逻辑处理参数
     *
     * @readonly
     * @memberof ActionContext
     */
    get defaultParam() {
        return this.paramsMap.get(this.defaultParamName);
    }

    /**
     * 上一次调用返回参数名称
     *
     * @type {string}
     * @memberof ActionContext
     */
    private lastReturnParamName: string = '';

    /**
     * 绑定上一次调用返回参数
     *
     * @readonly
     * @memberof ActionContext
     */
    public bindLastReturnParam(value: any) {
        const lastReturnParam = this.paramsMap.get(this.lastReturnParamName);
        if (lastReturnParam) {
            lastReturnParam.bind(value);
        }
    }

    /**
     * 应用上下文参数名称
     *
     * @type {string}
     * @memberof ActionContext
     */
    public appContextParamName: string = '';

    /**
     * 上下文数据(包括应用上下文和逻辑局部上下文参数)
     *
     * @readonly
     * @memberof UIActionContext
     */
    get context() {
        const navContextParam = this.paramsMap.get(this.appContextParamName);
        if (navContextParam) {
            return navContextParam.getReal();
        } else {
            return this.appContext;
        }
    }

    /**
     * 获取逻辑处理参数
     *
     * @param {string} key 逻辑处理参数的codeName
     * @memberof ActionContext
     */
    public getParam(key: string) {
        return this.paramsMap.get(key);
    }

    /**
     * 返回结果
     *
     * @type {*}
     * @memberof ActionContext
     */
    private result: any;

    /**
     * 设置返回结果
     *
     * @param {*} opts
     * @memberof ActionContext
     */
    public setResult(opts: any) {
        this.result = opts;
    }

    /**
     * 获取返回结果
     *
     * @return {*} 
     * @memberof ActionContext
     */
    public getResult() {
        return this.result;
    }

    /**
     * 构造函数
     * 
     * @param {IPSAppDELogic} logic 处理逻辑模型对象
     * @param {IContext} context 实体行为服务context
     * @param {IParams} params 实体行为服务data
     * @memberof ActionContext
     */
    constructor(logicParams: any[], context: any, params: any) {
        this.appContext = context;
        // 初始化逻辑处理参数

        // if (logic.getPSDELogicParams() && (logic.getPSDELogicParams() as IPSDELogicParam[]).length > 0) {
        //     for (let logicParam of (logic.getPSDELogicParams() as IPSDELogicParam[])) {
        //         const initValue = logicParam.default ? params : undefined;
        //         const logicParamInstance = AppDeLogicParamService.getLogicParamInstance(this, logicParam, initValue);
        //         this.paramsMap.set(logicParam.codeName, logicParamInstance);
        //         if (logicParam.default) this.defaultParamName = logicParam.codeName;
        //         if (logicParam.lastReturnParam) this.lastReturnParamName = logicParam.codeName;
        //         if(logicParam.appContextParam) this.appContextParamName = logicParam.codeName;
        //     }
        // }
    }
}