var point = "NEM_SXJSFW.SX_XXXX_XXX_X_XX_XX_XXXX_FCC_CI134";
var startTs = 1706166626000;
var endTs = 1706168006283;
var interval = 30;
var result = getHistorySnap(point, startTs, endTs, interval);


function getHistorySnap(point, startTs, endTs, interval) {
    var result = adapter.getHistorySnap(point, startTs, endTs, interval);
    return result;
}

function createUri(url) {
    var URI = Java.type('java.net.URI');
    return URI.create("url");
}

function taosUri() {
    return createUri("http://127.0.0.1:8011");
}

function goldenUri() {
    return createUri("http://127.0.0.1:8011");
}

function getLatest(keys) {
    var result = adapter.getLatest(taosUri(), keys);
    return result;
}

var PointInfo = Java.type('com.ruoyi.ucp.entity.PointInfo');
var DateUtil = Java.type('cn.hutool.core.date.DateUtil');
var StationInfoDay = Java.type('com.ruoyi.ucp.entity.StationInfoDay');
var StationInfoHour = Java.type('com.ruoyi.ucp.entity.StationInfoHour');

function combinePointkeys(entitys) {
    var pointkeys = [];
    for (var i = 0; i < entitys.length; i++) {
        pointkeys.push(entitys[i].pointKey);
    }
    return pointkeys.join(',');
}

function midnight() {
    return DateUtil.beginOfDay(DateUtil.date()).toJdkDate();
}

function hourZero() {
    return DateUtil.beginOfHour(DateUtil.date()).toJdkDate();
}

function yesterday() {
    return DateUtil.yesterday().getTime();
}

function getThisTime() {
    return DateUtil.date().getTime();
}

console.log(entity)

var pointInfo = new PointInfo();
pointInfo.uniformCode = "AQTS";
var entity = pointService.getByEntity(pointInfo);
var keys = combinePointkeys(entity);
var latest = adapter.getLatest(taosUri(), keys);

var stationInfoDays = [];
for (var i = 0; i < entity.length; i++) {
    var stationInfoDay = new StationInfoDay();
    stationInfoDay.stationId = entity[i].stationId;
    stationInfoDay.recordDate = midnight();
    stationInfoDay.aqts = latest.get(entity[i].pointKey).doubleValue;
    stationInfoDays.push(stationInfoDay);
}
stationInfoDayService.saveBatch(stationInfoDays);

var pointInfo = new PointInfo();
pointInfo.uniformCode = "RFDL";
pointInfo.stationId = "all";
var entity = pointService.getByEntity(pointInfo);
var keys = combinePointkeys(entity);
var latest = adapter.getHistorySection(taosUri(), keys, getThisTime());

var stationInfoHours = [];
for (var i = 0; i < entity.length; i++) {
    var stationInfoHour = new StationInfoHour();
    stationInfoHour.stationId = entity[i].stationId;
    stationInfoHour.recordDate = hourZero();
    stationInfoHour.rfdl = latest.get(entity[i].pointKey).doubleValue;
    stationInfoHours.push(stationInfoHour);
}
stationInfoHourService.saveBatch(stationInfoHours);


var pointInfo = new PointInfo();
pointInfo.uniformCode = "Z-FXYG-JX";
pointInfo.lineId = "all";
var entity = pointService.getByEntity(pointInfo);
var keys = combinePointkeys(entity);
var latest2 = adapter.getHistorySection(goldenUri(), keys, getThisTime());
var latest1 = adapter.getHistorySection(goldenUri(), keys, yesterday());

var pi = new PointInfo();
pi.recordDate = yesterdayDate();
pi.lineId = "all";
var e = lineInfoDayService.getByEntity(pi);

var lineInfoDays = [];
for (var i = 0; i < entity.length; i++) {
    var lineInfoDay;
    if(e.size()==0){
        lineInfoDay = new LineInfoDay();
        lineInfoDay.stationId = entity[i].stationId;
        lineInfoDay.projectId = entity[i].projectId;
        lineInfoDay.lineId = entity[i].lineId;
        lineInfoDay.recordDate = yesterdayDate();
    }else {
        lineInfoDay = e.get(entity[i].lineId);
    }
    lineInfoDay.rfdl = (latest2.get(entity[i].pointKey).value-latest1.get(entity[i].pointKey).value)*entity[i].coef
    lineInfoDays.push(lineInfoDay);
}
lineInfoDayService.saveOrUpdateBatch(lineInfoDays);



var pointInfo = new PointInfo();
pointInfo.uniformCode = "Z-ZXYG-JX";
pointInfo.lineId = "all";
var entity = pointService.getByEntity(pointInfo);
var keys = combinePointkeys(entity);
var latest2 = adapter.getHistorySection(goldenUri(), keys, getThisTime());
var latest1 = adapter.getHistorySection(goldenUri(), keys, DateUtil.offsetMinute(DateUtil.yesterday(), 3).getTime());

