/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ifish.mapper;

import com.ifish.bean.LiveRoomPageInfo;
import com.ifish.bean.Tbl_Live_Message;
import com.ifish.bean.Tbl_Live_Room;
import com.ifish.bean.Tbl_Live_Watch;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectKey;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.annotations.UpdateProvider;

/**
 *
 * @author Administrator
 */
public interface Tbl_Live_Room_Mapper {

    /**
     * 建立一个直播间
     */
    @Insert("INSERT INTO tbl_live_room (user_id,camera_id,room_name,room_desc,room_status,popularity_value,create_time,room_Img) VALUES("
            + "#{liveroom.userId},#{liveroom.cameraId},#{liveroom.roomName},#{liveroom.roomDesc},#{liveroom.roomStatus},#{liveroom.popularityValue},now(),#{liveroom.roomImg})")
    @SelectKey(statement = "select @@IDENTITY as room_id", keyProperty = "liveroom.roomId", keyColumn = "room_id", before = false, resultType = int.class)
    Integer insertLiveRoom(@Param("liveroom") Tbl_Live_Room liveRoom);

    /**
     * 插入一条直播间评论
     *
     * @param live_Message
     * @return
     */
    @Insert("INSERT INTO TBL_LIVE_MESSAGE (room_id,user_id,as_user_id,message_content,create_time) VALUES (#{livemessage.roomId},#{livemessage.userId},#{livemessage.asUserId},#{livemessage.messageContent},#{livemessage.createTime})")
    @SelectKey(statement = "select @@IDENTITY as message_id", keyProperty = "livemessage.messageId", keyColumn = "message_id", before = false, resultType = int.class)
    Integer insertLiveMessage(@Param("livemessage") Tbl_Live_Message live_Message);

    /**
     * 查询直播间总赞数
     *
     * @param roomID
     * @return
     */
    @Select("select count(*) from tbl_live_room_zan where room_id=#{roomid} ")
    Integer getLiveRoomZanCountByRoomId(@Param("roomid") Integer roomID);

    /**
     * 查询某个用户是否点赞了某个直播间
     *
     * @param roomID
     * @param userID
     * @return
     */
    @Select("SELECT COUNT(1) FROM TBL_LIVE_ROOM_ZAN WHERE ROOM_ID = #{roomid} AND USER_ID = #{userid}")
    Integer IsZan(@Param("roomid") Integer roomID, @Param("userid") Integer userID);

    /**
     * 取消点赞
     *
     * @param roomID
     * @param userID
     * @return
     */
    @Delete("DELETE FROM TBL_LIVE_ROOM_ZAN WHERE  ROOM_ID = #{roomid} AND USER_ID = #{userid}")
    Integer cancelZan(@Param("roomid") Integer roomID, @Param("userid") Integer userID);

    /**
     * 执行点赞
     *
     * @param roomID
     * @param userID
     * @return
     */
    @Insert("INSERT INTO TBL_LIVE_ROOM_ZAN(ROOM_ID,USER_ID,CREATE_TIME) VALUES (#{roomid},#{userid},NOW())")
    Integer ImplementZan(@Param("roomid") Integer roomID, @Param("userid") Integer userID);

    /**
     * 根据房间ID查询直播间信息
     *
     * @param roomid
     * @return
     */
    @Select("SELECT ROOM_ID,USER_ID,CAMERA_ID,ROOM_NAME,ROOM_DESC,ROOM_STATUS,POPULARITY_VALUE,CREATE_TIME,UPDATE_TIME,room_Img,zanNum FROM TBL_LIVE_ROOM WHERE ROOM_ID = #{roomid}")
    Tbl_Live_Room getTbl_Live_RoomById(@Param("roomid") Integer roomid);

    /**
     * 根据开播时间来查询直播间
     *
     * @param pageSize
     * @param FirstResult
     * @return
     */
    @Select(" select a.roomId,a.userId,a.userType,a.nickName,a.userImg,a.loginTime,a.createTime,a.cameraId,a.roomName,a.roomDesc,a.popularityValue,a.roomImg,a.numberDays,a.zanNum,count(*) as pinglunNum from ("
            + " SELECT l.room_id AS roomId,u.user_id AS userId,u.user_type AS userType,u.nick_name AS nickName,u.user_img AS userImg,u.login_time AS loginTime,l.create_time AS createTime,"
            + " td.camera_id AS cameraId,l.room_name AS roomName,l.room_desc AS roomDesc,l.popularity_value AS popularityValue,l.room_Img AS roomImg,"
            + " (TO_DAYS(NOW())-TO_DAYS(l.create_time)) AS numberDays,l.zanNum,b.message_id"
            + " from tbl_user u"
            + " LEFT JOIN tbl_device_user c ON u.user_id=c.user_id "
            + " LEFT JOIN tbl_device td ON c.device_id = td.device_id"
            + " LEFT JOIN tbl_live_room l ON u.user_id=l.user_id and l.camera_id = td.camera_id"
            + " LEFT JOIN tbl_live_message b ON l.room_id = b.room_id"
            + " where c.is_live='1' AND l.room_status='1' ) as a"
            + " GROUP BY a.roomId,a.userId,a.userType,a.nickName,a.userImg,a.loginTime,a.createTime,a.cameraId,a.roomName,a.roomDesc,a.popularityValue,a.roomImg,a.numberDays,a.zanNum"
            + " ORDER BY a.createTime DESC LIMIT ${first},${pagesize}")
    List<LiveRoomPageInfo> getLiveRoomListByCreateTime(@Param("pagesize") Integer pageSize, @Param("first") Integer FirstResult);

