<?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.enterpise.mapper.FaultCodeMapper">
    <resultMap id="FaultCodeVoResult" type="com.muyu.domain.FaultCode">
        <id property="faultcodeId" column="faultcode_id"></id>
        <result property="messageTypeId" column="message_type_id"></result>
        <result property="faultcodeNumber" column="faultcode_number"></result>
        <result property="faultGroup" column="fault_group"></result>
        <result property="faultBit" column="fault_bit"></result>
        <result property="faultValue" column="fault_value"></result>
        <result property="isWarning" column="is_warning"></result>
        <result property="faulttypeName" column="faulttype_name"></result>
        <result property="messageTypeName" column="message_type_name"></result>
        <result property="messageTypeCode" column="message_type_code"></result>
        <result property="messageTypeBelongs" column="message_type_belongs"></result>
    </resultMap>

    <sql id="selectfaultcodelist" >
        SELECT
            car_faultcode.faultcode_id,
            car_faultcode.message_type_id,
            car_fault_label.message_type_code,
            car_faultcode.faultcode_number,
            car_faultcode.fault_group,
            car_faultcode.fault_bit,
            car_faultcode.fault_value,
            car_fault_label.message_type_name,
            car_faultcode.is_warning
        FROM
            car_faultcode
                LEFT JOIN car_fault_label ON car_faultcode.message_type_id = car_fault_label.message_type_id
    </sql>



    <!--故障码列表展示-->
    <select id="selectfaultcodelist"  resultMap="FaultCodeVoResult">
        <include refid="selectfaultcodelist"></include>
        <where>
            <if test="faultcodeNumber!=null and faultcodeNumber!=''">
                and car_faultcode.faultcode_number=#{faultcodeNumber}
            </if>
            <if test="faultBit!=null and faultBit!=''">
                and car_faultcode.fault_bit=#{faultBit}
            </if>
        </where>
        limit #{pageNum},#{pageSize}
    </select>

    <!--新增故障码-->
    <select id="selectfaultName" resultType="com.muyu.domain.FaultLabel">
        select * from car_fault_label where message_type_name=#{messageTypeName}
    </select>
    <select id="selectfaultCode" resultType="com.muyu.domain.FaultCode">
        select * from car_faultcode where faultcode_number=#{faultcodeNumber}
    </select>
    <insert id="insertfaultlabel" keyProperty="messageTypeId" useGeneratedKeys="true">
        INSERT INTO `eight`.`car_fault_label`
    (`message_type_id`, `message_type_code`, `message_type_name`, `message_type_belongs`) VALUES
        (0, #{messageTypeCode}, #{messageTypeName}, '车辆基础');
    </insert>
    <insert id="insertfaultcode">
        INSERT INTO `eight`.`car_faultcode`
    (`faultcode_id`, `message_type_id`, `faultcode_number`, `fault_group`, `fault_bit`, `fault_value`, `is_warning`) VALUES
    (0, #{messageTypeId}, #{faultcodeNumber}, #{faultGroup}, #{faultBit}, #{faultValue}, #{isWarning});

    </insert>


    <!--修改故障码-->
    <update id="updfaultlabel">
      UPDATE `eight`.`car_fault_label`
        SET `message_type_code` = #{messageTypeCode},
            `message_type_name` = #{messageTypeName},
            `message_type_belongs` =#{messageTypeBelongs}
        WHERE `message_type_id` = #{messageTypeId};
    </update>
    <update id="updfaultcode">
        UPDATE `eight`.`car_faultcode`
        SET `message_type_id` = #{messageTypeId},
            `faultcode_number` = #{faultcodeNumber},
            `fault_group` = #{faultGroup},
            `fault_bit` = #{faultBit},
            `fault_value` = #{faultValue},
            `is_warning` = #{isWarning}
        WHERE `faultcode_id` = #{faultcodeId};
    </update>

    <!--删除故障码-->
    <select id="findByfaultcodeId" resultType="com.muyu.domain.FaultCode">
        select * from car_faultcode where faultcode_id=#{faultcodeId}
    </select>
    <delete id="delfaultcode">
        delete from car_faultcode where message_type_id=#{messageTypeId}
    </delete>
    <delete id="delfaultlabel">
        delete from car_fault_label where message_type_id=#{messageTypeId}
    </delete>
</mapper>
