<?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.ruoyi.ss.access.mapper.AccessMapper">

    <resultMap type="AccessVO" id="AccessResult" autoMapping="true"/>

    <sql id="selectAccessVo">
        select
            sa.access_id,
            sa.user_id,
            sa.access_key,
            sa.access_secret,
            sa.create_time,
            sa.expire_time,
            sa.surplus_count,
            su.user_name as user_name
        from ss_access sa
            left join sm_user su on su.user_id = sa.user_id
    </sql>

    <select id="selectAccessList" parameterType="AccessQuery" resultMap="AccessResult">
        <include refid="selectAccessVo"/>
        <where>
            <if test="accessId != null "> and sa.access_id = #{accessId}</if>
            <if test="userId != null "> and sa.user_id = #{userId}</if>
            <if test="accessKey != null  and accessKey != ''"> and sa.access_key = #{accessKey}</if>
            <if test="accessSecret != null  and accessSecret != ''"> and sa.access_secret = #{accessSecret}</if>
            <if test="userName != null  and userName != ''"> and su.user_name like CONCAT('%',#{userName},'%')</if>
            <if test="accessIds != null and accessIds.size() > 0">
                and sa.access_id in
                <foreach collection="accessIds" item="accessId" open="(" separator="," close=")">
                    #{accessId}
                </foreach>
            </if>
        </where>
    </select>

    <select id="selectAccessByAccessId" parameterType="Long" resultMap="AccessResult">
        <include refid="selectAccessVo"/>
        where sa.access_id = #{accessId}
    </select>

    <select id="selectCountByUserId" resultType="java.lang.Integer">
        select count(sa.access_id)
        from ss_access sa
        where sa.user_id = #{userId}
    </select>

    <select id="selectByAccessKey" resultType="com.ruoyi.ss.access.domain.AccessVO">
        <include refid="selectAccessVo"/>
        where sa.access_key = #{accessKey}
    </select>

    <insert id="insertAccess" parameterType="Access" useGeneratedKeys="true" keyProperty="accessId">
        insert into ss_access
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="userId != null">user_id,</if>
            <if test="accessKey != null and accessKey != ''">access_key,</if>
            <if test="accessSecret != null and accessSecret != ''">access_secret,</if>
            <if test="createTime != null">create_time,</if>
            <if test="expireTime != null">expire_time,</if>
            <if test="surplusCount != null">surplus_count,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="userId != null">#{userId},</if>
            <if test="accessKey != null and accessKey != ''">#{accessKey},</if>
            <if test="accessSecret != null and accessSecret != ''">#{accessSecret},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="expireTime != null">#{expireTime},</if>
            <if test="surplusCount != null">#{surplusCount},</if>
         </trim>
    </insert>

    <update id="updateAccess" parameterType="Access">
        update ss_access
        <trim prefix="SET" suffixOverrides=",">
            <if test="userId != null">user_id = #{userId},</if>
            <if test="accessKey != null and accessKey != ''">access_key = #{accessKey},</if>
            <if test="accessSecret != null and accessSecret != ''">access_secret = #{accessSecret},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="expireTime != null">expire_time = #{expireTime},</if>
            <if test="surplusCount != null">surplus_count = #{surplusCount},</if>
        </trim>
        where access_id = #{accessId}
    </update>

    <delete id="deleteAccessByAccessId" parameterType="Long">
        delete from ss_access where access_id = #{accessId}
    </delete>

    <delete id="deleteAccessByAccessIds" parameterType="String">
        delete from ss_access where access_id in
        <foreach item="accessId" collection="ids" open="(" separator="," close=")">
            #{accessId}
        </foreach>
    </delete>
</mapper>
