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



/**
 * 知识文章服务对象基类
 *
 * @export
 * @class KnowledgeArticleServiceBase
 * @extends {EntityServie}
 */
export default class KnowledgeArticleServiceBase extends EntityService {

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

    /**
     * 初始化基础数据
     *
     * @memberof KnowledgeArticleServiceBase
     */
    public initBasicData(){
        this.APPLYDEKEY ='knowledgearticle';
        this.APPDEKEY = 'knowledgearticleid';
        this.APPDENAME = 'knowledgearticles';
        this.APPDETEXT = 'title';
        this.APPNAME = 'businesscentral';
        this.SYSTEMNAME = 'ibizbusinesscentral';
    }

// 实体接口

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

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

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

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

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

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

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

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

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