import { Http,Util } from '@/utils';
import EntityService from '../entity-service';



/**
 * 资产服务对象基类
 *
 * @export
 * @class EAMAssetServiceBase
 * @extends {EntityServie}
 */
export default class EAMAssetServiceBase extends EntityService {

    /**
     * Creates an instance of  EAMAssetServiceBase.
     * 
     * @param {*} [opts={}]
     * @memberof  EAMAssetServiceBase
     */
    constructor(opts: any = {}) {
        super(opts);
    }

    /**
     * 初始化基础数据
     *
     * @memberof EAMAssetServiceBase
     */
    public initBasicData(){
        this.APPLYDEKEY ='eamasset';
        this.APPDEKEY = 'assetid';
        this.APPDENAME = 'eamassets';
        this.APPDETEXT = 'assetname';
        this.APPNAME = 'assetmanagement';
        this.SYSTEMNAME = 'ibizassetmanagement';
    }

// 实体接口

    /**
     * Select接口方法
     *
     * @param {*} [context={}]
     * @param {*} [data={}]
     * @param {boolean} [isloading]
     * @returns {Promise<any>}
     * @memberof EAMAssetServiceBase
     */
    public async Select(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
        if(context.eamlocation && context.eamasset){
            return Http.getInstance().get(`/eamlocations/${context.eamlocation}/eamassets/${context.eamasset}/select`,isloading);
        }
            return Http.getInstance().get(`/eamassets/${context.eamasset}/select`,isloading);
    }

    /**
     * Create接口方法
     *
     * @param {*} [context={}]
     * @param {*} [data={}]
     * @param {boolean} [isloading]
     * @returns {Promise<any>}
     * @memberof EAMAssetServiceBase
     */
    public async Create(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
        if(context.eamlocation && true){
            let masterData:any = {};
        let eamassetsData:any = [];
        if(!Object.is(this.tempStorage.getItem(context.srfsessionkey+'_eamassets'),'undefined')){
            eamassetsData = JSON.parse(this.tempStorage.getItem(context.srfsessionkey+'_eamassets') as any);
            if(eamassetsData && eamassetsData.length && eamassetsData.length > 0){
                eamassetsData.forEach((item:any) => {
                    if(item.srffrontuf){
                        if(Object.is(item.srffrontuf,"0")){
                            item.assetid = null;
                        }
                        delete item.srffrontuf;
                    }
                });
            }
        }
        masterData.eamassets = eamassetsData;
            Object.assign(data,masterData);
            if(!data.srffrontuf || data.srffrontuf !== "1"){
                data[this.APPDEKEY] = null;
            }
            if(data.srffrontuf){
                delete data.srffrontuf;
            }
            let tempContext:any = JSON.parse(JSON.stringify(context));
            let res:any = await Http.getInstance().post(`/eamlocations/${context.eamlocation}/eamassets`,data,isloading);
            this.tempStorage.setItem(tempContext.srfsessionkey+'_eamassets',JSON.stringify(res.data.eamassets ? res.data.eamassets : []));
            return res;
        }
        let masterData:any = {};
        let eamassetsData:any = [];
        if(!Object.is(this.tempStorage.getItem(context.srfsessionkey+'_eamassets'),'undefined')){
            eamassetsData = JSON.parse(this.tempStorage.getItem(context.srfsessionkey+'_eamassets') as any);
            if(eamassetsData && eamassetsData.length && eamassetsData.length > 0){
                eamassetsData.forEach((item:any) => {
                    if(item.srffrontuf){
                        if(Object.is(item.srffrontuf,"0")){
                            item.assetid = null;
                        }
                        delete item.srffrontuf;
                    }
                });
            }
        }
        masterData.eamassets = eamassetsData;
        Object.assign(data,masterData);
        if(!data.srffrontuf || data.srffrontuf !== "1"){
            data[this.APPDEKEY] = null;
        }
        if(data.srffrontuf){
            delete data.srffrontuf;
        }
        let tempContext:any = JSON.parse(JSON.stringify(context));
        let res:any = await Http.getInstance().post(`/eamassets`,data,isloading);
        this.tempStorage.setItem(tempContext.srfsessionkey+'_eamassets',JSON.stringify(res.data.eamassets ? res.data.eamassets : []));
        return res;
    }

