<?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.PersonalMedalDaoImpl" >
  <resultMap id="BaseResultMap" type="com.pcloud.book.personal.entity.PersonalMedal" >
    <id column="id" property="id" jdbcType="BIGINT" />
    <result column="grey_img_url" property="greyImgUrl" jdbcType="VARCHAR" />
    <result column="light_img_url" property="lightImgUrl" jdbcType="VARCHAR" />
    <result column="big_img_url" property="bigImgUrl" jdbcType="VARCHAR" />
    <result column="big_grey_img_url" property="bigGreyImgUrl" jdbcType="VARCHAR" />
    <result column="medal_name" property="medalName" jdbcType="VARCHAR" />
    <result column="content" property="content" jdbcType="VARCHAR" />
    <result column="star_limit_count" property="starLimitCount" jdbcType="INTEGER" />
    <result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
    <result column="update_time" property="updateTime" jdbcType="TIMESTAMP" />
  </resultMap>

  <sql id="Base_Column_List" >
    id, grey_img_url, light_img_url, big_img_url,big_grey_img_url, medal_name, content, star_limit_count, create_time, update_time
  </sql>

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

  <select id="getBaseMedalList" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List" />
    from personal_medal
  </select>

  <select id="getReceivingMedal" resultType="long" parameterType="map">
    SELECT id FROM personal_medal a
    WHERE star_limit_count <![CDATA[ <= ]]> #{starCount}
    AND NOT EXISTS(SELECT 1 FROM personal_medal_record WHERE medal_id = a.id AND wechat_user_id = #{wechatUserId})
  </select>

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

  <insert id="insert" parameterType="com.pcloud.book.personal.entity.PersonalMedal" >
    insert into personal_medal (id, grey_img_url, light_img_url, big_img_url,big_grey_img_url, medal_name, content,
      star_limit_count, create_time, update_time
      )
    values (#{id,jdbcType=BIGINT}, #{greyImgUrl,jdbcType=VARCHAR},#{lightImgUrl,jdbcType=VARCHAR},#{bigImgUrl,jdbcType=VARCHAR},#{bigGreyImgUrl,jdbcType=VARCHAR}, #{medalName,jdbcType=VARCHAR}, #{content,jdbcType=VARCHAR},
      #{starLimitCount,jdbcType=INTEGER}, #{createTime,jdbcType=TIMESTAMP}, #{updateTime,jdbcType=TIMESTAMP}
      )
  </insert>

</mapper>