<?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.hw.dms.mapper.DmsBaseShutReasonMapper">

    <resultMap type="DmsBaseShutReason" id="DmsBaseShutReasonResult">
        <result property="shutReasonId"    column="shut_reason_id"    />
        <result property="reasonCode"    column="reason_code"    />
        <result property="deviceId"    column="device_id"    />
        <result property="shutTypeId"    column="shut_type_id"    />
        <result property="shutReason"    column="shut_reason"    />
        <result property="isFlag"    column="is_flag"    />
        <result property="remark"    column="remark"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
    </resultMap>

    <sql id="selectDmsBaseShutReasonVo">
        select shut_reason_id, reason_code, device_id, shut_type_id, shut_reason, is_flag, remark, create_by, create_time, update_by, update_time from dms_base_shut_reason
    </sql>

    <select id="selectDmsBaseShutReasonList" parameterType="DmsBaseShutReason" resultMap="DmsBaseShutReasonResult">
        select a.shut_reason_id, a.reason_code, a.device_id, a.shut_type_id, a.shut_reason, a.is_flag, a.remark, a.create_by, a.create_time, a.update_by, a.update_time
        from dms_base_shut_reason a
        left join dms_base_device_ledger b on a.device_id = b.device_id
        <where>
            <if test="reasonCode != null  and reasonCode != ''"> and a.reason_code = #{reasonCode}</if>
            <if test="deviceId != null "> and a.device_id = #{deviceId}</if>
            <if test="deviceName != null "> and b.device_name like concat('%', #{deviceName},'%')</if>
            <if test="shutTypeId != null "> and a.shut_type_id = #{shutTypeId}</if>
            <if test="shutReason != null  and shutReason != ''"> and a.shut_reason = #{shutReason}</if>
            <if test="isFlag != null  and isFlag != ''"> and a.is_flag = #{isFlag}</if>
        </where>
    </select>

    <select id="selectDmsBaseShutReasonByShutReasonId" parameterType="Long" resultMap="DmsBaseShutReasonResult">
        <include refid="selectDmsBaseShutReasonVo"/>
        where shut_reason_id = #{shutReasonId}
    </select>

    <insert id="insertDmsBaseShutReason" parameterType="DmsBaseShutReason" useGeneratedKeys="true" keyProperty="shutReasonId">
        insert into dms_base_shut_reason
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="reasonCode != null">reason_code,</if>
            <if test="deviceId != null">device_id,</if>
            <if test="shutTypeId != null">shut_type_id,</if>
            <if test="shutReason != null">shut_reason,</if>
            <if test="isFlag != null and isFlag != ''">is_flag,</if>
            <if test="remark != null">remark,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="reasonCode != null">#{reasonCode},</if>
            <if test="deviceId != null">#{deviceId},</if>
            <if test="shutTypeId != null">#{shutTypeId},</if>
            <if test="shutReason != null">#{shutReason},</if>
            <if test="isFlag != null and isFlag != ''">#{isFlag},</if>
            <if test="remark != null">#{remark},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
         </trim>
    </insert>

    <update id="updateDmsBaseShutReason" parameterType="DmsBaseShutReason">
        update dms_base_shut_reason
        <trim prefix="SET" suffixOverrides=",">
            <if test="reasonCode != null">reason_code = #{reasonCode},</if>
            <if test="deviceId != null">device_id = #{deviceId},</if>
            <if test="shutTypeId != null">shut_type_id = #{shutTypeId},</if>
            <if test="shutReason != null">shut_reason = #{shutReason},</if>
            <if test="isFlag != null and isFlag != ''">is_flag = #{isFlag},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where shut_reason_id = #{shutReasonId}
    </update>

    <delete id="deleteDmsBaseShutReasonByShutReasonId" parameterType="Long">
        delete from dms_base_shut_reason where shut_reason_id = #{shutReasonId}
    </delete>

    <delete id="deleteDmsBaseShutReasonByShutReasonIds" parameterType="String">
        delete from dms_base_shut_reason where shut_reason_id in
        <foreach item="shutReasonId" collection="array" open="(" separator="," close=")">
            #{shutReasonId}
        </foreach>
    </delete>
</mapper>
