<?xml version="1.0" encoding="UTF-8"?>
<a:service xmlns:a="http://www.leaf-framework.org/application" xmlns:s="leaf.plugin.script" trace="true">
    <a:init-procedure>
        <s:server-script><![CDATA[
            importPackage(java.io);
            importPackage(Packages.com.hand.elecon.httpost);

            var up_file_token_url = info_query('UP_FILE_TOKEN_URL').code_value;
            //系统参数获取
            function info_query(parameter_code) {
                var info = new Object();
                var data = null;
                var parameter_map = $bm('wx.WX100.sys_parameter_value').queryAsMap({
                    parameter_code:parameter_code
                });
                var data = parameter_map.getChildren();
                info.code_value = data[0].code_value;
                return info;
            }

            //获取电子签署的flow_id
            var sign_cancel_bm = $bm('cont.CON505.contract_sign_cancel');
            var sign_cancel_map = sign_cancel_bm.queryAsMap({
                contract_id:$ctx.parameter.contract_id
            });
            var sign_cancel_data = sign_cancel_map.getChildren();
            var revoke_flows = [];
            for (var i = 0; i < sign_cancel_data.length;i++){
                revoke_flows.push({
                    "flowId":sign_cancel_data[i].ele_flow_id
                });
            }
            var revokeParam = {
                revokeFlows:revoke_flows
            }
            //调用中台取消流程
            var info = info_query('HL_SIGN_FLOW_CANCEL');
            var httpUrl = info.code_value;
            var logs_bm = $bm('wx.WX100.con_ele_savelogs');
            try {
                var token_sign_result = EleFileUpHttpPost.post(up_file_token_url, null, null);
            } catch (e) {
                raise_app_error('上传文件获取token失败，请联系技术人员检查！');
            }
            var token_sign_result_ob = JSON.parse(token_sign_result);
            var access_sign_token = token_sign_result_ob.access_token;
            var result = EleFileUpHttpPost.post(httpUrl, JSON.stringify(revokeParam), access_sign_token);
            logs_bm.insert({
                contract_id:$ctx.parameter.contract_id,
                httpurl:httpUrl,
                jsonPut:JSON.stringify(revokeParam),
                jsonResult:result,
                tokenResult:token_sign_result
            });
            var result_ob = JSON.parse(result); //根据返回参数形式再定
            var revoke_flag_update_bm = $bm('cont.CON505.contract_revoke_flag_update');
            if (result_ob.code != 0) {
                //rollback
                $bm('wx.WX100.rollback').update();
                raise_app_error('取消签约失败，请联系技术人员检查！');
            }else if(result_ob.code == 0){
                var revokeFlows = result_ob.revokeFlows;
                var all_revoke_flag = 'Y';
                var error_message;
                var result;
                for (var i = 0; i < revokeFlows.length; i++) {
                    if(revokeFlows[i].result == 0){
                        revoke_flag_update_bm.update({
                            contract_id:$ctx.parameter.contract_id,
                            flow_id:revokeFlows[i].flowId
                        });
                    }else{
                        all_revoke_flag = 'N';
                        result = revokeFlows[i].result;
                        error_message = revokeFlows[i].msg;
                    }
                }
                if(all_revoke_flag == 'Y'){
                    sign_cancel_bm.execute({
                        contract_id:$ctx.parameter.contract_id
                    });
                }else{
                    if(result = '1437111'){
                        $ctx.parameter.message = '签约已完毕，不可取消';
                    }else{
                        $ctx.parameter.message = error_message;
                    }
                }
            }
            ]]></s:server-script>
    </a:init-procedure>
<a:service-output output="/parameter"/>
</a:service>