<?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.LiveRoomMapper">
  <resultMap id="liveRoomVOMap" type="com.tmerclub.cloud.live.vo.LiveRoomVO">
    <id column="room_id" property="roomId" />
    <result column="shop_id" property="shopId"/>
    <result column="name" property="name"/>
    <result column="user_id" property="userId"/>
    <result column="live_status" property="liveStatus"/>
    <result column="cover_img" property="coverImg"/>
    <result column="share_img" property="shareImg"/>
    <result column="feeds_img" property="feedsImg"/>
    <result column="room_top" property="roomTop"/>
    <result column="start_time" property="startTime"/>
    <result column="end_time" property="endTime"/>
    <result column="create_time" property="createTime"/>
    <result column="update_time" property="updateTime"/>
    <result column="remark" property="remark"/>
    <collection property="spuList" ofType="com.tmerclub.cloud.common.product.vo.search.SpuSearchVO">
      <result column="spu_id" property="spuId"/>
    </collection>
  </resultMap>
  <sql id="Vo_Column_List">
    `room_id`,`shop_id`,`name`,`user_id`,`live_status`,`cover_img`,`share_img`,`feeds_img`,`room_top`,`start_time`,`end_time`,`create_time`,`update_time`,`remark`
  </sql>
  <select id="list" resultType="com.tmerclub.cloud.live.vo.LiveRoomVO">
    select <include refid="Vo_Column_List"/>
    from live_room lr
    <where>
      <if test="liveRoom.shopId != null">
        lr.shop_id = #{liveRoom.shopId}
      </if>
      <if test="liveRoom.roomTop != null">
        AND lr.room_top = #{liveRoom.roomTop}
      </if>
      <if test="liveRoom.name != null and liveRoom.name != ''">
        AND lr.`name` LIKE CONCAT('%',#{liveRoom.name},'%')
      </if>
      <if test="liveRoom.liveStatus != null">
        AND lr.live_status = #{liveRoom.liveStatus}
      </if>
      <if test="liveRoom.startTime != null">
        AND lr.start_time &gt;= #{liveRoom.startTime}
      </if>
      <if test="liveRoom.endTime != null">
        AND lr.end_time &lt;= #{liveRoom.endTime}
      </if>
      <if test="liveRoom.userIds != null">
        AND lr.user_id IN
        <foreach collection="liveRoom.userIds" item="userId" open="(" close=")" separator=",">
          #{userId}
        </foreach>
      </if>
    </where>
    order by start_time desc
  </select>
  <select id="getByRoomId" resultType="com.tmerclub.cloud.live.vo.LiveRoomVO">
    select <include refid="Vo_Column_List"/> from live_room where room_id = #{roomId}
  </select>
  <insert id="save" useGeneratedKeys="true" keyProperty="roomId">
    insert into live_room (`shop_id`,`name`,`user_id`,`live_status`,`cover_img`,`share_img`,`feeds_img`,`room_top`,`start_time`,`end_time`,`remark`)
    values (#{liveRoom.shopId},#{liveRoom.name},#{liveRoom.userId},#{liveRoom.liveStatus},#{liveRoom.coverImg},#{liveRoom.shareImg},#{liveRoom.feedsImg},#{liveRoom.roomTop},#{liveRoom.startTime},#{liveRoom.endTime},#{liveRoom.remark});
  </insert>
  <update id="update">
    update live_room
    <set>
      <if test="liveRoom.shopId != null">
        `shop_id` = #{liveRoom.shopId},
      </if>
      <if test="liveRoom.name != null">
        `name` = #{liveRoom.name},
      </if>
      <if test="liveRoom.userId != null">
        `user_id` = #{liveRoom.userId},
      </if>
      <if test="liveRoom.liveStatus != null">
        `live_status` = #{liveRoom.liveStatus},
      </if>
      <if test="liveRoom.coverImg != null">
        `cover_img` = #{liveRoom.coverImg},
      </if>
      <if test="liveRoom.shareImg != null">
        `share_img` = #{liveRoom.shareImg},
      </if>
      <if test="liveRoom.feedsImg != null">
        `feeds_img` = #{liveRoom.feedsImg},
      </if>
      <if test="liveRoom.roomTop != null">
        `room_top` = #{liveRoom.roomTop},
      </if>
      <if test="liveRoom.startTime != null">
        `start_time` = #{liveRoom.startTime},
      </if>
      <if test="liveRoom.endTime != null">
        `end_time` = #{liveRoom.endTime},
      </if>
      <if test="liveRoom.remark != null">
        `remark` = #{liveRoom.remark},
      </if>
    </set>
    where room_id = #{liveRoom.roomId}
  </update>
  <delete id="deleteById">
    delete from live_room where room_id = #{roomId}
  </delete>
  <select id="countRoomByParam" resultType="int">
    SELECT COUNT(room_id) as num FROM live_room
    <where>
      live_status != -1
      <if test="shopId != null">
        AND shop_id = #{shopId}
      </if>
      <if test="userId != null">
        AND user_id = #{userId}
      </if>
      <if test="roomId != null">
        AND room_id != #{roomId}
      </if>
      <if test="startTime != null and endTime != null">
        AND ((start_time &lt;= #{startTime} AND end_time &gt;= #{startTime})
        OR (start_time &gt;= #{startTime} AND start_time &lt;= #{endTime}))
      </if>
    </where>
  </select>
  <select id="getLiveRoomInfo" resultType="com.tmerclub.cloud.live.vo.LiveRoomVO">
    SELECT lr.*
    FROM live_room lr
    WHERE lr.`live_status` != -1 AND lr.`end_time` &gt; NOW()
    <if test="userId != null">
      AND lr.`user_id` = #{userId}
    </if>
    <if test="roomId != null">
      AND lr.room_id = #{roomId}
    </if>
    order by lr.start_time limit 1
  </select>
  <select id="listRoomAndDetail" resultMap="liveRoomVOMap">
    select lr.*, lrp.spu_id from(select lr.`room_id`,lr.`shop_id`,lr.`name`,lr.`user_id`,lr.`live_status`,lr.`cover_img`,lr.`share_img`,
    lr.`feeds_img`,lr.`room_top`,lr.`start_time`,lr.`end_time`,lr.`create_time`,lr.`update_time`,lr.`remark`
    from live_room lr
    left join live_room_prod lrp on lrp.room_id = lr.room_id
    where lr.live_status in (0, 1, 3) and lr.end_time >= now() AND lr.start_time &lt; ADDDATE(NOW(), INTERVAL 60 MINUTE)
    <if test="liveRoom.searchType == 1">
      <if test="liveRoom.name != null and liveRoom.name != ''">
        AND lr.`name` LIKE CONCAT('%',#{liveRoom.name},'%')
      </if>
    </if>
    <if test="liveRoom.searchType == 2">
      <if test="liveRoom.spuIds != null and liveRoom.spuIds.size() > 0">
        AND lrp.spu_id IN (
        <foreach collection="liveRoom.spuIds" item="spuId" separator=",">
          #{spuId}
        </foreach>
        )
      </if>
    </if>
    <if test="liveRoom.searchType == 3">
      <if test="liveRoom.nickName != null and liveRoom.nickName != ''">
        AND lr.user_id IN (
        <foreach collection="liveRoom.userIds" item="userId" separator=",">
          #{userId}
        </foreach>
        )
      </if>
    </if>
    GROUP BY lr.room_id
    ORDER BY lr.room_top DESC, lr.start_time) lr
    left join live_room_prod lrp on lrp.room_id = lr.room_id
  </select>
  <select id="countRoomAndDetail" resultType="long">
    SELECT COUNT(*) as num FROM(select lr.`room_id`,lr.`shop_id`,lr.`name`,lr.`user_id`,lr.`live_status`,lr.`cover_img`,lr.`share_img`,
    lr.`feeds_img`,lr.`room_top`,lr.`start_time`,lr.`end_time`,lr.`create_time`,lr.`update_time`,lr.`remark`
    from live_room lr
    left join live_room_prod lrp on lrp.room_id = lr.room_id
    where lr.live_status in (0, 1, 3) and lr.end_time > now() AND lr.start_time &lt; ADDDATE(NOW(), INTERVAL 60 MINUTE)
    <if test="liveRoom.searchType == 1">
      <if test="liveRoom.name != null and liveRoom.name != ''">
        AND lr.`name` LIKE CONCAT('%',#{liveRoom.name},'%')
      </if>
    </if>
    <if test="liveRoom.searchType == 2">
      <if test="liveRoom.spuIds != null and liveRoom.spuIds.size() > 0">
        AND lrp.spu_id IN (
        <foreach collection="liveRoom.spuIds" item="spuId" separator=",">
          #{spuId}
        </foreach>
        )
      </if>
    </if>
    <if test="liveRoom.searchType == 3">
      <if test="liveRoom.nickName != null and liveRoom.nickName != ''">
        AND lr.user_id IN (
        <foreach collection="liveRoom.userIds" item="userId" separator=",">
          #{userId}
        </foreach>
        )
      </if>
    </if>
    GROUP BY lr.room_id
    ORDER BY lr.room_top DESC, lr.start_time) t
  </select>
  <update id="updateLiveRoomStatus">
    update live_room set live_status = #{liveStatus}
    where room_id in
    <foreach collection="roomIds" item="roomId" open="(" close=")" separator=",">
      #{roomId}
    </foreach>
  </update>
  <select id="listLiveRoomByStatus" resultType="java.lang.Long">
    SELECT room_id FROM live_room
    <where>
      live_status != -1
      <if test="liveStatus != null">
        <if test="liveStatus == 1">
          AND start_time &lt;= NOW() AND end_time &gt;= NOW() AND live_status = 0
        </if>
        <if test="liveStatus == 2">
          AND end_time &lt; NOW() AND live_status in(0, 1, 3)
        </if>
      </if>
    </where>
  </select>
  <select id="listNeedStopLiveRoom" resultType="java.lang.Long">
    SELECT room_id
    FROM live_room
    WHERE user_id = #{userId} AND live_status = 1
  </select>
  <select id="listRoomIdsByShopId" resultType="java.lang.Long">
    SELECT room_id
    FROM live_room
    WHERE live_status in (0, 1, 3) AND shop_id in
    <foreach collection="shopIds" item="shopId" open="(" close=")" separator=",">
      #{shopId}
    </foreach>
  </select>
</mapper>
