<?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.xhkjedu.slive.mapper.agora.AgoralicenseMapper">
    <!--列表-->
    <select id="listAll" resultType="com.xhkjedu.slive.vo.agora.AgoralicenseVo">
        select l.agid, l.custom, l.cert, l.credential, l.activated, l.used, l.userid, l.createid, l.createtime
        ,min(lr.activatedtime)starttime,(max(lr.activatedtime)+lr.validityperiod*24*60*60)endtime
        ,l.username,l.loginname,l.cloudcode,c.cloudname
        from t_agoralicense l left join t_agoralicense_record lr on l.agid = lr.agid
        left join t_cloud c on l.cloudcode=c.cloudcode
        <where>
            <if test="license.activated!=null">and l.activated=#{license.activated}</if>
            <if test="license.used!=null">and l.used=#{license.used}</if>
            <if test="license.cloudcode!=null">and l.cloudcode=#{license.cloudcode}</if>
        </where>
        group by l.agid
    </select>
    <!--进入直播绑定license-->
    <select id="inLive" resultType="com.xhkjedu.slive.vo.agora.AgoralicenseVo">
        select agid, custom, cert, credential, activated, used, cloudcode, userid, createid, createtime
        from t_agoralicense where userid=#{userid} and activated=1 limit 1
    </select>
    <!--获取已激活license-->
    <select id="getActivatedLicense" resultType="com.xhkjedu.slive.vo.agora.AgoralicenseVo">
        select agid, custom, cert, credential, activated, used, cloudcode, userid, createid, createtime
        from t_agoralicense where activated=1 and used=0 order by rand() limit 1
    </select>
    <!--获取未激活license-->
    <select id="getLicense" resultType="com.xhkjedu.slive.vo.agora.AgoralicenseVo">
        select agid, custom, cert, credential, activated, used, cloudcode, userid, createid, createtime
        from t_agoralicense where activated=0 order by rand() limit 1
    </select>
    <!--退出直播解绑license-->
    <update id="outLive">
        update t_agoralicense set used=0,cloudcode=null,schoolid=null,userid=0,username=null,loginname=null
        where userid=#{userid};
    </update>
    <!--处理未释放的授权-->
    <update id="updateLicense">
        update t_agoralicense set used=0,cloudcode=null,schoolid=null,userid=0,username=null,loginname=null where used=1
    </update>
    <!--更新失效的license的激活状态-->
    <update id="updateActivate">
        update (SELECT agid,(max(activatedtime)+validityperiod*24*60*60)activatedtime
        FROM t_agoralicense_record GROUP BY agid)r LEFT JOIN t_agoralicense l ON r.agid=l.agid
        set l.activated=2
        WHERE r.activatedtime<![CDATA[ < ]]>#{nowtime}
    </update>
    <update id="endLive2">
        update t_agoralicense l set
        l.used=0,l.cloudcode=null,l.schoolid=null,l.userid=0,l.username=null,l.loginname=null
        where (
        <foreach collection="license.userids" item="userid" index="index" separator="or">
            l.userid=#{userid}
        </foreach>)
    </update>
    <!--获取学校正在使用直播人数-->
    <select id="getUseNum" resultType="java.lang.Integer">
        select count(*) from t_agoralicense where schoolid=#{schoolid} and used=1 and userid!=#{userid}
    </select>
</mapper>
