import { Http,Util } from '@/utils';
import EntityService from '../entity-service';
import GetOrgNameByUser from '@/service/cid/get-org-name-by-user-logic';
import GetCPXHXX from '@/service/cid/get-cpxhxx-logic';



/**
 * 车险单服务对象基类
 *
 * @export
 * @class CIDServiceBase
 * @extends {EntityServie}
 */
export default class CIDServiceBase extends EntityService {

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

    /**
     * 初始化基础数据
     *
     * @memberof CIDServiceBase
     */
    public initBasicData(){
        this.APPDEKEY = 'cidid';
        this.APPDENAME = 'cids';
        this.APPDETEXT = 'cidname';
    }

// 实体接口

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

    /**
     * Create接口方法
     *
     * @param {*} [context={}]
     * @param {*} [data={}]
     * @param {boolean} [isloading]
     * @returns {Promise<any>}
     * @memberof CIDServiceBase
     */
    public async Create(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
        let masterData:any = {};
        let jqxtbydsData:any = [];
        if(!Object.is(this.tempStorage.getItem(context.srfsessionkey+'_jqxtbyds'),'undefined')){
            jqxtbydsData = JSON.parse(this.tempStorage.getItem(context.srfsessionkey+'_jqxtbyds') as any);
        }
        masterData.jqxtbyds = jqxtbydsData;
        let syxtbydsData:any = [];
        if(!Object.is(this.tempStorage.getItem(context.srfsessionkey+'_syxtbyds'),'undefined')){
            syxtbydsData = JSON.parse(this.tempStorage.getItem(context.srfsessionkey+'_syxtbyds') as any);
        }
        masterData.syxtbyds = syxtbydsData;
        let customersData:any = [];
        if(!Object.is(this.tempStorage.getItem(context.srfsessionkey+'_customers'),'undefined')){
            customersData = JSON.parse(this.tempStorage.getItem(context.srfsessionkey+'_customers') as any);
        }
        masterData.customers = customersData;
        let jqxxxesData:any = [];
        if(!Object.is(this.tempStorage.getItem(context.srfsessionkey+'_jqxxxes'),'undefined')){
            jqxxxesData = JSON.parse(this.tempStorage.getItem(context.srfsessionkey+'_jqxxxes') as any);
        }
        masterData.jqxxxes = jqxxxesData;
        let syxxxesData:any = [];
        if(!Object.is(this.tempStorage.getItem(context.srfsessionkey+'_syxxxes'),'undefined')){
            syxxxesData = JSON.parse(this.tempStorage.getItem(context.srfsessionkey+'_syxxxes') as any);
        }
        masterData.syxxxes = syxxxesData;
        Object.assign(data,masterData);
        data.cid = null;
        let tempContext:any = JSON.parse(JSON.stringify(context));
        let res:any = await Http.getInstance().post(`/cids`,data,isloading);
        this.tempStorage.setItem(tempContext.srfsessionkey+'_jqxtbyds',JSON.stringify(res.data.jqxtbyds));
        this.tempStorage.setItem(tempContext.srfsessionkey+'_syxtbyds',JSON.stringify(res.data.syxtbyds));
        this.tempStorage.setItem(tempContext.srfsessionkey+'_customers',JSON.stringify(res.data.customers));
        this.tempStorage.setItem(tempContext.srfsessionkey+'_jqxxxes',JSON.stringify(res.data.jqxxxes));
        this.tempStorage.setItem(tempContext.srfsessionkey+'_syxxxes',JSON.stringify(res.data.syxxxes));
        return res;
    }

    /**
     * Get接口方法
     *
     * @param {*} [context={}]
     * @param {*} [data={}]
     * @param {boolean} [isloading]
     * @returns {Promise<any>}
     * @memberof CIDServiceBase
     */
    public async Get(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
        if(context.cid){
            let res:any = await Http.getInstance().get(`/cids/${context.cid}`,isloading);
            this.tempStorage.setItem(context.srfsessionkey+'_jqxtbyds',JSON.stringify(res.data.jqxtbyds));
            this.tempStorage.setItem(context.srfsessionkey+'_syxtbyds',JSON.stringify(res.data.syxtbyds));
            this.tempStorage.setItem(context.srfsessionkey+'_customers',JSON.stringify(res.data.customers));
            this.tempStorage.setItem(context.srfsessionkey+'_jqxxxes',JSON.stringify(res.data.jqxxxes));
            this.tempStorage.setItem(context.srfsessionkey+'_syxxxes',JSON.stringify(res.data.syxxxes));
            return res;
        }

    }

