package com.njcn.event.service.Impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.event.mapper.EventDetailMapper;
import com.njcn.event.pojo.po.EventDetail;
import com.njcn.event.service.EventDetailService;
import com.njcn.influxdb.utils.InfluxDbUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.influxdb.dto.QueryResult;
import org.influxdb.impl.InfluxDBResultMapper;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author denghuajun
 * @version 1.0.0
 * @date 2022/3/16
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class EventDetailServiceImpl extends ServiceImpl<EventDetailMapper, EventDetail> implements EventDetailService {

    private final InfluxDbUtils influxDbUtils;

    @Override
    public List<EventDetail> getEventDetailData(String id, String startTime, String endTime) {
        //组装sql语句
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("time >= '").append(DateUtil.beginOfDay(DateUtil.parse(startTime))).append("' and ").append("time <= '").append(DateUtil.endOfDay(DateUtil.parse(endTime))).append("' and ");
        //sql语句
        stringBuilder.append("line_id ='").append(id).append("'");

        String sql = "select * from pqs_eventdetail where " + stringBuilder.toString();
        System.out.println("sql>>>>>>>>>>>>>>>>>>>>>>>>"+sql);
        //获取暂降事件
        QueryResult result = influxDbUtils.query(sql);
        InfluxDBResultMapper influxDBResultMapper = new InfluxDBResultMapper();
        List<EventDetail> eventDetailList = influxDBResultMapper.toPOJO(result, EventDetail.class);
        return eventDetailList;
    }

    @Override
    public EventDetail getEventDetailByTime(String id, String time) {
        EventDetail eventDetail = new EventDetail();
        //组装sql语句
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("time >= '").append(time).append("' and ").append("line_id ='").append(id).append("' limit 1 tz('Asia/Shanghai')");

        String sql = "select * from pqs_eventdetail where " + stringBuilder;
        System.out.println("sql>>>>>>>>>>>>>>>>>>>>>>>>"+sql);
        //获取暂降事件
        QueryResult result = influxDbUtils.query(sql);
        InfluxDBResultMapper influxDBResultMapper = new InfluxDBResultMapper();
        List<EventDetail> detailList = influxDBResultMapper.toPOJO(result, EventDetail.class);
        if (!CollectionUtils.isEmpty(detailList)) {
            eventDetail = detailList.get(0);
        }
        return eventDetail;
    }

    @Override
    public List<EventDetail> getEventDetail(List<String> lineIndexes, String startTime, String endTime) {
        //组装sql语句
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("time >= '").append(DateUtil.beginOfDay(DateUtil.parse(startTime))).append("' and ").append("time <= '").append(DateUtil.endOfDay(DateUtil.parse(endTime))).append("' and ");
        for (int i = 0; i < lineIndexes.size(); i++) {
            if (lineIndexes.size() - i != 1) {
                stringBuilder.append("line_id ='").append(lineIndexes.get(i)).append("' or ");
            } else {
                stringBuilder.append("line_id ='").append(lineIndexes.get(i)).append("' ");
            }
        }
        //sql语句
        String sql = "SELECT * FROM pqs_eventdetail WHERE " + stringBuilder;
        //结果集
        QueryResult result = influxDbUtils.query(sql);
        //结果集映射到对象中
        InfluxDBResultMapper influxDBResultMapper = new InfluxDBResultMapper();
        List<EventDetail> eventDetailList = influxDBResultMapper.toPOJO(result, EventDetail.class);


        return eventDetailList;
    }

    @Override
    public List<EventDetail> getEventDetailLimit(List<String> lineIndexes, String startTime, String endTime, Integer pageSize, Integer pageNum) {
        //组装sql语句
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("time >= '").append(DateUtil.beginOfDay(DateUtil.parse(startTime))).append("' and ").append("time <= '").append(DateUtil.endOfDay(DateUtil.parse(endTime))).append("' and ");
        stringBuilder.append("(");
        for (int i = 0; i < lineIndexes.size(); i++) {
            if (lineIndexes.size() - i != 1) {
                stringBuilder.append("line_id ='").append(lineIndexes.get(i)).append("' or ");
            } else {
                stringBuilder.append("line_id ='").append(lineIndexes.get(i)).append("') ");
            }
        }
        int i = (pageNum - 1)*pageSize;
        stringBuilder.append("LIMIT ").append(pageSize).append(" OFFSET ").append(i);
        //sql语句
        String sql = "SELECT * FROM pqs_eventdetail WHERE " + stringBuilder;
        //结果集
        QueryResult result = influxDbUtils.query(sql);
        //结果集映射到对象中
        InfluxDBResultMapper influxDBResultMapper = new InfluxDBResultMapper();
        List<EventDetail> eventDetailList = influxDBResultMapper.toPOJO(result, EventDetail.class);


        return eventDetailList;
    }

}
