<?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.zcloud.mapper.datasource.bus.HiddenRegionMapper">

    <resultMap type="HiddenRegion" id="HiddenRegionMap">
        <id column="HIDDENREGION_ID" property="HIDDENREGION_ID"/>
        <result column="HIDDENREGION" property="HIDDENREGION"/>
        <result column="COMMENTS" property="COMMENTS"/>
        <result column="PERSON_CHARGE" property="PERSON_CHARGE"/>
        <result column="PARENT_ID" property="PARENT_ID"/>
    </resultMap>

    <!--表名 -->
    <sql id="tableName">
        BUS_HIDDENREGION
    </sql>

    <!-- 字段用于新增 -->
    <sql id="Field">
        HIDDENREGION_ID
        ,
		HIDDENREGION,
		PARENT_ID,
		PERSON_CHARGE,
		SORTINDEX,
		COMMENTS,
		ISDELETE,
		CREATOR,
		CREATTIME,
		OPERATOR,
		OPERATTIME,
		DEPARTMENT_ID,
		SBDL,
		SBMC,
		CORPINFO_ID
    </sql>

    <!-- 字段值 -->
    <sql id="FieldValue">
        #{HIDDENREGION_ID}
        ,
        #{HIDDENREGION},
        #{PARENT_ID},
        #{PERSON_CHARGE},
        #{SORTINDEX},
        #{COMMENTS},
        #{ISDELETE},
        #{CREATOR},
        #{CREATTIME},
        #{OPERATOR},
        #{OPERATTIME},
        #{DEPARTMENT_ID},
        #{SBDL},
        #{SBMC},
        #{CORPINFO_ID}
    </sql>

    <!-- 新增-->
    <insert id="save" parameterType="pd">
        insert into
        <include refid="tableName"></include>
        (
        <include refid="Field"></include>
        ) values (
        <include refid="FieldValue"></include>
        )
    </insert>

    <!-- 删除-->
    <delete id="delete" parameterType="pd">
        update
        <include refid="tableName"></include>
        set
        ISDELETE = '1'
        where
        HIDDENREGION_ID = #{HIDDENREGION_ID}
    </delete>

    <update id="edit" parameterType="pd">
        update
        <include refid="tableName"></include>
        set
        COMMENTS = #{COMMENTS},
        PERSON_CHARGE = #{PERSON_CHARGE},
        HIDDENREGION = #{HIDDENREGION},
        SORTINDEX = #{SORTINDEX},
        DEPARTMENT_ID = #{DEPARTMENT_ID},
        PERSON_CHARGE = #{PERSON_CHARGE},
        COMMENTS = #{COMMENTS},
        SBDL = #{SBDL},
        SBMC = #{SBMC}
        where
        HIDDENREGION_ID = #{HIDDENREGION_ID}
    </update>

    <select id="listPage" parameterType="page" resultType="pd">
        select
        <include refid="Field"></include>
        from
        <include refid="tableName"></include>
        where
        ISDELETE = '0'
        and CORPINFO_ID = #{pd.CORPINFO_ID}
        <if test="pd.HIDDENREGION != null and pd.HIDDENREGION != ''"><!-- 关键词检索 -->

            AND
            (
            HIDDENREGION LIKE CONCAT(CONCAT('%', #{pd.HIDDENREGION}),'%')
            )
        </if>
        <if test="pd.COMMENTS != null and pd.COMMENTS != ''"><!-- 关键词检索 -->
            AND
            (
            COMMENTS LIKE CONCAT(CONCAT('%', #{pd.COMMENTS}),'%')
            )
        </if>
        <if test="pd.PARENT_ID != null and pd.PARENT_ID != ''"><!-- 关键词检索 -->
            AND PARENT_ID = #{pd.PARENT_ID}
        </if>
        order by SORTINDEX,CREATTIME desc
    </select>

    <select id="getPaginglistPage" parameterType="page" resultType="pd">
        select
        <include refid="Field"></include>
        from
        <include refid="tableName"></include>
        where
        PARENT_ID = #{pd.HIDDENREGION_ID}
        and
        ISDELETE = '0'
    </select>
    <select id="getBacklistPage" parameterType="page" resultType="pd">
        select
        <include refid="Field"></include>
        from
        <include refid="tableName"></include>
        f
        where ISDELETE = '0' and f.CORPINFO_ID = #{pd.CORPINFO_ID}
        <if test="pd.HIDDENREGION_ID!= null and pd.HIDDENREGION_ID != ''"><!-- 检索 -->
            and f.PARENT_ID = #{pd.HIDDENREGION_ID}
        </if>
        order by
        SORTINDEX,
        CREATTIME desc
    </select>
    <select id="getListById" parameterType="page" resultType="pd">
        select
        <include refid="Field"></include>
        from
        <include refid="tableName"></include>
        where
        HIDDENREGION_ID = #{HIDDENREGION_ID}
    </select>

    <select id="getListSubset" parameterType="page" resultType="pd">
        select
        <include refid="Field"></include>
        from
        <include refid="tableName"></include>
        where
        PARENT_ID = #{HIDDENREGION_ID}
        and
        ISDELETE = '0'
    </select>

    <select id="getListTree" resultMap="HiddenRegionMap">
        select
        <include refid="Field"></include>
        from
        <include refid="tableName"></include>
        <where>
            <if test="PARENT_ID!=null">
                and PARNET_ID = #{PARNET_ID}
            </if>
        </where>

    </select>

    <!--判断是否含有下级-->
    <select id="hiddenRegionByParentId" parameterType="String">
        select
        <include refid="Field"></include>
        from
        <include refid="tableName"></include>
        where 1=1
        <!--<if test="_parameter  != null and _parameter  != ''">&lt;!&ndash; 关键词检索 &ndash;&gt;
            and PARENT_ID = #{parentId}
        </if>-->
        and PARENT_ID = #{parentId}
    </select>


    <!--通过ID获取其子一级菜单 -->
    <select id="listHiddenRegionByParentId" parameterType="String" resultMap="HiddenRegionMap">
        select
        <include refid="Field"></include>
        from
        <include refid="tableName"></include>
        where PARENT_ID = #{parentId}
        and
        ISDELETE = '0'
        and CORPINFO_ID = #{CORPINFO_ID}
        order by
        SORTINDEX,
        CREATTIME desc
    </select>

    <select id="hiddenRegionByName" parameterType="pd" resultType="pd">
        SELECT
        <include refid="Field"></include>
        FROM
        <include refid="tableName"></include>
        f
        WHERE f.ISDELETE = '0'
        and PARENT_ID = '0'
        and HIDDENREGION = #{HIDDENREGION}
    </select>
    <select id="getHiddenRegionCountByParentId" parameterType="pd" resultType="int">
        SELECT
        count(*)
        FROM
        <include refid="tableName"></include>
        f
        WHERE f.ISDELETE = '0'
        and PARENT_ID = #{PARENT_ID}
    </select>

    <!-- 隐患中的部位列表 -->
    <select id="listRegionByHidden" parameterType="pd" resultType="pd">
        SELECT hr.HIDDENREGION_ID,
               hr.HIDDENREGION
        FROM bus_hiddenregion hr
                 LEFT JOIN bus_hidden f ON hr.HIDDENREGION_ID = f.HIDDENPART
        WHERE hr.ISDELETE = 0
          AND f.ISDELETE = '0'
          AND f.CORPINFO_ID = #{CORPINFO_ID}
        GROUP BY hr.HIDDENREGION_ID
        ORDER BY hr.CREATTIME ASC
    </select>

    <select id="findById" parameterType="pd" resultType="pd">
        SELECT *
        FROM bus_hiddenregion hr
        WHERE hr.ISDELETE = 0
          AND hr.CORPINFO_ID = #{CORPINFO_ID}
          and hr.HIDDENREGION_ID = #{HIDDENREGION_ID}
    </select>

    <select id="findBypId" parameterType="pd" resultType="pd">
        select
        <include refid="Field"></include>
        from
        <include refid="tableName"></include>
        where
        HIDDENREGION_ID = #{HIDDENREGION_ID}
    </select>
</mapper>
