import { Http,Util } from '@/utils';
import EntityService from '../entity-service';
import ActiveLogic from '@/service/account/active-logic';
import FillContactLogic from '@/service/account/fill-contact-logic';
import InactiveLogic from '@/service/account/inactive-logic';



/**
 * 客户服务对象基类
 *
 * @export
 * @class AccountServiceBase
 * @extends {EntityServie}
 */
export default class AccountServiceBase extends EntityService {

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

    /**
     * 初始化基础数据
     *
     * @memberof AccountServiceBase
     */
    public initBasicData(){
        this.APPLYDEKEY ='account';
        this.APPDEKEY = 'accountid';
        this.APPDENAME = 'accounts';
        this.APPDETEXT = 'accountname';
        this.APPNAME = 'crm';
        this.SYSTEMNAME = 'ibizbusinesscentral';
    }

// 实体接口

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

    /**
     * Create接口方法
     *
     * @param {*} [context={}]
     * @param {*} [data={}]
     * @param {boolean} [isloading]
     * @returns {Promise<any>}
     * @memberof AccountServiceBase
     */
    public async Create(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
        let masterData:any = {};
        let leadsData:any = [];
        if(!Object.is(this.tempStorage.getItem(context.srfsessionkey+'_leads'),'undefined')){
            leadsData = JSON.parse(this.tempStorage.getItem(context.srfsessionkey+'_leads') as any);
            if(leadsData && leadsData.length && leadsData.length > 0){
                leadsData.forEach((item:any) => {
                    if(item.srffrontuf){
                        if(Object.is(item.srffrontuf,"0")){
                            item.leadid = null;
                        }
                        delete item.srffrontuf;
                    }
                });
            }
        }
        masterData.leads = leadsData;
        let listaccountsData:any = [];
        if(!Object.is(this.tempStorage.getItem(context.srfsessionkey+'_listaccounts'),'undefined')){
            listaccountsData = JSON.parse(this.tempStorage.getItem(context.srfsessionkey+'_listaccounts') as any);
            if(listaccountsData && listaccountsData.length && listaccountsData.length > 0){
                listaccountsData.forEach((item:any) => {
                    if(item.srffrontuf){
                        if(Object.is(item.srffrontuf,"0")){
                            item.relationshipsid = null;
                        }
                        delete item.srffrontuf;
                    }
                });
            }
        }
        masterData.listaccounts = listaccountsData;
        let contactsData:any = [];
        if(!Object.is(this.tempStorage.getItem(context.srfsessionkey+'_contacts'),'undefined')){
            contactsData = JSON.parse(this.tempStorage.getItem(context.srfsessionkey+'_contacts') as any);
            if(contactsData && contactsData.length && contactsData.length > 0){
                contactsData.forEach((item:any) => {
                    if(item.srffrontuf){
                        if(Object.is(item.srffrontuf,"0")){
                            item.contactid = null;
                        }
                        delete item.srffrontuf;
                    }
                });
            }
        }
        masterData.contacts = contactsData;
        let opportunitiesData:any = [];
        if(!Object.is(this.tempStorage.getItem(context.srfsessionkey+'_opportunities'),'undefined')){
            opportunitiesData = JSON.parse(this.tempStorage.getItem(context.srfsessionkey+'_opportunities') as any);
            if(opportunitiesData && opportunitiesData.length && opportunitiesData.length > 0){
                opportunitiesData.forEach((item:any) => {
                    if(item.srffrontuf){
                        if(Object.is(item.srffrontuf,"0")){
                            item.opportunityid = null;
                        }
                        delete item.srffrontuf;
                    }
                });
            }
        }
        masterData.opportunities = opportunitiesData;
        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(`/accounts`,data,isloading);
        this.tempStorage.setItem(tempContext.srfsessionkey+'_leads',JSON.stringify(res.data.leads ? res.data.leads : []));
        this.tempStorage.setItem(tempContext.srfsessionkey+'_listaccounts',JSON.stringify(res.data.listaccounts ? res.data.listaccounts : []));
        this.tempStorage.setItem(tempContext.srfsessionkey+'_contacts',JSON.stringify(res.data.contacts ? res.data.contacts : []));
        this.tempStorage.setItem(tempContext.srfsessionkey+'_opportunities',JSON.stringify(res.data.opportunities ? res.data.opportunities : []));
        return res;
    }

