<?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="ink.wgink.login.oauth2.server.dao.IOAuth2ClientDao">

    <cache/>

    <resultMap id="oAuth2ClientSimpleDTO" type="ink.wgink.login.oauth2.server.pojo.dtos.OAuth2ClientSimpleDTO">
        <id property="clientId" column="client_id"/>
        <result property="clientName" column="client_name"/>
        <result property="webServerRedirectUri" column="web_server_redirect_uri"/>
        <result property="environment" column="environment"/>
        <result property="systemSummary" column="system_summary"/>
        <result property="systemIcon" column="system_icon"/>
        <result property="gmtCreate" column="gmt_create"/>
    </resultMap>

    <resultMap id="oAuth2ClientDTO" type="ink.wgink.login.oauth2.server.pojo.dtos.OAuth2ClientDTO" extends="oAuth2ClientSimpleDTO">
        <result property="clientSecret" column="client_secret"/>
        <result property="resourceIds" column="resource_ids"/>
        <result property="scope" column="scope"/>
        <result property="authorizedGrantTypes" column="authorized_grant_types"/>
        <result property="authorities" column="authorities"/>
        <result property="accessTokenValidity" column="access_token_validity"/>
        <result property="refreshTokenValidity" column="refresh_token_validity"/>
        <result property="additionalInformation" column="additional_information"/>
        <result property="autoapprove" column="autoapprove"/>
        <result property="menuId" column="menu_id"/>
        <result property="menuName" column="menu_name"/>
        <result property="environment" column="environment"/>
        <result property="systemType" column="system_type"/>
        <result property="systemState" column="system_state"/>
        <result property="expireTime" column="expire_time"/>
        <result property="systemSummary" column="system_summary"/>
        <result property="systemIcon" column="system_icon"/>
        <result property="sort" column="sort"/>
        <result property="menuId" column="menu_id"/>
    </resultMap>

    <resultMap id="oAuth2ClientPO" type="ink.wgink.login.oauth2.server.pojo.pos.OAuth2ClientPO">
        <id property="clientId" column="client_id"/>
        <result property="clientName" column="client_name"/>
        <result property="webServerRedirectUri" column="web_server_redirect_uri"/>
        <result property="gmtCreate" column="gmt_create"/>
        <result property="clientSecret" column="client_secret"/>
        <result property="resourceIds" column="resource_ids"/>
        <result property="scope" column="scope"/>
        <result property="authorizedGrantTypes" column="authorized_grant_types"/>
        <result property="authorities" column="authorities"/>
        <result property="accessTokenValidity" column="access_token_validity"/>
        <result property="refreshTokenValidity" column="refresh_token_validity"/>
        <result property="additionalInformation" column="additional_information"/>
        <result property="autoapprove" column="autoapprove"/>
        <result property="menuId" column="menu_id"/>
        <result property="environment" column="environment"/>
        <result property="systemType" column="system_type"/>
        <result property="systemState" column="system_state"/>
        <result property="expireTime" column="expire_time"/>
        <result property="systemSummary" column="system_summary"/>
        <result property="systemIcon" column="system_icon"/>
        <result property="sort" column="sort"/>
        <result property="gmtCreate" column="gmt_create"/>
        <result property="creator" column="creator"/>
        <result property="gmtModified" column="gmt_create"/>
        <result property="modifier" column="modifier"/>
        <result property="isDelete" column="is_delete"/>
    </resultMap>

    <!-- 建表 -->
    <update id="createTable">
        CREATE TABLE IF NOT EXISTS `oauth_client_details` (
            `client_id` varchar(48) NOT NULL,
            `resource_ids` varchar(255) DEFAULT NULL,
            `client_secret` varchar(255) DEFAULT NULL,
            `scope` varchar(255) DEFAULT NULL,
            `authorized_grant_types` varchar(255) DEFAULT NULL,
            `web_server_redirect_uri` varchar(255) DEFAULT NULL,
            `authorities` varchar(255) DEFAULT NULL,
            `access_token_validity` int(11) DEFAULT NULL,
            `refresh_token_validity` int(11) DEFAULT NULL,
            `additional_information` varchar(4096) DEFAULT NULL,
            `autoapprove` varchar(256) DEFAULT NULL,
            `client_name` varchar(255) DEFAULT NULL,
            `menu_id` char(36) DEFAULT NULL COMMENT '菜单ID',
            `environment` varchar(10) DEFAULT 'formal' COMMENT '系统环境',
            `system_type` varchar(10) DEFAULT 'within' COMMENT '系统类型',
            `system_state` varchar(10) DEFAULT NULL COMMENT '系统状态',
            `expire_time` varchar(10) DEFAULT NULL COMMENT '系统到期时间',
            `system_summary` text COMMENT '系统介绍',
            `system_icon` varchar(255) DEFAULT NULL COMMENT '系统图标',
            `sort` double(11, 4) DEFAULT NULL COMMENT '排序',
            `gmt_create` datetime DEFAULT NULL,
            `creator` char(36) DEFAULT NULL,
            `gmt_modified` datetime DEFAULT NULL,
            `modifier` char(36) DEFAULT NULL,
            `is_delete` int(1) DEFAULT '0',
            PRIMARY KEY (`client_id`)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
    </update>

    <!-- Oauth客户端新增 -->
    <insert id="save" parameterType="map" keyProperty="clientId" useGeneratedKeys="true" flushCache="true">
        INSERT INTO oauth_client_details(
            client_id,
            client_name,
            resource_ids,
            client_secret,
            scope,
            authorized_grant_types,
            web_server_redirect_uri,
            authorities,
            access_token_validity,
            refresh_token_validity,
            additional_information,
            autoapprove,
            environment,
            system_type,
            system_state,
            expire_time,
            system_summary,
            system_icon,
            sort,
            creator,
            gmt_create,
            modifier,
            gmt_modified,
            is_delete
        ) VALUES(
            #{clientId},
            #{clientName},
            #{resourceIds},
            #{clientSecret},
            #{scope},
            #{authorizedGrantTypes},
            #{webServerRedirectUri},
            #{authorities},
            #{accessTokenValidity},
            #{refreshTokenValidity},
            #{additionalInformation},
            #{autoapprove},
            #{environment},
            #{systemType},
            #{systemState},
            #{expireTime},
            #{systemSummary},
            #{systemIcon},
            #{sort},
            #{creator},
            #{gmtCreate},
            #{modifier},
            #{gmtModified},
            #{isDelete}
        )
    </insert>

    <!-- Oauth客户端删除 -->
    <delete id="remove" parameterType="map" flushCache="true">
        UPDATE
            oauth_client_details
        SET
            is_delete = 1,
            modifier = #{modifier},
            gmt_modified = #{gmtModified}
        WHERE
            client_id IN
        <foreach collection="clientIds" index="index" open="(" separator="," close=")">
            #{secretIds[${index}]}
        </foreach>
    </delete>

    <!-- Oauth客户端修改 -->
    <update id="update" parameterType="map" flushCache="true">
        UPDATE
            oauth_client_details
        SET
        <if test="clientName != null and clientName != ''">
            client_name = #{clientName},
        </if>
        <if test="resourceIds != null and resourceIds != ''">
            resource_ids = #{resourceIds},
        </if>
        <if test="scope != null and scope != ''">
            scope = #{scope},
        </if>
        <if test="authorizedGrantTypes != null and authorizedGrantTypes != ''">
            authorized_grant_types = #{authorizedGrantTypes},
        </if>
        <if test="webServerRedirectUri != null and webServerRedirectUri != ''">
            web_server_redirect_uri = #{webServerRedirectUri},
        </if>
        <if test="authorities != null and authorities != ''">
            authorities = #{authorities},
        </if>
        <if test="accessTokenValidity != null and accessTokenValidity != ''">
            access_token_validity = #{accessTokenValidity},
        </if>
        <if test="refreshTokenValidity != null and refreshTokenValidity != ''">
            refresh_token_validity = #{refreshTokenValidity},
        </if>
        <if test="additionalInformation != null and additionalInformation != ''">
            additional_information = #{additionalInformation},
        </if>
        <if test="autoapprove != null and autoapprove != ''">
            autoapprove = #{autoapprove},
        </if>
        <if test="menuId != null and menuId != ''">
            menu_id = #{menuId},
        </if>
        <if test="environment != null and environment != ''">
            environment = #{environment},
        </if>
        <if test="systemType != null and systemType != ''">
            system_type = #{systemType},
        </if>
        <if test="systemState != null">
            system_state = #{systemState},
        </if>
        <if test="expireTime != null">
            expire_time = #{expireTime},
        </if>
        <if test="systemSummary != null">
            system_summary = #{systemSummary},
        </if>
        <if test="systemIcon != null">
            system_icon = #{systemIcon},
        </if>
        <if test="sort != null">
            sort = #{sort},
        </if>
            modifier = #{modifier},
            gmt_modified = #{gmtModified}
        WHERE
            client_id = #{clientId}
    </update>

    <!-- Oauth客户端列表 -->
    <select id="list" parameterType="map" resultMap="oAuth2ClientDTO" useCache="false">
        SELECT
            t1.client_id,
            t1.client_name,
            t1.resource_ids,
            t1.scope,
            t1.authorized_grant_types,
            t1.web_server_redirect_uri,
            t1.authorities,
            t1.access_token_validity,
            t1.refresh_token_validity,
            t1.additional_information,
            t1.autoapprove,
            t1.environment,
            t1.system_type,
            t1.system_state,
            t1.expire_time,
            t1.system_summary,
            t1.system_icon,
            t1.menu_id,
            LEFT(t1.gmt_create, 10) gmt_create
        FROM
            oauth_client_details t1
        WHERE
            t1.is_delete = 0
        <if test="keywords != null and keywords != ''">
        AND
            t1.client_name LIKE CONCAT('%', #{keywords}, '%')
        </if>
        <if test="startTime != null and startTime != ''">
        AND
            t1.gmt_create <![CDATA[ >= ]]> #{startTime}
        </if>
        <if test="startTime != null and startTime != ''">
        AND
            t1.gmt_create <![CDATA[ <= ]]> #{endTime}
        </if>
        <if test="systemType != null and systemType != ''">
        AND
            t1.system_type = #{systemType}
        </if>
        <if test="environment != null and environment != ''">
        AND
            t1.environment = #{environment}
        </if>
        <choose>
            <when test="sort != null and (sort == 'clientId' or sort == 'clientName')">
                ORDER BY
                <if test="sort == 'clientId'">
                    t1.client_id ${order}
                </if>
                <if test="sort == 'clientName'">
                    t1.client_name ${order}
                </if>
            </when>
            <otherwise>
            ORDER BY
                t1.sort
            </otherwise>
        </choose>
    </select>

    <!-- Oauth客户端列表（简单） -->
    <select id="listSimple" parameterType="map" resultMap="oAuth2ClientSimpleDTO" useCache="true">
        SELECT
            t1.client_id,
            t1.client_name,
            t1.web_server_redirect_uri,
            t1.environment,
            t1.system_summary,
            t1.system_icon,
            LEFT(t1.gmt_create, 10) gmt_create
        FROM
            oauth_client_details t1
        WHERE
            t1.is_delete = 0
        <if test="environment != null and environment != ''">
        AND
            t1.environment = #{environment}
        </if>
        <if test="menuIds != null and menuIds.size > 0">
        AND
            t1.menu_id IN
            <foreach collection="menuIds" index="index" open="(" separator="," close=")">
                #{menuIds[${index}]}
            </foreach>
        </if>
        <choose>
            <when test="sort != null and (sort == 'clientId' or sort == 'clientName')">
            ORDER BY
                <if test="sort == 'clientId'">
                    t1.client_id ${order}
                </if>
                <if test="sort == 'clientName'">
                    t1.client_name ${order}
                </if>
            </when>
            <otherwise>
            ORDER BY
                t1.sort
            </otherwise>
        </choose>
    </select>

    <!-- Oauth客户端详情 -->
    <select id="get" parameterType="map" resultMap="oAuth2ClientDTO" useCache="false">
        SELECT
            t1.client_id,
            t1.client_name,
            t1.resource_ids,
            t1.scope,
            t1.authorized_grant_types,
            t1.web_server_redirect_uri,
            t1.authorities,
            t1.access_token_validity,
            t1.refresh_token_validity,
            t1.additional_information,
            t1.autoapprove,
            t1.environment,
            t1.system_type,
            t1.system_state,
            t1.expire_time,
            t1.system_summary,
            t1.system_icon,
            t1.sort,
            t2.menu_id,
            t2.menu_name
        FROM
            oauth_client_details t1
        LEFT JOIN
            sys_menu t2
        ON
            t1.menu_id = t2.menu_id
        AND
            t2.is_delete = 0
        WHERE
            t1.is_delete = 0
        <if test="clientId != null and clientId != ''">
        AND
            t1.client_id = #{clientId}
        </if>
    </select>

    <!-- Oauth客户端统计 -->
    <select id="count" parameterType="map" resultType="java.lang.Integer" useCache="true">
        SELECT
            count(*)
        FROM
            oauth_client_details
        WHERE
            is_delete = 0
    </select>

    <!-- 获取Oauth客户端（单表） -->
    <select id="getPO" parameterType="map" resultMap="oAuth2ClientPO" useCache="true">
        SELECT
            t1.client_id,
            t1.client_name,
            t1.resource_ids,
            t1.client_secret,
            t1.scope,
            t1.authorized_grant_types,
            t1.web_server_redirect_uri,
            t1.authorities,
            t1.access_token_validity,
            t1.refresh_token_validity,
            t1.additional_information,
            t1.autoapprove,
            t1.environment,
            t1.system_type,
            t1.system_state,
            t1.expire_time,
            t1.system_summary,
            t1.system_icon,
            t1.sort,
            t1.menu_id,
            t1.gmt_create,
            t1.creator,
            t1.gmt_modified,
            t1.modifier,
            t1.is_delete
        FROM
            oauth_client_details t1
        WHERE
            t1.is_delete = 0
        <if test="clientId != null and clientId != ''">
        AND
            t1.client_id = #{clientId}
        </if>
    </select>

    <!-- 清空客户端 -->
    <update id="updateMenu" parameterType="map" flushCache="true">
        UPDATE
            oauth_client_details
        SET
            menu_id = #{menuId}
        WHERE
            client_id = #{clientId}
    </update>

</mapper>