<?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.pm.topology.mapper.ItemTopologyMapper">

    <resultMap type="pd" id="ItemTopologyResult">
        <result property="itemId"    column="item_id"    />
        <result property="parentId"    column="parent_id"    />
        <result property="ancestors"    column="ancestors"    />
        <result property="itemName"    column="item_name"    />
        <result property="orderNum"    column="order_num"    />
        <result property="status"    column="status"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="itemType"    column="item_type"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="deviceId"    column="device_id"    />
    </resultMap>

    <sql id="selectItemTopologyVo">
        select item_id, parent_id, ancestors, item_name, order_num, status, del_flag, item_type, create_by, create_time, update_by, update_time, device_id from item_topology
    </sql>

    <select id="selectItemTopologyList" parameterType="pd" resultMap="ItemTopologyResult">
        <include refid="selectItemTopologyVo"/>
        <where>
            <if test="itemName != null and itemName != ''"> and item_name like concat('%', #{itemName}, '%')</if>
            <if test="orderNum != null and orderNum != ''"> and order_num = #{orderNum}</if>
            <if test="status != null and status != ''"> and status = #{status}</if>
            <if test="itemType != null and itemType != ''"> and item_type = #{itemType}</if>
            <if test="deviceId != null and deviceId != ''"> and device_id = #{deviceId}</if>
        </where>
    </select>

    <select id="selectItemTopologyByItemId" parameterType="String" resultMap="ItemTopologyResult">
        <include refid="selectItemTopologyVo"/>
        where item_id = #{itemId}
    </select>

    <insert id="insertItemTopology" parameterType="pd">
        insert into item_topology
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="itemId != null and itemId != ''">item_id,</if>
            <if test="parentId != null and parentId != ''">parent_id,</if>
            <if test="ancestors != null and ancestors != ''">ancestors,</if>
            <if test="itemName != null and itemName != ''">item_name,</if>
            <if test="orderNum != null and orderNum != ''">order_num,</if>
            <if test="status != null and status != ''">status,</if>
            <if test="delFlag != null and delFlag != ''">del_flag,</if>
            <if test="itemType != null and itemType != ''">item_type,</if>
            <if test="createBy != null and createBy != ''">create_by,</if>
            <if test="createTime != null and createTime != ''">create_time,</if>
            <if test="updateBy != null and updateBy != ''">update_by,</if>
            <if test="updateTime != null and updateTime != ''">update_time,</if>
            <if test="deviceId != null and deviceId != ''">device_id,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="itemId != null and itemId != ''">#{itemId},</if>
            <if test="parentId != null and parentId != ''">#{parentId},</if>
            <if test="ancestors != null and ancestors != ''">#{ancestors},</if>
            <if test="itemName != null and itemName != ''">#{itemName},</if>
            <if test="orderNum != null and orderNum != ''">#{orderNum},</if>
            <if test="status != null and status != ''">#{status},</if>
            <if test="delFlag != null and delFlag != ''">#{delFlag},</if>
            <if test="itemType != null and itemType != ''">#{itemType},</if>
            <if test="createBy != null and createBy != ''">#{createBy},</if>
            <if test="createTime != null and createTime != ''">#{createTime},</if>
            <if test="updateBy != null and updateBy != ''">#{updateBy},</if>
            <if test="updateTime != null and updateTime != ''">#{updateTime},</if>
            <if test="deviceId != null and deviceId != ''">#{deviceId},</if>
        </trim>
    </insert>

    <update id="updateItemTopology" parameterType="pd">
        update item_topology
        <trim prefix="SET" suffixOverrides=",">
            <if test="parentId != null and parentId != ''">parent_id = #{parentId},</if>
            <if test="ancestors != null and ancestors != ''">ancestors = #{ancestors},</if>
            <if test="itemName != null and itemName != ''">item_name = #{itemName},</if>
            <if test="orderNum != null and orderNum != ''">order_num = #{orderNum},</if>
            <if test="status != null and status != ''">status = #{status},</if>
            <if test="delFlag != null and delFlag != ''">del_flag = #{delFlag},</if>
            <if test="itemType != null and itemType != ''">item_type = #{itemType},</if>
            <if test="createBy != null and createBy != ''">create_by = #{createBy},</if>
            <if test="createTime != null and createTime != ''">create_time = #{createTime},</if>
            <if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
            <if test="updateTime != null and updateTime != ''">update_time = #{updateTime},</if>
            <if test="deviceId != null and deviceId != ''">device_id = #{deviceId},</if>
        </trim>
        where item_id = #{itemId}
    </update>

    <delete id="deleteItemTopologyByItemId" parameterType="Long">
        delete from item_topology where item_id = #{itemId}
    </delete>

    <delete id="deleteItemTopologyByItemIds" parameterType="String">
        delete from item_topology where item_id in
        <foreach item="itemId" collection="array" open="(" separator="," close=")">
            #{itemId}
        </foreach>
    </delete>
</mapper>