    /**
     * Update接口方法
     *
     * @param {*} [context={}]
     * @param {*} [data={}]
     * @param {boolean} [isloading]
     * @returns {Promise<any>}
     * @memberof EAMAssetServiceBase
     */
    public async Update(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
        if(context.eamlocation && context.eamasset){
            let masterData:any = {};
        let eamassetsData:any = [];
        if(!Object.is(this.tempStorage.getItem(context.srfsessionkey+'_eamassets'),'undefined')){
            eamassetsData = JSON.parse(this.tempStorage.getItem(context.srfsessionkey+'_eamassets') as any);
            if(eamassetsData && eamassetsData.length && eamassetsData.length > 0){
                eamassetsData.forEach((item:any) => {
                    if(item.srffrontuf){
                        if(Object.is(item.srffrontuf,"0")){
                            item.assetid = null;
                        }
                        delete item.srffrontuf;
                    }
                });
            }
        }
        masterData.eamassets = eamassetsData;
            Object.assign(data,masterData);
            let res:any = await Http.getInstance().put(`/eamlocations/${context.eamlocation}/eamassets/${context.eamasset}`,data,isloading);
            this.tempStorage.setItem(context.srfsessionkey+'_eamassets',JSON.stringify(res.data.eamassets ? res.data.eamassets : []));
            return res;
        }
        let masterData:any = {};
        let eamassetsData:any = [];
        if(!Object.is(this.tempStorage.getItem(context.srfsessionkey+'_eamassets'),'undefined')){
            eamassetsData = JSON.parse(this.tempStorage.getItem(context.srfsessionkey+'_eamassets') as any);
            if(eamassetsData && eamassetsData.length && eamassetsData.length > 0){
                eamassetsData.forEach((item:any) => {
                    if(item.srffrontuf){
                        if(Object.is(item.srffrontuf,"0")){
                            item.assetid = null;
                        }
                        delete item.srffrontuf;
                    }
                });
            }
        }
        masterData.eamassets = eamassetsData;
        Object.assign(data,masterData);
            let res:any = await  Http.getInstance().put(`/eamassets/${context.eamasset}`,data,isloading);
            this.tempStorage.setItem(context.srfsessionkey+'_eamassets',JSON.stringify(res.data.eamassets ? res.data.eamassets : []));
            return res;
    }

    /**
     * Remove接口方法
     *
     * @param {*} [context={}]
     * @param {*} [data={}]
     * @param {boolean} [isloading]
     * @returns {Promise<any>}
     * @memberof EAMAssetServiceBase
     */
    public async Remove(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
        if(context.eamlocation && context.eamasset){
            return Http.getInstance().delete(`/eamlocations/${context.eamlocation}/eamassets/${context.eamasset}`,isloading);
        }
            return Http.getInstance().delete(`/eamassets/${context.eamasset}`,isloading);
    }

    /**
     * Get接口方法
     *
     * @param {*} [context={}]
     * @param {*} [data={}]
     * @param {boolean} [isloading]
     * @returns {Promise<any>}
     * @memberof EAMAssetServiceBase
     */
    public async Get(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
        if(context.eamlocation && context.eamasset){
            let res:any = await Http.getInstance().get(`/eamlocations/${context.eamlocation}/eamassets/${context.eamasset}`,isloading);
            this.tempStorage.setItem(context.srfsessionkey+'_eamassets',JSON.stringify(res.data.eamassets ? res.data.eamassets : []));
            return res;
        }
            let res:any = await Http.getInstance().get(`/eamassets/${context.eamasset}`,isloading);
            this.tempStorage.setItem(context.srfsessionkey+'_eamassets',JSON.stringify(res.data.eamassets ? res.data.eamassets : []));
            return res;
    }

    /**
     * GetDraft接口方法
     *
     * @param {*} [context={}]
     * @param {*} [data={}]
     * @param {boolean} [isloading]
     * @returns {Promise<any>}
     * @memberof EAMAssetServiceBase
     */
    public async GetDraft(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
        if(context.eamlocation && true){
            let res:any = await Http.getInstance().get(`/eamlocations/${context.eamlocation}/eamassets/getdraft`,isloading);
            res.data.eamasset = data.eamasset;
            this.tempStorage.setItem(context.srfsessionkey+'_eamassets',JSON.stringify(res.data.eamassets ? res.data.eamassets : []));
            return res;
        }
        let res:any = await  Http.getInstance().get(`/eamassets/getdraft`,isloading);
        res.data.eamasset = data.eamasset;
            this.tempStorage.setItem(context.srfsessionkey+'_eamassets',JSON.stringify(res.data.eamassets ? res.data.eamassets : []));
        return res;
    }

