<?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.auth.mapper.OauthClientDetailsMapper">
    <resultMap id="oauthClientResultMap" type="com.github.tangyi.auth.api.module.OauthClientDetails">
        <id column="id" property="id"/>
        <result column="client_id" property="clientId"/>
        <result column="resource_ids" property="resourceIds"/>
        <result column="client_secret" property="clientSecret"/>
        <result column="scope" property="scope"/>
        <result column="authorized_grant_types" property="authorizedGrantTypes"/>
        <result column="web_server_redirect_uri" property="webServerRedirectUri"/>
        <result column="authorities" property="authorities"/>
        <result column="access_token_validity" property="accessTokenValidity"/>
        <result column="refresh_token_validity" property="refreshTokenValidity"/>
        <result column="additional_information" property="additionalInformation"/>
        <result column="autoapprove" property="autoapprove"/>
        <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="oauthClientColumns">
        a.id,
        a.client_id,
        a.resource_ids,
        a.client_secret_plain_text,
        a.client_secret,
        a.scope,
        a.authorized_grant_types,
        a.web_server_redirect_uri,
        a.authorities,
        a.access_token_validity,
        a.refresh_token_validity,
        a.additional_information,
        a.autoapprove,
        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="clientId != null and clientId != ''">
            and a.client_id like #{clientId}
        </if>
        <if test="tenantCode != null and tenantCode != ''">
            and a.tenant_code = #{tenantCode}
        </if>
    </sql>

    <select id="get" resultMap="oauthClientResultMap">
        SELECT
        <include refid="oauthClientColumns"/>
        FROM oauth_client_details a
        WHERE a.id = #{id} and a.del_flag = 0
    </select>

    <select id="findList" resultMap="oauthClientResultMap">
        SELECT
        <include refid="oauthClientColumns"/>
        FROM oauth_client_details a
        WHERE a.del_flag = 0
        <include refid="whereColumnList"/>
        ORDER BY CREATE_DATE DESC
    </select>

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

    <insert id="insert">
        INSERT INTO oauth_client_details (
            id,
            client_id,
            resource_ids,
            client_secret_plain_text,
            client_secret,
            scope,
            authorized_grant_types,
            web_server_redirect_uri,
            authorities,
            access_token_validity,
            refresh_token_validity,
            additional_information,
            autoapprove,
            creator,
            create_date,
            modifier,
            modify_date,
            del_flag,
            application_code,
            tenant_code
        ) VALUES (
            #{id},
            #{clientId},
            #{resourceIds},
            #{clientSecretPlainText},
            #{clientSecret},
            #{scope},
            #{authorizedGrantTypes},
            #{webServerRedirectUri},
            #{authorities},
            #{accessTokenValidity},
            #{refreshTokenValidity},
            #{additionalInformation},
            #{autoapprove},
            #{creator},
            #{createDate},
            #{modifier},
            #{modifyDate},
            #{delFlag},
            #{applicationCode},
            #{tenantCode}
        )
    </insert>

    <update id="update">
        UPDATE oauth_client_details SET
        <if test="clientId != null">
            client_id = #{clientId},
        </if>
        <if test="resourceIds != null">
            resource_ids = #{resourceIds},
        </if>
        <if test="clientSecretPlainText != null">
            client_secret_plain_text = #{clientSecretPlainText},
        </if>
        <if test="clientSecret != null">
            client_secret = #{clientSecret},
        </if>
        <if test="scope != null">
            scope = #{scope},
        </if>
        <if test="authorizedGrantTypes != null">
            authorized_grant_types = #{authorizedGrantTypes},
        </if>
        <if test="webServerRedirectUri != null">
            web_server_redirect_uri = #{webServerRedirectUri},
        </if>
        <if test="authorities != null">
            authorities = #{authorities},
        </if>
        <if test="accessTokenValidity != null">
            access_token_validity = #{accessTokenValidity},
        </if>
        <if test="refreshTokenValidity != null">
            refresh_token_validity = #{refreshTokenValidity},
        </if>
        <if test="additionalInformation != null">
            additional_information = #{additionalInformation},
        </if>
        <if test="autoapprove != null">
            autoapprove = #{autoapprove},
        </if>
        <if test="delFlag != null">
            del_flag = #{delFlag},
        </if>
        modifier = #{modifier},
        modify_date = #{modifyDate}
        WHERE id = #{id}
    </update>

    <delete id="delete">
        UPDATE oauth_client_details SET
        DEL_FLAG = 1
        WHERE ID = #{id}
    </delete>

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