    /**
     * GetDraft接口方法
     *
     * @param {*} [context={}]
     * @param {*} [data={}]
     * @param {boolean} [isloading]
     * @returns {Promise<any>}
     * @memberof CIDServiceBase
     */
    public async GetDraft(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
        let res:any = await  Http.getInstance().get(`/cids/getdraft`,isloading);
        res.data.cid = data.cid;
            this.tempStorage.setItem(context.srfsessionkey+'_jqxtbyds',JSON.stringify(res.data.jqxtbyds));
            this.tempStorage.setItem(context.srfsessionkey+'_syxtbyds',JSON.stringify(res.data.syxtbyds));
            this.tempStorage.setItem(context.srfsessionkey+'_customers',JSON.stringify(res.data.customers));
            this.tempStorage.setItem(context.srfsessionkey+'_jqxxxes',JSON.stringify(res.data.jqxxxes));
            this.tempStorage.setItem(context.srfsessionkey+'_syxxxes',JSON.stringify(res.data.syxxxes));
        return res;
    }

    /**
     * Update接口方法
     *
     * @param {*} [context={}]
     * @param {*} [data={}]
     * @param {boolean} [isloading]
     * @returns {Promise<any>}
     * @memberof CIDServiceBase
     */
    public async Update(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
        let masterData:any = {};
        let jqxtbydsData:any = [];
        if(!Object.is(this.tempStorage.getItem(context.srfsessionkey+'_jqxtbyds'),'undefined')){
            jqxtbydsData = JSON.parse(this.tempStorage.getItem(context.srfsessionkey+'_jqxtbyds') as any);
        }
        masterData.jqxtbyds = jqxtbydsData;
        let syxtbydsData:any = [];
        if(!Object.is(this.tempStorage.getItem(context.srfsessionkey+'_syxtbyds'),'undefined')){
            syxtbydsData = JSON.parse(this.tempStorage.getItem(context.srfsessionkey+'_syxtbyds') as any);
        }
        masterData.syxtbyds = syxtbydsData;
        let customersData:any = [];
        if(!Object.is(this.tempStorage.getItem(context.srfsessionkey+'_customers'),'undefined')){
            customersData = JSON.parse(this.tempStorage.getItem(context.srfsessionkey+'_customers') as any);
        }
        masterData.customers = customersData;
        let jqxxxesData:any = [];
        if(!Object.is(this.tempStorage.getItem(context.srfsessionkey+'_jqxxxes'),'undefined')){
            jqxxxesData = JSON.parse(this.tempStorage.getItem(context.srfsessionkey+'_jqxxxes') as any);
        }
        masterData.jqxxxes = jqxxxesData;
        let syxxxesData:any = [];
        if(!Object.is(this.tempStorage.getItem(context.srfsessionkey+'_syxxxes'),'undefined')){
            syxxxesData = JSON.parse(this.tempStorage.getItem(context.srfsessionkey+'_syxxxes') as any);
        }
        masterData.syxxxes = syxxxesData;
        Object.assign(data,masterData);
        if(context.cid){
            let res:any = await  Http.getInstance().put(`/cids/${context.cid}`,data,isloading);
            this.tempStorage.setItem(context.srfsessionkey+'_jqxtbyds',JSON.stringify(res.data.jqxtbyds));
            this.tempStorage.setItem(context.srfsessionkey+'_syxtbyds',JSON.stringify(res.data.syxtbyds));
            this.tempStorage.setItem(context.srfsessionkey+'_customers',JSON.stringify(res.data.customers));
            this.tempStorage.setItem(context.srfsessionkey+'_jqxxxes',JSON.stringify(res.data.jqxxxes));
            this.tempStorage.setItem(context.srfsessionkey+'_syxxxes',JSON.stringify(res.data.syxxxes));
            return res;
        }
    }

