package com.usky.fire.service.impl;

import com.usky.common.core.exception.BusinessException;
import com.usky.common.core.influx.core.InfluxDbUtils;
import com.usky.fire.service.AlarmDetectorService;
import com.usky.fire.service.util.OnlineMethod;
import org.influxdb.dto.QueryResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 独立式无线探测器告警 服务实现类
 * </p>
 *
 * @author JCB
 * @since 2022-08-08
 */
@Service
public class AlarmDetectorServiceImpl implements AlarmDetectorService {

    @Autowired
    private InfluxDbUtils influxDbUtils;


    @Override
    public Map<String, Object> leftData(String field, String deviceCode) {
        Map<String, Object> maxMap = this.statusData(1, field, deviceCode);
        Map<String, Object> minMap = this.statusData(2, field, deviceCode);
        Map<String, Object> lastMap = this.statusData(3, field, deviceCode);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("max", maxMap.get("max"));
        map.put("maxTime", maxMap.get("time"));
        map.put("min", minMap.get("min"));
        map.put("minTime", minMap.get("time"));
        map.put("last", lastMap.get("last"));
        map.put("lastTime", lastMap.get("time"));
        return map;
    }


    @Override
    public Map<String, Object> statusData(Integer queryType, String field, String deviceCode) {
        String fields = "";
        switch (queryType) {
            case 1:
                fields = " MAX(" + field + "),";
                break;
            case 2:
                fields = " MIN(" + field + "),";
                break;
            case 3:
                fields = " LAST(" + field + "),";
                break;
            default:
                throw new BusinessException("查询数据类型未定义");
        }
        String sql = "SELECT " + fields + "time FROM sp_d" + deviceCode + " tz('Asia/Shanghai')";
        QueryResult query = influxDbUtils.query(sql);
        QueryResult.Result oneResult = query.getResults().get(0);
        Map<String, Object> map = new HashMap<String, Object>();
        if (oneResult.getSeries() != null) {
            List<List<Object>> valueList = oneResult.getSeries().stream().map(QueryResult.Series::getValues)
                    .collect(Collectors.toList()).get(0);
            List<String> columns = oneResult.getSeries().stream().map(QueryResult.Series::getColumns)
                    .collect(Collectors.toList()).get(0);
            if (valueList != null && valueList.size() > 0) {
                for (int i = 0; i < valueList.get(0).size(); i++) {
                    if (i == 0) {
                        map.put(columns.get(i), valueList.get(0).get(i) == null ? null : OnlineMethod.timeChange(valueList.get(0).get(i)));
                    } else {
                        map.put(columns.get(i), valueList.get(0).get(i) == null ? null : valueList.get(0).get(i));
                    }
                }
            } else {
                for (int i = 0; i < columns.size(); i++) {
                    map.put(columns.get(i), null);
                }
            }
        }
        return map;
    }

    @Override
    public Map<String, Object> statisticalChartData(String field, String deviceCode) {
        String sql = "SELECT " + field + " as A,time as b FROM sp_d" + deviceCode + " WHERE time >= now() - 7d order by time asc";
        QueryResult query = influxDbUtils.query(sql);
        QueryResult.Result oneResult = query.getResults().get(0);
        Map<String, Object> map = new HashMap<String, Object>();
        if (oneResult.getSeries() != null) {
            List<List<Object>> valueList = oneResult.getSeries().stream().map(QueryResult.Series::getValues)
                    .collect(Collectors.toList()).get(0);
            if (valueList != null && valueList.size() > 0) {
                List<Object> time = new ArrayList<>();
                List<Object> value = new ArrayList<>();
                for (int i = 0; i < valueList.size(); i++) {
                    time.add(OnlineMethod.timeChange(valueList.get(i).get(0)));
                    value.add(valueList.get(i).get(1));
                }
                map.put("time", time);
                map.put("value", value);
            }
        }
        return map;
    }
}
