app-form-service.ts 25.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 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
import { IPSAppDataEntity, IPSDEEditForm, IPSDEFormItem, IPSDEFormPage } from '@ibiz/dynamic-model-api';
import { ControlServiceBase, Util, DataServiceHelp, UtilServiceHelp } from 'ibiz-core';
import { AppFormModel } from 'ibiz-vue';
import { notNilEmpty } from 'qx-util';


/**
 * Main 部件服务对象
 *
 * @export
 * @class AppFormService
 */
export class AppFormService extends ControlServiceBase {

    /**
    * 表单实例对象
    *
    * @memberof MainModel
    */
    public declare controlInstance: IPSDEEditForm;

    /**
     * 数据服务对象
     *
     * @type {any}
     * @memberof AppFormService
     */
    public appEntityService!: any;

    /**
     * 远端数据
     *
     * @type {*}
     * @memberof AppFormService
     */
    private remoteCopyData: any = {};

    /**
     * ac服务参数Map
     *
     * @private
     * @type {Map<string, IPSAppDataEntity>}
     * @memberof AppFormService
     */
    private acServiceMap: Map<string, IPSAppDataEntity> = new Map();

    /**
     * 初始化AC参数
     *
     * @private
     * @memberof AppFormService
     */
    private async initAcParam() {
        if (this.controlInstance && this.controlInstance.getPSDEFormPages()) {
            for (let i = 0; i < (this.controlInstance.getPSDEFormPages() as IPSDEFormPage[]).length; i++) {
                const appDEFormPage = (this.controlInstance.getPSDEFormPages() as IPSDEFormPage[])[i];
                await this.initAcServiceParam(appDEFormPage);
            }
        }
    }

    /**
     * 初始化AC服务参数
     *
     * @private
     * @param {*} parentModel
     * @memberof AppFormService
     */
    private async initAcServiceParam(parentModel: any) {
        if (parentModel && parentModel.getPSDEFormDetails && parentModel.getPSDEFormDetails instanceof Function && parentModel.getPSDEFormDetails()?.length > 0) {
            for(let i = 0;i<parentModel.getPSDEFormDetails().length;i++){
                const detail = parentModel.getPSDEFormDetails()[i];
                if (detail && detail.detailType === 'FORMITEM' && detail.getPSEditor()) {
                    if (detail.getPSEditor()?.getPSAppDataEntity?.()) {
                        const appDataEntity = detail.getPSEditor().getPSAppDataEntity();
                        await appDataEntity.fill();
                        this.acServiceMap.set(appDataEntity.codeName, appDataEntity);
                    }
                } else {
                    this.initAcServiceParam(detail);
                }
            }
        }
    }

    /**
     * 初始化服务参数
     *
     * @type {boolean}
     * @memberof AppFormService
     */
    public async initServiceParam() {
        this.appEntityService = await DataServiceHelp.getInstance().getService(this.controlInstance?.getPSAppDataEntity(), { context: this.context });
        this.model = new AppFormModel(this.controlInstance);
    }

    /**
     * Creates an instance of AppFormService.
     * 
     * @param {*} [opts={}]
     * @memberof AppFormService
     */
    constructor(opts: any = {}, context?: any, args?: any) {
        super(opts, context, args);
        this.controlInstance = opts;
    }

    /**
     * loaded
     *
     * @memberof AppFormService
     */
    public async loaded() {
        await this.initServiceParam();
        await this.initAcParam();
    }

    /**
     * 处理数据
     *
     * @private
     * @param {Promise<any>} promise
     * @returns {Promise<any>}
     * @memberof AppFormService
     */
    private doItems(promise: Promise<any>, deKeyField: string, deName: string): Promise<any> {
        return new Promise((resolve, reject) => {
            promise.then((response: any) => {
                if (response && response.status === 200) {
                    const data = response.data;
                    data.forEach((item: any, index: number) => {
                        item[deName] = item[deKeyField];
                        data[index] = item;
                    });
                    resolve(data);
                } else {
                    reject([])
                }
            }).catch((response: any) => {
                reject([])
            });
        });
    }

