<?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.goafanti.common.dao.AchievementKeywordMapper" >
  <resultMap id="BaseResultMap" type="com.goafanti.common.model.AchievementKeyword" >
    <id column="id" property="id" jdbcType="VARCHAR" />
    <result column="achievement_id" property="achievementId" jdbcType="VARCHAR" />
    <result column="keyword" property="keyword" jdbcType="VARCHAR" />
  </resultMap>
  <sql id="Base_Column_List" >
    id, achievement_id, keyword
  </sql>
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.String" >
    select 
    <include refid="Base_Column_List" />
    from achievement_keyword
    where id = #{id,jdbcType=VARCHAR}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.String" >
    delete from achievement_keyword
    where id = #{id,jdbcType=VARCHAR}
  </delete>
  <insert id="insert" parameterType="com.goafanti.common.model.AchievementKeyword" >
    insert into achievement_keyword (id, achievement_id, keyword
      )
    values (#{id,jdbcType=VARCHAR}, #{achievementId,jdbcType=VARCHAR}, #{keyword,jdbcType=VARCHAR}
      )
  </insert>
  <insert id="insertSelective" parameterType="com.goafanti.common.model.AchievementKeyword" >
    insert into achievement_keyword
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        id,
      </if>
      <if test="achievementId != null" >
        achievement_id,
      </if>
      <if test="keyword != null" >
        keyword,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        #{id,jdbcType=VARCHAR},
      </if>
      <if test="achievementId != null" >
        #{achievementId,jdbcType=VARCHAR},
      </if>
      <if test="keyword != null" >
        #{keyword,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.goafanti.common.model.AchievementKeyword" >
    update achievement_keyword
    <set >
      <if test="achievementId != null" >
        achievement_id = #{achievementId,jdbcType=VARCHAR},
      </if>
      <if test="keyword != null" >
        keyword = #{keyword,jdbcType=VARCHAR},
      </if>
    </set>
    where id = #{id,jdbcType=VARCHAR}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.goafanti.common.model.AchievementKeyword" >
    update achievement_keyword
    set achievement_id = #{achievementId,jdbcType=VARCHAR},
      keyword = #{keyword,jdbcType=VARCHAR}
    where id = #{id,jdbcType=VARCHAR}
  </update>
  
  <insert id="insertBatch" parameterType="com.goafanti.common.model.AchievementKeyword">
    insert into achievement_keyword (id, achievement_id, keyword)
    values 
    <foreach item="item" index="index" collection="list" separator=",">
      (
      	#{item.id,jdbcType=VARCHAR}, #{item.achievementId,jdbcType=VARCHAR}, #{item.keyword,jdbcType=VARCHAR}
      )
    </foreach>
  </insert>
  
  <delete id="batchDeleteByAchievementId" parameterType="java.lang.String">
  	 delete from achievement_keyword where achievement_id = #{achievementId,jdbcType=VARCHAR}
       
  </delete>
  
  <delete id="batchDeleteByAchievementIds" parameterType="java.util.List">
  	 delete from achievement_keyword where achievement_id in
        <foreach item="item" collection="list" open="(" separator="," close=")">
            #{item}
        </foreach>
  </delete>
  
  <select id="selectAchievementDemand" parameterType="java.util.List"
  				resultType="com.goafanti.common.model.AchievementDemand">
  	select count(ak.achievement_id), ak.achievement_id as achievementId
  		from achievement_keyword ak 
  		left join achievement a on a.id = ak.achievement_id
  		where a.audit_status = 3 and a.deleted_sign = 0 and ak.keyword in
	  		<foreach item="item" collection="list" open="(" separator="," close=")">
	            #{item}
	        </foreach> 
        group by ak.achievement_id
        order by count(ak.achievement_id) desc
        LIMIT 20
  </select>
  
  <select id="selectKeywordsByAchievementId" parameterType="java.lang.String" resultMap="BaseResultMap" >
  	select 
	<include refid="Base_Column_List" />
  	from 
  	achievement_keyword where achievement_id = #{id,jdbcType=VARCHAR}
  </select>
  
</mapper>