<?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.PersonalMedalRecordDaoImpl" >
  <resultMap id="BaseResultMap" type="com.pcloud.book.personal.entity.PersonalMedalRecord" >
    <id column="id" property="id" jdbcType="BIGINT" />
    <result column="medal_id" property="medalId" jdbcType="BIGINT" />
    <result column="wechat_user_id" property="wechatUserId" jdbcType="BIGINT" />
    <result column="has_show" property="hasShow" jdbcType="INTEGER" />
    <result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
  </resultMap>

  <sql id="Base_Column_List" >
    id, medal_id, wechat_user_id, has_show, create_time
  </sql>

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

  <select id="getUserMedalList" resultType="com.pcloud.book.personal.dto.PersonalMedalRecordDto" parameterType="java.lang.Long">
    select
        a.id,
        a.medal_id medalId,
        a.wechat_user_id wechatUserId,
        a.has_show hasShow,
        b.big_img_url bigImgUrl,
        b.light_img_url lightImgUrl
    from personal_medal_record a,personal_medal b
    where a.medal_id = b.id AND a.wechat_user_id = #{wechatUserId,jdbcType=BIGINT}
  </select>

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

  <insert id="insert" parameterType="com.pcloud.book.personal.entity.PersonalMedalRecord" useGeneratedKeys="true" keyProperty="id" >
    INSERT INTO personal_medal_record (medal_id, wechat_user_id, has_show, create_time)
    SELECT #{medalId,jdbcType=BIGINT}, #{wechatUserId,jdbcType=BIGINT},#{hasShow,jdbcType=INTEGER}, NOW()
    WHERE NOT EXISTS(SELECT 1 FROM personal_medal_record WHERE medal_id = #{medalId,jdbcType=BIGINT} AND wechat_user_id = #{wechatUserId,jdbcType=BIGINT})
  </insert>

  <update id="update" parameterType="com.pcloud.book.personal.entity.PersonalMedalRecord" >
    update personal_medal_record
    <set >
      <if test="medalId != null" >
        medal_id = #{medalId,jdbcType=BIGINT},
      </if>
      <if test="hasShow != null" >
        has_show = #{hasShow,jdbcType=INTEGER},
      </if>
    </set>
    where id = #{id,jdbcType=BIGINT}
  </update>

  <update id="updateByPrimaryKey" parameterType="com.pcloud.book.personal.entity.PersonalMedalRecord" >
    update personal_medal_record
    set medal_id = #{medalId,jdbcType=BIGINT},
      wechat_user_id = #{wechatUserId,jdbcType=BIGINT},
      has_show = #{hasShow,jdbcType=INTEGER},
      create_time = #{createTime,jdbcType=TIMESTAMP}
    where id = #{id,jdbcType=BIGINT}
  </update>
</mapper>