    /**
     * 获取跨实体数据集合
     *
     * @param {string} serviceName 服务名称
     * @param {string} interfaceName 接口名称
     * @param {*} data
     * @param {boolean} [isloading]
     * @returns {Promise<any[]>}
     * @memberof  AppFormService
     */
    public getItems(serviceName: string, interfaceName: string, context: any = {}, data: any, isloading?: boolean): Promise<any[]> {
        data.page = data.page ? data.page : 0;
        data.size = data.size ? data.size : 1000;
        return new Promise((resolve: any, reject: any) => {
            if (this.acServiceMap.get(serviceName)) {
                DataServiceHelp.getInstance().getService(this.acServiceMap.get(serviceName) as IPSAppDataEntity).then((service: any) => {
                    if (service) {
                        resolve(this.doItems(service.execute(interfaceName, context, data, isloading), service.APPDEKEY.toLowerCase(), service.APPDENAME.toLowerCase()));
                    }
                }).catch((error: any) => {
                    reject([]);
                })
            } else {
                reject([]);
            }
        })
    }

    /**
     * 启动工作流
     *
     * @param {string} action
     * @param {*} [context={}]
     * @param {*} [data={}]
     * @param {boolean} [isloading]
     * @param {*} [localdata]
     * @returns {Promise<any>}
     * @memberof AppFormService
     */
    public wfstart(action: string, context: any = {}, data: any = {}, isloading?: boolean, localdata?: any): Promise<any> {
        data = this.handleWFData(data, true);
        context = this.handleRequestData(action, context, data).context;
        return new Promise((resolve: any, reject: any) => {
            let result: Promise<any>;
            if (this.appEntityService[action] && this.appEntityService[action] instanceof Function) {
                result = this.appEntityService.execute(context, data, localdata);
            } else {
                result = this.appEntityService.execute('WFStart', context, data, localdata);
            }
            result.then((response) => {
                this.handleResponse(action, response);
                resolve(response);
            }).catch(response => {
                reject(response);
            });
        });
    }

    /**
     * 提交工作流
     *
     * @param {string} action
     * @param {*} [context={}]
     * @param {*} [data={}]
     * @param {boolean} [isloading]
     * @param {*} [localdata]
     * @returns {Promise<any>}
     * @memberof AppFormService
     */
    public wfsubmit(action: string, context: any = {}, data: any = {}, isloading?: boolean, localdata?: any): Promise<any> {
        data = this.handleWFData(data, true);
        context = this.handleRequestData(action, context, data, true).context;
        return new Promise((resolve: any, reject: any) => {
            let result: Promise<any>;
            if (this.appEntityService[action] && this.appEntityService[action] instanceof Function) {
                result = this.appEntityService.execute(action, context, data, localdata);
            } else {
                result = this.appEntityService.execute('WFSubmit', context, data, localdata);
            }
            result.then((response) => {
                this.handleResponse(action, response);
                resolve(response);
            }).catch(response => {
                reject(response);
            });
        });
    }

    /**
     * 添加数据
     *
     * @param {string} action
     * @param {*} [context={}]
     * @param {*} [data={}]
     * @param {boolean} [isloading]
     * @param {boolean} [isWorkflow] 是否在工作流中添加数据
     * @returns {Promise<any>}
     * @memberof AppFormService
     */
    public add(action: string, context: any = {}, data: any = {}, isloading?: boolean, isWorkflow?: boolean): Promise<any> {
        const { data: Data, context: Context } = this.handleRequestData(action, context, data, isWorkflow);
        // 手动修改数据主键的情况
        this.controlInstance.getPSDEFormItems()?.find((item: IPSDEFormItem) => {
            if (!item.hidden && item.getPSAppDEField()?.keyField) {
                Object.assign(Data, {
                    [this.appDeKeyFieldName.toLowerCase()]: data[item.id],
                    srffrontuf: '1'
                });
                return true;
            }
        })
        return new Promise((resolve: any, reject: any) => {
            let result: Promise<any>;
            if (this.appEntityService && action) {
                result = this.appEntityService.execute(action, Context, Data);
            } else {
                result = this.appEntityService.execute('Create', Context, Data);
            }
            result.then((response) => {
                if (isWorkflow) {
                    resolve(response);
                } else {
                    this.handleResponse(action, response);
                    resolve(response);
                }
            }).catch(response => {
                reject(response);
            });
        });
    }

    /**
     * 删除数据
     *
     * @param {string} action
     * @param {*} [context={}]
     * @param {*} [data={}]
     * @param {boolean} [isloading]
     * @returns {Promise<any>}
     * @memberof AppFormService
     */
    public delete(action: string, context: any = {}, data: any = {}, isloading?: boolean): Promise<any> {
        const { data: Data, context: Context } = this.handleRequestData(action, context, data);
        return new Promise((resolve: any, reject: any) => {
            let result: Promise<any>;
            if (this.appEntityService && action) {
                result = this.appEntityService.execute(action, Context, Data);
            } else {
                result = this.appEntityService.execute('Remove', Context, Data);
            }
            result.then((response) => {
                resolve(response);
            }).catch(response => {
                reject(response);
            });
        });
    }

