<?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.ycwl.basic.mapper.RoleMapper">
    <insert id="add" parameterType="com.ycwl.basic.model.pc.role.req.AddOrUpdateRoleReqVO">
        insert into role(`id`, `name`)
        values (#{id}, #{name})
    </insert>

    <update id="delete">
        update
            role
        set is_remove=1,
            `update_time`=NOW()
        where id = #{id}
    </update>

    <update id="update" parameterType="com.ycwl.basic.model.pc.role.req.AddOrUpdateRoleReqVO">
        update
            role
        set `name`=#{name},
            `update_time`=NOW()
        where id = #{id}
    </update>

    <select id="list" resultType="com.ycwl.basic.model.pc.role.resp.RoleListRespVO">
        select
        `id`,
        `name`,
        `status`,
        create_time
        from
        role
        where
        is_remove=0
        <if test="name!=null and name!=''">
            and
            locate(#{name},`name`) > 0
        </if>
    </select>
    <select id="getMenuById" resultType="com.ycwl.basic.model.pc.menu.MenuNode">
        select m.`id`,
               m.`parent_id`,
               m.`target`,
               m.`name`,
               m.`type`,
               m.`sort`
        from menu m,
             role_menu rm
        where is_remove = 0
          and business_type = 0
          and rm.menu_id = m.id
          and rm.role_id = #{id}
    </select>


    <select id="getRoleStatus" resultType="java.lang.Integer">
        select
            `status`
        from
            role
        where
            id = #{id}
    </select>


    <update id="updateStatus">
        update
            role
        set status =
                (CASE
                    status
                     WHEN 1 THEN
                         0
                     WHEN 0 THEN
                         1
                     ELSE null
                    END)
        where id = #{id}
    </update>
</mapper>
