<?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.xxfc.platform.vehicle.mapper.VehicleWarningMsgMapper">


    <resultMap type="com.xxfc.platform.vehicle.pojo.VehicleWarningMsgQueryVo" id="vehicleListMap">
        <result javaType="Integer" column="id" property="id"></result>
        <association column="rule_id" property="vehicleWarningRule" select="com.xxfc.platform.vehicle.mapper.VehicleWarningRuleMapper.selectByPrimaryKey" javaType="com.xxfc.platform.vehicle.entity.VehicleWarningRule"></association>
    </resultMap>

    <select id="getByPage" resultType="com.xxfc.platform.vehicle.pojo.VehicleWarningMsgQueryVo"
            parameterType="com.xxfc.platform.vehicle.pojo.QueryVehicleWarningMsgVo">
        select
        vwn.id, vwn.type, vwn.`status`, vwn.msg, vwn.rule_id, vwn.msg_key, vwn.vehicle_id, vwn.create_time, vwn.update_time, vwn.remark, v.code as vehicleCode, v.number_plate
        from vehicle_warning_msg vwn left join vehicle v on vwn.vehicle_id = v.id
        where
        vwn.`status` = 0
        <if test="vehicleCode != null">
            and v.code = #{vehicleCode}
        </if>
        <if test="numberPlate != null and numberPlate != '' ">
            and v.number_plate = #{numberPlate}
        </if>
        <if test="type != null">
            and vwn.type = #{type}
        </if>
        <if test="colorType != null">
            and vwn.color_type = #{colorType}
        </if>
        <if test="companyIds != null and companyIds.size > 0">
            and v.park_branch_company_id in
            <foreach collection="companyIds" item="id" open="(" separator="," close=")">
                #{id}
            </foreach>
        </if>
    </select>

    <select id="getMsgByVehicle" parameterType="java.util.Map" resultType="com.xxfc.platform.vehicle.entity.VehicleWarningMsg" >
        select id, type, status, msg, rule_id, msg_key, vehicle_id, create_time, update_time, remark from vehicle_warning_msg
        where vehicle_id = #{_parameter}
    </select>

    <select id="getAllByparam" parameterType="com.xxfc.platform.vehicle.pojo.dto.VehiclePlanDto" resultMap="vehicleListMap" >
        select w.*
        from vehicle_warning_msg w
        left join vehicle v on w.vehicle_id = v.id
        LEFT JOIN branch_company bc2 ON v.subordinate_branch = bc2.id
        <where>
            <if test="vehicleId != null">
                and w.vehicle_id = #{vehicleId}
            </if>
            <if test="startTime != null">
                and w.create_time between #{startTime} and #{endTime}
            </if>
            <if test="numberPlate != null and numberPlate != ''">
                and v.number_plate = #{numberPlate}
            </if>
            <if test="status != null">
                and v.status = #{status}
            </if>
            <if test="modelId != null and modelId != ''">
                and v.model_id = #{modelId}
            </if>
            <if test="code !=null">
                and v.code=#{code}
            </if>
            <if test="companyIds != null and companyIds.size > 0">
                and v.park_branch_company_id in
                <foreach collection="companyIds" item="id" open="(" separator="," close=")">
                    #{id}
                </foreach>
            </if>
        </where>
    </select>

    <select id="getByPages" resultType="com.xxfc.platform.vehicle.pojo.VehicleWarningMsgQueryVo"
            parameterType="com.xxfc.platform.vehicle.pojo.QueryVehicleWarningMsgVo">

        SELECT id as vehicleId,code as vehicleCode,number_plate as numberPlate,mileage_last_update as mileageLastUpdate,status,
        IFNULL(park_branch_company_id,expect_destination_branch_company_id) AS subordinateBranch,
        CONCAT('当前公里数为：',mileage_last_update,' 保养公里数:',maintenance_mileage,' 请尽快保养!') as msg
        ,1 as type,update_time as createTime  FROM  vehicle
        WHERE mileage_last_update>=maintenance_mileage
        <if test="vehicleCode != null">
            and code = #{vehicleCode}
        </if>
        <if test="numberPlate != null and numberPlate != '' ">
            and number_plate = #{numberPlate}
        </if>
    </select>

</mapper>