<?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.yutou.bilibili.mybatis.Bili.mybatis.dao.BilibiliLiveDataDao">
  <resultMap id="BaseResultMap" type="com.yutou.bilibili.mybatis.Bili.mybatis.model.BilibiliLiveData">
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="uid" jdbcType="INTEGER" property="uid" />
    <result column="roomid" jdbcType="INTEGER" property="roomid" />
    <result column="type" jdbcType="VARCHAR" property="type" />
    <result column="msg" jdbcType="VARCHAR" property="msg" />
    <result column="giftId" jdbcType="INTEGER" property="giftid" />
    <result column="giftName" jdbcType="VARCHAR" property="giftname" />
    <result column="giftIndex" jdbcType="INTEGER" property="giftindex" />
    <result column="price" jdbcType="INTEGER" property="price" />
    <result column="priceOfcommission" jdbcType="INTEGER" property="priceofcommission" />
    <result column="subtime" jdbcType="TIMESTAMP" property="subtime" />
  </resultMap>
  <sql id="Example_Where_Clause">
    <where>
      <foreach collection="oredCriteria" item="criteria" separator="or">
        <if test="criteria.valid">
          <trim prefix="(" prefixOverrides="and" suffix=")">
            <foreach collection="criteria.criteria" item="criterion">
              <choose>
                <when test="criterion.noValue">
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue">
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue">
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue">
                  and ${criterion.condition}
                  <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
  <sql id="Update_By_Example_Where_Clause">
    <where>
      <foreach collection="example.oredCriteria" item="criteria" separator="or">
        <if test="criteria.valid">
          <trim prefix="(" prefixOverrides="and" suffix=")">
            <foreach collection="criteria.criteria" item="criterion">
              <choose>
                <when test="criterion.noValue">
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue">
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue">
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue">
                  and ${criterion.condition}
                  <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
  <sql id="Base_Column_List">
    id, `uid`, roomid, `type`, msg, giftId, giftName, giftIndex, price, priceOfcommission, 
    subtime
  </sql>
  <select id="selectByExample" parameterType="com.yutou.bilibili.mybatis.Bili.mybatis.model.BilibiliLiveDataExample" resultMap="BaseResultMap">
    select
    <if test="distinct">
      distinct
    </if>
    <include refid="Base_Column_List" />
    from bilibili_live_data
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
    <if test="orderByClause != null">
      order by ${orderByClause}
    </if>
  </select>
  <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
    select 
    <include refid="Base_Column_List" />
    from bilibili_live_data
    where id = #{id,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
    delete from bilibili_live_data
    where id = #{id,jdbcType=INTEGER}
  </delete>
  <delete id="deleteByExample" parameterType="com.yutou.bilibili.mybatis.Bili.mybatis.model.BilibiliLiveDataExample">
    delete from bilibili_live_data
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </delete>
  <insert id="insert" keyColumn="id" keyProperty="id" parameterType="com.yutou.bilibili.mybatis.Bili.mybatis.model.BilibiliLiveData" useGeneratedKeys="true">
    insert into bilibili_live_data (`uid`, roomid, `type`, 
      msg, giftId, giftName, 
      giftIndex, price, priceOfcommission, 
      subtime)
    values (#{uid,jdbcType=INTEGER}, #{roomid,jdbcType=INTEGER}, #{type,jdbcType=VARCHAR}, 
      #{msg,jdbcType=VARCHAR}, #{giftid,jdbcType=INTEGER}, #{giftname,jdbcType=VARCHAR}, 
      #{giftindex,jdbcType=INTEGER}, #{price,jdbcType=INTEGER}, #{priceofcommission,jdbcType=INTEGER}, 
      #{subtime,jdbcType=TIMESTAMP})
  </insert>
  <insert id="insertSelective" keyColumn="id" keyProperty="id" parameterType="com.yutou.bilibili.mybatis.Bili.mybatis.model.BilibiliLiveData" useGeneratedKeys="true">
    insert into bilibili_live_data
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="uid != null">
        `uid`,
      </if>
      <if test="roomid != null">
        roomid,
      </if>
      <if test="type != null">
        `type`,
      </if>
      <if test="msg != null">
        msg,
      </if>
      <if test="giftid != null">
        giftId,
      </if>
      <if test="giftname != null">
        giftName,
      </if>
      <if test="giftindex != null">
        giftIndex,
      </if>
      <if test="price != null">
        price,
      </if>
      <if test="priceofcommission != null">
        priceOfcommission,
      </if>
      <if test="subtime != null">
        subtime,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="uid != null">
        #{uid,jdbcType=INTEGER},
      </if>
      <if test="roomid != null">
        #{roomid,jdbcType=INTEGER},
      </if>
      <if test="type != null">
        #{type,jdbcType=VARCHAR},
      </if>
      <if test="msg != null">
        #{msg,jdbcType=VARCHAR},
      </if>
      <if test="giftid != null">
        #{giftid,jdbcType=INTEGER},
      </if>
      <if test="giftname != null">
        #{giftname,jdbcType=VARCHAR},
      </if>
      <if test="giftindex != null">
        #{giftindex,jdbcType=INTEGER},
      </if>
      <if test="price != null">
        #{price,jdbcType=INTEGER},
      </if>
      <if test="priceofcommission != null">
        #{priceofcommission,jdbcType=INTEGER},
      </if>
      <if test="subtime != null">
        #{subtime,jdbcType=TIMESTAMP},
      </if>
    </trim>
  </insert>
  <select id="countByExample" parameterType="com.yutou.bilibili.mybatis.Bili.mybatis.model.BilibiliLiveDataExample" resultType="java.lang.Long">
    select count(*) from bilibili_live_data
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </select>
  <update id="updateByExampleSelective" parameterType="map">
    update bilibili_live_data
    <set>
      <if test="record.id != null">
        id = #{record.id,jdbcType=INTEGER},
      </if>
      <if test="record.uid != null">
        `uid` = #{record.uid,jdbcType=INTEGER},
      </if>
      <if test="record.roomid != null">
        roomid = #{record.roomid,jdbcType=INTEGER},
      </if>
      <if test="record.type != null">
        `type` = #{record.type,jdbcType=VARCHAR},
      </if>
      <if test="record.msg != null">
        msg = #{record.msg,jdbcType=VARCHAR},
      </if>
      <if test="record.giftid != null">
        giftId = #{record.giftid,jdbcType=INTEGER},
      </if>
      <if test="record.giftname != null">
        giftName = #{record.giftname,jdbcType=VARCHAR},
      </if>
      <if test="record.giftindex != null">
        giftIndex = #{record.giftindex,jdbcType=INTEGER},
      </if>
      <if test="record.price != null">
        price = #{record.price,jdbcType=INTEGER},
      </if>
      <if test="record.priceofcommission != null">
        priceOfcommission = #{record.priceofcommission,jdbcType=INTEGER},
      </if>
      <if test="record.subtime != null">
        subtime = #{record.subtime,jdbcType=TIMESTAMP},
      </if>
    </set>
    <if test="_parameter != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByExample" parameterType="map">
    update bilibili_live_data
    set id = #{record.id,jdbcType=INTEGER},
      `uid` = #{record.uid,jdbcType=INTEGER},
      roomid = #{record.roomid,jdbcType=INTEGER},
      `type` = #{record.type,jdbcType=VARCHAR},
      msg = #{record.msg,jdbcType=VARCHAR},
      giftId = #{record.giftid,jdbcType=INTEGER},
      giftName = #{record.giftname,jdbcType=VARCHAR},
      giftIndex = #{record.giftindex,jdbcType=INTEGER},
      price = #{record.price,jdbcType=INTEGER},
      priceOfcommission = #{record.priceofcommission,jdbcType=INTEGER},
      subtime = #{record.subtime,jdbcType=TIMESTAMP}
    <if test="_parameter != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByPrimaryKeySelective" parameterType="com.yutou.bilibili.mybatis.Bili.mybatis.model.BilibiliLiveData">
    update bilibili_live_data
    <set>
      <if test="uid != null">
        `uid` = #{uid,jdbcType=INTEGER},
      </if>
      <if test="roomid != null">
        roomid = #{roomid,jdbcType=INTEGER},
      </if>
      <if test="type != null">
        `type` = #{type,jdbcType=VARCHAR},
      </if>
      <if test="msg != null">
        msg = #{msg,jdbcType=VARCHAR},
      </if>
      <if test="giftid != null">
        giftId = #{giftid,jdbcType=INTEGER},
      </if>
      <if test="giftname != null">
        giftName = #{giftname,jdbcType=VARCHAR},
      </if>
      <if test="giftindex != null">
        giftIndex = #{giftindex,jdbcType=INTEGER},
      </if>
      <if test="price != null">
        price = #{price,jdbcType=INTEGER},
      </if>
      <if test="priceofcommission != null">
        priceOfcommission = #{priceofcommission,jdbcType=INTEGER},
      </if>
      <if test="subtime != null">
        subtime = #{subtime,jdbcType=TIMESTAMP},
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.yutou.bilibili.mybatis.Bili.mybatis.model.BilibiliLiveData">
    update bilibili_live_data
    set `uid` = #{uid,jdbcType=INTEGER},
      roomid = #{roomid,jdbcType=INTEGER},
      `type` = #{type,jdbcType=VARCHAR},
      msg = #{msg,jdbcType=VARCHAR},
      giftId = #{giftid,jdbcType=INTEGER},
      giftName = #{giftname,jdbcType=VARCHAR},
      giftIndex = #{giftindex,jdbcType=INTEGER},
      price = #{price,jdbcType=INTEGER},
      priceOfcommission = #{priceofcommission,jdbcType=INTEGER},
      subtime = #{subtime,jdbcType=TIMESTAMP}
    where id = #{id,jdbcType=INTEGER}
  </update>
  <select id="queryGiftUserToDistinct"
          resultType="int">
    select count(distinct uid) from `bilibili_live_data`
    <where>
      <if test="startTime!=null">
        subtime>=#{startTime,jdbcType=TIMESTAMP}
      </if>
      <if test="endTime!=null">
        AND subtime <![CDATA[<=]]> #{endTime,jdbcType=TIMESTAMP}
      </if>
      <if test="roomid!=null">
        AND roomid=#{roomid,jdbcType=INTEGER}
      </if>

      <foreach collection="type" item='item' open="and (" close=")" separator="or">`type` = #{item,jdbcType=VARCHAR}
      </foreach>
    </where>
    ;
  </select>
  <select id="queryLiveData"
          resultMap="BaseResultMap">
    select * from `bilibili_live_data`
    <where>
      <if test="startTime!=null">
        subtime>=#{startTime,jdbcType=TIMESTAMP}
      </if>
      <if test="endTime!=null">
        AND subtime <![CDATA[<=]]> #{endTime,jdbcType=TIMESTAMP}
      </if>
      <if test="roomid!=null">
       AND roomid=#{roomid,jdbcType=INTEGER}
      </if>

    <foreach collection="type" item='item' open="and (" close=")" separator="or">`type` = #{item,jdbcType=VARCHAR}
    </foreach>
    </where>
     ;
  </select>
  <select id="queryTimeOfRoomid"
          resultMap="BaseResultMap">
    select * from `bilibili_live_data`
    <where>
      <if test="startTime!=null">
        subtime>=#{startTime,jdbcType=TIMESTAMP}
      </if>
      <if test="endTime!=null">
        AND subtime <![CDATA[<=]]> #{endTime,jdbcType=TIMESTAMP}
      </if>
      <if test="giftId!=-1">
        and giftId=#{giftId,jdbcType=INTEGER}
      </if>
      <if test="roomid!=null">
        AND roomid=#{roomid,jdbcType=INTEGER}
      </if>
    </where>
    ;
  </select>
  <select id="queryPriceTimeGroup" resultType="map">
    select sum(price)as price,group_concat(distinct DATE_FORMAT(subtime,'%d/%H')) as `time` from `bilibili_live_data`
    <where>
      <if test="roomid!=-1">
        roomid=#{roomid,jdbcType=INTEGER}
      </if>
      <if test="startTime!=null">
        AND subtime>=#{startTime,jdbcType=TIMESTAMP}
      </if>
      <if test="endTime!=null">
        AND subtime <![CDATA[<=]]> #{endTime,jdbcType=TIMESTAMP}
      </if>
    </where>
    group by subtime-subtime%(2.8*60*60);
  </select>
  <select id="queryGiftTimeGroup" resultType="map">
    select sum(giftIndex)  as price,group_concat(distinct giftName) as giftName ,group_concat(distinct DATE_FORMAT(subtime,'%d/%H')) as `time` from `bilibili_live_data`
    <where>
      <if test="roomid!=-1">
        roomid=#{roomid,jdbcType=INTEGER}
      </if>
      <if test="startTime!=null">
        AND subtime>=#{startTime,jdbcType=TIMESTAMP}
      </if>
      <if test="endTime!=null">
        AND subtime <![CDATA[<=]]> #{endTime,jdbcType=TIMESTAMP}
      </if>
    </where>
    group by subtime-subtime%(2.8*60*60);
  </select>

</mapper>