    /**
     * CheckKey接口方法
     *
     * @param {*} [context={}]
     * @param {*} [data={}]
     * @param {boolean} [isloading]
     * @returns {Promise<any>}
     * @memberof EAMAssetServiceBase
     */
    public async CheckKey(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
        if(context.eamlocation && context.eamasset){
            let masterData:any = {};
        let eamassetsData:any = [];
        if(!Object.is(this.tempStorage.getItem(context.srfsessionkey+'_eamassets'),'undefined')){
            eamassetsData = JSON.parse(this.tempStorage.getItem(context.srfsessionkey+'_eamassets') as any);
            if(eamassetsData && eamassetsData.length && eamassetsData.length > 0){
                eamassetsData.forEach((item:any) => {
                    if(item.srffrontuf){
                        if(Object.is(item.srffrontuf,"0")){
                            item.assetid = null;
                        }
                        delete item.srffrontuf;
                    }
                });
            }
        }
        masterData.eamassets = eamassetsData;
            Object.assign(data,masterData);
            let res:any = await Http.getInstance().post(`/eamlocations/${context.eamlocation}/eamassets/${context.eamasset}/checkkey`,data,isloading);
            this.tempStorage.setItem(context.srfsessionkey+'_eamassets',JSON.stringify(res.data.eamassets ? res.data.eamassets : []));
            return res;
        }
            return Http.getInstance().post(`/eamassets/${context.eamasset}/checkkey`,data,isloading);
    }

    /**
     * Save接口方法
     *
     * @param {*} [context={}]
     * @param {*} [data={}]
     * @param {boolean} [isloading]
     * @returns {Promise<any>}
     * @memberof EAMAssetServiceBase
     */
    public async Save(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
        if(context.eamlocation && context.eamasset){
            let masterData:any = {};
        let eamassetsData:any = [];
        if(!Object.is(this.tempStorage.getItem(context.srfsessionkey+'_eamassets'),'undefined')){
            eamassetsData = JSON.parse(this.tempStorage.getItem(context.srfsessionkey+'_eamassets') as any);
            if(eamassetsData && eamassetsData.length && eamassetsData.length > 0){
                eamassetsData.forEach((item:any) => {
                    if(item.srffrontuf){
                        if(Object.is(item.srffrontuf,"0")){
                            item.assetid = null;
                        }
                        delete item.srffrontuf;
                    }
                });
            }
        }
        masterData.eamassets = eamassetsData;
            Object.assign(data,masterData);
            let res:any = await Http.getInstance().post(`/eamlocations/${context.eamlocation}/eamassets/${context.eamasset}/save`,data,isloading);
            this.tempStorage.setItem(context.srfsessionkey+'_eamassets',JSON.stringify(res.data.eamassets ? res.data.eamassets : []));
            return res;
        }
        let masterData:any = {};
        let eamassetsData:any = [];
        if(!Object.is(this.tempStorage.getItem(context.srfsessionkey+'_eamassets'),'undefined')){
            eamassetsData = JSON.parse(this.tempStorage.getItem(context.srfsessionkey+'_eamassets') as any);
            if(eamassetsData && eamassetsData.length && eamassetsData.length > 0){
                eamassetsData.forEach((item:any) => {
                    if(item.srffrontuf){
                        if(Object.is(item.srffrontuf,"0")){
                            item.assetid = null;
                        }
                        delete item.srffrontuf;
                    }
                });
            }
        }
        masterData.eamassets = eamassetsData;
        Object.assign(data,masterData);
            let res:any = await  Http.getInstance().post(`/eamassets/${context.eamasset}/save`,data,isloading);
            this.tempStorage.setItem(context.srfsessionkey+'_eamassets',JSON.stringify(res.data.eamassets ? res.data.eamassets : []));
            return res;
    }

    /**
     * FetchDefault接口方法
     *
     * @param {*} [context={}]
     * @param {*} [data={}]
     * @param {boolean} [isloading]
     * @returns {Promise<any>}
     * @memberof EAMAssetServiceBase
     */
    public async FetchDefault(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
        if(context.eamlocation && true){
            let tempData:any = JSON.parse(JSON.stringify(data));
            return Http.getInstance().get(`/eamlocations/${context.eamlocation}/eamassets/fetchdefault`,tempData,isloading);
        }
        let tempData:any = JSON.parse(JSON.stringify(data));
        return Http.getInstance().get(`/eamassets/fetchdefault`,tempData,isloading);
    }
}