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



/**
 * 联系人服务对象基类
 *
 * @export
 * @class ContactServiceBase
 * @extends {EntityServie}
 */
export default class ContactServiceBase extends EntityService {

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

    /**
     * 初始化基础数据
     *
     * @memberof ContactServiceBase
     */
    public initBasicData(){
        this.APPLYDEKEY ='contact';
        this.APPDEKEY = 'contactid';
        this.APPDENAME = 'contacts';
        this.APPDETEXT = 'fullname';
        this.APPNAME = 'crm';
        this.SYSTEMNAME = 'ibizbusinesscentral';
    }

// 实体接口

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

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

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

    }

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

    }

    /**
     * Save接口方法
     *
     * @param {*} [context={}]
     * @param {*} [data={}]
     * @param {boolean} [isloading]
     * @returns {Promise<any>}
     * @memberof ContactServiceBase
     */
    public async Save(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
        if(context.account && context.contact){
            return Http.getInstance().post(`/accounts/${context.account}/contacts/${context.contact}/save`,data,isloading);
        }
        let masterData:any = {};
        let listcontactsData:any = [];
        if(!Object.is(this.tempStorage.getItem(context.srfsessionkey+'_listcontacts'),'undefined')){
            listcontactsData = JSON.parse(this.tempStorage.getItem(context.srfsessionkey+'_listcontacts') as any);
            if(listcontactsData && listcontactsData.length && listcontactsData.length > 0){
                listcontactsData.forEach((item:any) => {
                    if(item.srffrontuf){
                        if(Object.is(item.srffrontuf,"0")){
                            item.relationshipsid = null;
                        }
                        delete item.srffrontuf;
                    }
                });
            }
        }
        masterData.listcontacts = listcontactsData;
        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 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(`/contacts/${context.contact}/save`,data,isloading);
            this.tempStorage.setItem(context.srfsessionkey+'_listcontacts',JSON.stringify(res.data.listcontacts));
            this.tempStorage.setItem(context.srfsessionkey+'_leads',JSON.stringify(res.data.leads));
            this.tempStorage.setItem(context.srfsessionkey+'_opportunities',JSON.stringify(res.data.opportunities));
            return res;
    }

    /**
     * Update接口方法
     *
     * @param {*} [context={}]
     * @param {*} [data={}]
     * @param {boolean} [isloading]
     * @returns {Promise<any>}
     * @memberof ContactServiceBase
     */
    public async Update(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
        if(context.account && context.contact){
            return Http.getInstance().put(`/accounts/${context.account}/contacts/${context.contact}`,data,isloading);
        }
        let masterData:any = {};
        let listcontactsData:any = [];
        if(!Object.is(this.tempStorage.getItem(context.srfsessionkey+'_listcontacts'),'undefined')){
            listcontactsData = JSON.parse(this.tempStorage.getItem(context.srfsessionkey+'_listcontacts') as any);
            if(listcontactsData && listcontactsData.length && listcontactsData.length > 0){
                listcontactsData.forEach((item:any) => {
                    if(item.srffrontuf){
                        if(Object.is(item.srffrontuf,"0")){
                            item.relationshipsid = null;
                        }
                        delete item.srffrontuf;
                    }
                });
            }
        }
        masterData.listcontacts = listcontactsData;
        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 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(`/contacts/${context.contact}`,data,isloading);
            this.tempStorage.setItem(context.srfsessionkey+'_listcontacts',JSON.stringify(res.data.listcontacts));
            this.tempStorage.setItem(context.srfsessionkey+'_leads',JSON.stringify(res.data.leads));
            this.tempStorage.setItem(context.srfsessionkey+'_opportunities',JSON.stringify(res.data.opportunities));
            return res;
    }

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

    /**
     * Create接口方法
     *
     * @param {*} [context={}]
     * @param {*} [data={}]
     * @param {boolean} [isloading]
     * @returns {Promise<any>}
     * @memberof ContactServiceBase
     */
    public async Create(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
        if(context.account && true){
            if(!data.srffrontuf || data.srffrontuf !== "1"){
                data[this.APPDEKEY] = null;
            }
            if(data.srffrontuf){
                delete data.srffrontuf;
            }
            return Http.getInstance().post(`/accounts/${context.account}/contacts`,data,isloading);
        }
        let masterData:any = {};
        let listcontactsData:any = [];
        if(!Object.is(this.tempStorage.getItem(context.srfsessionkey+'_listcontacts'),'undefined')){
            listcontactsData = JSON.parse(this.tempStorage.getItem(context.srfsessionkey+'_listcontacts') as any);
            if(listcontactsData && listcontactsData.length && listcontactsData.length > 0){
                listcontactsData.forEach((item:any) => {
                    if(item.srffrontuf){
                        if(Object.is(item.srffrontuf,"0")){
                            item.relationshipsid = null;
                        }
                        delete item.srffrontuf;
                    }
                });
            }
        }
        masterData.listcontacts = listcontactsData;
        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 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(`/contacts`,data,isloading);
        this.tempStorage.setItem(tempContext.srfsessionkey+'_listcontacts',JSON.stringify(res.data.listcontacts));
        this.tempStorage.setItem(tempContext.srfsessionkey+'_leads',JSON.stringify(res.data.leads));
        this.tempStorage.setItem(tempContext.srfsessionkey+'_opportunities',JSON.stringify(res.data.opportunities));
        return res;
    }

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