    /**
     * Update接口方法
     *
     * @param {*} [context={}]
     * @param {*} [data={}]
     * @param {boolean} [isloading]
     * @returns {Promise<any>}
     * @memberof AccountServiceBase
     */
    public async Update(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
        let masterData:any = {};
        let leadsData:any = [];
        if(!Object.is(this.tempStorage.getItem(context.srfsessionkey+'_leads'),'undefined')){
            leadsData = JSON.parse(this.tempStorage.getItem(context.srfsessionkey+'_leads') as any);
            if(leadsData && leadsData.length && leadsData.length > 0){
                leadsData.forEach((item:any) => {
                    if(item.srffrontuf){
                        if(Object.is(item.srffrontuf,"0")){
                            item.leadid = null;
                        }
                        delete item.srffrontuf;
                    }
                });
            }
        }
        masterData.leads = leadsData;
        let listaccountsData:any = [];
        if(!Object.is(this.tempStorage.getItem(context.srfsessionkey+'_listaccounts'),'undefined')){
            listaccountsData = JSON.parse(this.tempStorage.getItem(context.srfsessionkey+'_listaccounts') as any);
            if(listaccountsData && listaccountsData.length && listaccountsData.length > 0){
                listaccountsData.forEach((item:any) => {
                    if(item.srffrontuf){
                        if(Object.is(item.srffrontuf,"0")){
                            item.relationshipsid = null;
                        }
                        delete item.srffrontuf;
                    }
                });
            }
        }
        masterData.listaccounts = listaccountsData;
        let contactsData:any = [];
        if(!Object.is(this.tempStorage.getItem(context.srfsessionkey+'_contacts'),'undefined')){
            contactsData = JSON.parse(this.tempStorage.getItem(context.srfsessionkey+'_contacts') as any);
            if(contactsData && contactsData.length && contactsData.length > 0){
                contactsData.forEach((item:any) => {
                    if(item.srffrontuf){
                        if(Object.is(item.srffrontuf,"0")){
                            item.contactid = null;
                        }
                        delete item.srffrontuf;
                    }
                });
            }
        }
        masterData.contacts = contactsData;
        let opportunitiesData:any = [];
        if(!Object.is(this.tempStorage.getItem(context.srfsessionkey+'_opportunities'),'undefined')){
            opportunitiesData = JSON.parse(this.tempStorage.getItem(context.srfsessionkey+'_opportunities') as any);
            if(opportunitiesData && opportunitiesData.length && opportunitiesData.length > 0){
                opportunitiesData.forEach((item:any) => {
                    if(item.srffrontuf){
                        if(Object.is(item.srffrontuf,"0")){
                            item.opportunityid = null;
                        }
                        delete item.srffrontuf;
                    }
                });
            }
        }
        masterData.opportunities = opportunitiesData;
        Object.assign(data,masterData);
            let res:any = await  Http.getInstance().put(`/accounts/${context.account}`,data,isloading);
            this.tempStorage.setItem(context.srfsessionkey+'_leads',JSON.stringify(res.data.leads ? res.data.leads : []));
            this.tempStorage.setItem(context.srfsessionkey+'_listaccounts',JSON.stringify(res.data.listaccounts ? res.data.listaccounts : []));
            this.tempStorage.setItem(context.srfsessionkey+'_contacts',JSON.stringify(res.data.contacts ? res.data.contacts : []));
            this.tempStorage.setItem(context.srfsessionkey+'_opportunities',JSON.stringify(res.data.opportunities ? res.data.opportunities : []));
            return res;
    }

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

    /**
     * Get接口方法
     *
     * @param {*} [context={}]
     * @param {*} [data={}]
     * @param {boolean} [isloading]
     * @returns {Promise<any>}
     * @memberof AccountServiceBase
     */
    public async Get(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
            let res:any = await Http.getInstance().get(`/accounts/${context.account}`,isloading);
            this.tempStorage.setItem(context.srfsessionkey+'_leads',JSON.stringify(res.data.leads ? res.data.leads : []));
            this.tempStorage.setItem(context.srfsessionkey+'_listaccounts',JSON.stringify(res.data.listaccounts ? res.data.listaccounts : []));
            this.tempStorage.setItem(context.srfsessionkey+'_contacts',JSON.stringify(res.data.contacts ? res.data.contacts : []));
            this.tempStorage.setItem(context.srfsessionkey+'_opportunities',JSON.stringify(res.data.opportunities ? res.data.opportunities : []));
            return res;
    }

    /**
     * GetDraft接口方法
     *
     * @param {*} [context={}]
     * @param {*} [data={}]
     * @param {boolean} [isloading]
     * @returns {Promise<any>}
     * @memberof AccountServiceBase
     */
    public async GetDraft(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
        let res:any = await  Http.getInstance().get(`/accounts/getdraft`,isloading);
        res.data.account = data.account;
            this.tempStorage.setItem(context.srfsessionkey+'_leads',JSON.stringify(res.data.leads ? res.data.leads : []));
            this.tempStorage.setItem(context.srfsessionkey+'_listaccounts',JSON.stringify(res.data.listaccounts ? res.data.listaccounts : []));
            this.tempStorage.setItem(context.srfsessionkey+'_contacts',JSON.stringify(res.data.contacts ? res.data.contacts : []));
            this.tempStorage.setItem(context.srfsessionkey+'_opportunities',JSON.stringify(res.data.opportunities ? res.data.opportunities : []));
        return res;
    }

    /**
     * Active接口方法
     *
     * @param {*} [context={}]
     * @param {*} [data={}]
     * @param {boolean} [isloading]
     * @returns {Promise<any>}
     * @memberof AccountServiceBase
     */
    public async Active(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
        let appLogic:ActiveLogic = new ActiveLogic({context:JSON.parse(JSON.stringify(context)),data:JSON.parse(JSON.stringify(data))});
        const result = await appLogic.onExecute(context,data,isloading?true:false);
        return {status:200,data:result};
    }

