package com.admin.major.manager.impl;

import cn.afterturn.easypoi.entity.ImageEntity;
import com.admin.common.utils.poi.WordUtil;
import com.admin.major.domain.dto.SpecialApplyViewDTO;
import com.admin.major.manager.ISpecialApplyExportManager;
import com.admin.major.mapper.EnhanceSpecialApplyMapper;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

@Service
public class SpecialApplyExportManagerImpl implements ISpecialApplyExportManager {

    @Autowired
    private EnhanceSpecialApplyMapper enhanceSpecialApplyMapper;

    //分流表模板路径
    private static String templatePath = "static/specialApply.docx";
    //分流表临时存放路径
    private static String temDir = "static/special";
    @Override
    public void downloadSpecialTable(Integer specialApplyId, HttpServletRequest request, HttpServletResponse response) throws IllegalAccessException, IOException {
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy年MM月dd日");
        Map<String,Object> params = new HashMap<>();
        SpecialApplyViewDTO specialApplyViewDTO = enhanceSpecialApplyMapper.selectSpecialApplyInfoById(specialApplyId);
        if (null != specialApplyViewDTO) {
            params.put("name", specialApplyViewDTO.getStudentName() == null ? "暂无" : specialApplyViewDTO.getStudentName());
            params.put("gender", specialApplyViewDTO.getGenderName() == null ? "暂无" : specialApplyViewDTO.getGenderName());
            params.put("class", specialApplyViewDTO.getGradeName() == null ? "暂无" : specialApplyViewDTO.getGradeName());
            params.put("number", specialApplyViewDTO.getStudentNO() == null ? "暂无" : specialApplyViewDTO.getStudentNO());
            params.put("gpa", specialApplyViewDTO.getAvgScore() == null ? "暂无" : specialApplyViewDTO.getAvgScore());
            params.put("rank", specialApplyViewDTO.getAvgScoreRank() == null ? "暂无" : specialApplyViewDTO.getAvgScoreRank());
            params.put("major", specialApplyViewDTO.getMajorName() == null ? "暂无" : specialApplyViewDTO.getMajorName());
            params.put("phone", specialApplyViewDTO.getMobileNo() == null ? "暂无" : specialApplyViewDTO.getMobileNo());
            params.put("reason", specialApplyViewDTO.getApplyReason() == null ? "暂无" : specialApplyViewDTO.getApplyReason());
            params.put("studentsign", specialApplyViewDTO.getStudentSign() == null ? "暂无" : new ImageEntity(Base64.decodeBase64(StringUtils.substringAfter(specialApplyViewDTO.getStudentSign(), ";base64,")), 100, 20));
            params.put("parentssign", specialApplyViewDTO.getGuardianSign() == null ? "暂无" : new ImageEntity(Base64.decodeBase64(StringUtils.substringAfter(specialApplyViewDTO.getGuardianSign(), ";base64,")), 100, 20));
            params.put("createtime", specialApplyViewDTO.getCreateTime() == null ? "暂无" : dateFormat.format(specialApplyViewDTO.getCreateTime()));
            WordUtil.exportWord(templatePath, temDir, specialApplyViewDTO.getStudentName() + "specialApply.docx", params, request, response);
        }
    }
}
