<?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.github.tangyi.user.mapper.RoleMapper">
    <resultMap id="roleResultMap" type="com.github.tangyi.user.api.module.Role">
        <id column="id" property="id"/>
        <result column="role_name" property="roleName" />
        <result column="role_code" property="roleCode" />
        <result column="role_desc" property="roleDesc" />
        <result column="status" property="status" />
        <result column="is_default" property="isDefault" />
        <result column="creator" property="creator" />
        <result column="create_date" property="createDate" />
        <result column="modifier" property="modifier" />
        <result column="modify_date" property="modifyDate" />
        <result column="del_flag" property="delFlag" />
        <result column="application_code" property="applicationCode" />
        <result column="tenant_code" property="tenantCode"/>
    </resultMap>

    <sql id="roleColumns">
        a.id,
        a.role_name,
        a.role_code,
        a.role_desc,
        a.status,
        a.is_default,
        a.creator,
        a.create_date,
        a.modifier,
        a.modify_date,
        a.del_flag,
        a.application_code,
        a.tenant_code
    </sql>

    <!-- where 条件 -->
    <sql id="whereColumnList">
        <if test="roleName != null and roleName != ''">
            and a.role_name like CONCAT('%', #{roleName},'%')
        </if>
        <if test="status != null and status != ''">
            and a.status = #{status}
        </if>
        <if test="isDefault != null and isDefault != ''">
            and a.is_default = #{isDefault}
        </if>
        <if test="tenantCode != null and tenantCode != ''">
            and a.tenant_code = #{tenantCode}
        </if>
    </sql>

    <select id="get" resultMap="roleResultMap">
        SELECT
        <include refid="roleColumns"/>
        FROM sys_role a
        WHERE a.id = #{id} and a.del_flag = 0
    </select>

    <select id="findList" resultMap="roleResultMap">
        SELECT
        <include refid="roleColumns"/>
        FROM sys_role  a
        WHERE a.del_flag = 0
        <include refid="whereColumnList"/>
    </select>

    <select id="findAllList" resultMap="roleResultMap">
        SELECT
        <include refid="roleColumns"/>
        FROM sys_role  a
        WHERE a.del_flag = 0
        <include refid="whereColumnList"/>
    </select>

    <select id="findListById" resultMap="roleResultMap">
        SELECT
        <include refid="roleColumns"/>
        FROM sys_role  a
        WHERE a.id IN
        <foreach collection="ids" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>

    <insert id="insert">
        INSERT INTO sys_role (
        id,
        role_name,
        role_code,
        role_desc,
        status,
        is_default,
        creator,
        create_date,
        modifier,
        modify_date,
        del_flag,
        application_code,
        tenant_code
        ) VALUES (
        #{id},
        #{roleName},
        #{roleCode},
        #{roleDesc},
        #{status},
        #{isDefault},
        #{creator},
        #{createDate},
        #{modifier},
        #{modifyDate},
        #{delFlag},
        #{applicationCode},
        #{tenantCode}
        )
    </insert>

    <update id="update">
        UPDATE sys_role  SET
        <if test="roleName != null">
            role_name = #{roleName},
        </if>
        <if test="roleCode != null">
            role_code = #{roleCode},
        </if>
        <if test="roleDesc != null">
            role_desc = #{roleDesc},
        </if>
        <if test="status != null">
            status = #{status},
        </if>
        <if test="isDefault != null">
            is_default = #{isDefault},
        </if>
        <if test="delFlag != null">
            del_flag = #{delFlag},
        </if>
        modifier = #{modifier},
        modify_date = #{modifyDate}
        WHERE
        <if test="id != null">
            id = #{id}
        </if>
    </update>

    <delete id="delete">
        UPDATE sys_role SET
        del_flag = 1
        WHERE id = #{id}
    </delete>

    <delete id="deleteAll">
        UPDATE sys_role SET
        del_flag = 1
        WHERE id in
        <foreach item="item" index="index" collection="array" open="("
                 separator="," close=")"> #{item}
        </foreach>
    </delete>
</mapper>
