package com.dcs.equipment.domain.vo;

import com.dcs.equipment.domain.DataTwinInfluxDBItem;
import com.influxdb.query.FluxRecord;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: wangpx
 * @date: 2025-05-07 09:39
 */
@Data
public class DataTwinInfluxDBVO {
    @ApiModelProperty(value = "字段名")
    private String name;
    @ApiModelProperty(value = "字段数据")
    private List<DataTwinInfluxDBItem> items;
    public void fillItems(List<FluxRecord> records) {
        if (items == null) {
            items = new ArrayList<>();
        }
        for (FluxRecord record : records) {
            DataTwinInfluxDBItem item = new DataTwinInfluxDBItem();
            item.setTime(formatTime(record.getTime().toString()));
            item.setValue(record.getValue().toString());
            items.add(item);
        }
    }

    private String formatTime(String time) {
        // 解析为 ZonedDateTime（UTC）
        ZonedDateTime utcZdt = ZonedDateTime.parse(time);

        // 转换为中国时区
        ZonedDateTime chinaZdt = utcZdt.withZoneSameInstant(ZoneId.of("Asia/Shanghai"));

        int month = chinaZdt.getMonthValue();
        int day = chinaZdt.getDayOfMonth();
        int hour = chinaZdt.getHour();
        String formatted = month + "/" + day + " " + hour + "时";
//        String formatted = String.format("%02d/%02d %02d时", month, day, hour);
        // 格式化为 “月日时”
//        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd HH时");
//        String formatted = chinaZdt.format(formatter);
        return formatted;
    }
}
