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



/**
 * 设备类型服务对象基类
 *
 * @export
 * @class EMEQTypeServiceBase
 * @extends {EntityServie}
 */
export default class EMEQTypeServiceBase extends EntityService {

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

    /**
     * 初始化基础数据
     *
     * @memberof EMEQTypeServiceBase
     */
    public initBasicData(){
        this.APPLYDEKEY ='emeqtype';
        this.APPDEKEY = 'emeqtypeid';
        this.APPDENAME = 'emeqtypes';
        this.APPDETEXT = 'emeqtypename';
        this.APPNAME = 'eamweb';
        this.SYSTEMNAME = 'eam_practice03';
    }

// 实体接口

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

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

    }

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

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

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

    }

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

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

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

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

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