    /**
     * 修改数据
     *
     * @param {string} action
     * @param {*} [context={}]
     * @param {*} [data={}]
     * @param {boolean} [isloading]
     * @param {boolean} [isWorkflow] 是否在工作流中修改数据
     * @returns {Promise<any>}
     * @memberof AppFormService
     */
    public update(action: string, context: any = {}, data: any = {}, isloading?: boolean, isWorkflow?: boolean): Promise<any> {
        const { data: Data, context: Context } = this.handleRequestData(action, context, data, isWorkflow);
        return new Promise((resolve: any, reject: any) => {
            let result: Promise<any>;
            if (this.appEntityService && action) {
                result = this.appEntityService.execute(action, Context, Data);
            } else {
                result = this.appEntityService.execute('Update', Context, Data);
            }
            result.then((response) => {
                if (isWorkflow) {
                    resolve(response);
                } else {
                    this.handleResponse(action, response);
                    resolve(response);
                }
            }).catch(response => {
                reject(response);
            });
        });
    }

    /**
     * 查询数据
     *
     * @param {string} action
     * @param {*} [context={}]
     * @param {*} [data={}]
     * @param {boolean} [isloading]
     * @returns {Promise<any>}
     * @memberof AppFormService
     */
    public get(action: string, context: any = {}, data: any = {}, isloading?: boolean): Promise<any> {
        const { data: Data, context: Context } = this.handleRequestData(action, context, data, true);
        return new Promise((resolve: any, reject: any) => {
            let result: Promise<any>;
            if (this.localSourceTag) {
                result = this.getLocalDataSource({ isSingleCtrl: true });
            } else {
                if (this.appEntityService && action) {
                    result = this.appEntityService.execute(action, Context, Data);
                } else {
                    result = this.appEntityService.execute('Get', Context, Data);
                }
            }
            result.then((response) => {
                this.setRemoteCopyData(response);
                this.handleResponse(action, response);
                resolve(response);
            }).catch(response => {
                reject(response);
            });
        });
    }

    /**
     * 加载草稿
     *
     * @param {string} action
     * @param {*} [context={}]
     * @param {*} [data={}]
     * @param {boolean} [isloading]
     * @returns {Promise<any>}
     * @memberof AppFormService
     */
    public loadDraft(action: string, context: any = {}, data: any = {}, isloading?: boolean): Promise<any> {
        if (this.controlInstance?.controlType == "SEARCHFORM") {
            return new Promise((resolve: any, reject: any) => {
382
                let response: any = { status: 200, data: data };
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
                this.handleResponse(action, response, true);
                resolve(response);
            });
        }
        const { data: Data, context: Context } = this.handleRequestData(action, context, data, true);
        // 仿真主键数据
        const PrimaryKey = Util.createUUID();
        if (this.controlInstance.controlType != 'SEARCHFORM') {
            Data[this.appDeKeyFieldName.toLowerCase()] = PrimaryKey;
            Data[this.appDeCodeName.toLowerCase()] = PrimaryKey;
        }
        return new Promise((resolve: any, reject: any) => {
            let result: Promise<any>;
            if (this.appEntityService && action) {
                result = this.appEntityService.execute(action, Context, Data);
            } else {
                result = this.appEntityService.execute('GetDraft', Context, Data);
            }
            result.then((response) => {
                this.setRemoteCopyData(response);
                if (response.data && (this.controlInstance.controlType != 'SEARCHFORM')) {
                    response.data[this.appDeKeyFieldName.toLowerCase()] = PrimaryKey;
                }
                this.handleResponse(action, response, true);
                resolve(response);
            }).catch(response => {
                reject(response);
            });
        });
    }

