<?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.cultivate.dao.impl.CultivateDaoImpl" >
  <resultMap id="BaseResultMap" type="com.pcloud.book.cultivate.entity.Cultivate" >
    <id column="id" property="id" jdbcType="BIGINT" />
    <result column="wechat_user_id" property="wechatUserId" jdbcType="BIGINT" />
    <result column="energy" property="energy" jdbcType="INTEGER" />
    <result column="fish_ball" property="fishBall" jdbcType="INTEGER" />
    <result column="star" property="star" jdbcType="INTEGER" />
    <result column="lighten" property="lighten" jdbcType="BOOLEAN" />
    <result column="is_home" property="isHome" jdbcType="BOOLEAN" />
    <result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
    <result column="update_time" property="updateTime" jdbcType="TIMESTAMP" />
  </resultMap>

  <sql id="Base_Column_List" >
    id, wechat_user_id, energy, fish_ball, star, lighten, create_time,update_time,is_home
  </sql>

  <select id="getRank" resultType="int">
    select count(1)+1 from cultivate where star>#{star}

  </select>

  <select id="getAllCounts" resultType="int">
    select count(1) from cultivate
  </select>

  <insert id="insert" parameterType="com.pcloud.book.cultivate.entity.Cultivate" useGeneratedKeys="true" keyProperty="id">
    insert into cultivate
    (
      wechat_user_id,
      energy,
      fish_ball,
      star,
      lighten,
      create_time,
      update_time,
      is_home
      )
    values (
      #{wechatUserId},
      #{energy},
      #{fishBall},
      #{star},
      #{lighten},
      NOW(),
      NOW(),
      1
      )
  </insert>

  <update id="update" parameterType="com.pcloud.book.cultivate.entity.Cultivate">
    update cultivate
    <set>
      <if test="wechatUserId != null">
        wechat_user_id = #{wechatUserId,jdbcType=BIGINT},
      </if>
      <if test="energy != null">
        energy = #{energy,jdbcType=INTEGER},
      </if>
      <if test="fishBall != null">
        fish_ball = #{fishBall,jdbcType=INTEGER},
      </if>
      <if test="star != null">
        star = #{star,jdbcType=INTEGER},
      </if>
      <if test="lighten != null">
        lighten = #{lighten,jdbcType=BOOLEAN},
      </if>
      update_time=NOW()
    </set>
    where id = #{id,jdbcType=BIGINT}
  </update>

  <select id="getById" parameterType="long" resultMap="BaseResultMap">
    select <include refid="Base_Column_List"/> from
    cultivate
    where id=#{id}
  </select>

  <select id="getByWechatUserId" parameterType="long" resultMap="BaseResultMap">
    select <include refid="Base_Column_List"/> from
    cultivate
    where wechat_user_id=#{wechatUserId}
    limit 1
  </select>

  <select id="getAllOrderByStar"  resultMap="BaseResultMap">
    select <include refid="Base_Column_List"/> from
    cultivate order by star desc
  </select>

  <select id="updateIsHome" parameterType="map">
    update cultivate
    set is_home=#{isHome}
    where id=#{cultivateId}
  </select>

    <select id="getRandomUserId" parameterType="map" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"/> FROM cultivate
        WHERE id >= (SELECT floor(RAND() * (SELECT count(1) FROM cultivate)))
        and id!=#{cultivateId}
        <if test="friendCultivateId !=null">
          and id!=#{friendCultivateId}
            ORDER BY lighten desc,id asc LIMIT 9
        </if>
        <if test="friendCultivateId ==null">
            ORDER BY lighten desc,id asc LIMIT 10
        </if>

    </select>

    <select id="getByCultivateIds" parameterType="map" resultMap="BaseResultMap">
        select
         <include refid="Base_Column_List"/>
        from
        cultivate
        where id IN
        <foreach collection="cultivateIds" open="(" close=")" separator="," item="item">
            #{item}
        </foreach>
        group by id
    </select>



</mapper>