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



/**
 * 商机服务对象基类
 *
 * @export
 * @class OpportunityServiceBase
 * @extends {EntityServie}
 */
export default class OpportunityServiceBase extends EntityService {

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

    /**
     * 初始化基础数据
     *
     * @memberof OpportunityServiceBase
     */
    public initBasicData(){
        this.APPLYDEKEY ='opportunity';
        this.APPDEKEY = 'opportunityid';
        this.APPDENAME = 'opportunities';
        this.APPDETEXT = 'opportunityname';
        this.APPNAME = 'businesscentral';
        this.SYSTEMNAME = 'ibizbusinesscentral';
    }

// 实体接口

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

    /**
     * Get接口方法
     *
     * @param {*} [context={}]
     * @param {*} [data={}]
     * @param {boolean} [isloading]
     * @returns {Promise<any>}
     * @memberof OpportunityServiceBase
     */
    public async Get(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
        if(context.contact && context.opportunity){
            return Http.getInstance().get(`/contacts/${context.contact}/opportunities/${context.opportunity}`,isloading);
        }
            let res:any = await Http.getInstance().get(`/opportunities/${context.opportunity}`,isloading);
            return res;

    }

    /**
     * GetDraft接口方法
     *
     * @param {*} [context={}]
     * @param {*} [data={}]
     * @param {boolean} [isloading]
     * @returns {Promise<any>}
     * @memberof OpportunityServiceBase
     */
    public async GetDraft(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
        if(context.contact && true){
            return Http.getInstance().get(`/contacts/${context.contact}/opportunities/getdraft`,isloading);
        }
        let res:any = await  Http.getInstance().get(`/opportunities/getdraft`,isloading);
        res.data.opportunity = data.opportunity;
        return res;
    }

    /**
     * Create接口方法
     *
     * @param {*} [context={}]
     * @param {*} [data={}]
     * @param {boolean} [isloading]
     * @returns {Promise<any>}
     * @memberof OpportunityServiceBase
     */
    public async Create(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
        if(context.contact && true){
            if(!data.srffrontuf || data.srffrontuf !== "1"){
                data[this.APPDEKEY] = null;
            }
            if(data.srffrontuf){
                delete data.srffrontuf;
            }
            return Http.getInstance().post(`/contacts/${context.contact}/opportunities`,data,isloading);
        }
        let masterData:any = {};
        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(`/opportunities`,data,isloading);
        return res;
    }

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

    }

    /**
     * Update接口方法
     *
     * @param {*} [context={}]
     * @param {*} [data={}]
     * @param {boolean} [isloading]
     * @returns {Promise<any>}
     * @memberof OpportunityServiceBase
     */
    public async Update(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
        if(context.contact && context.opportunity){
            return Http.getInstance().put(`/contacts/${context.contact}/opportunities/${context.opportunity}`,data,isloading);
        }
        let masterData:any = {};
        Object.assign(data,masterData);
            let res:any = await  Http.getInstance().put(`/opportunities/${context.opportunity}`,data,isloading);
            return res;
    }

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

    /**
     * Save接口方法
     *
     * @param {*} [context={}]
     * @param {*} [data={}]
     * @param {boolean} [isloading]
     * @returns {Promise<any>}
     * @memberof OpportunityServiceBase
     */
    public async Save(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
        if(context.contact && context.opportunity){
            return Http.getInstance().post(`/contacts/${context.contact}/opportunities/${context.opportunity}/save`,data,isloading);
        }
        let masterData:any = {};
        Object.assign(data,masterData);
            let res:any = await  Http.getInstance().post(`/opportunities/${context.opportunity}/save`,data,isloading);
            return res;
    }

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