<?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="io.renren.modules.levelManage.dao.UniversalTableDao">

    <!-- 可根据自己的需求，是否要使用 -->
    <resultMap type="io.renren.modules.levelManage.entity.UniversalTableEntity" id="UniversalTableMap">
        <result property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="createDate" column="create_date"/>
        <result property="createUser" column="create_user"/>
    </resultMap>

    <select id="selectById" resultType="map">
        SELECT
        <foreach collection="tableCols" item="listItem" separator="," >
        ${listItem}
        </foreach>
        FROM
        ${tableName}
        WHERE id=#{id}
    </select>

    <select id="list" resultType="map">
        SELECT
        <foreach collection="tableCols" item="listItem" separator="," >
            ${listItem}
        </foreach>
        FROM
        ${tableName}
        limit #{page}, #{size};
    </select>

    <select id="listBySonIds" resultType="map">
        SELECT
        <foreach collection="tableCols" item="listItem" separator="," >
            ${listItem}
        </foreach>
        FROM
        ${tableName}
        WHERE id in
        <foreach collection="ids" item="id" separator="," open="(" close=")">
            ${id}
        </foreach>
        limit #{page}, #{size};
    </select>

    <select id="listBySonIdsNotLimit" resultType="map">
        SELECT
        <foreach collection="tableCols" item="listItem" separator="," >
            ${listItem}
        </foreach>
        FROM
        ${tableName}
        WHERE id in
        <foreach collection="ids" item="id" separator="," open="(" close=")">
            ${id}
        </foreach>
    </select>

    <select id="selectByName" resultType="map">
        SELECT
        <foreach collection="tableCols" item="listItem" separator="," >
            ${listItem}
        </foreach>
        FROM
        ${tableName}
        WHERE SUBSTR(name,5) like concat('%',#{name},'%')
        limit #{page}, #{size};
    </select>

    <select id="getCountByName" resultType="INTEGER">
        SELECT COUNT(*) FROM ${tableName}
        WHERE
        SUBSTR(name,5) like concat('%',#{name},'%');
    </select>

    <insert id="saveWithCols" parameterType="io.renren.modules.levelManage.entity.UniversalTableEntity" useGeneratedKeys="true" keyProperty="universalTableEntity.id">
        INSERT into ${tableName}
        <foreach collection="cols" item="col" separator="," close=")" open="(">
            ${col.cols}
        </foreach>
        VALUES
        <foreach collection="cols" item="col" separator="," open="(" close=")">
            #{col.value}
        </foreach>
    </insert>

    <update id="updateWithCols">
        UPDATE ${tableName}
        SET
        <foreach collection="cols" item="col" separator="," >
            ${col.cols} = #{col.value}
        </foreach>
        WHERE
        id = #{id}
    </update>

    <delete id="removeByIdsFromTableName" parameterType="List">
        DELETE from ${tableName} where id in
        <foreach collection="ids" item="id" open="(" close=")" separator=",">
            ${id}
        </foreach>
        ;
    </delete>

    <select id="listAllItem" resultType="map">
        SELECT id, SUBSTR(name,5) as name
        FROM ${tableName}
    </select>

    <select id="listNotLeafItem" resultType="map">
        SELECT id, SUBSTR(name,5) as name
        FROM ${tableName}
        WHERE is_leaf!=1;
    </select>

    <select id="listNotLeafItemInfo" resultType="map">
        SELECT
        <foreach collection="tableCols" item="listItem" separator="," >
            ${listItem}
        </foreach>
        FROM
        ${tableName}
        WHERE is_leaf!=1;
    </select>
    <select id="listLeafItem" resultType="map">
        SELECT
        <foreach collection="tableCols" item="listItem" separator="," >
            ${listItem}
        </foreach>
        FROM
        ${tableName}
        WHERE is_leaf=1;
    </select>

    <update id="updateHbaseTableName" >
        UPDATE ${tableName} set hbaseTableName=#{hbaseTableName} where id=#{id}
    </update>

    <select id="selectNameById" resultType="String">
        SELECT name FROM ${tableName} where id=#{id}
    </select>

    <select id="getCount" resultType="INTEGER">
        SELECT count(*) FROM  ${tableName};
    </select>
    
    <select id="selectByNameWithParent" resultType="map">
        SELECT * FROM ${tableName}
        WHERE id IN
            (SELECT son_row_id FROM level_relation
             WHERE son_table_id=#{tableId}
             AND parent_row_id=#{parentRowId}
            )
        AND SUBSTR(name,5) LIKE concat('%',#{name},'%')
        limit #{page}, #{size};
    </select>

    <select id="getCountByNameWithParent" resultType="INTEGER">
        SELECT count(*) FROM ${tableName}
        WHERE id IN
            (SELECT son_row_id FROM level_relation
             WHERE son_table_id=#{tableId}
             AND parent_row_id=#{parentRowId}
            )
        AND SUBSTR(name,5) LIKE concat('%',#{name},'%');
    </select>

    <select id="listItemWithLeafInfo" resultType="Map">
        SELECT id, SUBSTR(name,5) as name,is_leaf
        FROM ${tableName}
    </select>

    <select id="listAllItemInfo" resultType="map">
        SELECT
        <foreach collection="tableCols" item="listItem" separator="," >
            ${listItem}
        </foreach>
        FROM
        ${tableName}
    </select>
</mapper>