package com.ningdatech.carapi.car.monitor.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.car.monitor.entity.SecurityMonitorJoin;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**
 * <p>
 * Mapper 接口
 * </p>
 *
 * @author PoffyZhang
 * @since 2022-10-08
 */
public interface SecurityMonitorJoinMapper extends BaseMapper<SecurityMonitorJoin> {

    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 " +
            " 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 " +
            " 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 ";
    String WRAPPER_SQL = "SELECT * from ( " + QUERY_SQL + " ) AS q ${ew.customSqlSegment}";

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

    @Select(WRAPPER_SQL)
    SecurityMonitorJoin getOneJoin(@Param("ew") LambdaQueryWrapper<SecurityMonitorJoin> wrapper);
}