    /**
     * Save接口方法
     *
     * @param {*} [context={}]
     * @param {*} [data={}]
     * @param {boolean} [isloading]
     * @returns {Promise<any>}
     * @memberof CIDServiceBase
     */
    public async Save(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
        let masterData:any = {};
        let jqxtbydsData:any = [];
        if(!Object.is(this.tempStorage.getItem(context.srfsessionkey+'_jqxtbyds'),'undefined')){
            jqxtbydsData = JSON.parse(this.tempStorage.getItem(context.srfsessionkey+'_jqxtbyds') as any);
        }
        masterData.jqxtbyds = jqxtbydsData;
        let syxtbydsData:any = [];
        if(!Object.is(this.tempStorage.getItem(context.srfsessionkey+'_syxtbyds'),'undefined')){
            syxtbydsData = JSON.parse(this.tempStorage.getItem(context.srfsessionkey+'_syxtbyds') as any);
        }
        masterData.syxtbyds = syxtbydsData;
        let customersData:any = [];
        if(!Object.is(this.tempStorage.getItem(context.srfsessionkey+'_customers'),'undefined')){
            customersData = JSON.parse(this.tempStorage.getItem(context.srfsessionkey+'_customers') as any);
        }
        masterData.customers = customersData;
        let jqxxxesData:any = [];
        if(!Object.is(this.tempStorage.getItem(context.srfsessionkey+'_jqxxxes'),'undefined')){
            jqxxxesData = JSON.parse(this.tempStorage.getItem(context.srfsessionkey+'_jqxxxes') as any);
        }
        masterData.jqxxxes = jqxxxesData;
        let syxxxesData:any = [];
        if(!Object.is(this.tempStorage.getItem(context.srfsessionkey+'_syxxxes'),'undefined')){
            syxxxesData = JSON.parse(this.tempStorage.getItem(context.srfsessionkey+'_syxxxes') as any);
        }
        masterData.syxxxes = syxxxesData;
        Object.assign(data,masterData);
        if(context.cid){
            let res:any = await  Http.getInstance().post(`/cids/${context.cid}/save`,data,isloading);
            this.tempStorage.setItem(context.srfsessionkey+'_jqxtbyds',JSON.stringify(res.data.jqxtbyds));
            this.tempStorage.setItem(context.srfsessionkey+'_syxtbyds',JSON.stringify(res.data.syxtbyds));
            this.tempStorage.setItem(context.srfsessionkey+'_customers',JSON.stringify(res.data.customers));
            this.tempStorage.setItem(context.srfsessionkey+'_jqxxxes',JSON.stringify(res.data.jqxxxes));
            this.tempStorage.setItem(context.srfsessionkey+'_syxxxes',JSON.stringify(res.data.syxxxes));
            return res;
        }
    }

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

    }

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

    /**
     * GetMRSYXXX接口方法
     *
     * @param {*} [context={}]
     * @param {*} [data={}]
     * @param {boolean} [isloading]
     * @returns {Promise<any>}
     * @memberof CIDServiceBase
     */
    public async GetMRSYXXX(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
        // URI参数传递情况未实现
    }

    /**
     * GetOrgNameByUser接口方法
     *
     * @param {*} [context={}]
     * @param {*} [data={}]
     * @param {boolean} [isloading]
     * @returns {Promise<any>}
     * @memberof CIDServiceBase
     */
    public async GetOrgNameByUser(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
        let appLogic:GetOrgNameByUser = new GetOrgNameByUser();
        const result = await appLogic.onExecute(context,data,isloading?true:false);
        return {status:200,data:result};
    }
            
     /**
     * JSBF接口方法
     *
     * @param {*} [context={}]
     * @param {*} [data={}]
     * @param {boolean} [isloading]
     * @returns {Promise<any>}
     * @memberof CIDServiceBase
     */
    public async JSBF(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
		return new Promise((resolve:any,reject:any) =>{
			Http.getInstance().get(`/jqxbjs/fetchdefault`,JSON.parse(JSON.stringify(context)),isloading).then((response:any) =>{
				if(context.srfsessionkey && !Object.is(this.tempStorage.getItem(context.srfsessionkey+'_jqxxxes'),'undefined')){
					let result1:any = response.data;
					this.tempStorage.setItem(context.srfsessionkey+'_jqxxxes',JSON.stringify(result1));
				}
				Http.getInstance().get(`/syxbjs/fetchdefault`,JSON.parse(JSON.stringify(context)),isloading).then((response1:any) =>{
					if(context.srfsessionkey && !Object.is(this.tempStorage.getItem(context.srfsessionkey+'_syxxxes'),'undefined')){
						let result2:any = response.data;
						this.tempStorage.setItem(context.srfsessionkey+'_syxxxes',JSON.stringify(result2));
					}
					return resolve({status:200,data: Object.assign(data,{jsbfbj:1111}) });
				})
			})
		})
    }

    /**
     * GetMRJQXXX接口方法
     *
     * @param {*} [context={}]
     * @param {*} [data={}]
     * @param {boolean} [isloading]
     * @returns {Promise<any>}
     * @memberof CIDServiceBase
     */
    public async GetMRJQXXX(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
        // URI参数传递情况未实现
    }

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

    /**
     * FetchTempDefault接口方法
     *
     * @param {*} [context={}]
     * @param {*} [data={}]
     * @param {boolean} [isloading]
     * @returns {Promise<any>}
     * @memberof CIDServiceBase
     */
    public async FetchTempDefault(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
        return Http.getInstance().get(`/cids/fetchdefault`,data,isloading);
    }
}