<?xml version="1.0" encoding="UTF-8"?>
<a:service xmlns:p="uncertain.proc" xmlns:a="http://www.leaf-framework.org/application" xmlns:s="leaf.plugin.script" trace="true">
    <a:init-procedure>
        <s:server-script><![CDATA[
            importPackage(Packages.com.hand.hls.hlcm.ccx.service);
            var service = $spring(ICcxIcInfoService);

            // 配置信息
            var interface_code = 'CCX_ORG_INTERFACE';
            var interface_parameter_bm = $bm('interface.hls_interface_parameter');
            var interface_parameter_result = interface_parameter_bm.queryAsMap({
                "interface_code": interface_code
            });
            var interface_parameter_records = interface_parameter_result.getChildren();

            var apiUrl = interface_parameter_records[0].INTERFACE_URL; //环境地址
            var account = interface_parameter_records[0].INTERFACE_USERNAME;  //用户名
            var privateKey = interface_parameter_records[0].INTERFACE_PASSWORD;  //密码

            //var apiUrl = 'https://tapi.ccxcredit.com/data-service/icinfo/report';
            //var account = 'DCFL0011';
            //var privateKey = '0c95ab2ec86a4a018f77eb56d7ed26e7';
            // 测试数据
            //var cid = '310000400308460';
            //var name = '上海汉得信息技术股份有限公司';
            var cid = $ctx.parameter.business_license_num;
            var name = $ctx.parameter.bp_name;
            try {
                //发起请求
                var resultString = service.sendCcxIcInfoMessage(apiUrl, account, privateKey, cid, name);
                //转成json数组
                var json_msg = JSON.parse(resultString);
                var json_str = null;
                //根据时间戳生成一个随机数作为外键
                var chk_list_id = (new Date()).valueOf();
                println("111:" + json_msg + json_msg.entities.length);
                var rescode = json_msg.resCode;
                var resmsg = json_msg.resMsg;
                var reqtid = json_msg.reqTid;
                var tid = json_msg.tid;
                var sign = json_msg.sign;
                println("rescode:" + rescode);
                //循环json数组取出data值
                for (var i = 0;i < json_msg.entities.length;i++) {
                    //获取接口的数据类型
                    var interface_type = json_msg.entities[i].type;
                    //如果接口返回数据为数据分析,进一步解析
                    if (interface_type == 20) {
                        //遍历map,获取相应key的数组
                        for (var key in json_msg.entities[i].data) {
                            //获取entDBRank
                            if (key == "entDBRank") {
                                interface_type = 21;
                                json_str = JSON.stringify(json_msg.entities[i].data.entDBRank);
                                println("entDBRank:" + chk_list_id + ',' + interface_type + ',' + json_str);
                                // 插入接口表
                                $bm('interface.CCX_INTERFACE.ccx_insert_interface').update({
                                    chk_list_id: chk_list_id,
                                    bp_id: $ctx.parameter.bp_id,
                                    interface_type: interface_type,
                                    json_str: json_str,
                                    rescode: rescode,
                                    resmsg: resmsg,
                                    reqtid: reqtid,
                                    tid: tid,
                                    sign: sign
                                });
                            } else if (key == "comRankInfo") { //获取comRankInfo的map
                                for (var key in json_msg.entities[i].data.comRankInfo) { //遍历comRankInfo
                                    if (key == "comRanks") { //获取comRanks的数组
                                        interface_type = 22; //设置interface_type为22,便于插入对应的表
                                        if (json_msg.entities[i].data.comRankInfo.comRanks != null && json_msg.entities[i].data.comRankInfo.comRanks != []) {
                                            for (var a = 0;a < json_msg.entities[i].data.comRankInfo.comRanks.length;a++) {
                                                println("year:" + json_msg.entities[i].data.comRankInfo.comRanks[a].year);
                                                json_msg.entities[i].data.comRankInfo.comRanks[a].comRankModels[0].year = json_msg.entities[i].data.comRankInfo.comRanks[a].year;
                                                json_str = JSON.stringify(json_msg.entities[i].data.comRankInfo.comRanks[a].comRankModels[0]);
                                                println("comRankModels:" + chk_list_id + ',' + interface_type + ',' + json_str);
                                                // 插入接口表
                                                $bm('interface.CCX_INTERFACE.ccx_insert_interface').update({
                                                    chk_list_id: chk_list_id,
                                                    bp_id: $ctx.parameter.bp_id,
                                                    interface_type: interface_type,
                                                    json_str: json_str,
                                                    rescode: rescode,
                                                    resmsg: resmsg,
                                                    reqtid: reqtid,
                                                    tid: tid,
                                                    sign: sign
                                                });
                                            }
                                        }

                                    } else if (key == "comRatios") {
                                        interface_type = 23; //设置interface_type为23,便于插入对应的表
                                        if (json_msg.entities[i].data.comRankInfo.comRatios != null && json_msg.entities[i].data.comRankInfo.comRatios != []) {
                                            for (var b = 0;b < json_msg.entities[i].data.comRankInfo.comRatios.length;b++) {
                                                json_str = JSON.stringify(json_msg.entities[i].data.comRankInfo.comRatios[b]);
                                                println("comRatios:" + chk_list_id + ',' + interface_type + ',' + json_str);
                                                // 插入接口表
                                                $bm('interface.CCX_INTERFACE.ccx_insert_interface').update({
                                                    chk_list_id: chk_list_id,
                                                    bp_id: $ctx.parameter.bp_id,
                                                    interface_type: interface_type,
                                                    json_str: json_str,
                                                    rescode: rescode,
                                                    resmsg: resmsg,
                                                    reqtid: reqtid,
                                                    tid: tid,
                                                    sign: sign
                                                });
                                            }
                                        }
                                    } else if (key == "avgIndustries") {
                                        interface_type = 24; //设置interface_type为24,便于插入对应的表
                                        if (json_msg.entities[i].data.comRankInfo.avgIndustries != null && json_msg.entities[i].data.comRankInfo.avgIndustries != []) {
                                            for (var c = 0;c < json_msg.entities[i].data.comRankInfo.avgIndustries.length;c++) {
                                                json_str = JSON.stringify(json_msg.entities[i].data.comRankInfo.avgIndustries[c]);
                                                println("avgIndustries:" + chk_list_id + ',' + interface_type + ',' + json_str);
                                                // 插入接口表
                                                $bm('interface.CCX_INTERFACE.ccx_insert_interface').update({
                                                    chk_list_id: chk_list_id,
                                                    bp_id: $ctx.parameter.bp_id,
                                                    interface_type: interface_type,
                                                    json_str: json_str,
                                                    rescode: rescode,
                                                    resmsg: resmsg,
                                                    reqtid: reqtid,
                                                    tid: tid,
                                                    sign: sign
                                                });
                                            }
                                        }
                                    }
                                }

                            }
                        }
                    } else {
                        json_str = JSON.stringify(json_msg.entities[i].data);
                        println("all:" + chk_list_id + ',' + interface_type + ',' + json_str);
                        // 插入接口表
                        $bm('interface.CCX_INTERFACE.ccx_insert_interface').update({
                            chk_list_id: chk_list_id,
                            bp_id: $ctx.parameter.bp_id,
                            interface_type: interface_type,
                            json_str: json_str,
                            rescode: rescode,
                            resmsg: resmsg,
                            reqtid: reqtid,
                            tid: tid,
                            sign: sign

                        });
                    }


                }
            } catch (e) {
                println("抛出异常:" + e);
            }
        ]]></s:server-script>
    </a:init-procedure>
    <a:service-output output="/parameter"/>
</a:service>