<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.securitycontrol.background.mapper.VehicleMapper">

    <!--获取列表-->
    <select id="getVehicleLists" resultType="com.securitycontrol.entity.background.vo.VehicleVo">
        SELECT  tc.id,tc.car_num as carNum,tc.user_name as userName,tc.phone ,tp.bid_code as bidCode
        FROM tb_car tc
        LEFT JOIN tb_project tp ON tp.bid_code = tc.bid_code
        where tc.del_flag = '0'
        <if test="userName !=null and userName!=''">
            AND (
            INSTR(tc.user_name,#{userName}) > 0
            )
        </if>

        <if test="bidCode != null and bidCode != ''">
            AND tp.bid_code = #{bidCode}
        </if>
    </select>

    <select id="getLists" resultType="com.securitycontrol.entity.background.vo.VehicleVo">
        SELECT thdv.source_type AS value
        FROM tb_bd_device tbd
        INNER JOIN tb_device_detail tdd ON tbd.device_id = tdd.device_id AND tdd.del_flag = 0
        INNER JOIN tb_his_device_value thdv ON tdd.id = thdv.attribute_id
        <if test="startDate!=null and startDate!='' and endDate!=null and endDate!=''">
            AND thdv.create_time BETWEEN CONCAT(#{startDate},' 00:00:00') AND CONCAT(#{endDate},' 23:59:59')
        </if>
        INNER JOIN tb_car tc ON tc.car_num = thdv.val AND tc.id = #{id}
        WHERE tbd.devic_type = #{typeCode} AND tdd.mode_name = #{modeName} AND tbd.del_flag = 0
    </select>

    <select id="getListsDto" resultType="com.securitycontrol.entity.background.dto.VehicleDto">
        SELECT accesss_time as accesssTime,car_num as carNum,
        case WHEN access_type= '0' then '入场' ELSE '出场' end accessType
        FROM `tb_car_access`
        WHERE car_id = #{id}
        <if test="date != null and date != ''">
            AND accesss_time BETWEEN CONCAT(#{startDate},' 00:00:00') AND CONCAT(#{endDate},' 23:59:59')
        </if>
    </select>
    <!--获取车辆入场 离场状态-->
    <select id="getCarAccessLists" resultType="java.lang.String">
        SELECT GROUP_CONCAT(CONCAT(tdd.mode_name,'@',thdv.val)) AS value
        FROM tb_his_device_value thdv
        INNER JOIN tb_device_detail tdd ON thdv.attribute_id = tdd.id AND tdd.del_flag = 0
        WHERE thdv.source_type = #{value}
        GROUP BY thdv.source_type
    </select>

    <!--新增/修改-->
    <insert id="addOrUpdateVehicle">
        <if test="type == 1">
            INSERT INTO tb_car
            <trim prefix="(" suffix=")" suffixOverrides=",">
                id,
                <if test="bidCode != null and bidCode!=''">bid_code,</if>
                <if test="carNum != null and carNum!=''">car_num,</if>
                <if test="userName != null and userName!=''">user_name,</if>
                <if test="phone != null and phone!=''">phone,</if>
                create_time,del_flag,
            </trim>
            <trim prefix="values (" suffix=")" suffixOverrides=",">
                #{id},
                <if test="bidCode != null and bidCode!=''">#{bidCode},</if>
                <if test="carNum != null and carNum!=''">#{carNum},</if>
                <if test="userName != null and userName!=''">#{userName},</if>
                <if test="phone != null and phone!=''">#{phone},</if>
                NOW(),0,
            </trim>
        </if>
        <if test="type == 2">
            UPDATE tb_car
            <set>
                <if test="bidCode != null and bidCode!=''">bid_code = #{bidCode},</if>
                <if test="carNum != null and carNum!=''">car_num = #{carNum},</if>
                <if test="userName != null and userName!=''">user_name = #{userName},</if>
                <if test="phone != null and phone!=''">phone = #{phone},</if>
                update_time = NOW()
            </set>
            WHERE id = #{id}
        </if>
    </insert>

    <!--删除-->
    <update id="delVehicleById">
        UPDATE tb_car set del_flag = '1' where id = #{id}
    </update>
</mapper>