package com.gyee.generation;


import com.gyee.common.util.SortUtils;
import com.gyee.generation.model.vo.CsvVo;
import com.gyee.generation.model.vo.PointVo;
import com.gyee.generation.util.StringUtils;
import com.gyee.generation.util.realtimesource.math.LineUtil;
import com.opencsv.CSVReader;

import java.io.FileReader;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


/**
 * OpenCSV CSVReader Example, Read line by line
 *
 * @author pankaj
 */
public class CsvTest {

    private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

    public static void main(String[] args) throws Exception {
        CSVReader csvReader = new CSVReader(new FileReader("d:\\NG01_02.csv"));
        String[] titles = csvReader.readNext(); //读取到第一行 是小标题
        //        时间	风机功率	风机风速	风机明细状态	欠发状态

        List<CsvVo> vols = new ArrayList<>();
        CsvVo vo = null;
        while (true) {
            vo = new CsvVo();
            String[] content = csvReader.readNext();
            if (content == null) {
                break;
            }
            vo.setDate(StringUtils.toDate(content[0], "yyyy/MM/dd HH:mm"));
            vo.setPower(Double.valueOf(content[1]));
            vo.setSpeed(Double.valueOf(content[2]));
            vo.setStatus(Integer.valueOf(content[3]));
            vo.setLackpower(Integer.valueOf(content[4]));


            vols.add(vo);
        }

        if (!vols.isEmpty()) {
            //            List<CsvVo> vos=vols.stream().filter(i->i.power>0
            //                    && i.getSpeed()>0
            //                    && i.getLackpower()<=2
            //                    && i.getStatus()==2
            //            ) .sorted(Comparator.comparing(CsvVo::getSpeed)).collect(Collectors.toList());
            List<CsvVo> vos = vols.stream().filter(i -> i.power > 0
                    && i.getSpeed() > 2.5
                    && i.getLackpower() <= 2
                    && i.getStatus() == 2
            ).collect(Collectors.toList());
            SortUtils.sort(vos, "speed", SortUtils.ASC);


            List<PointVo> temp = LineUtil.buildLine(vos.stream().mapToDouble(i -> i.getSpeed()).toArray(), vos.stream().mapToDouble(i -> i.getPower()).toArray(), vos.size(), 20, 0.01);
            if (!temp.isEmpty()) {
                for (PointVo v : temp) {
                    System.out.println("x=" + v.getX() + ",y=" + v.getY());
                }
            }
        }
    }


}
