const Parse = require("parse/node");
Parse.initialize("edu-textbook");
Parse.serverURL = "http://8.140.98.43/parse";
global.Parse = Parse;

import fs from "fs";
import path from "path";
import { downloadUrl, mergePdfListReduce, uploadFileToOSS } from "../../../lib/docs";
/**
 * 
 (npx vite build) -and (node temp/test-export.js)
 npx vite build && node temp/test-export.js

 npx vite build
 node temp/test-export.js

 @desc 本地合成测试指令
 sudo apt install poppler-utils
 libreoffice --headless --convert-to pdf *.jpg
 pdfunite 1*.pdf final.pdf
 libreoffice 更多用法可以在终端输入 libreoffice --help 或者 man libreoffice 。
 */
import { exportProcessReportDocs } from "../func-tbook-export";

async function main(){
    try{

        console.log("测试合集")
        // console.log(await exportProcessReportDocs("FR7KZtefyR"))
        // console.log("测试列表")
        // console.log(await exportProcessReportDocs(null,["9V575dapEM"]))
        // console.log(await exportProcessReportDocs(null,["c2WUE5PgCo"]))
        // console.log(await exportProcessReportDocs(null,["R9r0XtMsK3"])) // CIP顺序问题，后两个pdf到了最前面
        // console.log(await exportProcessReportDocs(null,["MU5YpUizfW"]))
        // console.log(await exportProcessReportDocs(null,["UUGgHPHsIC"]))
        // console.log(await exportProcessReportDocs(null,["KxhErArSbB"]))
        // console.log(await exportProcessReportDocs(null,["7cf69rSawD"]))
        // console.log(await exportProcessReportDocs(null,["kY6MbE1JXC"])) // 21个附件，超时
        // console.log(await exportProcessReportDocs(null,["O8kCxqdasp"])) // \u0002 乱码
        // console.log(await exportProcessReportDocs(null,["VK81UnQmkn"]))
        // console.log(await exportProcessReportDocs(null,["SD0NCAdscz"]))
        // console.log(await exportProcessReportDocs(null,["sbVgvo4qEk"]))
        // console.log(await exportProcessReportDocs(null,["4LnH46Mu03"]))
        // console.log(await exportProcessReportDocs(null,["xdIAG8BYnX"]))
        // console.log(await exportProcessReportDocs(null,["NJ2hWa17gi"]))
        // console.log(await exportProcessReportDocs(null,["tnEfUnTaQU"])) // C/C++ pathname error
        // console.log(await exportProcessReportDocs(null,["2s353GQBjk"]))
        // console.log(await exportProcessReportDocs(null,["KvvN9lWhri"]))
        // console.log(await exportProcessReportDocs(null,["orBBW42ZCn"])) // rar attach
        // console.log(await exportProcessReportDocs(null,["UwPtEptZTR"])) // pdf 七 八 加密
        // console.log(await exportProcessReportDocs(null,["TsjBrECA4m"])) // pdf 加密
        // console.log(await exportProcessReportDocs(null,["z7yhFusOns"])) 
        // console.log(await exportProcessReportDocs(null,["ybzJWkXqQG"])) 
        // console.log(await exportProcessReportDocs(null,["4UAwQ4nc5A"])) 
        // console.log(await exportProcessReportDocs(null,["ybzJWkXqQG"])) 
        // console.log(await exportProcessReportDocs(null,["yrmicQ8ZTR"])) 
        // console.log(await exportProcessReportDocs(null,["pdo6maunud","iWBI0FMTAS","Faw2Hl4qTf","lAG4JpaAIW","1aRyDbVyUu","GVNtZWdm50","5dq8TYCGfu","YkLYd4rv2v","889gBLs62l","fWTphlIAZu","DrHtcRrgXe","n6TxQZ0aOp","oV9W7FbyPj","sY1jZXS5rF","uvmtqWdMlM","rPaRkc0S3I","ikQXhMJPw7","ll0kl2RCBk","QCdJfwlBCh","F7aE1ACEgd","CcMa620Q5J","tk4zR6RbQN","6GLa6Po9p8","TbwoGVrSlR","xNDkRbGDP5","qg1ITwStRR","xHfG3po2yC","Tg7ndNBZvz","Hi1C6G43TM","mwCcj1VDrc"])) 
        // console.log(await exportProcessReportDocs(null,["nZy0XB9vHw"])) 
        
        // console.log(await exportProcessReportDocs(null,["3wJSkW9HNV"]))
        // console.log(await exportProcessReportDocs(null,["tHhfIDBkhf"]))
        // console.log(await exportProcessReportDocs(null,["cgTEtIvkK4"]))
        // console.log(await exportProcessReportDocs(null,["KvvN9lWhri"]))
        // console.log(await exportProcessReportDocs(null,["zVVwoy6ETT"],true))
        // console.log(await exportProcessReportDocs(null,["7PAU2PNcoB"],true))
        // console.log(await exportProcessReportDocs(null,["v8DjfCGfrP"],true))
        // console.log(await exportProcessReportDocs(null,["7sDvlq2w6Z"],true))
        
        // console.log(await exportProcessReportDocs(null,["Oa1yB6n64u","j4pt2MMTXM","jAMGxwrl1F","BwZpEoOtS8","X10kCktA0r"]))
        // console.log(await exportProcessReportDocs(null,["BwZpEoOtS8","X10kCktA0r"]))

        // 单独上传
        // console.log(await uploadFileToOSS("/home/ryan/workspace/nova/edu-textbook/server/cloud/tbook/test/export/all/"+"申报书-多位专业代码修复导出.zip"))
        // 单独上传 指定Key
        // console.log(await uploadFileToOSS("/home/ryan/Downloads/1727678422495-《物流英语》人员政治审查表.pdf",null,"/dHqLe4hyyU/2024-9-30/1727678422495-《物流英语》人员政治审查表.pdf"))

        // 根据export.txt 导出全部
        // await exportAllFromTxt()
        exportAll400();

    }catch(err){
        console.error(err)
    }
}
main()

