package com.miaxis.test;

import com.miaxis.TwzdApplication;
import com.miaxis.question.mapper.QuestionInfoMapper;
import com.miaxis.question.vo.QuestionInfoExcelTypeVo;
import com.miaxis.question.vo.QuestionInfoExcelVo;
import com.miaxis.wx.service.impl.WxGzhServiceImpl;
import org.apache.poi.hssf.usermodel.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.List;

@ActiveProfiles("prod")
@SpringBootTest(classes = TwzdApplication.class)
@RunWith(SpringRunner.class)
public class NormalTest {


    @Resource
    WxGzhServiceImpl wxGzhService;

    @Resource
    QuestionInfoMapper questionInfoMapper;




    @Test
    public void test0() throws Exception {
        wxGzhService.handlePublicMsg(null);
    }

    @Test
    public void test1() throws Exception {
        try {
            OutputStream out = new FileOutputStream("D:\\分类专题.xls");
            List<QuestionInfoExcelTypeVo> questionInfoExcelTypeVos = questionInfoMapper.selectIssueType1(1);
            String[] headers = { "id", "number", "issue", "image","excell_issue_name", "class_issue_name" ,"seque_issue_name","place_issue_name","question_type","class_sort"};
            HSSFWorkbook workbook = new HSSFWorkbook();
            for (int index=0; index < questionInfoExcelTypeVos.size();index++){
                try {
                    QuestionInfoExcelTypeVo questionInfoExcelTypeVo =  questionInfoExcelTypeVos.get(index);
                    List<QuestionInfoExcelVo> questionInfoExcelVos = questionInfoMapper.selecQuestionInfoExcel1(questionInfoExcelTypeVo.getTypeId());
                    exportExcel(workbook, index,questionInfoExcelTypeVo.getTitle().replaceAll("/","|") , headers, questionInfoExcelVos, out);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            //原理就是将所有的数据一起写入，然后再关闭输入流。
            workbook.write(out);
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void test2() throws Exception {
        try {
            OutputStream out = new FileOutputStream("D:\\精选专题.xls");
            List<QuestionInfoExcelTypeVo> questionInfoExcelTypeVos = questionInfoMapper.selectIssueType2(1);
            String[] headers = { "id", "number", "issue", "image","excell_issue_name", "class_issue_name" ,"seque_issue_name","place_issue_name","question_type","class_sort"};
            HSSFWorkbook workbook = new HSSFWorkbook();
            for (int index=0; index < questionInfoExcelTypeVos.size();index++){
                try {
                    QuestionInfoExcelTypeVo questionInfoExcelTypeVo =  questionInfoExcelTypeVos.get(index);
                    List<QuestionInfoExcelVo> questionInfoExcelVos = questionInfoMapper.selecQuestionInfoExcel2(questionInfoExcelTypeVo.getTypeId());
                    exportExcel(workbook, index,questionInfoExcelTypeVo.getTitle().replaceAll("/","|") , headers, questionInfoExcelVos, out);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            //原理就是将所有的数据一起写入，然后再关闭输入流。
            workbook.write(out);
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void test3() throws Exception {
        try {
            OutputStream out = new FileOutputStream("D:\\地方专题.xls");
            List<QuestionInfoExcelTypeVo> questionInfoExcelTypeVos = questionInfoMapper.selectIssueType3(1);
            String[] headers = { "id", "number", "issue", "image","excell_issue_name", "class_issue_name" ,"seque_issue_name","place_issue_name","question_type","class_sort"};
            HSSFWorkbook workbook = new HSSFWorkbook();
            for (int index=0; index < questionInfoExcelTypeVos.size();index++){
                try {
                    QuestionInfoExcelTypeVo questionInfoExcelTypeVo =  questionInfoExcelTypeVos.get(index);
                    List<QuestionInfoExcelVo> questionInfoExcelVos = questionInfoMapper.selecQuestionInfoExcel3(questionInfoExcelTypeVo.getTypeId());
                    exportExcel(workbook, index,questionInfoExcelTypeVo.getTitle().replaceAll("/","|") , headers, questionInfoExcelVos, out);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            //原理就是将所有的数据一起写入，然后再关闭输入流。
            workbook.write(out);
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void test4() throws Exception {
        try {
            OutputStream out = new FileOutputStream("D:\\顺序练习.xls");
            List<QuestionInfoExcelTypeVo> questionInfoExcelTypeVos = questionInfoMapper.selectIssueType4(1);
            String[] headers = { "id", "number", "issue", "image","excell_issue_name", "class_issue_name" ,"seque_issue_name","place_issue_name","question_type","class_sort"};
            HSSFWorkbook workbook = new HSSFWorkbook();
            for (int index=0; index < questionInfoExcelTypeVos.size();index++){
                try {
                    QuestionInfoExcelTypeVo questionInfoExcelTypeVo =  questionInfoExcelTypeVos.get(index);
                    List<QuestionInfoExcelVo> questionInfoExcelVos = questionInfoMapper.selecQuestionInfoExcel4(questionInfoExcelTypeVo.getTypeId());
                    exportExcel(workbook, index,questionInfoExcelTypeVo.getTitle().replaceAll("/","|") , headers, questionInfoExcelVos, out);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            //原理就是将所有的数据一起写入，然后再关闭输入流。
            workbook.write(out);
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



    /**
     * @Title: exportExcel
     * @Description: 导出Excel的方法
     * @author: evan @ 2014-01-09
     * @param workbook
     * @param sheetNum (sheet的位置，0表示第一个表格中的第一个sheet)
     * @param sheetTitle  （sheet的名称）
     * @param headers    （表格的标题）
     * @param result   （表格的数据）
     * @param out  （输出流）
     * @throws Exception
     */
    public void exportExcel(HSSFWorkbook workbook, int sheetNum,
                            String sheetTitle, String[] headers, List<QuestionInfoExcelVo> result,
                            OutputStream out) throws Exception {
        // 生成一个表格
        HSSFSheet sheet = workbook.createSheet();
        workbook.setSheetName(sheetNum, sheetTitle);
        // 设置表格默认列宽度为20个字节
        sheet.setDefaultColumnWidth((short) 20);
        // 生成一个样式
        HSSFCellStyle style = workbook.createCellStyle();
        // 把字体应用到当前的样式
        // 指定当单元格内容显示不下时自动换行
        style.setWrapText(true);

        // 产生表格标题行
        HSSFRow row = sheet.createRow(0);
        for (int i = 0; i < headers.length; i++) {
            HSSFCell cell = row.createCell((short) i);

            cell.setCellStyle(style);
            HSSFRichTextString text = new HSSFRichTextString(headers[i]);
            cell.setCellValue(text.toString());
        }
        // 遍历集合数据，产生数据行
        if (result != null) {
            int index =1;
            for (QuestionInfoExcelVo vo : result) {
                row = sheet.createRow(index);
                HSSFCell cell = row.createCell(0);
                cell.setCellValue(vo.getId());
                cell = row.createCell(1);
                cell.setCellValue(vo.getNumber());
                cell = row.createCell(2);
                cell.setCellValue(vo.getIssue());
                cell = row.createCell(3);
                cell.setCellValue(vo.getImage());
                cell = row.createCell(4);
                cell.setCellValue(vo.getExcellIssueName());
                cell = row.createCell(5);
                cell.setCellValue(vo.getClassIssueName());
                cell = row.createCell(6);
                cell.setCellValue(vo.getSequeIssueName());
                cell = row.createCell(7);
                cell.setCellValue(vo.getPlaceIssueName());
                cell = row.createCell(8);
                cell.setCellValue(vo.getQuestionType());
                cell = row.createCell(9);
                cell.setCellValue("");
                index++;
            }
        }
    }
















}
