<?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.yonge.cooleshow.auth.dal.dao.SysRoleDao">

    <resultMap type="com.yonge.cooleshow.auth.api.entity.SysRole" id="SysRole">
        <result column="id_" property="id"/>
        <result column="role_name_" property="roleName"/>
        <result column="role_code_" property="roleCode"/>
        <result column="role_desc_" property="roleDesc"/>
        <result column="is_enable_" property="isEnable"/>
        <result column="create_time_" property="createTime"/>
        <result column="update_time_" property="updateTime"/>
        <result column="del_flag_" property="delFlag"/>
    </resultMap>

    <!-- 根据主键查询一条记录 -->
    <select id="get" resultMap="SysRole">
		SELECT * FROM sys_role WHERE id_ = #{id}
	</select>

    <!-- 全查询 -->
    <select id="findAll" resultMap="SysRole">
		SELECT * FROM sys_role WHERE del_flag_ = 0 ORDER BY upate_time_ DESC
	</select>

    <!-- 向数据库增加一条记录 -->
    <insert id="insert" parameterType="com.yonge.cooleshow.auth.api.entity.SysRole" useGeneratedKeys="true" keyColumn="id"
            keyProperty="id">
        INSERT INTO sys_role (id_,role_name_,role_code_,role_desc_,create_time_,update_time_,is_enable_)
        VALUES(#{id},#{roleName},#{roleCode},#{roleDesc},now(),now(),#{isEnable})
    </insert>

    <!-- 根据主键查询一条记录 -->
    <update id="update" parameterType="com.yonge.cooleshow.auth.api.entity.SysRole">
        UPDATE sys_role
        <set>
            <if test="delFlag != null">
                del_flag_ = #{delFlag},
            </if>
            <if test="roleCode != null">
                role_code_ = #{roleCode},
            </if>
            <if test="roleName != null">
                role_name_ = #{roleName},
            </if>
            <if test="roleDesc != null">
                role_desc_ = #{roleDesc},
            </if>
            <if test="updateTime != null">
                update_time_ = NOW(),
            </if>
            <if test="isEnable != null">
                is_enable_ = #{isEnable},
            </if>
        </set>
        WHERE id_ = #{id}
    </update>

    <!-- 根据主键删除一条记录 -->
    <delete id="delete">
		UPDATE sys_role SET del_flag_ = 1 WHERE id_ = #{id}
	</delete>

    <!-- 分页查询 -->
    <select id="queryPage" resultMap="SysRole" parameterType="map">
        SELECT * FROM sys_role WHERE del_flag_ = 0
        	<if test="search != null and search != ''">
        		and role_name_ like CONCAT('%',#{search},'%')
        	</if>
        	<if test="isEnable != null">
        		and is_enable_ = #{isEnable}
        	</if>
        ORDER BY update_time_ DESC
        <include refid="global.limit"/>
    </select>

    <!-- 查询当前表的总记录数 -->
    <select id="queryCount" resultType="int">
		SELECT COUNT(*) FROM sys_role WHERE del_flag_ = 0
        	<if test="search != null and search != ''">
        		and role_name_ like CONCAT('%',#{search},'%')
        	</if>
        	<if test="isEnable != null">
        		and is_enable_ = #{isEnable}
        	</if>
	</select>

    <select id="findRoleByUserId" resultMap="SysRole">
		SELECT sr.* FROM sys_user_role sur LEFT JOIN sys_role sr ON sur.role_id_ = sr.id_
		WHERE sur.user_id_ = #{userId} and sr.is_enable_ = 1 AND sr.del_flag_ = 0
	</select>

    <select id="findRoleByCode" resultMap="SysRole">
        SELECT sr.* FROM sys_role WHERE role_code_ = #{code} AND sr.del_flag_ = 0
    </select>
    <select id="findByRoleName" resultMap="SysRole">
        SELECT * FROM sys_role WHERE role_name_ = #{roleName} AND del_flag_ = 0 LIMIT 1
    </select>
</mapper>