    /**
    * 前台逻辑
    * @param {string} action
    * @param {*} [context={}]
    * @param {*} [data={}]
    * @param {boolean} [isloading]
    * @returns {Promise<any>}
    * @memberof AppFormService
    */
    public frontLogic(action: string, context: any = {}, data: any = {}, isloading?: boolean): Promise<any> {
        const { data: Data, context: Context } = this.handleRequestData(action, context, data);
        return new Promise((resolve: any, reject: any) => {
            let result: Promise<any>;
            if (this.appEntityService && action) {
                result = this.appEntityService.execute(action, Context, Data);
            } else {
                return reject({ status: 500, data: { title: '失败', message: `实体服务缺少${action}方法` } });
            }
            result.then((response) => {
                this.handleResponse(action, response, true);
                resolve(response);
            }).catch(response => {
                reject(response);
            });
        })
    }

    /**
     * 处理请求数据
     * 
     * @param action 行为 
     * @param data 数据
     * @memberof AppFormService
     */
    public handleRequestData(action: string, context: any, data: any = {}, isMerge: boolean = false) {
        let mode: any = this.getMode();
        if (!mode && mode.getDataItems instanceof Function) {
            return data;
        }
        let formItemItems: any[] = mode.getDataItems();
        let requestData: any = {};
        if (isMerge && (data && data.viewparams)) {
            Object.assign(requestData, data.viewparams);
        }
        formItemItems.forEach((item: any) => {
            if (item && item.dataType && Object.is(item.dataType, 'FRONTKEY')) {
                if (item && item.prop) {
                    requestData[item.prop] = context[item.name];
                }
463 464 465 466 467 468 469 470
            }else if(item && item.dataType && Object.is(item.dataType,'REPEATER')){
                if (mode && mode.getRepeaterModel instanceof Function) {
                    let repeaterModel: any[] = mode.getRepeaterModel();
                    const tempRepeaterModel = repeaterModel.filter((element:any) =>{
                        return element.parentName === item.name;
                    })
                    requestData[item.prop] = this.transformValue(tempRepeaterModel,false,data[item.name]);
                }
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
            } else {
                if (item && item.prop && item.name && (data[item.name] || data[item.name] === 0 || data[item.name] === null)) {
                    requestData[item.prop] = data[item.name];
                } else {
                    if (item.dataType && Object.is(item.dataType, "FORMPART")) {
                        Object.assign(requestData, data[item.name]);
                    }
                }
            }
        });
        let tempContext: any = JSON.parse(JSON.stringify(context));
        if (tempContext && tempContext.srfsessionid) {
            tempContext.srfsessionkey = tempContext.srfsessionid;
            delete tempContext.srfsessionid;
        }
        return { context: tempContext, data: requestData };
    }

    /**
     * 通过属性名称获取表单项名称
     * 
     * @param name 实体属性名称 
     * @memberof AppFormService
     */
    public getItemNameByDeName(name: string): string {
        let itemName = name;
        let mode: any = this.getMode();
        if (!mode && mode.getDataItems instanceof Function) {
            return name;
        }
        let formItemItems: any[] = mode.getDataItems();
        formItemItems.forEach((item: any) => {
            if (item.prop === name) {
                itemName = item.name;
            }
        });
        return itemName.trim();
    }

