package com.njcn.event.service.majornetwork.Impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.event.mapper.majornetwork.EventDetailMapper;
import com.njcn.event.pojo.po.EventDetail;
import com.njcn.event.pojo.po.RmpEventDetailPO;
import com.njcn.event.service.majornetwork.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;
import java.util.Objects;

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

    private final InfluxDbUtils influxDbUtils;


    @Override
    public List<EventDetail> getEventDetailData(String id, String startTime, String endTime) {
        //数据暂降查询
        List<RmpEventDetailPO> info = this.list(new LambdaQueryWrapper<RmpEventDetailPO>()
                .eq(RmpEventDetailPO::getMeasurementPointId, id)
                .ge(StrUtil.isNotBlank(startTime), RmpEventDetailPO::getStartTime,DateUtil.beginOfDay(DateUtil.parse(startTime)))
                .le(StrUtil.isNotBlank(endTime), RmpEventDetailPO::getStartTime, DateUtil.endOfDay(DateUtil.parse(endTime)))
        );
        return  BeanUtil.copyToList(info, EventDetail.class);

//        //组装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("'").append(" tz('Asia/Shanghai')");
//
//        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 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;
        //获取暂降事件
        QueryResult result = influxDbUtils.query(sql);
        InfluxDBResultMapper influxDBResultMapper = new InfluxDBResultMapper();
        if (!Objects.isNull(result)) {
            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, List<Integer> waveType) {
        List<RmpEventDetailPO> info = this.list(new LambdaQueryWrapper<RmpEventDetailPO>()
                .ne(RmpEventDetailPO::getMeasurementPointId, lineIndexes)
                .ge(StrUtil.isNotBlank(startTime), RmpEventDetailPO::getStartTime,DateUtil.beginOfDay(DateUtil.parse(startTime)))
                .le(StrUtil.isNotBlank(endTime), RmpEventDetailPO::getStartTime, DateUtil.endOfDay(DateUtil.parse(endTime)))
        );
        return  BeanUtil.copyToList(info, EventDetail.class);

//        //组装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("' ");
//            }
//        }
//        stringBuilder.append(") and (");
//        for (int i = 0; i < waveType.size(); i++) {
//            if (waveType.size() - i != 1) {
//                stringBuilder.append("wave_type ='").append(waveType.get(i)).append("' or ");
//            } else {
//                stringBuilder.append("wave_type ='").append(waveType.get(i)).append("' ");
//            }
//        }
//        stringBuilder.append(") order by time desc tz('Asia/Shanghai");
//        //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, List<String> waveType) {
        Page<RmpEventDetailPO> pageInfo = this.page(new Page<>(pageNum, pageSize), new LambdaQueryWrapper<RmpEventDetailPO>()
                .in(RmpEventDetailPO::getMeasurementPointId, lineIndexes)
                .ge(StrUtil.isNotBlank(startTime), RmpEventDetailPO::getStartTime,DateUtil.beginOfDay(DateUtil.parse(startTime)))
                .le(StrUtil.isNotBlank(endTime), RmpEventDetailPO::getStartTime, DateUtil.endOfDay(DateUtil.parse(endTime)))
        );
        return BeanUtil.copyToList(pageInfo.getRecords(), EventDetail.class);
    }
}
