<template>

</template>

<script>
    export default {
        name: "batch-remote-request-after",
        props:{
            formatObj:{type:Object,required:true},
            dataRows:{type:Array,required:true},
            showFieldObj:{type:Object,required:true},
            listOutParam:{type:Object,default:() => {return {};}},// 外部传入参数
        },
        data(){
            return {
                afterCacheObj:{},
                lastParam:null,
            }
        },
        created() {
            this.loadSuccess();
        },
        methods:{
            loadSuccess(){

                // 在这里对afterCacheObj对象进行数据操作，可以新增属性或方法。
                return new Promise(resolve  => {
                    let url = this.formatObj.configData.requestUrl;
                    if(url && !url.startsWith("/")){
                        url = "/" + url;
                    }
                    // url = this.COMMON_FUNC.replaceRequestServeUrl(url);
                    let param = {};
                    let keyArrObj = {};
                    (this.formatObj.paramData || []).forEach(item => {
                        if(!param[item.paramKey]){
                            param[item.paramKey] = [];
                        }
                        if(item.paramType == "rowData"){
                            if((this.showFieldObj[item.paramVal] || {}).fieldName) {
                                keyArrObj[item.paramKey] = this.showFieldObj[item.paramVal].fieldName + "_" + this.formatObj.formId;
                            }else{
                                let fieldArr =  item.paramVal.split(",");
                                if(fieldArr.length == 2 && fieldArr[0] == "id"){
                                    keyArrObj[item.paramKey] = "ID_" + fieldArr[1];
                                }else{
                                    keyArrObj[item.paramKey] = fieldArr[0] + "_" + fieldArr[1];
                                }
                            }
                        }
                        if(item.paramType == "diy"){
                            param[item.paramKey] = item.paramVal;
                            let outPramKeyStart = "${listOutParam.";
                            if(param[item.paramKey].indexOf(outPramKeyStart) > -1) { //存在外部参数
                                let outPramKeyEnd = "}";
                                let startIndex = param[item.paramKey].indexOf(outPramKeyStart); //获取下表
                                let endIndex = param[item.paramKey].indexOf(outPramKeyEnd);
                                //获取外部参数值
                                let outParam = param[item.paramKey].substring(startIndex + outPramKeyStart.length, endIndex) || ''; // 去除}
                                let replaceStr = outPramKeyStart + outParam + outPramKeyEnd || '';
                                //获取外部参数值
                                let outParamVal = this.listOutParam[outParam] || '';
                                param[item.paramKey] = param[item.paramKey].replace(replaceStr, outParamVal); // 去除${listOutParam.
                            }
                        }
                    });

                    let keyObj = {};
                    // 绑定参数
                    (this.dataRows || []).forEach(item => {
                        Object.keys(keyArrObj).forEach(key => {
                            let field = keyArrObj[key];
                            if(item[field]){
                                if(!keyObj[key]){
                                    keyObj[key] = [];
                                }
                                keyObj[key].push(item[field]);
                            }
                        })
                    })

                    Object.keys(keyObj).forEach(key => {
                        keyObj[key] = (keyObj[key] || []).join(",");
                    })

                    Object.assign(param,keyObj);

                    let repeatRequest = false; // 这里的校验不能影响正常运行
                    try{
                        repeatRequest = (this.lastParam && this.isEqual(this.lastParam,param));
                    }catch (e) {
                        console.log("校验参数重复失败！");
                        console.log(e);
                        repeatRequest = false;
                    }
                    if(repeatRequest){
                        resolve();
                    }else{
                        url = url.startsWith("/") ? url : "/" + url;
                        if(url.indexOf("http://") == -1 && url.indexOf("https://") == -1){
                            if(window.ADMIN_URL && (url.indexOf("/admin/") > -1 || url.indexOf("/oaadmin/") > -1)){
                                url = this.platStringUtil.replaceAll(url,"/admin/","/");
                                url = this.platStringUtil.replaceAll(url,"/oaadmin/","/");
                                url = window.ADMIN_URL + (url.startsWith("/") ? url : "/" + url);
                            }else if(window.FLOW_URL && (url.indexOf("/flow/") > -1 || url.indexOf("/oaflow/") > -1)){
                                url = this.platStringUtil.replaceAll(url,"/oaflow/","/");
                                url = this.platStringUtil.replaceAll(url,"/flow/","/");
                                url = window.FLOW_URL + (url.startsWith("/") ? url : "/" + url);
                            }else if(window.OA_URL && (url.indexOf("/project/") > -1 || url.indexOf("/oaproject/") > -1)){
                                url = this.platStringUtil.replaceAll(url,"/oaproject/","/");
                                url = this.platStringUtil.replaceAll(url,"/project/","/");
                                url = window.OA_URL + (url.startsWith("/") ? url : "/" + url);
                            }else{
                                url = this.platStringUtil.replaceAll(url,"/oaform/","/");
                                url = this.platStringUtil.replaceAll(url,"/form/","/");
                                url = window.FORM_RENDER_URL + (url.startsWith("/") ? url : "/" + url);
                            }
                        }
                        this.axios.post(url,param).then(res => {
                            if(res && res.data){
                                this.afterCacheObj = res.data;
                                this.lastParam = param;
                            }
                            resolve();
                        })
                    }
                })
            },
            /**
             * 判断对象是否相等
             */
            isEqual(obj1,obj2){
                let vm = this;
                let o1 = obj1 instanceof Object;
                let o2 = obj2 instanceof Object;
                if(!o1 || !o2){/*  判断不是对象  */
                    return obj1 === obj2;
                }

                if(Array.isArray(obj1) && Array.isArray(obj2)){
                    return vm.compareArray(obj1,obj2);
                }

                if(Object.keys(obj1).length !== Object.keys(obj2).length){
                    return false;
                }

                for(let attr in obj1){
                    if (obj1.hasOwnProperty(attr)) {
                        let t1 = obj1[attr] instanceof Object;
                        let t2 = obj2[attr] instanceof Object;
                        if(t1 && t2){
                            return this.isEqual(obj1[attr],obj2[attr]);
                        }else if(obj1[attr] !== obj2[attr]){
                            return false;
                        }
                    }
                }
                return true;
            },
            compareArray(array1,array2){
                let vm = this;
                if(array1.length != array2.length){
                    return false;
                }
                let obj = {}; // 记录已经比对过的数组序号
                for(let i = 0 ; i < array1.length ; i++){
                    let caculate = true;
                    for(let j = 0; j < array2.length ; j++){
                        if(obj[j]){ // 已经比对过，跳过
                            continue;
                        }
                        caculate = vm.isEqual(array1[i],array2[j]);
                        if(caculate){
                            obj[j] = i;
                            break;
                        }
                    }
                    if(!caculate){
                        break;
                    }
                }
                return Object.keys(obj).length == array1.length;
            },
        }
    }
</script>

<style scoped>

</style>
