import { RemoteParams, SearchParam, UserFavoriteData } from '../http/RemoteParams'; import { LOAD_DATA_TYPE } from '../lookup-grid-options'; import { LookupGridComponent } from '../lookup-grid.component'; import { cloneDeep } from 'lodash-es'; import { FavoriteAction, LookupGridDisplayType } from '../lookup-displaytype'; import { Observable, of, forkJoin } from 'rxjs'; import { map, switchMap, tap, debounceTime } from 'rxjs/operators'; // 帮助默认个性化数据 const DefaultUserConfig = { tabIndex: 'datalist', favorite: null, size: null }; export class LookupHttpManager { // 每次帮助打开后,更新此值,做为个性化数据的初始值; // 关闭窗口时,与此进行对比。如果一样,则不保存; private _originalPersonalConfig = DefaultUserConfig; constructor(private ins: LookupGridComponent) { } private disablePagination() { return { pageIndex: 1, pageSize: 500 }; } /** 构造查询参数 */ buildQueryParams(event?: any, type: LOAD_DATA_TYPE = 'all') { const params: RemoteParams = {}; if (this.ins.condition) { params.condition = cloneDeep(this.ins.condition); } const searchParam: SearchParam = { category: type }; if (this.ins.isDoublleList() && this.ins.navigationFilter && type !== 'all') { if (this.ins.navigationFilter.idValue && type !== 'textchange') { params.relationFilter = [...this.ins.navigationFilter.idValue]; } } if (event) { if (type === 'fav' || type === 'selected') { event.pageInfo = this.disablePagination(); } if (event.pageInfo) { params.pageIndex = event.pageInfo.pageIndex; params.pageSize = event.pageInfo.pageSize; } if (event.search) { let sfield = event.search.field; if (sfield && sfield === '*') { sfield = '*'; } searchParam.searchField = sfield; searchParam.searchValue = event.search.value; } if (event.sortName) { searchParam.sortName = event.sortName; } if (event.sortOrder) { searchParam.sortOrder = event.sortOrder; } } if (type === 'fav' && event.favoriteIds) { searchParam.favoriteIds = event.favoriteIds; } if (this.ins.isTree() || this.ins.displayType === LookupGridDisplayType.NavTreeList) { params.enableFullTree = this.ins.enableFullTree; } // 查询时不构造完整树 if (type === 'textchange') { params.enableFullTree = false; } if (type === 'selected') { searchParam.category = 'fav'; params.enableFullTree = false; searchParam.favoriteIds = event.favoriteIds; } params.searchValue = JSON.stringify(searchParam); params.loadTreeDataType = this.ins.loadTreeDataType; params.customData = this.ins.customData; if (this.ins.helpId) { params.helpId = this.ins.helpId; } if (event.selectedInfo) { params.selectedInfo = event.selectedInfo; } return params; } getData(event?: any, type: LOAD_DATA_TYPE = 'all'): Observable { const uri = this.ins.gridOptions.uri; const params = this.buildQueryParams(event, type); if (uri || this.ins.beUri) { if (this.ins.beUri && this.ins.columns && this.ins.columns.length) { const allSearchFields = this.ins.columns.map(col => col.searchField).filter(f => f); if (!params.condition) { params.condition = {}; } if (!this.ins.isTree() && this.ins.pagination) { const { pageSize = this.ins.pageSize || 20, pageIndex } = { ...params }; params.condition.pagination = { pageSize, pageIndex }; } else { params.condition.pagination = { isUsePagination: false }; } const searchParam = JSON.parse(params.searchValue); if (searchParam.searchValue) { params.condition = this.ins.lookupUtils.mergeCondition(params.condition, allSearchFields, { field: searchParam.searchField, value: searchParam.searchValue }); } } const _uri = this.ins.beUri || uri; if (this.ins.http) { this.ins.http.context = this.ins.context; } if (this.ins._searchResult) { return of(this.ins._searchResult); } if (type !== 'allChildren') { return this.ins.http.getData(_uri, params); } else { const params1 = { searchValue: JSON.stringify({ category: type }), parentsIds: event.parentsIds, customData: params.customData, helpId: params.helpId }; return this.ins.http.getData(_uri, params1); } } else { return of(false); } } // getFavoriteData(params) { // return this.getData(params, 'fav'); // } getSelecedItems(selIds: any[]) { return this.getData({ favoriteIds: selIds }, 'selected'); } getPersonalConfig() { const defaultConf = cloneDeep(DefaultUserConfig); const key = this.ins.personalConfigService._newKey; let _conf = this.ins.personalConfigService.getPersonalData(key); if (!_conf || !Object.keys(_conf).length) { _conf = defaultConf; } const req = of(_conf); if (this.ins.favoriteDataFrom === 'locale' || this.ins.isTabChanged) { return req; } if (this.ins.http && this.ins.http['getUserSettings']) { return this.ins.http['getUserSettings'](key).pipe( map((ucs: any) => { if (ucs) { return ucs.textValue ? JSON.parse(ucs.textValue) : defaultConf; } return defaultConf; }) ); } else { return req; } } lookupRequest(event?: any, type: LOAD_DATA_TYPE = 'all') { if (!this.ins.usePersionalConf) { return this.getData(event, type); } const req = this.getPersonalConfig(); return req.pipe( tap((c: any) => { this.ins.personalConf = c; this.ins.personalConfigService.savePersonalConfig(c); if (!this.ins.isTabChanged) { this._originalPersonalConfig = cloneDeep(c); } }), switchMap((c: any) => { const { tabIndex, favorite, size } = c; if (!this.ins.isTabChanged) { this.ins.activeTab = tabIndex || 'datalist'; } if (size) { this.ins.dialogWidth = size.width; this.ins.dialogHeight = size.height; this.ins.dialog.reSize({ width: size.width, height: size.height }); } if (this.ins.activeTab === 'datalist') { return this.getData(event, type); } else if (this.ins.activeTab === 'favorite') { const favIds = favorite ? favorite[this.ins.localService.localeId] : []; if (!favIds || !favIds.length) { return this.getData(event, type).pipe( map(r => { if (r) { r.items = []; } return r; }) ); } const _fids = favIds.filter(n => n); event.favoriteIds = favIds; return this.getData(event, 'fav'); } else if (this.ins.activeTab === 'selected') { const selIds = this.ins.displayValue ? this.ins.displayValue.split(',') : []; const _sids = selIds.filter(n => n); return this.getSelecedItems(_sids); } }) ); } // 保存个性化数据 submitFavoriteData(action: any | FavoriteAction) { // 如果数据与默认的数据一至则不保存。 if (JSON.stringify(this.ins.personalConf) === JSON.stringify(this._originalPersonalConfig)) { return; } let msg = ''; if (action === FavoriteAction.add) { msg = this.ins.addFavoriteSuccess; } else if (action === FavoriteAction.delete) { msg = this.ins.delFavoriteSuccess; } this.ins.personalConfigService.savePersonalConfig(this.ins.personalConf || {}); if (this.ins.favoriteDataFrom !== 'locale') { this._originalPersonalConfig = cloneDeep(this.ins.personalConf); const configData: UserFavoriteData = { configkey1: this.ins.personalConfigService.personalConfigKey, configkey2: '', configkey3: '', textvalue: JSON.stringify(this.ins.personalConf) }; if (this.ins.http && this.ins.http['saveUserSettings']) { this.ins.savingFaoriteData = true; this.ins.showLoading(); return this.ins.http['saveUserSettings'](configData).subscribe((r) => { this.ins.savingFaoriteData = false; this.ins.closeLoading(); if (msg) { this.ins.notifyService.success(msg); } }); } } else { if (msg) { this.ins.notifyService.success(msg); } } } }