<?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.yunfeiyun.agmp.fms.mapper.TmnBlockMapper">
    
    <resultMap type="TmnBlock" id="TmnBlockResult">
        <result property="tmnblockId"    column="tmnblockId"    />
        <result property="tmnId"    column="tmnId"    />
        <result property="blockId"    column="blockId"    />
        <result property="tmnblockAssigndate"    column="tmnblockAssigndate"    />
    </resultMap>

    <sql id="selectTmnBlockVo">
        select tmnblockId, tmnId, blockId, tmnblockAssigndate from TmnBlock
    </sql>

    <select id="selectTmnBlockList" parameterType="TmnBlock" resultMap="TmnBlockResult">
        <include refid="selectTmnBlockVo"/>
        <where>  
            <if test="tmnId != null  and tmnId != ''"> and tmnId = #{tmnId}</if>
            <if test="blockId != null  and blockId != ''"> and blockId = #{blockId}</if>
            <if test="tmnblockAssigndate != null  and tmnblockAssigndate != ''"> and tmnblockAssigndate = #{tmnblockAssigndate}</if>
        </where>
    </select>

    <select id="selectTmnBlockDeviceList" parameterType="TmnBlockDeviceListReqVo" resultType="TmnBlockDeviceListResVo">
        select tl.tmnblockId, tl.tmnId, tb.tmnName, tl.blockId, tl.tmnblockAssigndate, tb.tmnBizcode, tb.tmnType
        from TmnBlock as tl
            LEFT JOIN TmnBase as tb ON tl.tmnId = tb.tmnId
        where blockId = #{blockId} and tmnType in
        <foreach item="ty" collection="tmnTypes" open="(" separator="," close=")">
            #{ty}
        </foreach>
    </select>
    
    <select id="selectTmnBlockByTmnblockId" parameterType="String" resultMap="TmnBlockResult">
        <include refid="selectTmnBlockVo"/>
        where tmnblockId = #{tmnblockId}
    </select>

    <select id="selectTmnBlockByTmnIdAndBlockId" parameterType="String" resultMap="TmnBlockResult">
        select tmnblockId, tmnId, blockId, tmnblockAssigndate
        from TmnBlock as tb
        where CONCAT(tb.tmnId, tb.blockId) In
        <foreach item="tbkey" collection="array" open="(" separator="," close=")">
            #{tbkey}
        </foreach>
    </select>

    <select id="selectTmnBlockListByTmnIds" parameterType="String" resultType="TmnBlock">
        select tb.tmnblockId, tb.tmnId, tb.blockId, tb.tmnblockAssigndate, fb.blockName
        from TmnBlock as tb
            LEFT JOIN FmsBlock as fb ON tb.blockId = fb.blockId
        where tmnId In
        <foreach item="tmnId" collection="array" open="(" separator="," close=")">
            #{tmnId}
        </foreach>
    </select>
        
    <insert id="insertTmnBlock" parameterType="TmnBlock">
        insert into TmnBlock
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="tmnblockId != null">tmnblockId,</if>
            <if test="tmnId != null">tmnId,</if>
            <if test="blockId != null">blockId,</if>
            <if test="tmnblockAssigndate != null">tmnblockAssigndate,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="tmnblockId != null">#{tmnblockId},</if>
            <if test="tmnId != null">#{tmnId},</if>
            <if test="blockId != null">#{blockId},</if>
            <if test="tmnblockAssigndate != null">#{tmnblockAssigndate},</if>
         </trim>
    </insert>

    <insert id="insertTmnBlockBatch" parameterType="TmnBlock">
        <foreach collection="list" item="item" separator=";">
            insert into TmnBlock
            <trim prefix="(" suffix=")" suffixOverrides=",">
                <if test="item.tmnblockId != null">tmnblockId,</if>
                <if test="item.tmnId != null">tmnId,</if>
                <if test="item.blockId != null">blockId,</if>
                <if test="item.tmnblockAssigndate != null">tmnblockAssigndate,</if>
            </trim>
            <trim prefix="values (" suffix=")" suffixOverrides=",">
                <if test="item.tmnblockId != null">#{item.tmnblockId},</if>
                <if test="item.tmnId != null">#{item.tmnId},</if>
                <if test="item.blockId != null">#{item.blockId},</if>
                <if test="item.tmnblockAssigndate != null">#{item.tmnblockAssigndate},</if>
            </trim>
        </foreach>
    </insert>

    <update id="updateTmnBlock" parameterType="TmnBlock">
        update TmnBlock
        <trim prefix="SET" suffixOverrides=",">
            <if test="tmnId != null">tmnId = #{tmnId},</if>
            <if test="blockId != null">blockId = #{blockId},</if>
            <if test="tmnblockAssigndate != null">tmnblockAssigndate = #{tmnblockAssigndate},</if>
        </trim>
        where tmnblockId = #{tmnblockId}
    </update>

    <delete id="deleteTmnBlockByTmnblockId" parameterType="String">
        delete from TmnBlock where tmnblockId = #{tmnblockId}
    </delete>

    <delete id="deleteTmnBlockByTmnblockIds" parameterType="String">
        delete from TmnBlock where tmnblockId in 
        <foreach item="tmnblockId" collection="array" open="(" separator="," close=")">
            #{tmnblockId}
        </foreach>
    </delete>
</mapper>