<#assign import_block>
<#list ctrl.getPSDEGridEditItems() as deItem>
<#if deItem.getPSEditor()?? && deItem.getPSEditor().getPSAppDataEntity?? && deItem.getPSEditor().getPSAppDataEntity()??>
<#assign _appde = deItem.getPSEditor().getPSAppDataEntity()/>
<#if _appde.getId() != appde.getId()>
<#if !P.exists("importService", _appde.getId(), "")>
import ${srfclassname('${_appde.getCodeName()}')}Service from '@/service/${srffilepath2(_appde.getCodeName())}/${srffilepath2(_appde.getCodeName())}-service';
</#if>
</#if>
</#if>
</#list>
<#if ctrl.getAggMode() == "ALL" && ctrl.getAggPSAppDataEntity()??>
<#assign aggAppDataEntity = ctrl.getAggPSAppDataEntity() >
import ${srfclassname('${aggAppDataEntity.getCodeName()}')}Service from '@/service/${srffilepath2(aggAppDataEntity.getCodeName())}/${srffilepath2(aggAppDataEntity.getCodeName())}-service';
</#if>
</#assign>
<#ibizinclude>
../@MACRO/SERVICE/SERVICE_HEADER.ts.ftl
</#ibizinclude>

<#list ctrl.getPSDEGridEditItems() as deItem>
    <#if deItem.getPSEditor()?? && deItem.getPSEditor().getPSAppDataEntity?? && deItem.getPSEditor().getPSAppDataEntity()??>
    <#assign _appde = deItem.getPSEditor().getPSAppDataEntity()/>
    <#if _appde.getId() != appde.getId()>
    <#if !P.exists("importService2", _appde.getId(), "")>

    /**
     * ${_appde.getLogicName()}服务对象
     *
     * @type {${srfclassname('${_appde.getCodeName()}')}Service}
     * @memberof ${srfclassname('${ctrl.codeName}')}Service
     */
    public ${_appde.getCodeName()?lower_case}Service: ${srfclassname('${_appde.getCodeName()}')}Service = new ${srfclassname('${_appde.getCodeName()}')}Service();
    </#if>
    </#if>
    </#if>
    </#list>

    /**
     * 处理数据
     *
     * @public
     * @param {Promise<any>} promise
     * @returns {Promise<any>}
     * @memberof ${srfclassname('${ctrl.codeName}')}Service
     */
    public 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  ${srfclassname('${ctrl.codeName}')}Service
     */
    @Errorlog
    public getItems(serviceName: string, interfaceName: string, context: any = {}, data: any, isloading?: boolean): Promise<any[]> {
    <#list ctrl.getPSDEGridEditItems() as deItem>
    <#if deItem.getPSEditor()?? && deItem.getPSEditor().getPSAppDataEntity?? && deItem.getPSEditor().getPSAppDataEntity()?? && deItem.getPSEditor().getPSAppDEDataSet?? && deItem.getPSEditor().getPSAppDEDataSet()??>
    <#assign _appde = deItem.getPSEditor().getPSAppDataEntity()/>
    <#assign deDataSet = deItem.getPSEditor().getPSAppDEDataSet()/>
    <#if !P.exists("importService4", _appde.getId() + deDataSet.getCodeName(), "")>
    <#if _appde.getId() == appde.getId()>
        if (Object.is(serviceName, '${srfclassname('${_appde.getCodeName()}')}Service') && Object.is(interfaceName, '${deDataSet.getCodeName()}')) {
            return this.doItems(this.appEntityService.${deDataSet.getCodeName()}(JSON.parse(JSON.stringify(context)), data, isloading), '${_appde.getKeyPSAppDEField().getCodeName()?lower_case}', '${_appde.getCodeName()?lower_case}');
        }
    <#else>
        if (Object.is(serviceName, '${srfclassname('${_appde.getCodeName()}')}Service') && Object.is(interfaceName, '${deDataSet.getCodeName()}')) {
            return this.doItems(this.${_appde.getCodeName()?lower_case}Service.${deDataSet.getCodeName()}(JSON.parse(JSON.stringify(context)),data, isloading), '${_appde.getKeyPSAppDEField().getCodeName()?lower_case}', '${_appde.getCodeName()?lower_case}');
        }
    </#if>
    </#if>
    </#if>
    </#list>

        return Promise.reject([])
    }

    /**
     * 添加数据
     *
     * @param {string} action
     * @param {*} [context={}]
     * @param {*} [data={}]
     * @param {boolean} [isloading]
     * @returns {Promise<any>}
     * @memberof ${srfclassname('${ctrl.codeName}')}Service
     */
    @Errorlog
    public add(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) => {
            const _appEntityService: any = this.appEntityService;
            let result: Promise<any>;
            if (_appEntityService[action] && _appEntityService[action] instanceof Function) {
                result = _appEntityService[action](Context,Data, isloading);
            }else{
                result =_appEntityService.Create(Context,Data, isloading);
            }
            result.then((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 ${srfclassname('${ctrl.codeName}')}Service
     */
    @Errorlog
    public delete(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) => {
            const _appEntityService: any = this.appEntityService;
            let result: Promise<any>;
            if (_appEntityService[action] && _appEntityService[action] instanceof Function) {
                result = _appEntityService[action](Context,Data, isloading);
            }else{
                result =_appEntityService.Remove(Context,Data, isloading);
            }
            result.then((response) => {
                resolve(response);
            }).catch(response => {
                reject(response);
            });      
        });
    }

    /**
     * 修改数据
     *
     * @param {string} action
     * @param {*} [context={}]
     * @param {*} [data={}]
     * @param {boolean} [isloading]
     * @returns {Promise<any>}
     * @memberof ${srfclassname('${ctrl.codeName}')}Service
     */
    @Errorlog
    public update(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) => {
            const _appEntityService: any = this.appEntityService;
            let result: Promise<any>;
            if (_appEntityService[action] && _appEntityService[action] instanceof Function) {
                result = _appEntityService[action](Data,Context,isloading);
            }else{
                result =_appEntityService.Update(Data,Context,isloading);
            }
            result.then((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 ${srfclassname('${ctrl.codeName}')}Service
     */
    @Errorlog
    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>;
            const _appEntityService: any = this.appEntityService;
            if (_appEntityService[action] && _appEntityService[action] instanceof Function) {
                result = _appEntityService[action](Context,Data, isloading);
            } else {
                result = this.appEntityService.Get(Context,Data, isloading);
            }
            result.then((response) => {
                //处理返回数据,补充判断标识
                if(response.data){
                    Object.assign(response.data,{srfuf:0});
                }
                resolve(response);
            }).catch(response => {
                reject(response);
            });
        });
    }

    /**
     * 查询数据
     *
     * @param {string} action
     * @param {*} [context={}]
     * @param {*} [data={}]
     * @param {boolean} [isloading]
     * @returns {Promise<any>}
     * @memberof ${srfclassname('${ctrl.codeName}')}Service
     */
    @Errorlog
    public search(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) => {
            const _appEntityService: any = this.appEntityService;
            let result: Promise<any>;
            if (_appEntityService[action] && _appEntityService[action] instanceof Function) {
                result = _appEntityService[action](Context,Data, isloading);
            }else{
                result =_appEntityService.FetchDefault(Context,Data, isloading);
            }
            result.then((response) => {
                this.handleResponse(action, response);
                resolve(response);
            }).catch(response => {
                reject(response);
            });      
        });
    }

