<?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.ningdatech.carapi.car.vehicle.mapper.VehicleBaseInfoMapper">

    <resultMap id="resultMap" type="com.ningdatech.carapi.car.datascreen.model.po.OverspeedStatisticsPO">
        <result column="region_name" jdbcType="VARCHAR" property="regionName"/>
        <result column="times" jdbcType="BIGINT" property="times"/>
    </resultMap>

    <select id="getCarPlatesByCompanyIds" parameterType="java.util.List"
            resultType="com.ningdatech.carapi.car.vehicle.model.po.VehicleCompanyPO">
        select
            vb.status,vb.moving,vb.car_plate,vb.self_no,ci.id company_id,ci.name company_name,
            vb.vehicle_type,vb.operator_id
        from nd_vehicle_base_info vb
        left join nd_company_info ci on vb.company_id = ci.id
        where vb.deleted = 0 and vb.audit_state = 2 and ci.id in
        <foreach collection="companyIds" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
    </select>

    <select id="getCarPlatesByUseCompanyId"
            resultType="com.ningdatech.carapi.car.vehicle.model.po.VehicleCompanyPO">
        select
            vb.status,vb.moving,vb.car_plate,vb.self_no,ci.id company_id,ci.name company_name,
            vb.vehicle_type,vb.operator_id
        from nd_vehicle_base_info vb
        left join nd_company_info ci on vb.company_id = ci.id
        where vb.deleted = 0 and vb.audit_state = 2 and ci.id = #{companyId} or vb.use_company_id = #{companyId}
    </select>

    <select id="getZj"
            resultType="java.util.List" resultMap="resultMap">
        SELECT
            r2.name region_name
        FROM nd_vehicle_base_info b
                 left join nd_region r on b.region_id = r.id
                 left join nd_region r2 on r.parent_code = r2.region_code
                 left join nd_company_info c on b.company_id = c.id
        where r2.parent_code = 330000
        ${param.dataScopeSql}
        group by r2.name
    </select>

    <select id="getCity"
            resultType="java.util.List" resultMap="resultMap">
        SELECT
            r.name region_name
        FROM nd_vehicle_base_info b
        left join nd_region r on b.region_id = r.id
        left join nd_company_info c on b.company_id = c.id
        where r.parent_code = #{param.regionCode}
        <if test="param.regionIds != null and param.regionIds.size>0">
            and b.region_id in
            <foreach collection="param.regionIds" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        ${param.dataScopeSql}
        group by r.name
    </select>

    <select id="getDistrict"
            resultType="java.util.List" resultMap="resultMap">
        SELECT
            r.name region_name
        FROM nd_vehicle_base_info b
                 left join nd_region r on b.region_id = r.id
                 left join nd_company_info c on b.company_id = c.id
        where b.region_id = #{param.regionId}
            ${param.dataScopeSql}
        group by r.name
    </select>

    <select id="searchVehicleNum" resultType="com.ningdatech.carapi.homepage.entity.po.VehicleNumPO">
        select
            count(ew.car_plate) totalNum,
            sum(case when ew.status = 1 then 1 else 0 end) totalOnlineNum,
            sum(case when ew.status &lt;&gt; 1 then 1 else 0 end) totalOfflineNum
        from(select
        v.car_plate,v.status
        from nd_vehicle_base_info v
         left join nd_region r on v.region_id = r.id
         left join nd_company_info c on v.company_id = c.id
        where v.deleted = 0 and v.audit_state = 2
        <if test="param.regionIds != null and param.regionIds.size>0">
            and v.region_id in
            <foreach collection="param.regionIds" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="param.companyIds != null and param.companyIds.size>0">
            and c.id in
            <foreach collection="param.companyIds" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="param.companyIds != null and param.companyIds.size == 0">
            and c.id in (0)
        </if>
        <if test="param.operatorId != null">
            and v.operator_id = #{param.operatorId}
        </if>
        ${param.dataScopeSql}
        group by v.car_plate,v.status) ew
    </select>

    <update id="updateOfflineNotInCarPlates">
        update nd_vehicle_base_info
        set status = 0,moving = 0
        where car_plate not in (
            <foreach collection="cars" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        )
    </update>

    <update id="batchUpdate">
        update nd_vehicle_base_info
        set status = 0,moving = 0
    </update>
</mapper>
