<?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.muyu.mapper.SysCarMapper">
    <insert id="addSysCar">
        INSERT INTO `four`.`sys_car`
        ( `car_vin`, `car_type_id`, `state`, `car_motor_manufacturer`, `car_motor_model`,
          `car_battery_manufacturer`, `car_battery_model`, `strategy_id`,`group_id`, `create_by`, `create_time`, `update_by`, `update_time`, `remark`,)
        VALUES (#{carVin}, #{carTypeId}, '1', #{carMotorManufacturer}, #{carMotorModel},
                #{carBatteryManufacturer}, #{carBatteryModel}, #{strategyId},#{groupId},#{createBy}, #{createTime}, #{updateBy}, #{updateTime}, #{remark})
    </insert>
    <update id="updSysCarById">
        UPDATE `four`.`sys_car`
        SET `car_vin` = #{carVin},
            `car_type_id` = #{carTypeId},
            `state` = #{state},
            `car_motor_manufacturer` = #{carMotorManufacturer},
            `car_motor_model` = #{carMotorModel},
            `car_battery_manufacturer` = #{carBatteryManufacturer},
            `car_battery_model` = #{carBatteryModel},
            `strategy_id` = #{strategyId},
            `group_id`=#{groupId}
            `create_by` = #{createBy},
            `create_time` = #{createTime},
            `update_by` = #{updateBy},
            `update_time` = #{updateTime},
            `remark` = #{remark} WHERE `id` = #{id}
    </update>
    <select id="selectSysCarVoList" resultType="com.muyu.domain.resp.SysCarVo">
        SELECT * ,car_type.type_name,warn_strategy.strategy_name,electronic_fence_group.group_name
        FROM `sys_car`
        LEFT JOIN car_type ON sys_car.car_type_id=car_type.id
        LEFT JOIN warn_strategy ON sys_car.strategy_id=warn_strategy.id
        LEFT JOIN electronic_fence_group ON sys_car.group_id=electronic_fence_group.id
        <where>
            <if test="carVin!=null and carVin!=''">
                sys_car.car_vin=#{carVin}
            </if>
            <if test="state!=null and state!=''">
              and  sys_car.state=#{state}
            </if>
            <if test="carMotorManufacturer!=null and carMotorManufacturer!=''">
             and  sys_car.car_motor_manufacturer=#{carMotorManufacturer}
            </if>
            <if test="carBatteryManufacturer!=null and carBatteryManufacturer!=''">
                and  sys_car.car_battery_manufacturer=#{carBatteryManufacturer}
            </if>
            <if test="carMotorModel!=null and carMotorModel!=''">
                and  sys_car.car_motor_model=#{carMotorModel}
            </if>
            <if test="carBatteryModel!=null and carBatteryModel!=''">
                and  sys_car.car_battery_model=#{carBatteryModel}
            </if>
        </where>
    </select>


    <select id="selectSysCarVoById" resultType="com.muyu.domain.resp.SysCarVo">
        SELECT * ,car_type.type_name,warn_strategy.strategy_name,electronic_fence_group.group_name
        FROM `sys_car`
                 LEFT JOIN car_type ON sys_car.car_type_id=car_type.id
                 LEFT JOIN warn_strategy ON sys_car.strategy_id=warn_strategy.id
                 LEFT JOIN electronic_fence_group ON sys_car.group_id=electronic_fence_group.id
        where sys_car.id=#{id}
    </select>
    <select id="findFenceByCarVin" resultType="com.muyu.domain.resp.SysCarFaultLogVo">
        SELECT
            sys_car_fault_log.*,
            sys_car_fault.*
        FROM
            sys_car_fault_log
                LEFT JOIN sys_car_fault ON sys_car_fault_log.sys_car_fault_id = sys_car_fault.id
        WHERE
            sys_car_fault_log.vin = #{carVin};
    </select>
</mapper>
