<?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.CultivateRecordDaoImpl" >
  <resultMap id="BaseResultMap" type="com.pcloud.book.cultivate.entity.CultivateRecord" >
    <id column="id" property="id" jdbcType="BIGINT" />
    <result column="cultivate_id" property="cultivateId" jdbcType="BIGINT" />
    <result column="wechat_user_id" property="wechatUserId" jdbcType="BIGINT" />
    <result column="change_type" property="changeType" jdbcType="INTEGER" />
    <result column="remark" property="remark" jdbcType="VARCHAR" />
    <result column="energy_change" property="energyChange" jdbcType="INTEGER" />
    <result column="fish_ball_change" property="fishBallChange" jdbcType="INTEGER" />
    <result column="fish_ball_change_type" property="fishBallChangeType" jdbcType="INTEGER" />
    <result column="star_change" property="starChange" jdbcType="INTEGER" />
    <result column="skill_type" property="skillType" jdbcType="INTEGER" />
    <result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
    <result column="friend_cultivate_id" property="friendCultivateId" jdbcType="BIGINT" />
  </resultMap>

  <sql id="Base_Column_List" >
    id, cultivate_id, wechat_user_id, change_type, remark, energy_change, fish_ball_change, fish_ball_change_type, star_change, skill_type, create_time, friend_cultivate_id
  </sql>

  <insert id="insert" parameterType="com.pcloud.book.cultivate.entity.CultivateRecord" useGeneratedKeys="true" keyProperty="id">
    insert into cultivate_record (cultivate_id, wechat_user_id, change_type,
     remark, energy_change, fish_ball_change, fish_ball_change_type,star_change, skill_type, create_time,friend_cultivate_id
      )
    values (#{cultivateId},#{wechatUserId},#{changeType},#{remark},#{energyChange},#{fishBallChange},#{fishBallChangeType},
      #{starChange}, #{skillType}, NOW(),#{friendCultivateId}
      )
  </insert>

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

  <select id="getEnergyConsumptionAfter" parameterType="map" resultType="integer">
    select -ifnull(sum(energy_change),0) from
    cultivate_record
    where id>#{cultivateRecordId}
    and wechat_user_id=#{wechatUserId}
    and change_type=5
  </select>

  <select id="getLastEnergyConsumptionRecord" parameterType="map" resultMap="BaseResultMap">
    select <include refid="Base_Column_List"/>
    from cultivate_record
    where cultivate_id=#{cultivateId}
    and wechat_user_id=#{wechatUserId}
    order by create_time desc
    limit 1
  </select>

  <select id="getUserGatherFishBallCount" parameterType="map" resultType="integer">
    SELECT
        IFNULL(SUM(t2.fish_ball_change), 0)
    FROM
        cultivate_record t2
    WHERE
        t2.id > (
            SELECT
                t.id
            FROM
                cultivate_record t
            WHERE
                t.change_type = 2
            AND t.wechat_user_id = #{wechatUserId}
            AND t.cultivate_id = #{cultivateId}
            ORDER BY
                create_time DESC
            LIMIT 1
        )
    AND t2.change_type = 1
    AND t2.wechat_user_id = #{wechatUserId}
    AND t2.cultivate_id = #{cultivateId}
  </select>

    <select id="getCultivateRecordList" parameterType="map" resultMap="BaseResultMap">
        select <include refid="Base_Column_List"/>
        from cultivate_record
        where cultivate_id=#{cultivateId}
        and wechat_user_id=#{wechatUserId}
        order by create_time desc,id desc
    </select>

    <select id="getLastLighten" parameterType="map" resultMap="BaseResultMap">
        SELECT
               <include refid="Base_Column_List"/>
            FROM
                cultivate_record t
            WHERE
                t.change_type = 2
            AND t.wechat_user_id = #{wechatUserId}
            AND t.cultivate_id = #{cultivateId}
            ORDER BY
                create_time DESC
            LIMIT 1
    </select>

    <select id="getLastEnergyConsumptionBefore" parameterType="map" resultMap="BaseResultMap">
        select <include refid="Base_Column_List"/>
        from cultivate_record
        where wechat_user_id=#{wechatUserId}
        and id &lt; #{cultivateRecordId}
        and change_type=5
        order by create_time desc
        limit 1
    </select>

    <select id="getHoldSeatFriendList" parameterType="map" resultMap="BaseResultMap">
        select <include refid="Base_Column_List"/>
        from cultivate_record
        where cultivate_id = #{cultivateId}
        and id > #{lastLightenRecordId}
        and change_type in (11,13,14)
        and friend_cultivate_id is not null
    </select>

    <select id="getBookSearchRecord" parameterType="map" resultType="com.pcloud.book.book.vo.SearchBookVO">
        select a.id,a.content,a.book_id bookId ,b.book_name bookName,a.agent_id agentId,c.SECOND_TEMPLET_ID secondTempletId,b.publish ,c.ADVISER_ID adviserId,c.channel_id channelId
        ,a.create_time createTime,b.author,b.COVER_IMG coverImg, b.ISBN isbn
        from search_record a,book b ,book_adviser c
        where a.book_id=b.book_id
        and a.book_id=c.book_id
        and a.user_delete=0
        and b.is_delete=0
        and c.is_delete=0
        and a.wechat_user_id is not null
        and a.wechat_user_id=#{wechatUserId}
        group by a.book_id
        order by a.create_time desc
    </select>

</mapper>