<?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.ym.mec.biz.dal.dao.ImLiveBroadcastRoomDao">
    <resultMap id="BaseResultMap" type="com.ym.mec.biz.dal.entity.ImLiveBroadcastRoom">
        <id column="id_" jdbcType="INTEGER" property="id"/>
        <result column="tenant_id_" jdbcType="INTEGER" property="tenantId"/>
        <result column="speaker_id_" jdbcType="INTEGER" property="speakerId"/>
        <result column="room_uid_" jdbcType="VARCHAR" property="roomUid"/>
        <result column="room_title_" jdbcType="VARCHAR" property="roomTitle"/>
        <result column="live_start_time_" jdbcType="TIMESTAMP" property="liveStartTime"/>
        <result column="live_end_time_" jdbcType="TIMESTAMP" property="liveEndTime"/>
        <result column="live_remark_" jdbcType="VARCHAR" property="liveRemark"/>
        <result column="pre_template_" jdbcType="VARCHAR" property="preTemplate"/>
        <result column="room_config_" jdbcType="VARCHAR" property="roomConfig"/>
        <result column="live_state_" jdbcType="INTEGER" property="liveState"/>
        <result column="room_state_" jdbcType="INTEGER" property="roomState"/>
        <result column="popularize_" jdbcType="INTEGER" property="popularize"/>
        <result column="popularize_type_" jdbcType="VARCHAR" property="popularizeType"/>
        <result column="os_" jdbcType="VARCHAR" property="os"/>
        <result column="created_by_" jdbcType="INTEGER" property="createdBy"/>
        <result column="created_time_" jdbcType="TIMESTAMP" property="createdTime"/>
        <result column="updated_by_" jdbcType="INTEGER" property="updatedBy"/>
        <result column="updated_time_" jdbcType="TIMESTAMP" property="updatedTime"/>
    </resultMap>

    <insert id="insertBatch" keyColumn="id_" keyProperty="id" useGeneratedKeys="true"
            parameterType="com.ym.mec.biz.dal.entity.ImLiveBroadcastRoom">
        insert into im_live_broadcast_room(tenant_id_, speaker_id_, room_uid_, room_title_, live_start_time_,
        live_end_time_, live_remark_, pre_template_, room_config_, live_state_, room_state_,
        popularize_, popularize_type_, os_, created_by_, created_time_, updated_by_, updated_time_)
        values
        <foreach collection="entities" item="entity" separator=",">
            (#{entity.tenantId}, #{entity.speakerId}, #{entity.roomUid}, #{entity.roomTitle}, #{entity.liveStartTime},
            #{entity.liveEndTime}, #{entity.liveRemark}, #{entity.preTemplate}, #{entity.roomConfig},
            #{entity.liveState}, #{entity.roomState}, #{entity.popularize}, #{entity.popularizeType},#{entity.os},
            #{entity.createdBy}, #{entity.createdTime}, #{entity.updatedBy},#{entity.updatedTime})
        </foreach>
    </insert>

    <select id="queryPage" resultType="com.ym.mec.biz.dal.vo.ImLiveBroadcastRoomVo">
        select distinct a.id_ AS id,
        a.tenant_id_ AS tenantId,
        t.name_ AS tenantName,
        t.logo_ AS tenantLogo,
        a.os_ AS os,
        a.room_uid_ AS roomUid,
        a.room_title_ AS roomTitle,
        a.live_remark_ AS liveRemark,
        a.speaker_id_ AS speakerId,
        b.real_name_ AS speakerName,
        b.im_token_ as imToken,
        b.avatar_ AS speakerPic,
        a.live_start_time_ AS liveStartTime,
        a.live_state_ AS liveState,
        a.room_state_ AS roomState,
        c.real_name_ AS createdByName,
        a.pre_template_ AS preTemplate,
        a.room_config_ AS roomConfig,
        a.popularize_ AS popularize,
        a.popularize_type_ AS popularizeType
        from im_live_broadcast_room as a
        left join tenant_info AS t on a.tenant_id_ = t.id_
        left join sys_user AS b on a.speaker_id_ = b.id_
        left join sys_user AS c on a.created_by_ = c.id_
        <where>
            a.room_state_ in(0, 2)
            <if test="param.search != null ">
                AND (
                a.`id_` LIKE CONCAT('%', #{param.search},'%')
                OR a.`room_title_` LIKE CONCAT('%', #{param.search},'%')
                )
            </if>
            <if test="param.tenantId != null ">
                AND a.tenant_id_ = #{param.tenantId}
            </if>
            <if test="param.roomUid != null">
                and a.room_uid_ = #{param.roomUid}
            </if>
            <if test="param.liveState != null">
                and a.live_state_ = #{param.liveState}
            </if>
            <if test="param.startTime != null">
                <![CDATA[ AND a.live_start_time_  >= #{param.startTime} ]]>
            </if>
            <if test="param.endTime != null">
                <![CDATA[ AND a.live_start_time_  <= #{param.endTime} ]]>
            </if>
            <if test="param.popularize != null">
                and a.popularize_ = #{param.popularize}
            </if>
            <if test="param.speakerId != null">
                and  a.speaker_id_ = #{param.speakerId}
            </if>
        </where>
    </select>

    <select id="queryBaseUserInfo" resultType="com.ym.mec.biz.dal.vo.BaseRoomUserVo">
        select
        id_ AS userId,
        username_ AS userName
        from sys_user
        <where>
            id_ IN
            <foreach collection="list" item="id" open="(" separator="," close=")">
                #{id}
            </foreach>
        </where>
    </select>
    <resultMap id="LiveRoomGoodsOrderVo" type="com.ym.mec.biz.dal.vo.LiveRoomGoodsOrderVo">
        <result property="userId" column="user_id_"/>
        <result property="userName" column="username_"/>
        <result property="phone" column="phone_"/>
        <result property="goodsName" column="goods_name_"/>
        <result property="actualPrice" column="actual_amount_"/>
        <result property="expectPrice" column="expect_amount_"/>
        <result property="balance" column="balance_payment_amount_"/>
        <result property="couponRemitFee" column="coupon_remit_fee_"/>
        <result property="payTime" column="pay_time_"/>
        <result property="goodsName" column="goods_name_"/>
    </resultMap>
    <sql id="queryLiveRoomGoodsOrderSql">
        <where>
            spo.group_type_ = 'LIVE_BUY' AND spo.type_ = 'LIVE_BUY'
            AND spo.status_ = 'SUCCESS'
            <if test="search != null and search != ''">
                AND (su.id_ = #{search} OR su.username_ LIKE CONCAT('%',#{search},'%') OR su.phone_ LIKE CONCAT('%',#{search},'%'))
            </if>
            <if test="roomUid != null and roomUid != ''">
                AND spo.music_group_id_ = #{roomUid}
            </if>
            <if test="startTime != null">
                AND DATE_FORMAT(spo.pay_time_, '%Y-%m-%d') >= #{startTime}
            </if>
            <if test="endTime != null">
                AND DATE_FORMAT(spo.pay_time_, '%Y-%m-%d') &lt;= #{endTime}
            </if>
        </where>
    </sql>
    <select id="queryLiveRoomGoodsOrderList" resultMap="LiveRoomGoodsOrderVo">
        SELECT su.id_ user_id_,su.username_,su.phone_,spo.actual_amount_,spo.expect_amount_,
        spo.balance_payment_amount_,spo.coupon_remit_fee_,spo.pay_time_,GROUP_CONCAT(lg.name_) goods_name_ FROM student_payment_order spo
        LEFT JOIN student_payment_order_detail spod ON spo.id_ = spod.payment_order_id_
        LEFT JOIN live_goods lg ON FIND_IN_SET(lg.id_,spod.goods_id_list_)
        LEFT JOIN sys_user su ON su.id_ = spo.user_id_
        <include refid="queryLiveRoomGoodsOrderSql"/>
        GROUP BY spo.id_
        ORDER BY spo.pay_time_ DESC
        <include refid="global.limit"/>
    </select>
    <select id="countLiveRoomGoodsOrderList" resultType="java.lang.Integer">
        SELECT COUNT(DISTINCT spo.id_) FROM student_payment_order spo
        LEFT JOIN sys_user su ON su.id_ = spo.user_id_
        <include refid="queryLiveRoomGoodsOrderSql"/>
    </select>
    <select id="sumLiveRoomGoodsOrderList" resultType="java.util.Map">
        SELECT COUNT(DISTINCT spo.user_id_) buyNum,SUM(spo.expect_amount_) totalAmount,
        SUM(spo.balance_payment_amount_) balance,SUM(spo.actual_amount_) actualAmount
        FROM student_payment_order spo
        LEFT JOIN sys_user su ON su.id_ = spo.user_id_
        <include refid="queryLiveRoomGoodsOrderSql"/>
    </select>

    <select id="queryRoomUser" resultType="com.ym.mec.biz.dal.vo.RoomReservationUserVo">
        select
        su.id_ as userId
        ,su.username_ as username
        ,su.phone_ as phone
        ,su.organ_id_ as organId
        ,o.name_ as organName
        ,(select group_concat(sub.name_) from subject sub
        where find_in_set(sub.id_,s.subject_id_list_) and sub.del_flag_ = 0 ) as subjectName
        from im_live_room_reservation ilrr
        left join sys_user su on su.id_ = ilrr.user_id_
        left join organization o on o.id_ = su.organ_id_
        left join student s on s.user_id_ = su.id_
        <where>
            ilrr.room_uid_ = #{query.roomUid}
            <if test="query.search != null and query.search != ''">
                and (su.id_ like concat('%',#{query.search},'%')
                or su.phone_ like concat('%',#{query.search},'%')
                or su.username_ like concat('%',#{query.search},'%'))
            </if>
            <if test="query.subjectId != null">
                and find_in_set(#{query.subjectId},s.subject_id_list_)
            </if>
            <if test="query.organId != null">
                and su.organ_id_ = #{query.organId}
            </if>
        </where>
    </select>

    <select id="querySchoolIds" resultType="string">
        select GROUP_CONCAT(id_)  from cooperation_organ where  find_in_set(organ_id_,#{organIds})
    </select>

    <select id="queryTeamIds" resultType="string">
    select group_concat(id_)
    from music_group where organ_id_ in (#{organIds}) group by organ_id_
    </select>

</mapper>