<#if ctrl.getPSDEDataExport?? && ctrl.getPSDEDataExport()??>
    /**
     * 查询实体导出数据
     *
     * @param {string} action
     * @param {*} [context={}]
     * @param {*} [data={}]
     * @param {boolean} [isloading]
     * @returns {Promise<any>}
     * @memberof ${srfclassname('${ctrl.codeName}')}Service
     */
    @Errorlog
    public searchDEExportData(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) => {
            const _appEntityService: any = this.appEntityService;
            let result: Promise<any>;
            if (_appEntityService[action] && _appEntityService[action] instanceof Function) {
                result = _appEntityService[action](Context,Data, isloading);
            }else{
                result =_appEntityService.FetchDefault(Context,Data, isloading);
            }
            result.then((response) => {
                let model: any = this.getMode();
                model.isDEExport = true;
                this.handleResponse(action, response);
                model.isDEExport = false;
                resolve(response);
            }).catch(response => {
                reject(response);
            });      
        });
    }
</#if>

    /**
     * 加载草稿
     *
     * @param {string} action
     * @param {*} [context={}]
     * @param {*} [data={}]
     * @param {boolean} [isloading]
     * @returns {Promise<any>}
     * @memberof ${srfclassname('${ctrl.codeName}')}Service
     */
    @Errorlog
    public loadDraft(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>;
            const _appEntityService: any = this.appEntityService;
            if (_appEntityService[action] && _appEntityService[action] instanceof Function) {
                result = _appEntityService[action](Context,Data, isloading);
            } else {
                result = this.appEntityService.GetDraft(Context,Data, isloading);
            }
            result.then((response) => {
                //处理返回数据,补充判断标识
                if(response.data){
                    Object.assign(response.data,{srfuf:'0'});
                    //仿真主键数据
                    response.data.${appde.getKeyPSAppDEField().getCodeName()?lower_case} = Util.createUUID();
                }
                this.handleResponse(action, response, true);
                this.mergeDefaults(response);
                resolve(response);
            }).catch(response => {
                reject(response);
            });
        });
    }

    /**
     * 合并配置的默认值
     * @param {*} 
     * @memberof ${srfclassname('${ctrl.codeName}')}Service
     */
    public mergeDefaults(response:any = {}){ 
        if(response.data){                    
    <#list ctrl.getPSDEGridEditItems() as edititem><#t>
      <#if edititem.getCreateDV?? && edititem.getCreateDV()??><#t>
        <#if !(edititem.getCreateDV() == '')><#t>
            Object.assign(response.data,{'${edititem.getCodeName()?lower_case}':'${edititem.getCreateDV()}'});
        </#if>
      </#if>
    </#list> 
        }
    }

    <#if ctrl.getAggMode() == "ALL" && ctrl.getAggPSAppDataEntity()??>
    <#assign aggAppDataEntity = ctrl.getAggPSAppDataEntity() >
    /**
     * 表格聚合加载数据
     *
     * @param {string} action
     * @param {*} [context={}]
     * @param {*} [data={}]
     * @param {boolean} [isloading]
     * @returns {Promise<any>}
     * @memberof ${srfclassname('${ctrl.codeName}')}Service
     */
    @Errorlog
    public getAggData(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) => {
            const _appEntityService: any = new ${srfclassname('${aggAppDataEntity.getCodeName()}')}Service({ $store: this.getStore() });
            let result: Promise<any>;
            if (_appEntityService[action] && _appEntityService[action] instanceof Function) {
                result = _appEntityService[action](Context,Data, isloading);
            }else{
                result = Promise.reject({});
            }
            result.then((response) => {
                this.handleResponse(action, response);
                resolve(response);
            }).catch(response => {
                reject(response);
            });      
        });
    }
    </#if>

    /**
     * 前台逻辑
     * @param {string} action
     * @param {*} [context={}]
     * @param {*} [data={}]
     * @param {boolean} [isloading]
     * @returns {Promise<any>}
     * @memberof MainService
     */
    @Errorlog
    public frontLogic(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>;
            const _appEntityService: any = this.appEntityService;
            if (_appEntityService[action] && _appEntityService[action] instanceof Function) {
                result = _appEntityService[action](Context,Data, isloading);
            } else {
                return Promise.reject({ status: 500, data: { title: '失败', message: '系统异常' } });
            }
            result.then((response) => {
                this.handleResponse(action, response);
                resolve(response);
            }).catch(response => {
                reject(response);
            });
        })
    }
    
<#ibizinclude>
../@MACRO/SERVICE/SERVICE_BOTTOM.ts.ftl
</#ibizinclude>