<?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.AchievementDemandMapper" >
  <resultMap id="BaseResultMap" type="com.goafanti.common.model.AchievementDemand" >
    <id column="achievement_id" property="achievementId" jdbcType="VARCHAR" />
    <id column="demand_id" property="demandId" jdbcType="VARCHAR" />
  </resultMap>
  <delete id="deleteByPrimaryKey" parameterType="com.goafanti.common.model.AchievementDemand" >
    delete from achievement_demand
    where achievement_id = #{achievementId,jdbcType=VARCHAR}
      and demand_id = #{demandId,jdbcType=VARCHAR}
  </delete>
  <insert id="insert" parameterType="com.goafanti.common.model.AchievementDemand" >
    insert into achievement_demand (achievement_id, demand_id)
    values (#{achievementId,jdbcType=VARCHAR}, #{demandId,jdbcType=VARCHAR})
  </insert>
  <insert id="insertSelective" parameterType="com.goafanti.common.model.AchievementDemand" >
    insert into achievement_demand
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="achievementId != null" >
        achievement_id,
      </if>
      <if test="demandId != null" >
        demand_id,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="achievementId != null" >
        #{achievementId,jdbcType=VARCHAR},
      </if>
      <if test="demandId != null" >
        #{demandId,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>
  
  <insert id="insertBatch" parameterType="com.goafanti.common.model.AchievementDemand" >
    insert into achievement_demand (achievement_id, demand_id)
    values 
    <foreach item="item" index="index" collection="list" separator=",">
      (
      	#{item.achievementId,jdbcType=VARCHAR}, #{item.demandId,jdbcType=VARCHAR}
      )
    </foreach>
  </insert>
  
   <delete id="deleteByDemandId" parameterType="java.lang.String">
  	 delete from achievement_demand where demand_id = #{id,jdbcType=VARCHAR}
  </delete>
  
   <delete id="batchDeleteByDemandIds" parameterType="java.util.List">
  	 delete from achievement_demand where demand_id in
        <foreach item="item" collection="list" open="(" separator="," close=")">
            #{item}
        </foreach>
  </delete>
  
   <delete id="deleteByAchievementId" parameterType="java.lang.String">
  	 delete from achievement_demand where achievement_id = #{id,jdbcType=VARCHAR}
  </delete>
  
   <delete id="batchDeleteByAchievementIds" parameterType="java.util.List">
  	 delete from achievement_demand where achievement_id in
        <foreach item="item" collection="list" open="(" separator="," close=")">
            #{item}
        </foreach>
  </delete>
  
  <select id="selectAchievementDemandListByDemandId" parameterType="java.lang.String" resultType="com.goafanti.achievement.bo.AchievementDemandListBo">
  	select  	
		a.id, a.serial_number as serialNumber, a.data_category as dataCategory, 
  		a.name, a.keyword, a.category,  a.owner_type as ownerType, 
  		ui.username as username, oi.unit_name as unitName,
  		a.release_date as releaseDate, a.owner_id as uid
	from achievement a 
	LEFT JOIN achievement_demand ad  on ad.achievement_id = a.id
	LEFT JOIN user_identity ui on ui.uid = a.owner_id
    LEFT JOIN organization_identity oi on oi.uid = a.owner_id
		where ad.demand_id = #{id,jdbcType=VARCHAR}
		and a.deleted_sign = 0  
		and a.audit_status = 3 
	</select>
  
  <select id="selectAchievementDemandListByAchievementId" parameterType="java.lang.String" resultType="com.goafanti.demand.bo.DemandAchievementListBo">
  	select
  		d.id, d.serial_number as serialNumber, d.data_category as dataCategory,
  		d.name, d.keyword, d.demand_type as demandType, 
  		d.release_date as releaseDate, d.validity_period as validityPeriod, oi.unit_name as unitName,
  		ui.username, d.employer_id as uid
  	from demand d
  	LEFT JOIN achievement_demand ad on ad.demand_id = d.id
  	LEFT JOIN user_identity ui on ui.uid = d.employer_id
  	LEFT JOIN organization_identity oi on oi.uid = d.employer_id
  	where ad.achievement_id = #{id,jdbcType=VARCHAR}
  	and d.deleted_sign = 0
  	and d.audit_status = 3
  </select>
</mapper>