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



/**
 * 功能位置服务对象基类
 *
 * @export
 * @class EAMLocationServiceBase
 * @extends {EntityServie}
 */
export default class EAMLocationServiceBase extends EntityService {

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

    /**
     * 初始化基础数据
     *
     * @memberof EAMLocationServiceBase
     */
    public initBasicData(){
        this.APPLYDEKEY ='eamlocation';
        this.APPDEKEY = 'eamlocationid';
        this.APPDENAME = 'eamlocations';
        this.APPDETEXT = 'eamlocationname';
        this.APPNAME = 'assetmanagement';
        this.SYSTEMNAME = 'ibizassetmanagement';
    }

// 实体接口

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

    /**
     * Create接口方法
     *
     * @param {*} [context={}]
     * @param {*} [data={}]
     * @param {boolean} [isloading]
     * @returns {Promise<any>}
     * @memberof EAMLocationServiceBase
     */
    public async Create(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
        let masterData:any = {};
        let eamlocationsData:any = [];
        if(!Object.is(this.tempStorage.getItem(context.srfsessionkey+'_eamlocations'),'undefined')){
            eamlocationsData = JSON.parse(this.tempStorage.getItem(context.srfsessionkey+'_eamlocations') as any);
            if(eamlocationsData && eamlocationsData.length && eamlocationsData.length > 0){
                eamlocationsData.forEach((item:any) => {
                    if(item.srffrontuf){
                        if(Object.is(item.srffrontuf,"0")){
                            item.eamlocationid = null;
                        }
                        delete item.srffrontuf;
                    }
                });
            }
        }
        masterData.eamlocations = eamlocationsData;
        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`,data,isloading);
        this.tempStorage.setItem(tempContext.srfsessionkey+'_eamlocations',JSON.stringify(res.data.eamlocations ? res.data.eamlocations : []));
        return res;
    }

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

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

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

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

    /**
     * CheckKey接口方法
     *
     * @param {*} [context={}]
     * @param {*} [data={}]
     * @param {boolean} [isloading]
     * @returns {Promise<any>}
     * @memberof EAMLocationServiceBase
     */
    public async CheckKey(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
            return Http.getInstance().post(`/eamlocations/${context.eamlocation}/checkkey`,data,isloading);
    }

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

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