<?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.ResourceMapper">

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

    <sql id="FIELDS">
        id, name, type, sort, display_name,
        create_time, pid, handler
    </sql>

    <select id="selectByTypeAndHandler" resultType="ResourceDO">
        SELECT
          <include refid="FIELDS"/>
        FROM resource
        WHERE type = #{type}
        AND handler = #{handler}
        AND deleted = 0
        LIMIT 1
    </select>

    <select id="selectListByType" parameterType="Integer" resultType="ResourceDO">
        SELECT
          <include refid="FIELDS"/>
        FROM resource
        <where>
            <if test="type != null">
               type = #{type}
            </if>
            AND deleted = 0
        </where>
    </select>

    <select id="selectListByTypeAndRoleIds" resultType="ResourceDO">
        SELECT
            r.id, r.name, r.type, r.sort, r.display_name,
            r.create_time, r.pid, r.handler
        FROM resource r, role_resource rr
        WHERE r.deleted = 0
        AND rr.deleted = 0
        <if test="type != null">
            AND r.type = #{type}
        </if>
        AND rr.role_id IN
            <foreach item="roleId" collection="roleIds" separator="," open="(" close=")" index="">
                #{roleId}
            </foreach>
        AND r.id = rr.resource_id
    </select>

    <select id="selectByName" parameterType="String" resultType="ResourceDO">
        SELECT
          <include refid="FIELDS"/>
        FROM resource
        WHERE name = #{name}
        AND deleted = 0
        LIMIT 1
    </select>

    <select id="selectById" resultType="ResourceDO">
        SELECT
          <include refid="FIELDS"/>
        FROM resource
        WHERE id = #{id}
        AND deleted = 0
    </select>

    <select id="selectListByIds" resultType="ResourceDO">
        SELECT
          <include refid="FIELDS"/>
        FROM resource
        WHERE id IN
            <foreach item="id" collection="ids" separator="," open="(" close=")" index="">
                #{id}
            </foreach>
        AND deleted = 0
    </select>

    <select id="selectCountByPid" resultType="int">
        SELECT
          COUNT(1)
        FROM resource
        WHERE pid = #{pid}
        AND deleted = 0
    </select>

    <insert id="insert" parameterType="ResourceDO" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
        INSERT INTO resource (
          name, type, sort, display_name, handler,
          pid, create_time, deleted
        ) VALUES (
          #{name}, #{type}, #{sort}, #{displayName}, #{handler},
          #{pid}, #{createTime}, #{deleted}
        )
    </insert>

    <update id="update" parameterType="ResourceDO">
        UPDATE resource
        <set>
            <if test="name != null">
                name = #{name},
            </if>
            <if test="sort != null">
                sort = #{sort},
            </if>
            <if test="displayName != null">
                display_name = #{displayName},
            </if>
            <if test="pid != null">
                pid = #{pid},
            </if>
            <if test="handler != null">
                handler = #{handler},
            </if>
            <if test="deleted != null">
                deleted = #{deleted}
            </if>
        </set>
        WHERE id = #{id}
    </update>

</mapper>