<?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.pcloud.book.personal.dao.impl.PersonalStarRecordDaoImpl" >
  <resultMap id="BaseResultMap" type="com.pcloud.book.personal.entity.PersonalStarRecord" >
    <id column="id" property="id" jdbcType="BIGINT" />
    <result column="wechat_user_id" property="wechatUserId" jdbcType="BIGINT" />
    <result column="star_count" property="starCount" jdbcType="INTEGER" />
    <result column="type" property="type" jdbcType="INTEGER" />
    <result column="receive_date" property="receiveDate" jdbcType="VARCHAR" />
    <result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
  </resultMap>

  <sql id="Base_Column_List" >
    id, wechat_user_id, star_count, type, receive_date, create_time
  </sql>

  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Long" >
    select 
    <include refid="Base_Column_List" />
    from personal_star_record
    where id = #{id,jdbcType=BIGINT}
  </select>

  <select id="hasReceive" parameterType="map" resultType="int">
    SELECT COUNT(1) FROM `personal_star_record`
    WHERE wechat_user_id = #{wechatUserId,jdbcType=BIGINT}
    <if test="receiveDate != null">
      AND receive_date = #{receiveDate,jdbcType=VARCHAR}
    </if>
    AND type = #{type,jdbcType=INTEGER}
    LIMIT 1;
  </select>

  <select id="getReceiveStarCount" parameterType="map" resultType="int">
    SELECT IFNULL(SUM(star_count),0) FROM `personal_star_record`
    WHERE wechat_user_id = #{wechatUserId,jdbcType=BIGINT}
  </select>

  <select id="getReceiveStarCountList"  resultType="com.pcloud.book.personal.dto.PersonalStarReceiveDto">
  SELECT wechat_user_id wechatUserId, IFNULL(SUM(star_count),0) startCount
  FROM `personal_star_record` GROUP BY wechat_user_id  order by startCount desc
  </select>

  <delete id="deleteByPrimaryKey" parameterType="java.lang.Long" >
    delete from personal_star_record
    where id = #{id,jdbcType=BIGINT}
  </delete>

  <!-- 如果当前没有领取，则新增 -->
  <insert id="insert" parameterType="com.pcloud.book.personal.entity.PersonalStarRecord" useGeneratedKeys="true" keyProperty="id" >
    INSERT INTO personal_star_record (wechat_user_id, star_count, type, receive_date, create_time)
    SELECT #{wechatUserId,jdbcType=BIGINT}, #{starCount,jdbcType=INTEGER},
      #{type,jdbcType=INTEGER}, #{receiveDate,jdbcType=VARCHAR}, #{createTime,jdbcType=TIMESTAMP}
      WHERE NOT EXISTS(SELECT 1 FROM `personal_star_record` WHERE wechat_user_id = #{wechatUserId,jdbcType=BIGINT} AND receive_date = #{receiveDate,jdbcType=VARCHAR} and type=#{type})
  </insert>

  <insert id="insert4Type" parameterType="com.pcloud.book.personal.entity.PersonalStarRecord" useGeneratedKeys="true" keyProperty="id">
    INSERT INTO personal_star_record (wechat_user_id, star_count, type, receive_date, create_time)
    SELECT #{wechatUserId,jdbcType=BIGINT}, #{starCount,jdbcType=INTEGER},
      #{type,jdbcType=INTEGER}, #{receiveDate,jdbcType=VARCHAR}, #{createTime,jdbcType=TIMESTAMP}
      WHERE NOT EXISTS(SELECT 1 FROM `personal_star_record` WHERE wechat_user_id = #{wechatUserId,jdbcType=BIGINT} AND type = #{type,jdbcType=INTEGER})
  </insert>

  <update id="update" parameterType="com.pcloud.book.personal.entity.PersonalStarRecord" >
    update personal_star_record
    <set >
      <if test="starCount != null" >
        star_count = #{starCount,jdbcType=INTEGER},
      </if>
      <if test="type != null" >
        type = #{type,jdbcType=INTEGER},
      </if>
      <if test="receiveDate != null" >
        receive_date = #{receiveDate,jdbcType=VARCHAR},
      </if>
    </set>
    where id = #{id,jdbcType=BIGINT}
  </update>

  <update id="updateByPrimaryKey" parameterType="com.pcloud.book.personal.entity.PersonalStarRecord" >
    update personal_star_record
    set wechat_user_id = #{wechatUserId,jdbcType=BIGINT},
      star_count = #{starCount,jdbcType=INTEGER},
      type = #{type,jdbcType=INTEGER},
      receive_date = #{receiveDate,jdbcType=VARCHAR},
      create_time = #{createTime,jdbcType=TIMESTAMP}
    where id = #{id,jdbcType=BIGINT}
  </update>

  <select id="getLoginDays" parameterType="map" resultType="int">
    select count(1) from personal_star_record where wechat_user_id=#{wechatUserId} and type=1
  </select>

  <select id="getTaskCount" parameterType="map" resultType="int">
    select count(1) from personal_star_record where wechat_user_id=#{wechatUserId} and (type=2 or type=3)
  </select>
</mapper>