package com.gci.boot.modules.gis.service.impl;

import cn.hutool.core.date.BetweenFormatter;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gci.boot.common.util.DateUtils;
import com.gci.boot.common.util.FormatUtil;
import com.gci.boot.modules.gis.entity.TbGisActiveSafetyAlarm;
import com.gci.boot.modules.gis.entity.TbGisSpeedingAlarm;
import com.gci.boot.modules.gis.mapper.TbGisSpeedingAlarmMapper;
import com.gci.boot.modules.gis.service.ITbGisSpeedingAlarmService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gci.boot.modules.statis.vo.AlarmTrendVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 超速记录表 服务实现类
 * </p>
 *
 * @author 2464
 * @since 2023-09-01
 */
@Service
public class TbGisSpeedingAlarmServiceImpl extends ServiceImpl<TbGisSpeedingAlarmMapper, TbGisSpeedingAlarm> implements ITbGisSpeedingAlarmService {

    @Autowired
    private TbGisSpeedingAlarmMapper tbGisSpeedingAlarmMapper;

    @Override
    public IPage<TbGisSpeedingAlarm> pageBuild(Page<TbGisSpeedingAlarm> page, LambdaQueryWrapper<TbGisSpeedingAlarm> lambdaQueryWrapper) {
        Page<TbGisSpeedingAlarm> list = tbGisSpeedingAlarmMapper.pageBuild(page, lambdaQueryWrapper);
        for (TbGisSpeedingAlarm record : list.getRecords()) {
            String duration = FormatUtil.formattedTimeDifference(record.getStartTime(), record.getEndTime());
            record.setDuration(duration);
        }
        return list;
    }

    @Override
    public List<TbGisSpeedingAlarm> findListByTime(Date startTime, Date endTime) {
        LambdaQueryWrapper<TbGisSpeedingAlarm> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.between(TbGisSpeedingAlarm::getStartTime, startTime, endTime);
        List<TbGisSpeedingAlarm> list = tbGisSpeedingAlarmMapper.selectList(queryWrapper);
        return list;
    }

    @Override
    public List<AlarmTrendVO> findTrendListByTime(Date startTime, Date endTime) {
        return this.tbGisSpeedingAlarmMapper.findTrendListByTime(DateUtils.formatTime(startTime), DateUtils.formatTime(endTime));
    }
}
