package com.qmth.distributed.print;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.qmth.boot.tools.io.IOUtils;
import com.qmth.teachcloud.common.bean.dto.excel.BasicCourseImportDto;
import com.qmth.teachcloud.common.enums.ExceptionResultEnum;
import com.qmth.teachcloud.common.util.ExcelUtil;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.*;

/**
 * @Description: 导入导出工具类
 * @Author: CaoZixuan
 * @Date: 2021-09-27
 */
@SpringBootTest
@RunWith(SpringRunner.class)
public class ImportTest {

    /**
     * 整理并获得新的课程导入excel - 老的excel中课程编码是重复的！
     * @throws Exception
     */
    @Test
    public void combineClazzName() throws Exception {
        File file = new File("E:" + File.separator + "导入导出" + File.separator + "课程导入模板.xlsx");

        FileInputStream input = new FileInputStream(file);

        MultipartFile multipartFile =new MockMultipartFile("file", file.getName(), "text/plain", IOUtils.toByteArray(input));
        List<LinkedMultiValueMap<Integer, Object>> finalList = ExcelUtil.excelReader(multipartFile.getInputStream(), Lists.newArrayList(BasicCourseImportDto.class), (finalExcelList, finalColumnNameList, finalExcelErrorList) -> {
            if (finalExcelErrorList.size() > 0) {
                throw ExceptionResultEnum.ERROR.exception(JSONObject.toJSONString(finalExcelErrorList));
            }
            return finalExcelList;
        });

        Map<String,BasicCourseImportDto> courseMap = new HashMap<>();
        if (Objects.nonNull(finalList) && finalList.size() > 0) {
            for (int i = 0; i < finalList.size(); i++) {
                LinkedMultiValueMap<Integer, Object> map = finalList.get(i);
                List<Object> courseList = map.get(i);
                for (int y = 0; y < Objects.requireNonNull(courseList).size(); y++) {
                    if (courseList.get(y) instanceof BasicCourseImportDto) {
                        BasicCourseImportDto basicCourseImportDto = (BasicCourseImportDto) courseList.get(y);
                        String courseCode = basicCourseImportDto.getCourseCode();
                        String courseName = basicCourseImportDto.getCourseName();
                        String teachingRoomName = basicCourseImportDto.getTeachingRoomName();
                        String clazz = basicCourseImportDto.getClazz();

                        if (courseMap.containsKey(courseCode)){
                            BasicCourseImportDto old = courseMap.get(courseCode);
                            String oldClazz = old.getClazz();
                            oldClazz = oldClazz + "," + clazz;
                            old.setClazz(oldClazz);
                        } else {
                            courseMap.put(courseCode,basicCourseImportDto);
                        }

                    }
                }
            }
        }
        List<BasicCourseImportDto> basicCourseImportDtoList = new ArrayList<>();
        for (String courseCode : courseMap.keySet()) {
            basicCourseImportDtoList.add(courseMap.get(courseCode));
        }
        System.out.println(JSON.toJSONString(basicCourseImportDtoList));

        File outfile = new File("E:" + File.separator + "导入导出" + File.separator + "新课程导入模板.xlsx");
        if (!outfile.getParentFile().exists()){ //文件目录不存在
            outfile.getParentFile().mkdirs(); //创建父目录
        }

        //2.通过子类实例化
        OutputStream outputStream = new FileOutputStream(outfile,true);
        ExcelUtil.excelMake(BasicCourseImportDto.class, basicCourseImportDtoList, outputStream);
        outputStream.close();
    }
}
