package com.admin.major.manager.impl;

import cn.afterturn.easypoi.entity.ImageEntity;
import cn.afterturn.easypoi.word.WordExportUtil;
import cn.afterturn.easypoi.word.entity.MyXWPFDocument;
import com.admin.common.utils.poi.WordUtil;
import com.admin.major.domain.dto.MajorApplyViewDTO;
import com.admin.major.manager.IMajorApplyExportManager;
import com.admin.major.mapper.EnhanceMajorApplyMapper;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MajorApplyExportManagerImpl implements IMajorApplyExportManager {

    @Autowired
    private EnhanceMajorApplyMapper enhanceMajorApplyMapper;

    //分流表模板路径
    private static String templatePath = "static/majorApply.docx";
    //分流表临时存放路径
    //private static String temDir = "admin-major/src/main/resources/static/major";
    @Override
    public void downloadApplyTable(Integer[] majorApplyIds, HttpServletRequest request, HttpServletResponse response) throws Exception {
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy年MM月dd日");
        Map<String,Object> params = new HashMap<>();
        List<XWPFDocument> listDoc = new ArrayList<>();
        List<String> listName = new ArrayList<>();
        for (int i =0; i<majorApplyIds.length; i++) {
            MajorApplyViewDTO majorApplyViewDTO = enhanceMajorApplyMapper.selectMajorApplyInfoById(majorApplyIds[i]);
            if (null != majorApplyViewDTO) {
                params.put("number", majorApplyViewDTO.getStudentNO() == null ? "暂无" : majorApplyViewDTO.getStudentNO());
                params.put("name", majorApplyViewDTO.getStudentName() == null ? "暂无" : majorApplyViewDTO.getStudentName());
                params.put("class", majorApplyViewDTO.getGradeName() == null ? "暂无" : majorApplyViewDTO.getGradeName());
                params.put("phone", majorApplyViewDTO.getMobileNo() == null ? "暂无" : majorApplyViewDTO.getMobileNo());
                params.put("majorId1st", majorApplyViewDTO.getMajorName1st() == null ? "暂无" : majorApplyViewDTO.getMajorName1st());
                params.put("majorId2nd", majorApplyViewDTO.getMajorName2st() == null ? "暂无" : majorApplyViewDTO.getMajorName2st());
                params.put("majorId3rd", majorApplyViewDTO.getMajorName3st() == null ? "暂无" : majorApplyViewDTO.getMajorName3st());
                params.put("majorId4th", majorApplyViewDTO.getMajorName4st() == null ? "暂无" : majorApplyViewDTO.getMajorName4st());
                params.put("majorId5th", majorApplyViewDTO.getMajorName5st() == null ? "暂无" : majorApplyViewDTO.getMajorName5st());
                params.put("sign", majorApplyViewDTO.getStudentSign() == null ? "暂无" : new ImageEntity(Base64.decodeBase64(StringUtils.substringAfter(majorApplyViewDTO.getStudentSign(), ";base64,")), 120, 70));
                params.put("date", majorApplyViewDTO.getApplyDate() == null ? "暂无" : dateFormat.format(majorApplyViewDTO.getApplyDate()));
                //WordUtil.exportWord(templatePath, temDir, majorApplyViewDTO.getStudentName() + "志愿表.docx", params, request, response);
                InputStream resourceAsStream = WordUtil.class.getClassLoader().getResourceAsStream(templatePath);
                Assert.notNull(resourceAsStream,"模板文件不存在");
                XWPFDocument doc = new MyXWPFDocument(resourceAsStream);
                WordExportUtil.exportWord07(doc, params);
                listDoc.add(doc);
                listName.add(params.get("name").toString());
            }
        }
        WordUtil.exportZip(listDoc,listName,response);
    }
}
