package com.chinaweal.aiccs.aiccs.abnormal.service.impl;

import com.chinaweal.aiccs.aiccs.abnormal.service.CreatePdfService;
import com.chinaweal.aiccs.common.util.PdfUtil;
import com.chinaweal.aiccs.common.util.StringUtils;
import com.chinaweal.aiccs.config.CommonConfig;
import com.chinaweal.youfool.framework.springboot.exception.custom.BusinessException;
import com.deepoove.poi.XWPFTemplate;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @author Lee
 * @since 2021-07-12 18:06
 */
@Service
@Slf4j
public class createPdfServiceImpl implements CreatePdfService {

    /**
     * 生成pdf
     *
     * @param bizseq 业务主键
     * @param path   编译模板路径
     * @param map    参数Map
     * @return
     */
    @Override
    public String createPdfWithHtmlTemplate(String bizseq, String path, Map<String, Object> map) {
        if (StringUtils.isNotBlank(bizseq)) {
            String uuid = UUID.randomUUID().toString();

            try (
                    ByteArrayOutputStream wordOs = new ByteArrayOutputStream() // word输出流
            ) {
                String resource = createPdfServiceImpl.class.getClassLoader().getResource(path).getPath();

                // html内容处理
                String htmlContent = map.get("htmlContent").toString();
                Document htmldoc = Jsoup.parse(htmlContent);
                Elements links = htmldoc.getElementsByTag("p");
                ArrayList<Object> list = new ArrayList<>();
                for (Element link : links) {
                    if (link.getElementsByAttributeValue("style", "text-align:right;").size() > 0) {
                        Elements value = link.getElementsByAttributeValue("style", "text-align:right;");
                        HashMap<String, Object> map2 = new HashMap<>();
                        map2.put("rightContent", value.text().trim());
                        list.add(map2);
                    } else if (link.getElementsByAttributeValue("style", "text-align:left;").size() > 0) {
                        Elements value = link.getElementsByAttributeValue("style", "text-align:left;");
                        HashMap<String, Object> map3 = new HashMap<>();
                        map3.put("leftContent", value.text().trim());
                        list.add(map3);
                    } else {
                        HashMap<String, Object> map1 = new HashMap<>();
                        map1.put("pragraph", link.text().trim());
                        list.add(map1);
                    }
                }
                map.put("htmlContent", list);

                // 文件存储改造 2021-11-7
                XWPFTemplate.compile(resource).render(map).write(wordOs);

                // word转pdf
                String uploadPath = CommonConfig.getWritPath();
                String pdfPath = uploadPath + File.separator + uuid + ".pdf";
                PdfUtil.convertDocx2Pdf(wordOs, pdfPath);

                return pdfPath;
            } catch (Exception e) {
                log.error("生成word文件失败, biSeq: {}", bizseq, e);
                throw new BusinessException("生成word文件失败");
            }
        }
        return null;
    }

    @Override
    public void initPdfUtil() {
        String realPath = CommonConfig.getUploadPath();
        try {
            String wordPath = "static/template/init.docx";
            ClassPathResource classPathResource = new ClassPathResource(wordPath);
            String resource = classPathResource.getURL().getPath();
            String pdfPath = realPath + File.separator + "init.pdf";
            PdfUtil.convertDocx2Pdf(resource, pdfPath);
        } catch (Exception e) {
            log.error("word转pdf失败", e);
        }
    }
}
