package com.sunxung.factoring.component.datatransfer;

import com.deepoove.poi.XWPFTemplate;
import com.sunxung.factoring.component.datatransfer.model.purchcontract.PurchContractChangeTemplateVo;
import com.sunxung.factoring.component.util.FileUtil;
import com.sunxung.factoring.dict.impl.FileModuleDict;
import org.apache.commons.io.FileUtils;
import org.springframework.core.io.ClassPathResource;

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

public class WordWriter {


    /**
     * 采购合同
     *
     * @param
     * @param data
     * @throws IOException
     */
    public static File buildPurchContractFile(String contractName, Map data) throws IOException {
        //模板地址
        ClassPathResource classPathResource = new ClassPathResource("ftl/purchcontract/export/pruchContract.docx");
        XWPFTemplate template = XWPFTemplate.compile(classPathResource.getInputStream()).render(data);
        String path = FileUtil.getAbsolutePath(contractName, ".docx", FileModuleDict.ChildEnum.PURCHASE_CONTRACT_FILE);
        File file = new File(path);
        if (!file.exists()) {
            FileUtils.createParentDirectories(file);
        }
        template.writeToFile(path);
        return file;
    }

    /**
     * 采购信息补充信息合同
     *
     * @param
     * @param data
     * @throws IOException
     */
    public static File buildPurchContractChangeFile(String contractName, PurchContractChangeTemplateVo data) throws IOException {
        //模板地址
        ClassPathResource classPathResource = new ClassPathResource("ftl/purchcontract/export/pruchContractChange.docx");
        XWPFTemplate template = XWPFTemplate.compile(classPathResource.getInputStream()).render(data);
        String path = FileUtil.getAbsolutePath(contractName, ".docx", FileModuleDict.ChildEnum.PURCHASE_CONTRACT_SUPPLEMENT_FILE);
        File file = new File(path);
        if (!file.exists()) {
            FileUtils.createParentDirectories(file);
        }
        template.writeToFile(path);
        return file;
    }

    /**
     * 采购合同
     *
     * @param
     * @param data
     * @throws IOException
     */
    public static File buildSettlementFile(String settlementName, Map data) throws IOException {
        //模板地址
        ClassPathResource classPathResource = new ClassPathResource("ftl/bill.export/settlement.docx");
        XWPFTemplate template = XWPFTemplate.compile(classPathResource.getInputStream()).render(data);
        String path = FileUtil.getAbsolutePath(settlementName, ".docx",  FileModuleDict.ChildEnum.SETTLEMENT_UPSTREAM_UNSIGNED_FILE);
        File file = new File(path);
        if (!file.exists()) {
            FileUtils.createParentDirectories(file);
        }
        template.writeToFile(path);
        return file;
    }

    public static File buildAcceptanceFile(String fileName,  Map data) throws IOException {
        //模板地址
        ClassPathResource classPathResource = new ClassPathResource("ftl/acceptancesheet/export/AcceptanceSheet.docx");
        XWPFTemplate template = XWPFTemplate.compile(classPathResource.getInputStream()).render(data);
        String path = FileUtil.getAbsolutePath(fileName, ".docx",  FileModuleDict.ChildEnum.ACCEPTANCE_FILE);
        File file = new File(path);
        if (!file.exists()) {
            FileUtils.createParentDirectories(file);
        }
        template.writeToFile(path);
        return file;
    }
}
