<?xml version="1.0" encoding="UTF-8"?>
<a:service xmlns:ns1="leaf.application.action" xmlns:s="leaf.plugin.script" xmlns:a="http://www.leaf-framework.org/application" xmlns:p="uncertain.proc" trace="true">
    <a:init-procedure>
        <s:server-script><![CDATA[
            // println($ctx.parameter.toXML());
            
            function document_check_before_delete(base_table, base_table_pk_value, _status) {
                if (_status == 'delete') {
                    $ctx.parameter.delete_flag = 'Y';
                    var check_before_delete_bm = $bm('cont.CON500.con_contract_check_before_delete');
                    check_before_delete_bm.update({
                        document_id: $ctx.parameter.document_id,
                        document_category: $ctx.parameter.document_category,
                        document_type: $ctx.parameter.document_type,
                        function_code: $ctx.parameter.function_code,
                        function_usage: $ctx.parameter.function_usage,
                        table_name: base_table.toUpperCase(),
                        table_pk: base_table_pk_value
                    });
                } else {
                    $ctx.parameter.delete_flag = 'N';
                }
            }
            
            function recursive_dynamic_save(root, base_table, query_only, parent_table, parent_base_table_pk) {
                if (!root || !base_table) return;
                var children = root.getChildren();
                if (!children.length) return;
                var query_base_table_bm = $bm('cont.CON500.con_hls_doc_layout_tab_query');
                var sub_tn_map = query_base_table_bm.queryAsMap({
                    "parent_table": base_table.toUpperCase(),
                    'layout_code': $ctx.parameter.layout_code,
                    'enabled_flag': 'Y'
                });
                var sub_base_tables = sub_tn_map.getChildren();
                if (query_only == 'Y' && sub_base_tables.length == 0) {
                    return;
                }
                $ctx.dynamic_base_table = base_table;
                var bm = $bm('cont.CON500.con_contract_common');
                var pk = $ctx.pkmap[base_table];
                for (var i = 0;i < children.length;i++) {
                    var _status = children[i]._status;
                    // var status_bm=$bm('bm');
                    // status_bm.update(children[i]);校验单据状态
                    var parent_error_code = '';
                    document_check_before_delete(base_table, children[i][pk], _status);
                    try {
                        bm.option = {
                            updatePassedFieldOnly: 'true'
                        }
                        if (parent_table && parent_base_table_pk && !children[i][parent_base_table_pk]) {
                            parent_error_code = 'HLS_TABLE_UNIQUE.SAVE_PARENT_FIRST';
                            raise_app_error(parent_error_code);
                        }
                        if (query_only != 'Y') {
                            bm[_status](children[i]);
                        }
                    } catch (e) {
                        if (parent_error_code) {
                            raise_app_error(parent_error_code);
                        } else {
                            raise_app_error('HLS_TABLE_UNIQUE.' + base_table.toUpperCase());
                        }
                    }
                    if (!parent_table && children[i].getData().getParent().getName() == 'parameter') {
                        if (!$ctx.parameter.document_id) {
                            $ctx.parameter.document_id = children[i][pk];
                        }
                        if ($ctx.parameter.document_category) {
                            children[i].document_category = $ctx.parameter.document_category;
                        }
                        if ($ctx.parameter.document_type) {
                            children[i].document_type = $ctx.parameter.document_type
                        }
                        if ($ctx.parameter.function_code) {
                            children[i].function_code = $ctx.parameter.function_code;
                        }
                        if ($ctx.parameter.function_usage) {
                            children[i].function_usage = $ctx.parameter.function_usage;
                        }
                        if (_status == 'insert') {
                            var get_num_bm = $bm('cont.CON500.con_contract_update_special_fields');
                            children[i].document_id = children[i][pk] || null;
                            get_num_bm.update(children[i]);
                        }
                    }
                    for (var j = 0;j < sub_base_tables.length;j++) {
                        var bind_name = sub_base_tables[j].tab_code + '_' + sub_base_tables[j].base_table.toLowerCase();
                        var sub_root = children[i].getChild(bind_name);
                        if (sub_root && pk) {
                            var sub_children = sub_root.getChildren();
                            if (bind_name.indexOf('prj_cdd_item_doc_ref') == -1) {
                                for (var m = 0;m < sub_children.length;m++) {
                                    sub_children[m][pk] = children[i][pk];
                                }
                                recursive_dynamic_save(sub_root, sub_base_tables[j].base_table, sub_base_tables[j].query_only, sub_base_tables[j].parent_table, sub_base_tables[j].parent_base_table_pk);
                            } else {
                                var spec_bm = $bm('prj.PRJ500.prj_project_doc_cdd_item');
                                for (var m = 0;m < sub_children.length;m++) {
                                    sub_children[m]['document_id'] = children[i][pk];
                                    spec_bm[sub_children[m]['_status']](sub_children[m]);
                                }
                            }
                        }
                    }
                }
            }
            
            function document_check_after_save() {
                var last_check_bm = $bm('cont.CON500.con_contract_check_after_save');
                last_check_bm.update({
                    document_id: $ctx.parameter.document_id,
                    document_category: $ctx.parameter.document_category,
                    document_type: $ctx.parameter.document_type,
                    function_code: $ctx.parameter.function_code,
                    function_usage: $ctx.parameter.function_usage
                });
            }
            recursive_dynamic_save($ctx.parameter, $ctx.parameter.base_table, $ctx.parameter.query_only, $ctx.parameter.parent_table, $ctx.parameter.parent_base_table_pk);
            if (!$ctx.parameter.delete_flag || $ctx.parameter.delete_flag == 'N') {
                document_check_after_save();
            }
        ]]></s:server-script>
    </a:init-procedure>
    <a:service-output output="/parameter"/>
</a:service>
