<?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.tmerclub.cloud.live.mapper.LiveRoomProdMapper">
  <resultMap id="liveRoomProdMap" type="com.tmerclub.cloud.live.model.LiveRoomProd">
    <id column="room_prod_id" property="roomProdId" />
    <result column="spu_id" property="spuId"/>
    <result column="room_id" property="roomId"/>
    <result column="explain_status" property="explainStatus"/>
  </resultMap>
  <insert id="saveBatch">
      insert into live_room_prod (`spu_id`,`room_id`,`explain_status`)
      values
      <foreach collection="liveRoomProdList" item="liveRoomProd" separator=",">
        (#{liveRoomProd.spuId},#{liveRoomProd.roomId},#{liveRoomProd.explainStatus})
      </foreach>
  </insert>
  <delete id="deleteByRoomId">
    delete from live_room_prod where room_id = #{roomId}
  </delete>
  <delete id="deleteBySpuId">
    delete from live_room_prod where spu_id = #{spuId} and room_id in
    <foreach collection="roomIds" item="roomId" open="(" close=")" separator=",">
      #{roomId}
    </foreach>
  </delete>
  <select id="getByRoomId" resultType="com.tmerclub.cloud.live.vo.LiveRoomProdVO">
    select `spu_id`, `room_id`, `explain_status`, `room_prod_id`
    from live_room_prod
    where room_id = #{roomId}
    order by room_prod_id
  </select>
  <update id="updateExplainStatus">
    update live_room_prod set explain_status = #{explainStatus}
    where room_id = #{roomId} and spu_id = #{spuId}
  </update>
  <select id="listRoomIdBySpuId" resultType="java.lang.Long">
    SELECT lr.room_id
    FROM live_room_prod rp
    JOIN live_room lr ON lr.room_id = rp.room_id
    WHERE rp.spu_id = #{spuId} AND lr.live_status IN (0, 1, 3) AND lr.end_time > NOW()
  </select>
  <select id="getLivingRoomBySpuId" resultType="com.tmerclub.cloud.live.vo.LiveRoomProdVO">
    select lr.room_id, lr.user_id
    from live_room_prod rp
    JOIN live_room lr ON lr.room_id = rp.room_id
    WHERE rp.spu_id = #{spuId} AND lr.live_status = 1 AND lr.end_time > NOW()
  </select>
</mapper>
