package com.ningdatech.carapi.driver.mapper;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ningdatech.carapi.driver.entity.DriverAbnormalBehaviorAnalysis;
import com.ningdatech.carapi.driver.model.po.AlarmWarnFilePO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * Mapper 接口
 * </p>
 *
 * @author PoffyZhang
 * @since 2022-11-07
 */
public interface BehaviourAnalysisMapper extends BaseMapper<DriverAbnormalBehaviorAnalysis> {

    String QUERY_SQL = "SELECT " +
            " (case when p.alarm_id is not null then p.alarm_id else v.alarm_id end) alarm_id, " +
            " (case when p.plate_no is not null then p.plate_no else v.plate_no end) car_plate, " +
            " c.name company_name,c.region_id,b.vehicle_type, " +
            " (case when p.alarm_type is not null then p.alarm_type else v.alarm_type end) alarm_type, " +
            " (case when p.capture_time is not null then p.capture_time else v.alarm_time end) alarm_time," +
            " (case when p.region_name is not null then p.region_name else v.region_name end) alarm_address, " +
            " p.url picture_url,v.url video_url,g.driver_id,g.driver_name " +
            " from nd_vehicle_base_info b " +
            " left join nd_company_info c on b.company_id = c.id " +
            " right join nd_alarm_data_picture p on b.car_plate = p.plate_no " +
            " right join nd_alarm_data_video v on p.alarm_id = v.alarm_id " +
            " left join nd_vehicle_get_on g on p.plate_no = g.car_plate or v.plate_no = g.car_plate " +
            " group by " +
            " (case when p.alarm_id is not null then p.alarm_id else v.alarm_id end), " +
            " (case when p.plate_no is not null then p.plate_no else v.plate_no end), " +
            " c.name,c.region_id,b.vehicle_type, " +
            " (case when p.alarm_type is not null then p.alarm_type else v.alarm_type end), " +
            " (case when p.capture_time is not null then p.capture_time else v.alarm_time end), " +
            " (case when p.region_name is not null then p.region_name else v.region_name end),p.url,v.url,g.driver_id,g.driver_name ";
    String WRAPPER_SQL = "SELECT * from ( " + QUERY_SQL + " ) AS q ${ew.customSqlSegment}";

    @Select(WRAPPER_SQL)
    Page<DriverAbnormalBehaviorAnalysis> pageBehiviour(Page page, @Param("ew") LambdaQueryWrapper<DriverAbnormalBehaviorAnalysis> wrapper);

    @Select(WRAPPER_SQL)
    DriverAbnormalBehaviorAnalysis getOneBehiviour(@Param("ew") LambdaQueryWrapper<DriverAbnormalBehaviorAnalysis> wrapper);

    List<DriverAbnormalBehaviorAnalysis> selectDriverAbnormalBehaviorAnalysisByCarPlate(@Param("index") long index);

    List<AlarmWarnFilePO> selectDriverAbnormalBehaviorAnalysisAppendixData(@Param("alarmMsgSnList") List<Integer> alarmMsgSnList);

    Long selectOneByAlarmMsgSn(@Param("alarmMsgSn") Integer alarmMsgSn);
}
