<?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.ktg.iscs.mapper.IsKeyMapper">

    <resultMap type="IsKey" id="IsKeyResult">
        <result property="keyId"    column="key_id"    />
        <result property="keyCode"    column="key_code"    />
        <result property="keyName"    column="key_name"    />
        <result property="hardwareId"    column="hardware_id"    />
        <result property="keyNfc"    column="key_nfc"    />
        <result property="macAddress"    column="mac_address"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="remark"    column="remark"    />
        <result property="keySpec"    column="key_spec"    />
        <result property="exStatus"    column="ex_status"    />
        <result property="exRemark"    column="ex_remark"    />
    </resultMap>

    <sql id="selectIsKeyVo">
        select * from is_key
    </sql>

    <select id="selectIsKeyList" parameterType="IsKey" resultMap="IsKeyResult">
        <include refid="selectIsKeyVo"/>
        <where>
            <if test="keyCode != null  and keyCode != ''"> and key_code = #{keyCode}</if>
            <if test="keyName != null  and keyName != ''"> and key_name like concat('%', #{keyName}, '%')</if>
            <if test="hardwareId != null "> and hardware_id = #{hardwareId}</if>
            <if test="keyNfc != null  and keyNfc != ''"> and key_nfc = #{keyNfc}</if>
            <if test="macAddress != null  and macAddress != ''"> and mac_address = #{macAddress}</if>
        </where>
    </select>

    <select id="selectIsKeyByKeyId" parameterType="Long" resultMap="IsKeyResult">
        <include refid="selectIsKeyVo"/>
        where key_id = #{keyId}
    </select>

    <insert id="insertIsKey" parameterType="IsKey" useGeneratedKeys="true" keyProperty="keyId">
        insert into is_key
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="keyCode != null and keyCode != ''">key_code,</if>
            <if test="keyName != null and keyName != ''">key_name,</if>
            <if test="hardwareId != null">hardware_id,</if>
            <if test="keyNfc != null">key_nfc,</if>
            <if test="macAddress != null">mac_address,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="remark != null and remark != ''">remark,</if>
            <if test="keySpec != null and keySpec != ''">key_spec,</if>
            <if test="exStatus != null and exStatus != ''">ex_status,</if>
            <if test="exRemark != null and exRemark != ''">ex_remark,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="keyCode != null and keyCode != ''">#{keyCode},</if>
            <if test="keyName != null and keyName != ''">#{keyName},</if>
            <if test="hardwareId != null">#{hardwareId},</if>
            <if test="keyNfc != null">#{keyNfc},</if>
            <if test="macAddress != null">#{macAddress},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="remark != null and remark != ''">#{remark},</if>
            <if test="keySpec != null and keySpec != ''">#{keySpec},</if>
            <if test="exStatus != null and exStatus != ''">#{exStatus},</if>
            <if test="exRemark != null and exRemark != ''">#{exRemark},</if>
         </trim>
    </insert>

    <update id="updateIsKey" parameterType="IsKey">
        update is_key
        <trim prefix="SET" suffixOverrides=",">
            <if test="keyCode != null and keyCode != ''">key_code = #{keyCode},</if>
            <if test="keyName != null and keyName != ''">key_name = #{keyName},</if>
            <if test="hardwareId != null">hardware_id = #{hardwareId},</if>
            <if test="keyNfc != null">key_nfc = #{keyNfc},</if>
            <if test="macAddress != null">mac_address = #{macAddress},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="remark != null and remark != ''">remark = #{remark},</if>
            <if test="keySpec != null and keySpec != ''">key_spec = #{keySpec},</if>
            <if test="exStatus != null and exStatus != ''">ex_status = #{exStatus},</if>
            <if test="exRemark != null and exRemark != ''">ex_remark = #{exRemark},</if>
        </trim>
        where key_id = #{keyId}
    </update>

    <delete id="deleteIsKeyByKeyId" parameterType="Long">
        delete from is_key where key_id = #{keyId}
    </delete>

    <delete id="deleteIsKeyByKeyIds" parameterType="String">
        delete from is_key where key_id in
        <foreach item="keyId" collection="array" open="(" separator="," close=")">
            #{keyId}
        </foreach>
    </delete>

    <select id="getIsKeyPage" resultType="com.ktg.iscs.domain.vo.key.IsKeyVO">
        SELECT
        k.*,
        h.hardware_name
        FROM
        is_key k
        LEFT JOIN is_hardware h ON k.hardware_id = h.id
        <where>
            <if test="isKey.keyCode != null and isKey.keyCode.trim != ''">
                and k.key_code like concat('%',#{isKey.keyCode},'%')
            </if>
            <if test="isKey.keyName != null and isKey.keyName.trim != ''">
                and k.key_name like concat('%',#{isKey.keyName},'%')
            </if>
            <if test="isKey.hardwareId != null">
                and k.hardware_id = #{isKey.hardwareId}
            </if>
        </where>
        ORDER BY
        k.key_id DESC

    </select>

</mapper>
