//#import excel.js
//#import $owl_download_template:services/modelService.jsx

function main() {
    var params = JSON.parse($body);

    var id = "owl_download_template_org_batch_update_parentid";
    var jDownloadTemplate = owl_download_templateService.get(id);
    var fileUrl = "";
    if (!jDownloadTemplate) {
        var index = 0;
        var titles = [
            {"index": index++, "columnWidth": "20", "field": "merchantId", "title": "组织ID"},
            {"index": index++, "columnWidth": "20", "field": "merchantName", "title": "组织名称"},
            {"index": index++, "columnWidth": "20", "field": "oriParentId", "title": "原父组织ID"},
            {"index": index++, "columnWidth": "20", "field": "oriParentName", "title": "原父组织名称"},
            {"index": index++, "columnWidth": "20", "field": "newParentId", "title": "新父组织ID"},
            {"index": index++, "columnWidth": "20", "field": "newParentName", "title": "新父组织名称"}
        ];
        var recordList = [
            {
                merchantId: "owl_orgs_m_50000",
                merchantName: "测试商家",
                oriParentId: "owl_orgs_600001",
                oriParentName: "自营商家",
                newParentId: "owl_orgs_600002",
                newParentName: "外部商家"
            }
        ];

        var file_type_name = "批量调整商家所属父组织模板";

        fileUrl = Excel.createExcelList(file_type_name, titles, recordList);

        jDownloadTemplate = {};
        jDownloadTemplate.file_type_id = "org_batch_update_parentid";
        jDownloadTemplate.file_type_name = file_type_name;
        jDownloadTemplate.file = {url: fileUrl, name: file_type_name};
        owl_download_templateService.add(jDownloadTemplate);
    } else {
        fileUrl = jDownloadTemplate.file.url;
    }

    var ret = {
        state: 'ok',
        action: 'navigate',
        target: "_blank",
        url: fileUrl
    }
    out.print(JSON.stringify(ret));

}

main();
