<?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.iformall.mapper.WxAuthorizerInfoMapper">
    <resultMap id="BaseResultMap" type="com.iformall.domain.po.WxAuthorizerInfo">
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="tenant_id" jdbcType="VARCHAR" property="tenantId"/>
        <result column="type" jdbcType="INTEGER" property="type"/>
        <result column="authorizer_appid" jdbcType="VARCHAR" property="authorizerAppid"/>
        <result column="head_img" jdbcType="VARCHAR" property="headImg"/>
        <result column="alias" jdbcType="VARCHAR" property="alias"/>
        <result column="qrcode_url" jdbcType="VARCHAR" property="qrcodeUrl"/>
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
        <result column="update_time" jdbcType="TIMESTAMP" property="updateTime"/>
        <result column="authorization_status" jdbcType="INTEGER" property="authorizationStatus"/>
        <result column="auth_time" jdbcType="TIMESTAMP" property="authTime"/>
        <result column="base_status" jdbcType="INTEGER" property="baseStatus"/>
        <result column="base_time" jdbcType="TIMESTAMP" property="baseTime"/>
        <result column="domain_status" jdbcType="INTEGER" property="domainStatus"/>
        <result column="domain_time" jdbcType="TIMESTAMP" property="domainTime"/>
        <result column="webdomain_status" jdbcType="INTEGER" property="webdomainStatus"/>
        <result column="webdomain_time" jdbcType="TIMESTAMP" property="webdomainTime"/>
        <result column="template_status" jdbcType="INTEGER" property="templateStatus"/>
        <result column="template_time" jdbcType="TIMESTAMP" property="templateTime"/>
        <result column="current_version" jdbcType="VARCHAR" property="currentVersion"/>
        <result column="current_desc" jdbcType="VARCHAR" property="currentDesc"/>
        <result column="release_time" jdbcType="TIMESTAMP" property="releaseTime"/>
        <result column="open_appid" jdbcType="VARCHAR" property="openAppid"/>
        <result column="bind_open_time" jdbcType="TIMESTAMP" property="bindOpenTime"/>
        <result column="refresh_token" jdbcType="VARCHAR" property="refreshToken"/>
        <result column="access_token" jdbcType="VARCHAR" property="accessToken"/>
        <result column="access_token_expire" jdbcType="TIMESTAMP" property="accessTokenExpire"/>
    </resultMap>

    <sql id="allColumns">
	    `id`,`tenant_id`,`type`,`authorizer_appid`,`head_img`,`alias`,`qrcode_url`,`create_time`,`authorization_status`,`auth_time`,
	    `base_status`, `base_time`, `domain_status`, `domain_time`, `webdomain_status`, `webdomain_time`, `template_status`, `template_time`,
	    `current_version`,`current_desc`,`release_time`,
	    `open_appid`,`bind_open_time`,`refresh_token`,
	    `access_token`,`access_token_expire`
	</sql>

    <sql id="dynamicWhereConditions">
        where 1 = 1
        <if test=" null != id ">
            and `id` = #{id}
        </if>
        <if test=" null != tenantId ">
            and `tenant_id` = #{tenantId}
        </if>
        <if test=" null != type ">
            and `type` = #{type}
        </if>
        <if test=" null != authorizerAppid ">
            and `authorizer_appid` = #{authorizerAppid}
        </if>
        <if test=" null != headImg ">
            and `head_img` like concat('%', #{headImg},'%')
        </if>
        <if test=" null != alias ">
            and `alias` like concat('%', #{alias},'%')
        </if>
        <if test=" null != qrcodeUrl ">
            and `qrcode_url` like concat('%', #{qrcodeUrl},'%')
        </if>
        <if test=" null != createTime ">
            and `create_time` = #{createTime}
        </if>
        <if test=" null != updateTime ">
            and `update_time` = #{updateTime}
        </if>
        <if test=" null != authorizationStatus ">
            and `authorization_status` = #{authorizationStatus}
        </if>
        <if test=" null != authTime ">
            and `auth_time` = #{authTime}
        </if>
        <if test=" null != baseStatus ">
            and `base_status` = #{baseStatus}
        </if>
        <if test=" null != baseTime ">
            and `base_time` = #{baseTime}
        </if>
        <if test=" null != domainStatus ">
            and `domain_status` = #{domainStatus}
        </if>
        <if test=" null != domainTime ">
            and `domain_time` = #{domainTime}
        </if>
        <if test=" null != webdomainStatus ">
            and `webdomain_status` = #{webdomainStatus}
        </if>
        <if test=" null != webdomainTime ">
            and `webdomain_time` = #{webdomainTime}
        </if>
        <if test=" null != templateStatus ">
            and `template_status` = #{templateStatus}
        </if>
        <if test=" null != templateTime ">
            and `template_time` = #{templateTime}
        </if>
        <if test=" null != currentVersion ">
            and `current_version` = #{currentVersion}
        </if>
        <if test=" null != currentDesc ">
            and `current_desc` like concat('%', #{currentDesc},'%')
        </if>
        <if test=" null != releaseTime ">
            and `release_time` = #{releaseTime}
        </if>
        <if test=" null != openAppid ">
            and `open_appid` = #{openAppid}
        </if>
        <if test=" null != bindOpenTime ">
            and `bind_open_time` = #{bindOpenTime}
        </if>
        <if test=" null != ids ">
            and id in
            <foreach collection="ids" index="index" item="idItem" open="(" separator="," close=")">
                #{idItem}
            </foreach>
        </if>
        <if test=" null != sortColumns">order by ${sortColumns}</if>
    </sql>

    <select id="findList" parameterType="com.iformall.domain.po.WxAuthorizerInfo" resultMap="BaseResultMap">
        select
        <include refid="allColumns"/>
        from wx_authorizer_info
        <include refid="dynamicWhereConditions"/>
    </select>

    <update id="updateReleaseInfo" parameterType="com.iformall.domain.po.WxAuthorizerInfo">
        update wx_authorizer_info
        set  `current_version` = #{currentVersion}, `current_desc` = #{currentDesc}, `release_time` = #{releaseTime}, `update_time` = #{updateTime}
        where `authorizer_appid` = #{authorizerAppid}
    </update>

    <update id="updateRefreshToken" parameterType="com.iformall.domain.po.WxAuthorizerInfo">
        update wx_authorizer_info
        set  `refresh_token` = #{refreshToken}
        where `authorizer_appid` = #{authorizerAppid}
    </update>

    <update id="updateAccessToken" parameterType="com.iformall.domain.po.WxAuthorizerInfo">
        update wx_authorizer_info
        set  `access_token` = #{accessToken}, `access_token_expire` = #{accessTokenExpire}
        where `authorizer_appid` = #{authorizerAppid}
    </update>

    <update id="updateAuthBackInfo" parameterType="com.iformall.domain.po.WxAuthorizerInfo">
        update wx_authorizer_info
        set  `auth_back_num` = `auth_back_num` + 1, `auth_back_time` = #{authBackTime}
        where `authorizer_appid` = #{authorizerAppid}
    </update>

    <update id="removeOpenAppId" parameterType="com.iformall.domain.po.WxAuthorizerInfo">
        update wx_authorizer_info
        set  `open_appid` = null, `bind_open_time` = null
        where `authorizer_appid` = #{authorizerAppid}
    </update>

    <select id="findMp" parameterType="com.iformall.domain.po.WxAuthorizerInfo" resultMap="BaseResultMap">
        select
        <include refid="allColumns"/>
        FROM wx_authorizer_info
        where open_appid = (select open_appid from wx_authorizer_info where authorizer_appid = #{authorizerAppid})
        and `type` = 3 and `tenant_id` = #{tenantId}
    </select>

    <select id="findWeChatMp" parameterType="com.iformall.domain.po.WxAuthorizerInfo" resultMap="BaseResultMap">
        select
        <include refid="allColumns"/>
        from wx_authorizer_info
        where `authorizer_appid` = #{authorizerAppid}
    </select>

    <select id="findByAppid" parameterType="string" resultMap="BaseResultMap">
        select
        <include refid="allColumns"/>
        from wx_authorizer_info
        where `authorizer_appid` = #{value}
    </select>

    <resultMap id="VBaseResultMap" type="com.iformall.domain.vo.WxWeappInfo">
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="tenant_id" jdbcType="VARCHAR" property="tenantId"/>
        <result column="type" jdbcType="INTEGER" property="type"/>
        <result column="authorizer_appid" jdbcType="VARCHAR" property="authorizerAppid"/>
        <result column="head_img" jdbcType="VARCHAR" property="headImg"/>
        <result column="alias" jdbcType="VARCHAR" property="alias"/>
        <result column="qrcode_url" jdbcType="VARCHAR" property="qrcodeUrl"/>
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
        <result column="update_time" jdbcType="TIMESTAMP" property="updateTime"/>
        <result column="authorization_status" jdbcType="INTEGER" property="authorizationStatus"/>
        <result column="auth_time" jdbcType="TIMESTAMP" property="authTime"/>
        <result column="base_status" jdbcType="INTEGER" property="baseStatus"/>
        <result column="base_time" jdbcType="TIMESTAMP" property="baseTime"/>
        <result column="domain_status" jdbcType="INTEGER" property="domainStatus"/>
        <result column="domain_time" jdbcType="TIMESTAMP" property="domainTime"/>
        <result column="webdomain_status" jdbcType="INTEGER" property="webdomainStatus"/>
        <result column="webdomain_time" jdbcType="TIMESTAMP" property="webdomainTime"/>
        <result column="template_status" jdbcType="INTEGER" property="templateStatus"/>
        <result column="template_time" jdbcType="TIMESTAMP" property="templateTime"/>
        <result column="current_version" jdbcType="VARCHAR" property="currentVersion"/>
        <result column="current_desc" jdbcType="VARCHAR" property="currentDesc"/>
        <result column="release_time" jdbcType="TIMESTAMP" property="releaseTime"/>
        <result column="open_appid" jdbcType="VARCHAR" property="openAppid"/>
        <result column="bind_open_time" jdbcType="TIMESTAMP" property="bindOpenTime"/>
        <result column="name" jdbcType="VARCHAR" property="name"/>
    </resultMap>

    <sql id="allVColumns">
	    ai.`id`,ai.`tenant_id`,ai.`type`,ai.`authorizer_appid`,ai.`head_img`,ai.`alias`,ai.`qrcode_url`,ai.`create_time`,ai.`update_time`,
	    ai.`authorization_status`,ai.`auth_time`,
	    ai.`base_status`,ai.`base_time`,
        ai.`domain_status`,ai.`domain_time`,
        ai.`webdomain_status`,ai.`webdomain_time`,
        ai.`template_status`,ai.`template_time`,
	    ai.`current_version`,ai.`current_desc`,ai.`release_time`,
	    ai.`open_appid`,ai.`bind_open_time`,
	    a.`name`
	</sql>

    <sql id="dynamicVWhereConditions">
        where 1 = 1
        <if test=" null != id ">
            and ai.`id` = #{id}
        </if>
        <if test=" null != name ">
            and a.`name` like concat('%', #{name},'%')
        </if>
        <if test=" null != tenantId ">
            and ai.`tenant_id` = #{tenantId}
        </if>
        <if test=" null != type ">
            and ai.`type` = #{type}
        </if>
        <if test=" null != authorizerAppid ">
            and ai.`authorizer_appid` = #{authorizerAppid}
        </if>
        <if test=" null != authorizationStatus ">
            and ai.`authorization_status` = #{authorizationStatus}
        </if>
        <if test=" null != authTime ">
            and ai.`auth_time` = #{authTime}
        </if>
        <if test=" null != baseStatus ">
            and ai.`base_status` = #{baseStatus}
        </if>
        <if test=" null != baseTime ">
            and ai.`base_time` = #{baseTime}
        </if>
        <if test=" null != domainStatus ">
            and ai.`domain_status` = #{domainStatus}
        </if>
        <if test=" null != domainTime ">
            and ai.`domain_time` = #{domainTime}
        </if>
        <if test=" null != webdomainStatus ">
            and ai.`webdomain_status` = #{webdomainStatus}
        </if>
        <if test=" null != webdomainTime ">
            and ai.`webdomain_time` = #{webdomainTime}
        </if>
        <if test=" null != templateStatus ">
            and ai.`template_status` = #{templateStatus}
        </if>
        <if test=" null != templateTime ">
            and ai.`template_time` = #{templateTime}
        </if>
        <if test=" null != currentVersion ">
            and ai.`current_version` = #{currentVersion}
        </if>
        <if test=" null != openAppid ">
            and ai.`open_appid` = #{openAppid}
        </if>
        <if test=" null != ids ">
            and ai.id in
            <foreach collection="ids" index="index" item="idItem" open="(" separator="," close=")">
                #{idItem}
            </foreach>
        </if>
        <if test=" null != sortColumns">order by ${sortColumns}</if>
    </sql>

    <select id="findVoList" parameterType="com.iformall.domain.vo.WxWeappInfo" resultMap="VBaseResultMap">
        select
        <include refid="allVColumns"/>
        from wx_authorizer_info ai
        left join wx_appinfo a on ai.`authorizer_appid` = a.app_id
        <include refid="dynamicVWhereConditions"/>
    </select>

    <select id="findWeappList" resultMap="VBaseResultMap">
        select
        <include refid="allVColumns"/>
        from wx_authorizer_info ai
        left join wx_appinfo a on ai.`authorizer_appid` = a.app_id
        where a.type = 1 or a.type = 2
    </select>

    <select id="findWxMpList" resultMap="VBaseResultMap">
        select
        <include refid="allVColumns"/>
        from wx_authorizer_info ai
        left join wx_appinfo a on ai.`authorizer_appid` = a.app_id
        where a.type = 3 or a.type = 4
    </select>


</mapper>
