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



/**
 * 部门服务对象基类
 *
 * @export
 * @class PFDeptServiceBase
 * @extends {EntityServie}
 */
export default class PFDeptServiceBase extends EntityService {

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

    /**
     * 初始化基础数据
     *
     * @memberof PFDeptServiceBase
     */
    public initBasicData(){
        this.APPLYDEKEY ='pfdept';
        this.APPDEKEY = 'pfdeptid';
        this.APPDENAME = 'pfdepts';
        this.APPDETEXT = 'pfdeptname';
        this.APPNAME = 'eamweb';
        this.SYSTEMNAME = 'eam_practice03';
    }

// 实体接口

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

    /**
     * Save接口方法
     *
     * @param {*} [context={}]
     * @param {*} [data={}]
     * @param {boolean} [isloading]
     * @returns {Promise<any>}
     * @memberof PFDeptServiceBase
     */
    public async Save(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
        let masterData:any = {};
        let emplansData:any = [];
        if(!Object.is(this.tempStorage.getItem(context.srfsessionkey+'_emplans'),'undefined')){
            emplansData = JSON.parse(this.tempStorage.getItem(context.srfsessionkey+'_emplans') as any);
            if(emplansData && emplansData.length && emplansData.length > 0){
                emplansData.forEach((item:any) => {
                    if(item.srffrontuf){
                        if(Object.is(item.srffrontuf,"0")){
                            item.emplanid = null;
                        }
                        delete item.srffrontuf;
                    }
                });
            }
        }
        masterData.emplans = emplansData;
        let emwo_dpsData:any = [];
        if(!Object.is(this.tempStorage.getItem(context.srfsessionkey+'_emwo_dps'),'undefined')){
            emwo_dpsData = JSON.parse(this.tempStorage.getItem(context.srfsessionkey+'_emwo_dps') as any);
            if(emwo_dpsData && emwo_dpsData.length && emwo_dpsData.length > 0){
                emwo_dpsData.forEach((item:any) => {
                    if(item.srffrontuf){
                        if(Object.is(item.srffrontuf,"0")){
                            item.emwo_dpid = null;
                        }
                        delete item.srffrontuf;
                    }
                });
            }
        }
        masterData.emwo_dps = emwo_dpsData;
        let emitempusesData:any = [];
        if(!Object.is(this.tempStorage.getItem(context.srfsessionkey+'_emitempuses'),'undefined')){
            emitempusesData = JSON.parse(this.tempStorage.getItem(context.srfsessionkey+'_emitempuses') as any);
            if(emitempusesData && emitempusesData.length && emitempusesData.length > 0){
                emitempusesData.forEach((item:any) => {
                    if(item.srffrontuf){
                        if(Object.is(item.srffrontuf,"0")){
                            item.emitempuseid = null;
                        }
                        delete item.srffrontuf;
                    }
                });
            }
        }
        masterData.emitempuses = emitempusesData;
        let emenconsumsData:any = [];
        if(!Object.is(this.tempStorage.getItem(context.srfsessionkey+'_emenconsums'),'undefined')){
            emenconsumsData = JSON.parse(this.tempStorage.getItem(context.srfsessionkey+'_emenconsums') as any);
            if(emenconsumsData && emenconsumsData.length && emenconsumsData.length > 0){
                emenconsumsData.forEach((item:any) => {
                    if(item.srffrontuf){
                        if(Object.is(item.srffrontuf,"0")){
                            item.emenconsumid = null;
                        }
                        delete item.srffrontuf;
                    }
                });
            }
        }
        masterData.emenconsums = emenconsumsData;
        let emplantemplsData:any = [];
        if(!Object.is(this.tempStorage.getItem(context.srfsessionkey+'_emplantempls'),'undefined')){
            emplantemplsData = JSON.parse(this.tempStorage.getItem(context.srfsessionkey+'_emplantempls') as any);
            if(emplantemplsData && emplantemplsData.length && emplantemplsData.length > 0){
                emplantemplsData.forEach((item:any) => {
                    if(item.srffrontuf){
                        if(Object.is(item.srffrontuf,"0")){
                            item.emplantemplid = null;
                        }
                        delete item.srffrontuf;
                    }
                });
            }
        }
        masterData.emplantempls = emplantemplsData;
        Object.assign(data,masterData);
            let res:any = await  Http.getInstance().post(`/pfdepts/${context.pfdept}/save`,data,isloading);
            this.tempStorage.setItem(context.srfsessionkey+'_emplans',JSON.stringify(res.data.emplans));
            this.tempStorage.setItem(context.srfsessionkey+'_emwo_dps',JSON.stringify(res.data.emwo_dps));
            this.tempStorage.setItem(context.srfsessionkey+'_emitempuses',JSON.stringify(res.data.emitempuses));
            this.tempStorage.setItem(context.srfsessionkey+'_emenconsums',JSON.stringify(res.data.emenconsums));
            this.tempStorage.setItem(context.srfsessionkey+'_emplantempls',JSON.stringify(res.data.emplantempls));
            return res;
    }

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

    }

    /**
     * Create接口方法
     *
     * @param {*} [context={}]
     * @param {*} [data={}]
     * @param {boolean} [isloading]
     * @returns {Promise<any>}
     * @memberof PFDeptServiceBase
     */
    public async Create(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
        let masterData:any = {};
        let emplansData:any = [];
        if(!Object.is(this.tempStorage.getItem(context.srfsessionkey+'_emplans'),'undefined')){
            emplansData = JSON.parse(this.tempStorage.getItem(context.srfsessionkey+'_emplans') as any);
            if(emplansData && emplansData.length && emplansData.length > 0){
                emplansData.forEach((item:any) => {
                    if(item.srffrontuf){
                        if(Object.is(item.srffrontuf,"0")){
                            item.emplanid = null;
                        }
                        delete item.srffrontuf;
                    }
                });
            }
        }
        masterData.emplans = emplansData;
        let emwo_dpsData:any = [];
        if(!Object.is(this.tempStorage.getItem(context.srfsessionkey+'_emwo_dps'),'undefined')){
            emwo_dpsData = JSON.parse(this.tempStorage.getItem(context.srfsessionkey+'_emwo_dps') as any);
            if(emwo_dpsData && emwo_dpsData.length && emwo_dpsData.length > 0){
                emwo_dpsData.forEach((item:any) => {
                    if(item.srffrontuf){
                        if(Object.is(item.srffrontuf,"0")){
                            item.emwo_dpid = null;
                        }
                        delete item.srffrontuf;
                    }
                });
            }
        }
        masterData.emwo_dps = emwo_dpsData;
        let emitempusesData:any = [];
        if(!Object.is(this.tempStorage.getItem(context.srfsessionkey+'_emitempuses'),'undefined')){
            emitempusesData = JSON.parse(this.tempStorage.getItem(context.srfsessionkey+'_emitempuses') as any);
            if(emitempusesData && emitempusesData.length && emitempusesData.length > 0){
                emitempusesData.forEach((item:any) => {
                    if(item.srffrontuf){
                        if(Object.is(item.srffrontuf,"0")){
                            item.emitempuseid = null;
                        }
                        delete item.srffrontuf;
                    }
                });
            }
        }
        masterData.emitempuses = emitempusesData;
        let emenconsumsData:any = [];
        if(!Object.is(this.tempStorage.getItem(context.srfsessionkey+'_emenconsums'),'undefined')){
            emenconsumsData = JSON.parse(this.tempStorage.getItem(context.srfsessionkey+'_emenconsums') as any);
            if(emenconsumsData && emenconsumsData.length && emenconsumsData.length > 0){
                emenconsumsData.forEach((item:any) => {
                    if(item.srffrontuf){
                        if(Object.is(item.srffrontuf,"0")){
                            item.emenconsumid = null;
                        }
                        delete item.srffrontuf;
                    }
                });
            }
        }
        masterData.emenconsums = emenconsumsData;
        let emplantemplsData:any = [];
        if(!Object.is(this.tempStorage.getItem(context.srfsessionkey+'_emplantempls'),'undefined')){
            emplantemplsData = JSON.parse(this.tempStorage.getItem(context.srfsessionkey+'_emplantempls') as any);
            if(emplantemplsData && emplantemplsData.length && emplantemplsData.length > 0){
                emplantemplsData.forEach((item:any) => {
                    if(item.srffrontuf){
                        if(Object.is(item.srffrontuf,"0")){
                            item.emplantemplid = null;
                        }
                        delete item.srffrontuf;
                    }
                });
            }
        }
        masterData.emplantempls = emplantemplsData;
        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(`/pfdepts`,data,isloading);
        this.tempStorage.setItem(tempContext.srfsessionkey+'_emplans',JSON.stringify(res.data.emplans));
        this.tempStorage.setItem(tempContext.srfsessionkey+'_emwo_dps',JSON.stringify(res.data.emwo_dps));
        this.tempStorage.setItem(tempContext.srfsessionkey+'_emitempuses',JSON.stringify(res.data.emitempuses));
        this.tempStorage.setItem(tempContext.srfsessionkey+'_emenconsums',JSON.stringify(res.data.emenconsums));
        this.tempStorage.setItem(tempContext.srfsessionkey+'_emplantempls',JSON.stringify(res.data.emplantempls));
        return res;
    }

    /**
     * GetDraft接口方法
     *
     * @param {*} [context={}]
     * @param {*} [data={}]
     * @param {boolean} [isloading]
     * @returns {Promise<any>}
     * @memberof PFDeptServiceBase
     */
    public async GetDraft(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
        let res:any = await  Http.getInstance().get(`/pfdepts/getdraft`,isloading);
        res.data.pfdept = data.pfdept;
            this.tempStorage.setItem(context.srfsessionkey+'_emplans',JSON.stringify(res.data.emplans));
            this.tempStorage.setItem(context.srfsessionkey+'_emwo_dps',JSON.stringify(res.data.emwo_dps));
            this.tempStorage.setItem(context.srfsessionkey+'_emitempuses',JSON.stringify(res.data.emitempuses));
            this.tempStorage.setItem(context.srfsessionkey+'_emenconsums',JSON.stringify(res.data.emenconsums));
            this.tempStorage.setItem(context.srfsessionkey+'_emplantempls',JSON.stringify(res.data.emplantempls));
        return res;
    }

    /**
     * Get接口方法
     *
     * @param {*} [context={}]
     * @param {*} [data={}]
     * @param {boolean} [isloading]
     * @returns {Promise<any>}
     * @memberof PFDeptServiceBase
     */
    public async Get(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
            let res:any = await Http.getInstance().get(`/pfdepts/${context.pfdept}`,isloading);
            this.tempStorage.setItem(context.srfsessionkey+'_emplans',JSON.stringify(res.data.emplans));
            this.tempStorage.setItem(context.srfsessionkey+'_emwo_dps',JSON.stringify(res.data.emwo_dps));
            this.tempStorage.setItem(context.srfsessionkey+'_emitempuses',JSON.stringify(res.data.emitempuses));
            this.tempStorage.setItem(context.srfsessionkey+'_emenconsums',JSON.stringify(res.data.emenconsums));
            this.tempStorage.setItem(context.srfsessionkey+'_emplantempls',JSON.stringify(res.data.emplantempls));
            return res;

    }

    /**
     * Update接口方法
     *
     * @param {*} [context={}]
     * @param {*} [data={}]
     * @param {boolean} [isloading]
     * @returns {Promise<any>}
     * @memberof PFDeptServiceBase
     */
    public async Update(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
        let masterData:any = {};
        let emplansData:any = [];
        if(!Object.is(this.tempStorage.getItem(context.srfsessionkey+'_emplans'),'undefined')){
            emplansData = JSON.parse(this.tempStorage.getItem(context.srfsessionkey+'_emplans') as any);
            if(emplansData && emplansData.length && emplansData.length > 0){
                emplansData.forEach((item:any) => {
                    if(item.srffrontuf){
                        if(Object.is(item.srffrontuf,"0")){
                            item.emplanid = null;
                        }
                        delete item.srffrontuf;
                    }
                });
            }
        }
        masterData.emplans = emplansData;
        let emwo_dpsData:any = [];
        if(!Object.is(this.tempStorage.getItem(context.srfsessionkey+'_emwo_dps'),'undefined')){
            emwo_dpsData = JSON.parse(this.tempStorage.getItem(context.srfsessionkey+'_emwo_dps') as any);
            if(emwo_dpsData && emwo_dpsData.length && emwo_dpsData.length > 0){
                emwo_dpsData.forEach((item:any) => {
                    if(item.srffrontuf){
                        if(Object.is(item.srffrontuf,"0")){
                            item.emwo_dpid = null;
                        }
                        delete item.srffrontuf;
                    }
                });
            }
        }
        masterData.emwo_dps = emwo_dpsData;
        let emitempusesData:any = [];
        if(!Object.is(this.tempStorage.getItem(context.srfsessionkey+'_emitempuses'),'undefined')){
            emitempusesData = JSON.parse(this.tempStorage.getItem(context.srfsessionkey+'_emitempuses') as any);
            if(emitempusesData && emitempusesData.length && emitempusesData.length > 0){
                emitempusesData.forEach((item:any) => {
                    if(item.srffrontuf){
                        if(Object.is(item.srffrontuf,"0")){
                            item.emitempuseid = null;
                        }
                        delete item.srffrontuf;
                    }
                });
            }
        }
        masterData.emitempuses = emitempusesData;
        let emenconsumsData:any = [];
        if(!Object.is(this.tempStorage.getItem(context.srfsessionkey+'_emenconsums'),'undefined')){
            emenconsumsData = JSON.parse(this.tempStorage.getItem(context.srfsessionkey+'_emenconsums') as any);
            if(emenconsumsData && emenconsumsData.length && emenconsumsData.length > 0){
                emenconsumsData.forEach((item:any) => {
                    if(item.srffrontuf){
                        if(Object.is(item.srffrontuf,"0")){
                            item.emenconsumid = null;
                        }
                        delete item.srffrontuf;
                    }
                });
            }
        }
        masterData.emenconsums = emenconsumsData;
        let emplantemplsData:any = [];
        if(!Object.is(this.tempStorage.getItem(context.srfsessionkey+'_emplantempls'),'undefined')){
            emplantemplsData = JSON.parse(this.tempStorage.getItem(context.srfsessionkey+'_emplantempls') as any);
            if(emplantemplsData && emplantemplsData.length && emplantemplsData.length > 0){
                emplantemplsData.forEach((item:any) => {
                    if(item.srffrontuf){
                        if(Object.is(item.srffrontuf,"0")){
                            item.emplantemplid = null;
                        }
                        delete item.srffrontuf;
                    }
                });
            }
        }
        masterData.emplantempls = emplantemplsData;
        Object.assign(data,masterData);
            let res:any = await  Http.getInstance().put(`/pfdepts/${context.pfdept}`,data,isloading);
            this.tempStorage.setItem(context.srfsessionkey+'_emplans',JSON.stringify(res.data.emplans));
            this.tempStorage.setItem(context.srfsessionkey+'_emwo_dps',JSON.stringify(res.data.emwo_dps));
            this.tempStorage.setItem(context.srfsessionkey+'_emitempuses',JSON.stringify(res.data.emitempuses));
            this.tempStorage.setItem(context.srfsessionkey+'_emenconsums',JSON.stringify(res.data.emenconsums));
            this.tempStorage.setItem(context.srfsessionkey+'_emplantempls',JSON.stringify(res.data.emplantempls));
            return res;
    }

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

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