package com.loan.system.utils;

import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.util.PoitlIOUtils;
import com.loan.system.domain.entity.Contract;
import com.loan.system.domain.pojo.ContractInformation;

import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.HashMap;

/**
 * @author : EdwinXu
 * @date : Created in 2021/1/26 16:15
 */
public class PoiWordUtil {
    public static void writeApprove(HttpServletResponse response, String path, ContractInformation contract, String downloadName){
        File file = new File(path);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        XWPFTemplate template = XWPFTemplate.compile(file).render(
                new HashMap<String, Object>(){{
//                    put("projectName", approveById.getProjectName());
//                    put("copies", approveById.getCopies());
//                    put("submittingDepartment", approveById.getSubmittingDepartment());
//                    put("submittingDate", format.format(approveById.getSubmittingDate()));
//                    put("submitter", approveById.getSubmitter());
//                    put("acceptDate", format.format(approveById.getAcceptDate()));
//                    put("accepter", approveById.getAccepter());
//                    put("logisticsServicesReviewComments", approveById.getLogisticsServicesReviewComments());
//                    put("publicAffairsReviewComments", approveById.getPublicAffairsReviewComments());
//                    put("leadersInChargeReviewComments", approveById.getLeadersInChargeReviewComments());
//                    put("contractCommissionSignedOptions", approveById.getContractCommissionSignedOptions());
//                    put("contractNumber", approveById.getContractNumber());
                }});
        try {
            response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document;charset=UTF-8");
            response.setHeader("Content-Disposition","attachment;filename=\""+downloadName+".docx"+"\"");
            OutputStream out = response.getOutputStream();
            BufferedOutputStream bos = new BufferedOutputStream(out);
            template.write(bos);
            bos.flush();
            out.flush();
            PoitlIOUtils.closeQuietlyMulti(template, bos, out);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
