ui-logic-param-base.ts 6.1 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 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 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
import { IParams, Util } from "ibiz-core";

/**
 * 界面逻辑参数基类
 *
 * @export
 * @class AppDeUILogicParamBase
 */
export class AppDeUILogicParamBase {

    /**
     * 代码名称
     *
     * @type {*}
     * @memberof AppDeUILogicParamBase
     */
    protected strCodeName: any;

    /**
     * 操作会话
     *
     * @type {*}
     * @memberof AppDeUILogicParamBase
     */
    protected actionSession: any;

    /**
     * 逻辑参数模型
     *
     * @type {*}
     * @memberof AppDeUILogicParamBase
     */
    protected logicParamModel: any;

    /**
     * 逻辑类型
     *
     * @type {*}
     * @memberof AppDeUILogicParamBase
     */
    protected logicParamType: any;

    /**
     * 实际值
     *
     * @type {*}
     * @memberof AppDeUILogicParamBase
     */
    protected realValue: any;

    /**
     * Creates an instance of AppDeUILogicParamBase.
     * @param {*} opts
     * @memberof AppDeUILogicParamBase
     */
    public constructor(opts: any) {
        const { actionSession, model, params } = opts;
        this.actionSession = actionSession;
        this.logicParamModel = model;
        this.strCodeName = model.codeName;
        this.init(params);
    }

    /**
     * 初始化
     *
     * @protected
     * @memberof AppDeUILogicParamBase
     */
    protected init(params: IParams) {
        this.setReal(this.getDefaultValue(params, {}));
    }

    /**
     * 获取默认值
     *
     * @protected
     * @memberof AppDeUILogicParamBase
     */
    protected getDefaultValue(params: IParams, defaultValue: any) {
        const { args } = params;
        if (this.logicParamModel.default) {
            return args;
        } else {
            return defaultValue;
        }
    }

    /**
     * 获取实际参数值
     *
     * @memberof AppDeUILogicParamBase
     */
    public getReal() {
        return this.realValue;
    }

    /**
     * 设置实际参数值
     *
     * @param {*} opts
     * @memberof AppDeUILogicParamBase
     */
    public setReal(opts: any) {
        this.realValue = opts;
    }

    /**
     * 设置指定属性值
     *
     * @param {string} strName
     * @param {*} value
     * @memberof AppDeUILogicParamBase
     */
    public set(strName: string, value: any) {
        if (Object.prototype.toString.call(this.realValue) !== '[object Object]') {
            throw new Error(`逻辑参数${this.strCodeName}无法执行绑定非对象类型参数`);
        }
        this.realValue[strName] = value;
    }

    /**
     * 获取指定属性值
     *
     * @param {string} strName
     * @memberof AppDeUILogicParamBase
     */
    public get(strName: string) {
        if (Object.prototype.toString.call(this.realValue) !== '[object Object]') {
            throw new Error(`逻辑参数${this.strCodeName}非对象类型参数无法执行获取指定属性值`);
        }
        return this.realValue[strName];
    }

    /**
     * 重置指定属性
     *
     * @param {string} strName
     * @memberof AppDeUILogicParamBase
     */
    public reset(strName: string) {
        if (Object.prototype.toString.call(this.realValue) !== '[object Object]') {
            throw new Error(`逻辑参数${this.strCodeName}非对象类型参数无法执行重置指定属性`);
        }
        this.realValue[strName] = null;
    }

    /**
     * 重置全部
     *
     * @memberof AppDeUILogicParamBase
     */
    public resetAll() {
        if (Object.prototype.toString.call(this.realValue) !== '[object Object]') {
            throw new Error(`逻辑参数${this.strCodeName}非对象类型参数无法执行重置全部`);
        }
        this.setReal({});
    }

    /**
     * 拷贝当前变量到指定变量
     *
     * @param {*} dstParam
     * @memberof AppDeUILogicParamBase
     */
    public copyTo(dstParam: any) {
        if (Object.is(typeof (this.realValue), 'object')) {
            dstParam.setReal(Util.deepCopy(this.realValue));
        } else {
            dstParam.setReal(this.realValue);
        }
    }

    /**
     * 绑定指定参数对象
     *
     * @param {*} opts
     * @memberof AppDeUILogicParamBase
     */
    public bind(opts: any) {
        this.setReal(opts);
    }

    /**
     * 重新建立参数对象
     *
     * @memberof AppDeUILogicParamBase
     */
    public renew() {
        this.setReal({});
    }

    /**
     * 附加参数对象
     *
     * @param {number} nPos
     * @param {*} paramObject
     * @param {number} nSrcPos
     * @param {number} nSrcLength
     * @memberof AppDeUILogicParamBase
     */
    public append(nPos: number, paramObject: any, nSrcPos: number, nSrcLength: number) {
        if (Object.prototype.toString.call(paramObject) !== '[object Array]') {
            throw new Error(`逻辑参数${this.strCodeName}源数据不是数据对象列表类型`);
        }
        if (this.realValue && !Array.isArray(this.realValue)) {
            throw new Error(`逻辑参数${this.strCodeName}不是数据对象列表类型`);
        }
        // 补足参数
        if (nPos === -1) {
            nPos = 0;
        }
        if (nSrcPos === -1) {
            nSrcPos = 0;
        }
        if (nSrcLength === -1) {
            nSrcLength = paramObject.length;
        }
        const list: Array<any> = this.realValue;
        if (nPos > list.length) {
            throw new Error(`逻辑参数${this.strCodeName}插入位置溢出`);
        }
        const srcList = paramObject.slice(nSrcPos, nSrcLength);
        list.splice(nPos, 0, ...srcList);
    }

    /**
     * 排序参数对象
     *
     * @param {string} strField
     * @param {string} strSortDir
     * @memberof AppDeUILogicParamBase
     */
    public sort(strField: string, strSortDir: string) {
        if (this.realValue && !Array.isArray(this.realValue)) {
            throw new Error(`逻辑参数${this.strCodeName}不是数据对象列表类型`);
        }
        // 是否降序
        const bSortDesc: boolean = Object.is(strSortDir?.toLowerCase(), 'desc') ? true : false;
        // 对数组排序(升序)
        this.realValue.sort((a: any, b: any) => {
            return a[strField] - b[strField];
        })
        if (bSortDesc) {
            this.realValue.reverse();
        }
    }
}