<?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.reading.dao.impl.ReadingActivityDaoImpl" >
  <resultMap id="BaseResultMap" type="com.pcloud.book.reading.entity.ReadingActivity" >
    <id column="id" property="id" jdbcType="BIGINT" />
    <result column="theme" property="theme" jdbcType="VARCHAR" />
    <result column="brand_id" property="brandId" jdbcType="BIGINT" />
    <result column="join_condition" property="joinCondition" jdbcType="TINYINT" />
    <result column="join_keyword" property="joinKeyword" jdbcType="VARCHAR" />
    <result column="join_amount" property="joinAmount" jdbcType="DECIMAL" />
    <result column="join_type" property="joinType" jdbcType="TINYINT" />
    <result column="join_qrcode_pic" property="joinQrcodePic" jdbcType="VARCHAR" />
    <result column="rule_type" property="ruleType" jdbcType="TINYINT" />
    <result column="rule" property="rule" jdbcType="VARCHAR" />
    <result column="rule_resource_id" property="ruleResourceId" jdbcType="VARCHAR" />
    <result column="start_time" property="startTime" jdbcType="TIMESTAMP" />
    <result column="start_slogan_type" property="startSloganType" jdbcType="TINYINT" />
    <result column="start_slogan" property="startSlogan" jdbcType="VARCHAR" />
    <result column="start_resource_id" property="startResourceId" jdbcType="VARCHAR" />
    <result column="end_time" property="endTime" jdbcType="TIMESTAMP" />
    <result column="end_slogan_type" property="endSloganType" jdbcType="TINYINT" />
    <result column="end_slogan" property="endSlogan" jdbcType="VARCHAR" />
    <result column="end_resource_id" property="endResourceId" jdbcType="VARCHAR" />
    <result column="riddle_open" property="riddleOpen" jdbcType="BIT" />
    <result column="award_open" property="awardOpen" jdbcType="BIT" />
    <result column="award_text" property="awardText" jdbcType="VARCHAR" />
    <result column="award_pic" property="awardPic" jdbcType="VARCHAR" />
    <result column="clock_open" property="clockOpen" jdbcType="BIT" />
    <result column="clock_remind_time" property="clockRemindTime" jdbcType="VARCHAR" />
    <result column="clock_remind_content" property="clockRemindContent" jdbcType="VARCHAR" />
    <result column="clock_link" property="clockLink" jdbcType="VARCHAR" />
    <result column="clock_serve_id" property="clockServeId" jdbcType="BIGINT" />
    <result column="clock_serve_type" property="clockServeType" jdbcType="VARCHAR" />
    <result column="clock_result_open" property="clockResultOpen" jdbcType="BIT" />
    <result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
    <result column="create_user" property="createUser" jdbcType="BIGINT" />
    <result column="update_time" property="updateTime" jdbcType="TIMESTAMP" />
  </resultMap>

  <resultMap id="BaseDTOResultMap" type="com.pcloud.book.reading.dto.ReadingActivityDTO" >
    <id column="id" property="id" jdbcType="BIGINT" />
    <result column="theme" property="theme" jdbcType="VARCHAR" />
    <result column="brand_id" property="brandId" jdbcType="BIGINT" />
    <result column="join_condition" property="joinCondition" jdbcType="TINYINT" />
    <result column="join_keyword" property="joinKeyword" jdbcType="VARCHAR" />
    <result column="join_amount" property="joinAmount" jdbcType="DECIMAL" />
    <result column="join_type" property="joinType" jdbcType="TINYINT" />
    <result column="join_qrcode_pic" property="joinQrcodePic" jdbcType="VARCHAR" />
    <result column="rule_type" property="ruleType" jdbcType="TINYINT" />
    <result column="rule" property="rule" jdbcType="VARCHAR" />
    <result column="rule_resource_id" property="ruleResourceId" jdbcType="VARCHAR" />
    <result column="start_time" property="startTime" jdbcType="TIMESTAMP" />
    <result column="start_slogan_type" property="startSloganType" jdbcType="TINYINT" />
    <result column="start_slogan" property="startSlogan" jdbcType="VARCHAR" />
    <result column="start_resource_id" property="startResourceId" jdbcType="VARCHAR" />
    <result column="end_time" property="endTime" jdbcType="TIMESTAMP" />
    <result column="end_slogan_type" property="endSloganType" jdbcType="TINYINT" />
    <result column="end_slogan" property="endSlogan" jdbcType="VARCHAR" />
    <result column="end_resource_id" property="endResourceId" jdbcType="VARCHAR" />
    <result column="riddle_open" property="riddleOpen" jdbcType="BIT" />
    <result column="award_open" property="awardOpen" jdbcType="BIT" />
    <result column="award_text" property="awardText" jdbcType="VARCHAR" />
    <result column="award_pic" property="awardPic" jdbcType="VARCHAR" />
    <result column="clock_open" property="clockOpen" jdbcType="BIT" />
    <result column="clock_remind_time" property="clockRemindTime" jdbcType="VARCHAR" />
    <result column="clock_remind_content" property="clockRemindContent" jdbcType="VARCHAR" />
    <result column="clock_link" property="clockLink" jdbcType="VARCHAR" />
    <result column="clock_serve_id" property="clockServeId" jdbcType="BIGINT" />
    <result column="clock_serve_type" property="clockServeType" jdbcType="VARCHAR" />
    <result column="clock_result_open" property="clockResultOpen" jdbcType="BIT" />
    <result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
    <result column="create_user" property="createUser" jdbcType="BIGINT" />
    <result column="update_time" property="updateTime" jdbcType="TIMESTAMP" />
  </resultMap>

  <sql id="Base_Column_List" >
    id, theme, brand_id, join_condition, join_keyword, join_amount, join_type, join_qrcode_pic, 
    rule_type, rule, rule_resource_id, start_time, start_slogan_type, start_slogan, start_resource_id, end_time, end_slogan_type,
    end_slogan,end_resource_id, riddle_open, award_open, award_text, award_pic, clock_open, clock_remind_time, clock_remind_content,
    clock_link, clock_serve_id, clock_serve_type, clock_result_open, create_time, create_user, update_time
  </sql>

  <sql id="Base_Column_List_ra" >
    ra.id,  ra.theme,  ra.brand_id,  ra.join_condition,  ra.join_keyword, ra.join_amount, ra.join_type, ra.join_qrcode_pic,
    ra.rule_type, ra.rule, ra.rule_resource_id, ra.start_time, ra.start_slogan_type, ra.start_slogan, ra.start_resource_id, ra.end_time, ra.end_slogan_type,
    ra.end_slogan,ra.end_resource_id, ra.riddle_open, ra.award_open, ra.award_text, ra.award_pic, ra.clock_open, ra.clock_remind_time, ra.clock_remind_content,
    ra.clock_link, ra.clock_serve_id, ra.clock_serve_type, ra.clock_result_open, ra.create_time, ra.create_user, ra.update_time
  </sql>

  <insert id="insert" parameterType="com.pcloud.book.reading.entity.ReadingActivity" useGeneratedKeys="true" keyProperty="id">
    insert into reading_activity (theme, brand_id,
    join_condition, join_keyword, join_amount,
    join_type, join_qrcode_pic, rule_type,
    rule, rule_resource_id, start_time, start_slogan_type,
    start_slogan, start_resource_id, end_time, end_slogan_type,
    end_slogan, end_resource_id, riddle_open, award_open,
    award_text, award_pic,
    clock_open, clock_remind_time, clock_remind_content,
    clock_link, clock_serve_id, clock_serve_type, clock_result_open,
    create_time, create_user, update_time
    )
    values (#{theme,jdbcType=VARCHAR}, #{brandId,jdbcType=BIGINT},
    #{joinCondition,jdbcType=TINYINT}, #{joinKeyword,jdbcType=VARCHAR}, #{joinAmount,jdbcType=DECIMAL},
    #{joinType,jdbcType=TINYINT}, #{joinQrcodePic,jdbcType=VARCHAR}, #{ruleType,jdbcType=TINYINT},
    #{rule,jdbcType=VARCHAR}, #{ruleResourceId}, #{startTime,jdbcType=TIMESTAMP}, #{startSloganType,jdbcType=TINYINT},
    #{startSlogan,jdbcType=VARCHAR}, #{startResourceId}, #{endTime,jdbcType=TIMESTAMP}, #{endSloganType,jdbcType=TINYINT},
    #{endSlogan,jdbcType=VARCHAR}, #{endResourceId}, #{riddleOpen,jdbcType=BIT}, #{awardOpen,jdbcType=BIT},
    #{awardText,jdbcType=VARCHAR}, #{awardPic,jdbcType=VARCHAR},
    #{clockOpen,jdbcType=BIT}, #{clockRemindTime,jdbcType=VARCHAR}, #{clockRemindContent,jdbcType=VARCHAR},
    #{clockLink,jdbcType=VARCHAR}, #{clockServeId,jdbcType=BIGINT}, #{clockServeType,jdbcType=VARCHAR},
    #{clockResultOpen,jdbcType=BIT}, NOW(), #{createUser,jdbcType=BIGINT}, NOW()
    )
  </insert>

  <select id="getByTheme" parameterType="string"  resultMap="BaseResultMap">
    SELECT <include refid="Base_Column_List"/>
    FROM reading_activity
    WHERE theme = #{theme,jdbcType=VARCHAR} order by id desc limit 1
  </select>

  <select id="getById" parameterType="long" resultMap="BaseResultMap">
    SELECT <include refid="Base_Column_List"/>
    FROM reading_activity
    WHERE id = #{id}
  </select>

  <update id="updateAdditionSetting" parameterType="com.pcloud.book.reading.entity.ReadingActivity" >
    update reading_activity
    set
    riddle_open = #{riddleOpen,jdbcType=BIT},
    award_open = #{awardOpen,jdbcType=BIT},
    award_text = #{awardText,jdbcType=VARCHAR},
    award_pic = #{awardPic,jdbcType=VARCHAR},
    clock_open = #{clockOpen,jdbcType=BIT},
    clock_remind_time = #{clockRemindTime,jdbcType=VARCHAR},
    clock_remind_content = #{clockRemindContent,jdbcType=VARCHAR},
    clock_link = #{clockLink,jdbcType=VARCHAR},
    clock_serve_id = #{clockServeId,jdbcType=BIGINT},
    clock_serve_type = #{clockServeType,jdbcType=VARCHAR},
    clock_result_open = #{clockResultOpen,jdbcType=BIT},
    update_time = NOW()
    where id = #{id,jdbcType=BIGINT}
  </update>

  <!--获取列表-->
  <select id="getList" parameterType="map" resultMap="BaseDTOResultMap">
    select <include refid="Base_Column_List"/>
    from reading_activity
    order by create_time desc
  </select>
  
  <select id="themeExist" parameterType="string" resultType="boolean">
    SELECT
	EXISTS (
		SELECT
			id
		FROM
			reading_activity
		WHERE
			theme = #{theme}
	)
  </select>

  <update id="update" parameterType="com.pcloud.book.reading.entity.ReadingActivity" >
    update reading_activity
    <set >
      <if test="theme != null" >
        theme = #{theme,jdbcType=VARCHAR},
      </if>
      <if test="ruleType != null" >
        rule_type = #{ruleType,jdbcType=TINYINT},
      </if>
      <if test="rule != null" >
        rule = #{rule,jdbcType=VARCHAR},
      </if>
      <if test="ruleResourceId != null" >
        rule_resource_id = #{ruleResourceId,jdbcType=VARCHAR},
      </if>
      <if test="startTime != null" >
        start_time = #{startTime,jdbcType=TIMESTAMP},
      </if>
      <if test="startSloganType != null" >
        start_slogan_type = #{startSloganType,jdbcType=TINYINT},
      </if>
      <if test="startSlogan != null" >
        start_slogan = #{startSlogan,jdbcType=VARCHAR},
      </if>
      <if test="startResourceId != null" >
        start_resource_id = #{startResourceId,jdbcType=VARCHAR},
      </if>
      <if test="endTime != null" >
        end_time = #{endTime,jdbcType=TIMESTAMP},
      </if>
      <if test="endSloganType != null" >
        end_slogan_type = #{endSloganType,jdbcType=TINYINT},
      </if>
      <if test="endSlogan != null" >
        end_slogan = #{endSlogan,jdbcType=VARCHAR},
      </if>
      <if test="endResourceId != null" >
        end_resource_id = #{endResourceId,jdbcType=VARCHAR},
      </if>
        update_time = NOW(),
    </set>
    where id = #{id,jdbcType=BIGINT}
  </update>

  <select id="getByThemeAndJoinCondition" parameterType="map"  resultMap="BaseResultMap">
    SELECT <include refid="Base_Column_List"/>
    FROM reading_activity
    WHERE theme = #{theme}
    and join_condition =#{joinCondition}
    order by id desc limit 1
  </select>
  <select id="getJoinActivityByKeyword" resultMap="BaseResultMap" parameterType="map">
    SELECT
    <include refid="Base_Column_List"/>
    FROM
    reading_activity
    WHERE
    join_condition = 2
    AND join_keyword = #{joinKeyword}
    AND id IN (
    SELECT
    activity_id
    FROM
    reading_user
    WHERE
    wx_user_id = #{wxUserId}
    AND alt_id = #{altId}
    )
    AND (end_time IS NULL OR end_time > CURRENT_TIME)
    ORDER BY
    create_time DESC
    LIMIT 1
  </select>
</mapper>