package com.ruoyi;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.text.csv.CsvUtil;
import cn.hutool.core.text.csv.CsvWriter;
import cn.hutool.core.util.CharsetUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.ucp.entity.*;
import com.ruoyi.ucp.feign.AdapterApi;
import com.ruoyi.ucp.service.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import java.net.URI;
import java.util.*;

@SpringBootTest
@RunWith(SpringRunner.class)
public class GetDataTest {

    @Resource
    private IPointInfoService pointInfoService;
    @Resource
    private AdapterApi adapterApi;
    @Test
    public void test() {
        DateTime start = DateUtil.parse("2023-06-01");
        DateTime end = DateUtil.parse("2024-05-31");

        String[] names = {"AI004","AI005","AI006","AI007","AI008","AI009","AI073","AI037","AI039","AI038","AI047","AI048","AI056","AI057","AI060","DI022","AI066","AI067","AI069","AI072","AI074","DI025","DI080","AI082","AI084","AI083","AI065"};
        QueryWrapper<PointInfo> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(PointInfo::getInstitutionType, "turbine")
//                .eq(PointInfo::getStationId,"GJNY_SXGS_LJS_FDC_STA")
                .eq(PointInfo::getTurbineId,"GJNY_SXGS_LJS_F_WT_0001_EQ")
                .in(PointInfo::getUniformCode,names);
        List<PointInfo> list = pointInfoService.list(wrapper);
        for (PointInfo pi : list) {
            CsvWriter writer = null;
            if(pi.getPointKey().equals("INITIAL")){
                //指定路径和编码
                writer = CsvUtil.getWriter("D:/数据/"+pi.getTurbineId()+"/"+pi.getName()+"无测点"+
                        ".csv", CharsetUtil.CHARSET_GBK);
                continue;
            }
            List<PointData> s = adapterApi.getHistorySnap(URI.create("http://172.16.12.103:8011/ts"), pi.getPointKey(),
                    start.getTime(), end.getTime(), 60);

                //指定路径和编码
                writer = CsvUtil.getWriter("D:/数据/"+pi.getTurbineId()+"/"+pi.getName()+
                        ".csv", CharsetUtil.CHARSET_GBK);
                //按行写出
                writer.write(new String[] {"时间","时间戳","对齐时间戳", "值"});

                long j=start.getTime();
            for (int i = 0; i <s.size(); i++) {
                String[] r = new String[4];
                r[0]=DateUtil.formatDateTime(new Date(j));
                r[1]=Long.toString(s.get(i).getTs());
                r[2]=Long.toString(j);
                r[3]=Double.toString(s.get(i).getValue());
                writer.write(r);
                j+=60*1000;
            }

        }
    }
}
