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



/**
 * 发票服务对象基类
 *
 * @export
 * @class InvoiceServiceBase
 * @extends {EntityServie}
 */
export default class InvoiceServiceBase extends EntityService {

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

    /**
     * 初始化基础数据
     *
     * @memberof InvoiceServiceBase
     */
    public initBasicData(){
        this.APPLYDEKEY ='invoice';
        this.APPDEKEY = 'invoiceid';
        this.APPDENAME = 'invoices';
        this.APPDETEXT = 'invoicename';
        this.APPNAME = 'crm';
        this.SYSTEMNAME = 'ibizbusinesscentral';
    }

// 实体接口

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

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

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

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

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

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

    /**
     * Cancel接口方法
     *
     * @param {*} [context={}]
     * @param {*} [data={}]
     * @param {boolean} [isloading]
     * @returns {Promise<any>}
     * @memberof InvoiceServiceBase
     */
    public async Cancel(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
            return Http.getInstance().post(`/invoices/${context.invoice}/cancel`,data,isloading);
    }

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

    /**
     * Finish接口方法
     *
     * @param {*} [context={}]
     * @param {*} [data={}]
     * @param {boolean} [isloading]
     * @returns {Promise<any>}
     * @memberof InvoiceServiceBase
     */
    public async Finish(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
            return Http.getInstance().post(`/invoices/${context.invoice}/finish`,data,isloading);
    }

    /**
     * Paid接口方法
     *
     * @param {*} [context={}]
     * @param {*} [data={}]
     * @param {boolean} [isloading]
     * @returns {Promise<any>}
     * @memberof InvoiceServiceBase
     */
    public async Paid(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
            return Http.getInstance().post(`/invoices/${context.invoice}/paid`,data,isloading);
    }

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

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