searchbar-control-base.tsx 15.0 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 249 250 251 252 253 254 255 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 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 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546
import { IPSAppDEField, IPSApplication, IPSAppUtil, IPSCodeItem, IPSDEFSearchMode, IPSSearchBar, IPSSearchBarFilter } from '@ibiz/dynamic-model-api';
import { GetModelService, LogUtil, SearchBarControlInterface, Util } from 'ibiz-core';
import moment from 'moment';
import { AppSearchBarService } from '../ctrl-service/app-searchbar-service';
import { MDControlBase } from './md-control-base';

export class SearchBarControlBase extends MDControlBase implements SearchBarControlInterface {

    /**
     * 搜索栏部件实例对象
     * 
     * @type {IBizSearchBarModel}
     * @memberof SearchBarControlBase
     */
    public declare controlInstance: IPSSearchBar;

    /**
     * 是否展开搜索栏部件
     * 
     * @type {boolean}
     * @memberof SearchBarControlBase
     */
    public isExpandSearchForm: boolean = false;

    /**
     * 过滤属性模型集合
     * 
     * @type {any}
     * @memberof SearchBarControlBase
     */
    public detailsModel: any = {};

    /**
     * 过滤项集合
     * 
     * @type {any[]}
     * @memberof SearchBarControlBase
     */
    public filterItems: any[] = [];

    /**
     * 应用实体名称
     * 
     * @type {string}
     * @memberof SearchBarControlBase
     */
    public appdeName: string = "";

    /**
     * modelid
     * 
     * @type {string}
     * @memberof SearchBarControlBase
     */
    public modelId: string = "";

    /**
     * 功能服务名称
     * 
     * @type {string}
     * @memberof SearchBarControlBase
     */
    public utilServiceName: string = "";

    /**
     * 历史记录
     * 
     * @type {any[]}
     * @memberof SearchBarControlBase
     */
    protected historyItems: any[] = [];

    /**
     * 选中记录
     * 
     * @type {any}
     * @memberof SearchBarControlBase
     */
    protected selectItem: any = null;

    /**
     * 存储项名称
     * 
     * @type {string}
     * @memberof SearchBarControlBase
     */
    protected saveItemName: string = "";

    /**
     * 查询参数(视图为代理模式时使用)
     *
     * @protected
     * @type {*}
     * @memberof SearchBarControlBase
     */
    protected queryParams: any = {};

    /**
     * 快速分组项(TODO 后续删除)
     *
     * @protected
     * @type {any[]}
     * @memberof SearchBarControlBase
     */
    protected quickGroupItems: any[] = [];

    /**
     * 是否首次提交事件
     *
     * @protected
     * @type {boolean}
     * @memberof SearchBarControlBase
     */
    protected isFirstEmit: boolean = true;

    /**
     * 视图是否是代理模式
     *
     * @type {boolean}
     * @memberof SearchBarControlBase
     */
    viewIsProxyMode: boolean = false;

    /**
     * 过滤属性集合
     * 
     * @memberof SearchBarControlBase
     */
    get filterFields() {
        return Object.values(this.detailsModel);
    }

    /**
     * 监听动态参数变化
     *
     * @param {*} newVal
     * @param {*} oldVal
     * @memberof SearchBarControlBase
     */
    public onDynamicPropsChange(newVal: any, oldVal: any) {
        this.isExpandSearchForm = newVal.isExpandSearchForm;
        //搜索栏绘制之后关闭清空数据
        if (!this.isExpandSearchForm && this.controlIsLoaded) {
            this.filterItems = [];
        }
        super.onDynamicPropsChange(newVal, oldVal);
    }

    /**
     * 监听静态参数变化
     *
     * @param {*} newVal
     * @param {*} oldVal
     * @memberof SearchBarControlBase
     */
    public onStaticPropsChange(newVal: any, oldVal: any) {
        this.viewIsProxyMode = newVal.viewIsProxyMode ? true : false;
        super.onStaticPropsChange(newVal, oldVal);
    }

    /**
     * 部件模型数据初始化
     *
     * @memberof SearchBarControlBase
     */
    public async ctrlModelInit() {
        await super.ctrlModelInit();
        this.service = new AppSearchBarService();
        this.modelId = `searchbar_${this.appDeCodeName ? this.appDeCodeName.toLowerCase() : 'app'}_${this.controlInstance.codeName?.toLowerCase()}`;
        await this.initUtilService();
        if (this.viewIsProxyMode) {
            await this.loadQuickGroupItem();
        }
    }

    /**
     * 初始化功能服务名称
     *
     * @memberof SearchBarControlBase
     */
    public async initUtilService() {
        const appUtil: IPSAppUtil = ((await (await GetModelService(this.context))?.app as IPSApplication).getAllPSAppUtils() || []).find((util: any) => {
            return util.utilType == 'FILTERSTORAGE';
        }) as IPSAppUtil;
        if (appUtil) {
            this.utilServiceName = appUtil.codeName?.toLowerCase();
        }
    }