    /**
     * 根据开播时间来查询直播间
     *
     * @param pageSize
     * @param FirstResult
     * @return
     */
    @Select(" select a.roomId,a.userId,a.userType,a.nickName,a.userImg,a.loginTime,a.createTime,a.cameraId,a.roomName,a.roomDesc,a.popularityValue,a.roomImg,a.numberDays,a.zanNum,count(*) as pinglunNum from ("
            + " SELECT l.room_id AS roomId,u.user_id AS userId,u.user_type AS userType,u.nick_name AS nickName,u.user_img AS userImg,u.login_time AS loginTime,l.create_time AS createTime,"
            + " td.camera_id AS cameraId,l.room_name AS roomName,l.room_desc AS roomDesc,l.popularity_value AS popularityValue,l.room_Img AS roomImg,"
            + " (TO_DAYS(NOW())-TO_DAYS(l.create_time)) AS numberDays,l.zanNum,b.message_id"
            + " from tbl_user u"
            + " LEFT JOIN tbl_device_user c ON u.user_id=c.user_id "
            + " LEFT JOIN tbl_device td ON c.device_id = td.device_id"
            + " LEFT JOIN tbl_live_room l ON u.user_id=l.user_id and l.camera_id = td.camera_id"
            + " LEFT JOIN tbl_live_message b ON l.room_id = b.room_id"
            + " where c.is_live='1' AND l.room_status='1' ) as a"
            + " GROUP BY a.roomId,a.userId,a.userType,a.nickName,a.userImg,a.loginTime,a.createTime,a.cameraId,a.roomName,a.roomDesc,a.popularityValue,a.roomImg,a.numberDays,a.zanNum"
            + " ORDER BY a.popularityValue DESC LIMIT ${first},${pagesize}")
    List<LiveRoomPageInfo> getLiveRoomListByRenqi(@Param("pagesize") Integer pageSize, @Param("first") Integer FirstResult);

    /**
     * 查询所有直播间数量
     *
     * @return
     */
    @Select("SELECT COUNT(1)"
            + " from tbl_user u"
            + " LEFT JOIN tbl_device_user c ON u.user_id=c.user_id "
            + " LEFT JOIN tbl_device td ON c.device_id = td.device_id"
            + " LEFT JOIN tbl_live_room l ON u.user_id=l.user_id and l.camera_id = td.camera_id"
            + " where c.is_live='1' AND l.room_status='1'")
    Integer getLiveRoomListCount();

    /**
     * 修改直播间信息
     *
     * @param live_Room
     * @return
     */
    @UpdateProvider(type = Tbl_Live_Room_MapperSql.class, method = "updateLiveRoom")
    Integer updateLiveRoom(@Param("live") Tbl_Live_Room live_Room);

    /**
     * 修改人气值
     *
     * @param live_Room
     * @return
     */
    @Update("UPDATE TBL_LIVE_ROOM SET popularity_value = #{live.popularityValue} WHERE room_id = #{live.roomId}")
    Integer updateLiveRoomPopularityValue(@Param("live") Tbl_Live_Room live_Room);

    /**
     * 根据直播间ID获取此直播间评论列表
     *
     * @param roomid
     * @return
     */
    @Select("select l.message_id as messageId,l.room_id as roomId,l.user_id as userId,l.as_user_id as asUserId,"
            + "uu.nick_name as asUserName,u.user_img as userImg,u.nick_name as userName,l.message_content as messageContent,l.create_time as createTime "
            + "from tbl_live_message l "
            + "LEFT JOIN tbl_user u ON l.user_id = u.user_id "
            + "LEFT JOIN tbl_user uu ON l.as_user_id=uu.user_id "
            + "WHERE room_id = #{roomid} LIMIT ${first},${page} ")
    List<Map> getLive_MessagesListByRoomId(@Param("roomid") Integer roomid, @Param("first") Integer firstResult, @Param("page") Integer pageSize);

    /**
     * 根据直播间和用户ID获取观看记录
     *
     * @param roomId
     * @param userId
     * @return
     */
    @Select("SELECT ROOM_ID,USER_ID,UPDATE_TIME,CREATE_TIME FROM TBL_LIVE_WATCH WHERE ROOM_ID=#{roomid} AND USER_ID=#{userid}")
    Tbl_Live_Watch getTbl_Live_WatchByRoomIdAndUserId(@Param("roomid") Integer roomId, @Param("userid") Integer userId);

    /**
     * 修改用户直播观看记录
     *
     * @param live
     * @return
     */
    @Update("UPDATE TBL_LIVE_WATCH SET UPDATE_TIME=#{live.updateTime} WHERE room_id=#{live.roomId} AND user_id=#{live.userId}")
    Integer updateTbl_Live_Watch(@Param("live") Tbl_Live_Watch live);

    /**
     * 插入一条用户观看记录
     *
     * @param live
     * @return
     */
    @Insert("INSERT INTO TBL_LIVE_WATCH(room_id,user_id,update_time,create_time) VALUES (#{live.roomId},#{live.userId},#{live.updateTime},#{live.createTime})")
    Integer insertTbl_Live_Watch(@Param("live") Tbl_Live_Watch live);

}