    /**
     * 重写处理返回数据
     *
     * @param {string} action
     * @param {*} response
     * @memberof AppFormService
     */
    public handleResponseData(action: string, data: any = {}, isCreate?: boolean, codelistArray?: any) {
        if (data.srfopprivs) {
            this.getStore().commit('authresource/setSrfappdeData', { key: `${this.deName}-${data[this.appDeKeyFieldName.toLowerCase()]}`, value: data.srfopprivs });
        }
        let model: any = this.getMode();
        if (!model && model.getDataItems instanceof Function) {
            return data;
        }
        let item: any = {};
        let dataItems: any[] = model.getDataItems();
        dataItems.forEach(dataitem => {
528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549
            if(dataitem && dataitem.dataType && Object.is(dataitem.dataType,'REPEATER')){
                if(model && model.getRepeaterModel instanceof Function){
                    let repeaterModel: any[] = model.getRepeaterModel();
                    const tempRepeaterModel = repeaterModel.filter((item:any) =>{
                        return item.parentName === dataitem.name;
                    })
                    item[dataitem.name] = this.transformValue(tempRepeaterModel,true,data[dataitem.name]);
                }
            }else{
                let val = notNilEmpty(data[dataitem.prop]) ? data[dataitem.prop] : null;
                if (val === null) {
                    val = notNilEmpty(data[dataitem.name]) ? data[dataitem.name] : null;
                }
                if ((isCreate === undefined || isCreate === null) && Object.is(dataitem.dataType, 'GUID') && Object.is(dataitem.name, 'srfkey') && (val && !Object.is(val, ''))) {
                    isCreate = true;
                }
                item[dataitem.name] = val;
                // 转化代码表
                if (codelistArray && dataitem.codelist) {
                    if (codelistArray.get(dataitem.codelist.tag) && codelistArray.get(dataitem.codelist.tag).get(val)) {
                        item[dataitem.name] = codelistArray.get(dataitem.codelist.tag).get(val);
                    }
550 551 552 553 554 555 556
                }
            }
        });
        item.srfuf = data.srfuf ? data.srfuf : (isCreate ? "0" : "1");
        return item;
    }

557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626
    /**
     * 转化表单重复器值
     *
     * @param {*} model
     * @param {boolean} isReverse
     * @param {any[]} [source=[]]
     * @memberof Usr0222816489Service
     */
    public transformValue(model: any, isReverse: boolean, source: any) {
        let returnValues: any;
        const tempMap: Map<string, Array<string>> = new Map();
        if (model && model.length > 0) {
            model.forEach((element: any) => {
                if (element.valueItem) {
                    if (isReverse) {
                        if (tempMap.has(element.valueItem)) {
                            const tempArray: Array<string> = tempMap.get(element.valueItem) as Array<string>;
                            tempArray?.push(element.codeName);
                            tempMap.set(element.valueItem, tempArray);
                        } else {
                            tempMap.set(element.valueItem, [element.codeName]);
                        }
                    } else {
                        tempMap.set(element.codeName, [element.valueItem]);
                    }
                } else {
                    tempMap.set(element.codeName, [element.codeName]);
                }
            });
        }
        if (source && source instanceof Array) {
            returnValues = [];
            if(source.length > 0){
                source.forEach((data: any) => {
                    let tempItem: any = {};
                    if (data && Object.keys(data).length > 0) {
                        Object.keys(data).forEach((field: string) => {
                            if (tempMap.has(field)) {
                               const keys: string[] = tempMap.get(field) as string[];
                                if(keys && keys.length >0){
                                    keys.forEach((key:string) =>{
                                        tempItem[key] = data[field];
                                    })
                                }
                            } else {
                                tempItem[field] = data[field];
                            }
                        });
                    }
                    returnValues.push(tempItem);
                });
            }
        } else if (source && source instanceof Object && Object.keys(source).length > 0) {
            returnValues = {};
            Object.keys(source).forEach((field: string) => {
                if (tempMap.has(field)) {
                    const keys: string[] = tempMap.get(field) as string[];
                    if(keys && keys.length >0){
                        keys.forEach((key:string) =>{
                            returnValues[key] = source[field];
                        })
                    }
                } else {
                    returnValues[field] = source[field];
                }
            });
        }
        return returnValues;
    }

627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696
    /**
     * 设置远端数据
     * 
     * @param result 远端请求结果 
     * @memberof AppFormService
     */
    public setRemoteCopyData(result: any) {
        if (result && result.status === 200) {
            this.remoteCopyData = Util.deepCopy(result.data);
        }
    }

    /**
     * 获取远端数据
     * 
     * @memberof AppFormService
     */
    public getRemoteCopyData() {
        return this.remoteCopyData;
    }

    /**
     * 加载数据模型
     *
     * @param {string} serviceName
     * @param {*} context
     * @param {*} viewparams
     * @memberof AppFormService
     */
    public loadModel(serviceName: string, context: any, viewparams: any) {
        return new Promise((resolve: any, reject: any) => {
            UtilServiceHelp.getInstance().getService(context, serviceName).then((service: any) => {
                if (service) {
                    service.loadModelData(JSON.stringify(context), viewparams).then((response: any) => {
                        resolve(response);
                    }).catch((response: any) => {
                        reject(response);
                    });
                }
            }).catch((response: any) => {
                reject(response);
            });
        });
    }

    /**
     * 保存模型
     *
     * @param {string} serviceName
     * @param {*} context
     * @param {*} viewparams
     * @returns
     * @memberof AppFormService
     */
    public saveModel(serviceName: string, context: any, viewparams: any) {
        return new Promise((resolve: any, reject: any) => {
            UtilServiceHelp.getInstance().getService(context, serviceName).then((service: any) => {
                if (service) {
                    service.saveModelData(JSON.stringify(context), viewparams).then((response: any) => {
                        resolve(response);
                    }).catch((response: any) => {
                        reject(response);
                    });
                }
            }).catch((response: any) => {
                reject(response);
            });
        });
    }
}