package com.nantian.cloud.jly.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nantian.cloud.jly.entity.CheckPersonsAmend;
import com.nantian.cloud.jly.mapper.CheckPersonsAmendMapper;
import com.nantian.cloud.jly.service.CheckPersonsAmendService;
import com.nantian.cloud.jly.utils.EditWordZZUtil;
import com.nantian.cloud.jly.vo.JJF1356Vo;
import com.nantian.sc.cloud.common.minio.service.MinioTemplate;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.util.IOUtils;
import org.apache.poi.xwpf.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanMap;
import org.springframework.stereotype.Service;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigInteger;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @date 2018-09-27
 */
@Service("CheckPersonsAmendService")
public class CheckPersonsAmendServiceImpl extends ServiceImpl<CheckPersonsAmendMapper, CheckPersonsAmend> implements CheckPersonsAmendService {

    @Autowired
    private MinioTemplate minioTemplate;

    @Override
    public void deleteAmend(BigInteger checkPlanId, String epc) {
        baseMapper.deleteAmend(checkPlanId,epc);
    }

    @Override
    public XWPFDocument getJJF1356(List<JJF1356Vo> listMap, HttpServletResponse response) throws IOException, InvalidFormatException {


        for (JJF1356Vo jjf1356Vo : listMap) {
            String accord = jjf1356Vo.getAccord();
            if (accord.equals("0")){
                jjf1356Vo.setAccord("□");
            }else {
                jjf1356Vo.setAccord("■");
            }
            String inapplicability = jjf1356Vo.getInapplicability();
            if (inapplicability.equals("0")){
                jjf1356Vo.setInapplicability("□");
            }else {
                jjf1356Vo.setInapplicability("■");
            }
            String inconformity = jjf1356Vo.getInconformity();
            if (inconformity.equals("0")){
                jjf1356Vo.setInconformity("□");
            }else {
                jjf1356Vo.setInconformity("■");
            }
        }

        InputStream inputStream = minioTemplate.getObject("jly", "JJF1356— 2012.docx");
        XWPFDocument document = new XWPFDocument(OPCPackage.open(inputStream));
        List<XWPFTable> tables = document.getTables();
        EditWordZZUtil editWordZZUtil = new EditWordZZUtil();
        int flag = 0;
        JJF1356Vo jjf1356Vo = null;
        for (int i = 0; i < tables.size(); i++) {
            XWPFTable table = tables.get(i);
            List<XWPFTableRow> rows = table.getRows();
            for (XWPFTableRow row : rows) {
                String text = this.getTableCellText(row);
                if (editWordZZUtil.matcher(text).find()){
                    jjf1356Vo = listMap.get(flag);
                    BeanMap maps = BeanMap.create(jjf1356Vo);
                    List<XWPFTableCell> tableCells = row.getTableCells();
                    for (XWPFTableCell cell : tableCells) {
                        List<XWPFParagraph> paragraphs = cell.getParagraphs();
                        for (XWPFParagraph paragraph : paragraphs) {
                            if (editWordZZUtil.matcher(paragraph.getText()).find()){
                                editWordZZUtil.replaceParam(paragraph, maps);
                            }
                        }
                    }
                    flag ++;
                }
            }
        }
        OutputStream outputStream = null;
        try {
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("JJF1356— 2012.docx", "UTF-8"));
            response.setCharacterEncoding("UTF-8");
            response.setContentType("charset=GBK");
            outputStream = response.getOutputStream();
            document.write(outputStream);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            outputStream.close();
            inputStream.close();
            document.close();
        }
        return null;
    }

    private String getTableCellText(XWPFTableRow row){
        String str = null;
        List<XWPFTableCell> tableCells = row.getTableCells();
        for (XWPFTableCell cell : tableCells) {
            List<XWPFParagraph> paragraphs = cell.getParagraphs();
            for (XWPFParagraph paragraph : paragraphs) {
                str += paragraph.getText();
            }
        }
        return str;
    }
}

