package com.hwrj.cloud.admin;


import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.fastjson.JSONObject;
import com.hwrj.cloud.admin.excelEntity.UmsCompanyInfoExcel;
import com.hwrj.cloud.admin.util.EasyExcelUtil;
import com.hwrj.cloud.common.util.FileUtil;
import netscape.javascript.JSObject;
import org.apache.http.entity.ContentType;
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.SpringJUnit4ClassRunner;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Collector;
import java.util.stream.Collectors;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = AdminApplication.class)
public class UploadTest {

    @Test
    public void readFile(){

        String filepath = "D:\\developerUtil\\code\\hwrj\\forest\\到数据";//D盘下的file文件夹的目录
        List<File> file = getFile(filepath);
        file.stream().forEach(item->{
            String path = item.getPath();
            if (path.endsWith(".xlsx")){
                System.out.println(path);
            }
        });


    }

    public void fileToMuti(File pdf){
        System.out.println(pdf.getPath());
        //        System.out.println("shojdsdfnldsjl");
        ExecutorService fixedThreadPool = Executors.newFixedThreadPool(10);
//        final int i = j;
        fixedThreadPool.execute(new Runnable() {
            @Override
            public void run() {
                try {
//                    File pdf = fileList[i];
                    System.out.println(pdf.getPath());
                    FileInputStream fileInputStream = null;
                    fileInputStream = new FileInputStream(pdf);
                    MultipartFile multipartFile = new MockMultipartFile(pdf.getName(), pdf.getName(),
                            ContentType.APPLICATION_OCTET_STREAM.toString(), fileInputStream);
//                        String url = ossFileUtils.upload(multipartFile.getOriginalFilename(), multipartFile);
                } catch (Exception e) {
                    System.out.println(e.getMessage());

                }
            }
        });

    }

    public List<File> getFile(String path){
        List<File> files = new ArrayList<>();
        File file = new File(path);//File类型可以是文件也可以是文件夹
        File[] fileList = file.listFiles();//将该目录下的所有文件放置在一个File类型的数组中
        for (int j = 0; j < fileList.length; j++) {
            File f = fileList[j];
            if (f.isFile()){
                files.add(f);
                if (f.getPath().endsWith(".xlsx")){
                    fileToMuti(f);
                }
            }else {
                getFile(f.getPath());
            }
        }
        return files;

    }

@Test
    public void ssh(){

        try {
            File pdf = new File("C:\\Users\\qb\\Desktop\\myDuizhang.xlsx");
            System.out.println(pdf.getPath());
            FileInputStream fileInputStream = null;
            fileInputStream = new FileInputStream(pdf);
            MultipartFile multipartFile = new MockMultipartFile(pdf.getName(), pdf.getName(),
                    ContentType.APPLICATION_OCTET_STREAM.toString(), fileInputStream);
//                        String url = ossFileUtils.upload(multipartFile.getOriginalFilename(), multipartFile);
//            List<Applay> excelData = getExcelData(multipartFile);
        } catch (Exception e) {
            System.out.println(e.getMessage());

        }
    }

    public List<Applay> getExcelData(MultipartFile file) throws IOException {
        List<Applay> listApplay = new ArrayList<>();
        List<Object> list = EasyExcelUtil.readExcel(file, new Applay(),1,1);
        if(list != null && list.size() > 0){
            for(Object o : list){
                Applay applay = (Applay) o;
                System.out.println(JSONObject.toJSONString(applay));
                listApplay.add(applay);
            }
        }
        List<ResultApplay> reList = new ArrayList<>();
        Map<String, List<Applay>> listMap = listApplay.stream().collect(Collectors.groupingBy(Applay::getTime));
        List<String> keys = new ArrayList<>(listMap.keySet());
        for (String key:keys){
            ResultApplay re = new ResultApplay();
            List<Applay> applays = listMap.get(key);
            Map<String, List<Applay>> collect = applays.stream().collect(Collectors.groupingBy(Applay::getType));
//            List<Applay> get = new ArrayList<>();
            List<Applay> put = new ArrayList<>();
            List<Applay> get = collect.get("借");
            if (collect.containsKey("贷")){
                put = collect.get("贷");
            }
            re.setTime(key);

            BigDecimal reduce = get.stream()
                    // 将user对象的age取出来map为Bigdecimal
                    .map(Applay::getMoney)
                    // 使用reduce()聚合函数,实现累加器
                    .reduce(BigDecimal.ZERO, BigDecimal::add);
            re.setGet(reduce);
            if (put != null && put.size()>0){
                BigDecimal p = put.stream()
                        // 将user对象的age取出来map为Bigdecimal
                        .map(Applay::getMoney)
                        // 使用reduce()聚合函数,实现累加器
                        .reduce(BigDecimal.ZERO, BigDecimal::add);
                re.setPut(p);
            }else {
                re.setPut(new BigDecimal(0));
            }
            BigDecimal subtract = reduce.subtract(re.getPut());
            re.setTotal(subtract);
            reList.add(re);
            System.out.println(JSONObject.toJSONString(re));
        }
        addExcel(reList);
        return listApplay;
    }

    public void addExcel(List<ResultApplay> data){
        String fileName = "C:\\Users\\qb\\Desktop\\" + "simpleWrite" + System.currentTimeMillis() + ".xlsx";

        // 这里 需要指定写用哪个class去写
        ExcelWriter excelWriter = null;
        try {
            excelWriter = EasyExcel.write(fileName, ResultApplay.class).build();
            WriteSheet writeSheet = EasyExcel.writerSheet("模板").build();
            excelWriter.write(data, writeSheet);
        } finally {
            // 千万别忘记finish 会帮忙关闭流
            if (excelWriter != null) {
                excelWriter.finish();
            }
        }

    }



}
