<?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.ho.business.mapper.TopologyMapper">


    <sql id="Base_Column_List">
        id
        ,src_id,pid,
        group_id,dept_id,station_id,
        name,category,type,
        sub_type,link1,link2,
        link3,link4
    </sql>

    <select id="selectByPrimaryKey" resultType="com.ho.business.entity.Topology">
        select
        <include refid="Base_Column_List"/>
        from topology
        where id = #{id}
    </select>

    <select id="selectOne" resultType="com.ho.business.entity.Topology">
        select
        <include refid="Base_Column_List"/>
        from topology
        <where>
            <if test="stationId != null">
                and station_id = #{stationId}
            </if>
            <if test="srcId != null">
                and src_id =#{srcId}
            </if>
            <if test="pid!=null">
                and pid = #{pid}
            </if>
        </where>

    </select>
    <select id="selectByStationId" resultType="com.ho.business.entity.Topology">
        SELECT t1.t1.id,
               t1.src_id,
               t1.pid,
               t1.group_id,
               t1.dept_id,
               t1.station_id,
               t1.NAME,
               t1.category,
               t1.TYPE,
               t1.sub_type,
               t1.link1,
               t1.link2,
               t1.link3,
               t1.link4
        FROM (SELECT id,
                     src_id,
                     pid,
                     group_id,
                     dept_id,
                     station_id,
                     NAME,
                     category,
                     TYPE,
                     sub_type,
                     link1,
                     link2,
                     link3,
                     link4
              FROM topology
              WHERE station_id = #{id jdbcType=INTEGER}) t1
        WHERE t1.id NOT IN
              (
                  SELECT id
                  FROM topology
                  WHERE station_id = 1
                    AND category = 11
                    AND TYPE = 4
                    AND sub_type = 8
              )

    </select>

    <select id="selectAll" resultType="com.ho.business.entity.Topology">
        select
        <include refid="Base_Column_List"/>
        from topology
    </select>


    <select id="selectBysev" resultType="com.ho.business.entity.Topology">
        select
        <include refid="Base_Column_List"/>
        from topology
        <where>
            <if test="groupId != null">
                and group_id=#{groupId}
            </if>
            <if test="deptId != null">
                and dept_id=#{deptId}
            </if>
            <if test="stationId != null">
                and station_id = #{stationId}
            </if>
            <if test="srcId != null">
            and src_id =#{srcId}
            </if>
        </where>


    </select>

    <select id="selectList" resultType="com.ho.business.entity.Topology">
        select
        <include refid="Base_Column_List"/>
        from topology
        <where>
            <if test="stationId != null">
                and station_id = #{stationId}
            </if>
            <if test="srcId != null">
                and src_id =#{srcId}
            </if>
            <if test="pid!=null">
                and pid = #{pid}
            </if>
            <if test="category!=null">
                and category = #{category}
            </if>
            <if test="type!=null">
                    and `type` = #{type}
            </if>
            <if test="subType != null">
                and sub_type = #{subType}
            </if>
            <if test="pids != null and pids.size() != 0">
                and pid in
                <foreach collection="pids" open="(" close=")" separator="," item="pid">
                    #{pid}
                </foreach>
            </if>

            <if test="stationIds != null and stationIds.size() != 0">
                and station_id in
                <foreach collection="stationIds" open="(" close=")" separator="," item="item">
                    #{item}
                </foreach>
            </if>

        </where>

    </select>

    <select id="selectListByPid" resultType="com.ho.business.entity.Topology">
        select
        <include refid="Base_Column_List"/>
        from topology
        <where>
            <if test="stationId != null">
                and station_id = #{stationId}
            </if>
            <if test="list != null and list.size !=0">
                and src_id in
                <foreach collection="list" item="item" separator="," open="(" close=")">
                     #{item}
                </foreach>
            </if>
        </where>

    </select>
    <select id="selectByWarehouse" resultType="com.ho.business.entity.Topology">
            SELECT
        <include refid="Base_Column_List"/>
            from topology
            WHERE pid=#{srcId}
            AND category=2 AND TYPE=9 AND sub_type=1
    </select>

    <select id="selectByStack" resultType="com.ho.business.entity.Topology">
        SELECT
        <include refid="Base_Column_List"/>
        from topology
        WHERE pid=#{srcId}
        AND category=11 AND TYPE=4 AND sub_type=5
    </select>

    <select id="selectByStation" resultType="com.ho.business.entity.Topology">
        SELECT
        <include refid="Base_Column_List"/>
        from topology
        <where>
            <if test="groupId != null">
           and group_id=#{groupId}
             </if>
            <if test="deptId != null">
           and dept_id=#{deptId}
            </if>
            <if test="stationId != null">
            and station_id=#{stationId}
             </if>
        </where>
        AND category=1 AND TYPE=1 AND sub_type=3
    </select>

    <select id="getAllTopology" resultType="com.ho.business.entity.Topology">
        select
        <include refid="Base_Column_List"/>
        from topology
        where station_id = #{stationId,jdbcType=INTEGER}
    </select>



    <select id="getStationTopology" resultType="com.ho.business.entity.Topology">
        SELECT
        <include refid="Base_Column_List"/>
        from topology
        WHERE station_id = #{stationId,jdbcType=INTEGER}
        AND category= 1 AND TYPE= 1 AND sub_type= 3
    </select>

    <select id="getCategoryTopology" resultType="com.ho.business.entity.Topology">
        SELECT
        <include refid="Base_Column_List"/>
        from topology
        <where>
            <if test="stationId != null">
                and station_id = #{stationId}
            </if>
            <if test="category!=null">
                and category = #{category}
            </if>
            <if test="type!=null">
                and `type` = #{type}
            </if>
            <if test="subType != null">
                and sub_type = #{subType}
            </if>
            <if test="subTypeList != null and subTypeList.size !=0">
                and sub_type in
                <foreach collection="subTypeList" item="item" separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
            <if test="srcId != null">
                src_id = #{srcId}
            </if>
        </where>
    </select>

    <delete id="deleteBytopologyId" parameterType="java.lang.Integer">
        delete
        from topology
        where id = #{id,jdbcType=INTEGER}
    </delete>
    <insert id="insertTopology" keyColumn="id" keyProperty="id" parameterType="com.ho.business.entity.Topology"
            useGeneratedKeys="true">
        insert into topology
        ( id, src_id, pid
        , group_id, dept_id, station_id
        , name, category, type
        , sub_type, link1, link2
        , link3, link4)
        values ( #{id,jdbcType=INTEGER}, #{srcId,jdbcType=INTEGER}, #{pid,jdbcType=INTEGER}
               , #{groupId,jdbcType=INTEGER}, #{deptId,jdbcType=INTEGER}, #{stationId,jdbcType=INTEGER}
               , #{name,jdbcType=VARCHAR}, #{category,jdbcType=INTEGER}, #{type,jdbcType=INTEGER}
               , #{subType,jdbcType=INTEGER}, #{link1,jdbcType=INTEGER}, #{link2,jdbcType=INTEGER}
               , #{link3,jdbcType=INTEGER}, #{link4,jdbcType=INTEGER})
    </insert>
    <insert id="insertSelective">
        insert into topology
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="srcId != null">src_id,</if>
            <if test="pid != null">pid,</if>
            <if test="groupId != null">group_id,</if>
            <if test="deptId != null">dept_id,</if>
            <if test="stationId != null">station_id,</if>
            <if test="name != null">name,</if>
            <if test="category != null">category,</if>
            <if test="type != null">type,</if>
            <if test="subType != null">sub_type,</if>
            <if test="link1 != null">link1,</if>
            <if test="link2 != null">link2,</if>
            <if test="link3 != null">link3,</if>
            <if test="link4 != null">link4,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="srcId != null">#{srcId,jdbcType=INTEGER},</if>
            <if test="pid != null">#{pid,jdbcType=INTEGER},</if>
            <if test="groupId != null">#{groupId,jdbcType=INTEGER},</if>
            <if test="deptId != null">#{deptId,jdbcType=INTEGER},</if>
            <if test="stationId != null">#{stationId,jdbcType=INTEGER},</if>
            <if test="name != null">#{name,jdbcType=VARCHAR},</if>
            <if test="category != null">#{category,jdbcType=INTEGER},</if>
            <if test="type != null">#{type,jdbcType=INTEGER},</if>
            <if test="subType != null">#{subType,jdbcType=INTEGER},</if>
            <if test="link1 != null">#{link1,jdbcType=INTEGER},</if>
            <if test="link2 != null">#{link2,jdbcType=INTEGER},</if>
            <if test="link3 != null">#{link3,jdbcType=INTEGER},</if>
            <if test="link4 != null">#{link4,jdbcType=INTEGER},</if>
        </trim>
    </insert>

    <update id="updateByPrimaryKeySelective" parameterType="com.ho.business.entity.Topology">
        update topology
        <set>
            <if test="srcId != null">
                src_id = #{srcId,jdbcType=INTEGER},
            </if>
            <if test="pid != null">
                pid = #{pid,jdbcType=INTEGER},
            </if>
            <if test="groupId != null">
                group_id = #{groupId,jdbcType=INTEGER},
            </if>
            <if test="deptId != null">
                dept_id = #{deptId,jdbcType=INTEGER},
            </if>
            <if test="stationId != null">
                station_id = #{stationId,jdbcType=INTEGER},
            </if>
            <if test="name != null">
                name = #{name,jdbcType=VARCHAR},
            </if>
            <if test="category != null">
                category = #{category,jdbcType=INTEGER},
            </if>
            <if test="type != null">
                type = #{type,jdbcType=INTEGER},
            </if>
            <if test="subType != null">
                sub_type = #{subType,jdbcType=INTEGER},
            </if>
            <if test="link1 != null">
                link1 = #{link1,jdbcType=INTEGER},
            </if>
            <if test="link2 != null">
                link2 = #{link2,jdbcType=INTEGER},
            </if>
            <if test="link3 != null">
                link3 = #{link3,jdbcType=INTEGER},
            </if>
            <if test="link4 != null">
                link4 = #{link4,jdbcType=INTEGER},
            </if>
        </set>
        where id = #{id,jdbcType=INTEGER}
    </update>
    <update id="updateByPrimaryKey" parameterType="com.ho.business.entity.Topology">
        update topology
        set src_id     = #{srcId,jdbcType=INTEGER},
            pid        = #{pid,jdbcType=INTEGER},
            group_id   = #{groupId,jdbcType=INTEGER},
            dept_id    = #{deptId,jdbcType=INTEGER},
            station_id = #{stationId,jdbcType=INTEGER},
            name       = #{name,jdbcType=VARCHAR},
            category   = #{category,jdbcType=INTEGER},
            type       = #{type,jdbcType=INTEGER},
            sub_type   = #{subType,jdbcType=INTEGER},
            link1      = #{link1,jdbcType=INTEGER},
            link2      = #{link2,jdbcType=INTEGER},
            link3      = #{link3,jdbcType=INTEGER},
            link4      = #{link4,jdbcType=INTEGER}
        where id = #{id,jdbcType=INTEGER}
    </update>


</mapper>
