package com.inkasso.factoring.common.util;

import cn.hutool.core.lang.Assert;
import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.plugin.table.LoopRowTableRenderPolicy;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.core.io.ClassPathResource;

import java.io.File;
import java.io.IOException;
import java.util.Map;

/**
 * @Author zhaoms
 * @Email miaosen.zhao@changan-inkasso.com
 * @Date 2023-12-19 19:17
 */
public class PoitlToDocx {
    private static Logger logger = LogManager.getLogger(PoitlToDocx.class);

    /**
     * @author lsc
     * @param classPathResource word模板文件路径
     * @param fileDir      生成的文件存放地址
     * @param fileName     生成的文件名
     * @param paramMap     参数集合
     * @return 返回word生成的路径
     */
    public static String createWord(ClassPathResource classPathResource, String fileDir, String fileName, Map<String, Object> paramMap) {
//        Assert.notNull(templatePath, "word模板文件路径不能为空");
        Assert.notNull(fileDir, "生成的文件存放地址不能为空");
        Assert.notNull(fileName, "生成的文件名不能为空");
        File dir = new File(fileDir);
        if (!dir.exists()) {
            logger.info("目录不存在，创建文件夹{}!", fileDir);
            dir.mkdirs();
        }
        String filePath = fileDir + "\\" + fileName;
        Configure config = null;
        if(paramMap.containsKey("dealGoodsList")){
            LoopRowTableRenderPolicy policy = new LoopRowTableRenderPolicy();
             config = Configure.builder().bind("dealGoodsList", policy).build();
        }

        try {
            // 读取模板渲染参数
            XWPFTemplate template = XWPFTemplate.compile(classPathResource.getInputStream(),config).render(paramMap);
            // 将模板参数写入路径
            template.writeToFile(filePath);
            template.close();
        } catch (Exception e) {
            logger.error("生成word异常{}", e.getMessage());
            e.printStackTrace();
        }
        return filePath;
    }

    public static void main(String[] args) throws IOException {


//        ArrayList<ContractLeaseback> cars = new ArrayList<>();
//        ContractLeaseback contractLeaseback = new ContractLeaseback();
//        contractLeaseback.setCarBrand("1");
//        contractLeaseback.setCarModel("1");
//        contractLeaseback.setCarFrame("1");
//        cars.add(contractLeaseback);
//        ContractLeaseback contractLeaseback2 = new ContractLeaseback();
//        contractLeaseback2.setCarBrand("2");
//        contractLeaseback2.setCarModel("2");
//        contractLeaseback2.setCarFrame("2");
//        cars.add(contractLeaseback2);
//
//        LoopRowTableRenderPolicy policy = new LoopRowTableRenderPolicy();
//        Configure config = Configure.builder()
//                .bind("cars", policy).build();
//
//
//        HashMap<Object, Object> map = new HashMap<>();
//        map.put("cars",cars);
//        map.put("lesseeName","上课了的附件");
//
//        XWPFTemplate template = XWPFTemplate.compile("D:\\a长安银科\\重卡系统4.6\\补充协议\\补充协议.docx", config).render(
//                map
//        );
//        template.writeToFile("D:\\a长安银科\\重卡系统4.6\\补充协议\\补充协议3.docx");
//        template.close();



    }
}
