<?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="cn.iocoder.mall.admin.dao.RoleResourceMapper">

    <!--<insert id="insert" parameterType="UserDO" useGeneratedKeys="true" keyProperty="id">-->
        <!--INSERT INTO users (-->
          <!--id, mobile, create_time-->
        <!--) VALUES (-->
          <!--#{id}, #{mobile}, #{createTime}-->
        <!--)-->
    <!--</insert>-->

    <select id="selectByResourceHandler" parameterType="String" resultType="RoleResourceDO">
        SELECT
          rr.id, rr.role_id, rr.resource_id
        FROM resource r, role_resource rr
        WHERE r.handler = #{resourceHandler}
        AND r.id = rr.resource_id
        AND rr.deleted = 0;
    </select>

    <select id="selectByResourceId" parameterType="Integer" resultType="RoleResourceDO">
      SELECT
        id, role_id, resource_id
      FROM role_resource
      WHERE resource_id = #{resourceId}
      AND deleted = 0
    </select>

    <update id="updateToDeletedByResourceId" parameterType="Integer">
      UPDATE role_resource
      SET deleted = 1
      WHERE resource_id = #{resourceId}
      AND deleted = 0
    </update>

    <update id="updateToDeletedByRoleId" parameterType="Integer">
      UPDATE role_resource
      SET deleted = 1
      WHERE role_id = #{roleId}
      AND deleted = 0
    </update>

    <insert id="insertList">
        INSERT INTO role_resource (
          resource_id, role_id, create_time, deleted
        ) VALUES
        <foreach collection="roleResources" item="roleResource" separator=",">
            (#{roleResource.resourceId}, #{roleResource.roleId}, #{roleResource.createTime}, #{roleResource.deleted})
        </foreach>
    </insert>

</mapper>