    /**
     * 部件初始化
     * 
     * @memberof SearchBarControlBase
     */
    public ctrlInit() {
        super.ctrlInit();
        this.initDetailsModel();
        this.load();
    }

    /**
     * 初始化过滤属性模型集合
     * 
     * @memberof SearchBarControlBase
     */
    public initDetailsModel() {
        const barFilters: Array<IPSSearchBarFilter> = this.controlInstance.getPSSearchBarFilters() || [];
        if (barFilters.length === 0) {
            return;
        }
        barFilters.forEach((filter: IPSSearchBarFilter) => {
            const appDeField = filter.getPSAppDEField() as IPSAppDEField;
            let tempModel: any = {
                label: appDeField?.logicName,
                localtag: "",
                name: filter.name,
                prop: appDeField?.codeName?.toLowerCase() || filter.name,
                disabled: false
            };
            const searchMode: IPSDEFSearchMode | null = filter.getPSDEFSearchMode();
            if (searchMode && searchMode.valueOP) {
                Object.assign(tempModel, { mode: searchMode.valueOP });
            }
            Object.assign(this.detailsModel, { [filter.name]: tempModel });
        })
    }

    /**
     * 删除过滤项
     *
     * @param {number} index 索引
     * @memberof SearchBarControlBase
     */
    public onRemove(index: number) {
        this.filterItems.splice(index, 1);
    }

    /**
     * 搜索
     *
     * @return {*}
     * @memberof SearchBarControlBase
     */
    public search() {
        this.handleCtrlEvents('onsearch', { action: 'Search', navContext: this.context, navParam: this.viewparams, navData: this.navdatas, data: this.getData() }).then((res: boolean) => {
            if(res) {
                this.ctrlEvent({
                    controlname: this.controlInstance.name,
                    action: "search",
                    data: this.getData()
                });
            }
        })
    }

    /**
     * 搜索值变化(视图代理模式下)
     *
     * @protected
     * @param {*} value
     * @param {('QUICKSEARCH' | 'QUICKGROUP' | string)} [type='QUICKSEARCH']
     * @memberof SearchBarControlBase
     */
    protected searchValueChange(value: any, type: 'QUICKSEARCH' | 'QUICKGROUP' = 'QUICKSEARCH') {
        if (type === 'QUICKSEARCH') {
            this.queryParams.quickSearchValue = value;
        } else if (type === 'QUICKGROUP') {
            this.queryParams.quickGroupData = value.data;
        }
        //  快速分组第一次不抛值
        if (type === 'QUICKSEARCH' || !this.isFirstEmit) {
            this.ctrlEvent({ controlname: this.controlInstance.name, action: 'search', data: this.queryParams });
        }
        this.isFirstEmit = false;
    }

    /**
     * 保存
     *
     * @param {string} [name] 名称
     * @memberof SearchBarControlBase
     */
    public onSave(name?: string) {
        let time = moment();
        this.historyItems.push({
            name: (name ? name : time.format('YYYY-MM-DD HH:mm:ss')),
            value: time.unix().toString(),
            data: Util.deepCopy(this.filterItems)
        })
        this.selectItem = time.unix().toString();
        let param: any = {};
        Object.assign(param, {
            model: Util.deepCopy(this.historyItems),
            appdeName: this.appDeCodeName,
            modelid: this.modelId,
            utilServiceName: this.utilServiceName,
            ...this.viewparams
        });
        let post = this.service.saveModel(this.utilServiceName, this.context, param);
        post.then((response: any) => {
            this.ctrlEvent({ controlname: this.controlInstance.name, action: "save", data: response.data });
        }).catch((response: any) => {
            LogUtil.log(response);
        });
    }

    /**
     * 重置
     *
     * @return {*}
     * @memberof SearchBarControlBase
     */
    public reset() {
        this.handleCtrlEvents('onreset', { action: 'Reset', navContext: this.context, navParam: this.viewparams, navData: this.navdatas, data: this.getData() }).then((res: boolean) => {
            if(res) {
                this.filterItems = [];
            }
        })
    }

    /**
     * 加载
     *
     * @return {*}
     * @memberof SearchBarControlBase
     */
    public load() {
        let param: any = {};
        Object.assign(param, {
            appdeName: this.appDeCodeName,
            modelid: this.modelId,
            utilServiceName: this.utilServiceName,
            ...this.viewparams
        });
        let tempContext: any = Util.deepCopy(this.context);
        this.onControlRequset('load', tempContext, param);
        let post = this.service.loadModel(this.utilServiceName, tempContext, param);
        post.then((response: any) => {
            this.onControlResponse('load', response);
            if (response.status == 200) {
                this.historyItems = response.data;
            }
            this.ctrlEvent({ controlname: this.controlInstance.name, action: "load", data: this.historyItems });
        }).catch((response: any) => {
            this.onControlResponse('load', response);
            LogUtil.log(response);
        });
    }