    /**
     * AddList接口方法
     *
     * @param {*} [context={}]
     * @param {*} [data={}]
     * @param {boolean} [isloading]
     * @returns {Promise<any>}
     * @memberof AccountServiceBase
     */
    public async AddList(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
            return Http.getInstance().post(`/accounts/${context.account}/addlist`,data,isloading);
    }

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

    /**
     * Inactive接口方法
     *
     * @param {*} [context={}]
     * @param {*} [data={}]
     * @param {boolean} [isloading]
     * @returns {Promise<any>}
     * @memberof AccountServiceBase
     */
    public async Inactive(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
        let appLogic:InactiveLogic = new InactiveLogic({context:JSON.parse(JSON.stringify(context)),data:JSON.parse(JSON.stringify(data))});
        const result = await appLogic.onExecute(context,data,isloading?true:false);
        return {status:200,data:result};
    }

    /**
     * Save接口方法
     *
     * @param {*} [context={}]
     * @param {*} [data={}]
     * @param {boolean} [isloading]
     * @returns {Promise<any>}
     * @memberof AccountServiceBase
     */
    public async Save(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
        let masterData:any = {};
        let leadsData:any = [];
        if(!Object.is(this.tempStorage.getItem(context.srfsessionkey+'_leads'),'undefined')){
            leadsData = JSON.parse(this.tempStorage.getItem(context.srfsessionkey+'_leads') as any);
            if(leadsData && leadsData.length && leadsData.length > 0){
                leadsData.forEach((item:any) => {
                    if(item.srffrontuf){
                        if(Object.is(item.srffrontuf,"0")){
                            item.leadid = null;
                        }
                        delete item.srffrontuf;
                    }
                });
            }
        }
        masterData.leads = leadsData;
        let listaccountsData:any = [];
        if(!Object.is(this.tempStorage.getItem(context.srfsessionkey+'_listaccounts'),'undefined')){
            listaccountsData = JSON.parse(this.tempStorage.getItem(context.srfsessionkey+'_listaccounts') as any);
            if(listaccountsData && listaccountsData.length && listaccountsData.length > 0){
                listaccountsData.forEach((item:any) => {
                    if(item.srffrontuf){
                        if(Object.is(item.srffrontuf,"0")){
                            item.relationshipsid = null;
                        }
                        delete item.srffrontuf;
                    }
                });
            }
        }
        masterData.listaccounts = listaccountsData;
        let contactsData:any = [];
        if(!Object.is(this.tempStorage.getItem(context.srfsessionkey+'_contacts'),'undefined')){
            contactsData = JSON.parse(this.tempStorage.getItem(context.srfsessionkey+'_contacts') as any);
            if(contactsData && contactsData.length && contactsData.length > 0){
                contactsData.forEach((item:any) => {
                    if(item.srffrontuf){
                        if(Object.is(item.srffrontuf,"0")){
                            item.contactid = null;
                        }
                        delete item.srffrontuf;
                    }
                });
            }
        }
        masterData.contacts = contactsData;
        let opportunitiesData:any = [];
        if(!Object.is(this.tempStorage.getItem(context.srfsessionkey+'_opportunities'),'undefined')){
            opportunitiesData = JSON.parse(this.tempStorage.getItem(context.srfsessionkey+'_opportunities') as any);
            if(opportunitiesData && opportunitiesData.length && opportunitiesData.length > 0){
                opportunitiesData.forEach((item:any) => {
                    if(item.srffrontuf){
                        if(Object.is(item.srffrontuf,"0")){
                            item.opportunityid = null;
                        }
                        delete item.srffrontuf;
                    }
                });
            }
        }
        masterData.opportunities = opportunitiesData;
        Object.assign(data,masterData);
            let res:any = await  Http.getInstance().post(`/accounts/${context.account}/save`,data,isloading);
            this.tempStorage.setItem(context.srfsessionkey+'_leads',JSON.stringify(res.data.leads ? res.data.leads : []));
            this.tempStorage.setItem(context.srfsessionkey+'_listaccounts',JSON.stringify(res.data.listaccounts ? res.data.listaccounts : []));
            this.tempStorage.setItem(context.srfsessionkey+'_contacts',JSON.stringify(res.data.contacts ? res.data.contacts : []));
            this.tempStorage.setItem(context.srfsessionkey+'_opportunities',JSON.stringify(res.data.opportunities ? res.data.opportunities : []));
            return res;
    }

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

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

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

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

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

    /**
     * FillContact接口方法
     *
     * @param {*} [context={}]
     * @param {*} [data={}]
     * @param {boolean} [isloading]
     * @returns {Promise<any>}
     * @memberof AccountServiceBase
     */
    public async FillContact(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
        let appLogic:FillContactLogic = new FillContactLogic({context:JSON.parse(JSON.stringify(context)),data:JSON.parse(JSON.stringify(data))});
        const result = await appLogic.onExecute(context,data,isloading?true:false);
        return {status:200,data:result};
    }
}