// mergePdfListReduce([
//     path.join("/home/ryan/workspace/nova/edu-textbook/server/cloud/tbook/template/模板-推荐申报表.docx"),
//     path.join("/home/ryan/workspace/nova/edu-textbook/server/cloud/tbook/template/模板-推荐申报表.docx")
// ],{
//     files: [
//       '/home/ryan/workspace/nova/edu-textbook/server/cloud/tbook/test/temp/temp/112_1f15d43c0534288c61089099792e44e7.docx'
//     ],
//     properties: { pageSize: 'A4', margin: { top: 0, right: 0, bottom: 0, left: 0 } },
//     pdfa: false,
//     pdfUA: false,
//     merge: true
//   });

async function exportAll400(){
    let query = new Parse.Query("EduTextbook");
    query.select("objectId");
    query.limit(20000);
    query.notEqualTo("isDeleted",true);
    query.notEqualTo("discard",true);
    query.equalTo("render",true);
    query.containedIn("status",['400']);
    let list = await query.find();
    list = list.map(item=>item?.id)
    console.log(list.length)
    exportAllFromTxt(list)
}
async function exportAllFromTxt(list){
    if(!list?.length){
        let txt = fs.readFileSync(path.join(__dirname,"../","export.txt")).toString();
        list = txt.split("\n");
    }
    list = list.filter(item=>{
        let tdir = path.join(__dirname,"../export",item);
        if(!fs.existsSync(tdir)){return true};
        let fileList = fs.readdirSync(tdir);
        if(!fileList?.length){
            return true;
        }
        if(fileList?.length&&fileList.join("")?.indexOf("pdf")==-1){
            return true
        }
        return false;
    });
    // list = list.slice(0,1);
    console.log(list?.length)
    for (let index = 0; index < list.length; index++) {
        let id = list[index];
        console.log(index+1,id)
        let docDir = path.join(__dirname,"../export",id)
        console.log(docDir)
        if(!fs.existsSync(docDir)) {fs.mkdirSync(docDir)};

        let data = await exportProcessReportDocs(null,[id],true)
        // console.log(data) 
        let docList = data?.docsList || []
        // console.log(docList)
        for (let index = 0; index < docList.length; index++) {
            let doc = docList[index]
            let docDir = path.join(__dirname,"../export",doc.objectId);
            if(!fs.existsSync(docDir)) {fs.mkdirSync(docDir)};
            let urlPdf = doc.urlPdf;
            console.log(doc)
            // let targetPath = path.join(docDir,path.basename(pdfPath));
            // fs.copyFileSync(pdfPath,targetPath)
            // console.log(targetPath)
            let filepath = await downloadUrl(urlPdf,{tempDir:docDir,keepFileName:true});
            console.log(filepath)
            if(!filepath || !fs.existsSync(filepath)){
                fs.rmdirSync(docDir,{recursive:true,force:true});
            }
        }
    }
}