    /**
     * 改变过滤条件
     *
     * @param {*} evt
     * @memberof SearchBarControlBase
     */
    public onFilterChange(evt: any) {
        let item: any = this.historyItems.find((item: any) => Object.is(evt, item.value));
        if (item) {
            this.selectItem = item.value;
            this.filterItems = Util.deepCopy(item.data);
        }
    }

    /**
     * 打开弹框
     *
     * @return {*}
     * @memberof SearchBarControlBase
     */
    public openPoper() {
        this.saveItemName = '';
    }

    /**
     * 确定
     *
     * @return {*}
     * @memberof SearchBarControlBase
     */
    public onOk() {
        if (this.Environment && this.Environment.isPreviewMode) {
            return;
        }
        let propip: any = this.$refs.propip;
        propip.handleMouseleave();
        this.onSave(this.saveItemName);
    }

    /**
     * 取消设置
     *
     * @return {*}
     * @memberof SearchBarControlBase
     */
    public onCancel() {
        if (this.Environment && this.Environment.isPreviewMode) {
            return;
        }
        let propip: any = this.$refs.propip;
        propip.handleMouseleave();
        // this.onSave();
    }

    /**
     * 获取数据集
     * 
     * @memberof SearchBarControlBase
     */
    public getDatas(): any[] {
        return [];
    }

    /**
     * 获取单项数据
     * 
     * @memberof SearchBarControlBase
     */
    public getData() {
        let data: any = {};
        if (this.filterFields.length > 0) {
            let filter: any = this.getFilter();
            Object.assign(data, { filter: filter ? filter : null })
        }
        if (this.viewIsProxyMode) {
            Object.assign(data, this.queryParams);
        }
        return data;
    }

    /**
     * 获取过滤树
     * 
     * @memberof SearchBarControlBase
     */
    public getFilter() {
        if (this.filterItems.length === 0) {
            return null;
        }
        let ands: any[] = this.transformAnd(this.filterItems);
        this.transformResult(ands, '$and');
        if (ands.length === 0) {
            return null;
        }
        return { '$and': ands };
    }

    /**
     * 处理结果集
     *
     * @param {any[]} datas 数据
     * @param {string} pName
     * @memberof SearchBarControlBase
     */
    public transformResult(datas: any[], pName: string) {
        let items: any[] = [];
        for (let i = datas.length - 1; i >= 0; i--) {
            let data: any = datas[i];
            let field: string = Object.is(pName, '$and') ? '$or' : '$and';
            if (data.hasOwnProperty(field)) {
                items.push(data);
                datas.splice(i, 1);
                this.transformResult(data[field], field);
            }

        }
        if (items.length > 0) {
            let item: any = {};
            item[pName] = items;
            datas.push(item);
        }
    }

    /**
     * 处理并且逻辑
     *
     * @param {any[]} datas 数据
     * @return {*}  {*}
     * @memberof SearchBarControlBase
     */
    public transformAnd(datas: any[]): any {
        let result: any[] = [];
        datas.forEach((data: any) => {
            let item: any = {};
            if (data.field && data.mode) {
                item[data.field] = {};
                let valField: string = data.editor ? data.editor : data.field;
                item[data.field][data.mode] = (data[valField] == null ? '' : data[valField]);
                result.push(item)
            } else if (Object.is(data.label, '$and')) {
                let items: any[] = this.transformAnd(data.children);
                result = [...result, ...items];
            } else if (Object.is(data.label, '$or')) {
                item[data.label] = this.transformOr(data.children);
                result.push(item)
            }
        })
        return result;
    }

    /**
     * 处理或逻辑
     *
     * @param {any[]} datas 数据
     * @return {*} 
     * @memberof SearchBarControlBase
     */
    public transformOr(datas: any[]) {
        let result: any[] = [];
        datas.forEach((data: any) => {
            let item: any = {};
            if (data.field && data.mode) {
                item[data.field] = {};
                let valField: string = data.editor ? data.editor : data.field;
                item[data.field][data.mode] = (data[valField] == null ? '' : data[valField]);
                result.push(item);
            } else if (Object.is(data.label, '$and')) {
                item[data.label] = this.transformAnd(data.children);
                result.push(item)
            } else if (Object.is(data.label, '$or')) {
                item[data.label] = this.transformOr(data.children);
                result.push(item);
            }
        })
        return result;
    }

    /**
     * 加载快速分组项
     *
     * @protected
     * @memberof SearchBarControlBase
     */
    protected async loadQuickGroupItem() {
        //  TODO 待补充搜索栏快速分组代码表模型(补充后此逻辑删除)
        this.quickGroupItems = [];
        (this.controlInstance.getPSSearchBarGroups?.() || []).forEach((item: any) => {
            this.quickGroupItems.push({
                label: item.caption,
                value: item.name,
                data: item.data ? JSON.parse(item.data) : {},
                id: item.name
            });
        });
    }

}