var pi = new PointInfo();
pi.recordDate = yesterdayDate();
pi.lineId = "all";
var e = lineInfoDayService.getByEntity(pi);

var lineInfoDays = [];
for (var i = 0; i < entity.length; i++) {
    var lineInfoDay;
    if(e.size()==0){
        lineInfoDay = new LineInfoDay();
        lineInfoDay.stationId = entity[i].stationId;
        lineInfoDay.projectId = entity[i].projectId;
        lineInfoDay.lineId = entity[i].lineId;
        lineInfoDay.recordDate = yesterdayDate();
    }else {
        lineInfoDay = e.get(entity[i].lineId);
    }
    lineInfoDay.rfdl = (latest2.get(entity[i].pointKey).value-latest1.get(entity[i].pointKey).value)*entity[i].coef
    lineInfoDays.push(lineInfoDay);
}
lineInfoDayService.saveOrUpdateBatch(lineInfoDays);


//Z-ZXYG-CX	swdl
// Z-FXYG-CX	gwdl
// Z-ZXYG-ZYB	cydl

var swdlPi = new PointInfo();
swdlPi.uniformCode = "Z-ZXYG-CX";
var gwdlPi = new PointInfo();
gwdlPi.uniformCode = "Z-FXYG-CX";
var cydlPi = new PointInfo();
cydlPi.uniformCode = "Z-ZXYG-ZYB";
var swdlEt = pointService.getByEntity(swdlPi);
var gwdlEt = pointService.getByEntity(gwdlPi);
var gwdlMap = toMap(gwdlEt, "station");
var cydlEt = pointService.getByEntity(cydlPi);
var cydlMap = toMap(cydlEt, "station");
var swdlK = combinePointkeys(swdlEt);
var gwdlK = combinePointkeys(gwdlEt);
var cydlK = combinePointkeys(cydlEt);
var swdlL2 = adapter.getHistorySection(goldenUri(), swdlK, getThisTime());
var swdlL1 = adapter.getHistorySection(goldenUri(), swdlK, DateUtil.offsetMinute(DateUtil.yesterday(), 3).getTime());
var gwdlL2 = adapter.getHistorySection(goldenUri(), gwdlK, getThisTime());
var gwdlL1 = adapter.getHistorySection(goldenUri(), gwdlK, DateUtil.offsetMinute(DateUtil.yesterday(), 3).getTime());
var cydlL2 = adapter.getHistorySection(goldenUri(), cydlK, getThisTime());
var cydlL1 = adapter.getHistorySection(goldenUri(), cydlK, DateUtil.offsetMinute(DateUtil.yesterday(), 3).getTime());

var pi = new PointInfo();
pi.recordDate = yesterdayDate();
var entity = stationInfoDayService.getByEntity(pi);

var stationInfoDays = [];
for (var i = 0; i < swdlEt.length; i++) {
    var stationInfoDay;
    if(entity.size()==0){
        stationInfoDay = new StationInfoDay();
        stationInfoDay.stationId = swdlEt[i].stationId;
        stationInfoDay.recordDate = yesterdayDate();
    }else {
        stationInfoDay = entity.get(swdlEt[i].stationId);
    }
    stationInfoDay.swdl = (swdlL2.get(swdlEt[i].pointKey).value-swdlL1.get(swdlEt[i].pointKey).value)*swdlEt[i].coef
    var ge = gwdlMap[swdlEt[i].stationId];
    stationInfoDay.gwdl = (gwdlL2.get(ge.pointKey).value-gwdlL1.get(ge.pointKey).value)*ge.coef
    var ce = cydlMap[swdlEt[i].stationId];
    stationInfoDay.cydl = (cydlL2.get(ce.pointKey).value-cydlL1.get(ce.pointKey).value)*ce.coef
    stationInfoDays.push(stationInfoDay);
}
stationInfoDayService.saveOrUpdateBatch(stationInfoDays);

//lineId;
//
// projectId;
//
// stationId;
//
// turbineId;
function toMap(pointInfo,key) {
    var map = {};
    for (var i = 0; i < pointInfo.length; i++) {
        var m = pointInfo[i];
        if("line"==key){
            map[pointInfo.lineId] = m;
        }else if("project"==key){
            map[pointInfo.projectId] = m;
        }else if("station"==key){
            map[pointInfo.stationId] = m;
        }else if("turbine"==key){
            map[pointInfo.turbineId] = m;
        }else {
            map[pointInfo.uniformCode] = m;
        }
    }
    return map;
}
