<?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.UserInterestMapper" >
  <resultMap id="BaseResultMap" type="com.goafanti.common.model.UserInterest" >
    <id column="id" property="id" jdbcType="VARCHAR" />
    <result column="from_uid" property="fromUid" jdbcType="VARCHAR" />
    <result column="to_uid" property="toUid" jdbcType="VARCHAR" />
    <result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
  </resultMap>
  <sql id="Base_Column_List" >
    id, from_uid, to_uid, create_time
  </sql>
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.String" >
    select 
    <include refid="Base_Column_List" />
    from user_interest
    where id = #{id,jdbcType=VARCHAR}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.String" >
    delete from user_interest
    where id = #{id,jdbcType=VARCHAR}
  </delete>
  <insert id="insert" parameterType="com.goafanti.common.model.UserInterest" >
    insert into user_interest (id, from_uid, to_uid, 
      create_time)
    values (#{id,jdbcType=VARCHAR}, #{fromUid,jdbcType=VARCHAR}, #{toUid,jdbcType=VARCHAR}, 
      #{createTime,jdbcType=TIMESTAMP})
  </insert>
  <insert id="insertSelective" parameterType="com.goafanti.common.model.UserInterest" >
    insert into user_interest
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        id,
      </if>
      <if test="fromUid != null" >
        from_uid,
      </if>
      <if test="toUid != null" >
        to_uid,
      </if>
      <if test="createTime != null" >
        create_time,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        #{id,jdbcType=VARCHAR},
      </if>
      <if test="fromUid != null" >
        #{fromUid,jdbcType=VARCHAR},
      </if>
      <if test="toUid != null" >
        #{toUid,jdbcType=VARCHAR},
      </if>
      <if test="createTime != null" >
        #{createTime,jdbcType=TIMESTAMP},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.goafanti.common.model.UserInterest" >
    update user_interest
    <set >
      <if test="fromUid != null" >
        from_uid = #{fromUid,jdbcType=VARCHAR},
      </if>
      <if test="toUid != null" >
        to_uid = #{toUid,jdbcType=VARCHAR},
      </if>
      <if test="createTime != null" >
        create_time = #{createTime,jdbcType=TIMESTAMP},
      </if>
    </set>
    where id = #{id,jdbcType=VARCHAR}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.goafanti.common.model.UserInterest" >
    update user_interest
    set from_uid = #{fromUid,jdbcType=VARCHAR},
      to_uid = #{toUid,jdbcType=VARCHAR},
      create_time = #{createTime,jdbcType=TIMESTAMP}
    where id = #{id,jdbcType=VARCHAR}
  </update>
  
  <select id="findByFromUidAndToUid" resultMap="BaseResultMap">
   	select 
    <include refid="Base_Column_List" />
    from user_interest
    where from_uid = #{fromUid,jdbcType=VARCHAR}
    and to_uid =  #{toUid,jdbcType=VARCHAR}
  </select>
  
  <select id="findInterestUserListByPage" parameterType="java.lang.String" resultType="com.goafanti.user.bo.InterestUserListBo">
	SELECT
			uii.username,
			oi.unit_name AS unitName,
			u.number,
			u.type,
			u.id AS uid,
			ui.id AS interestId,
			ui.create_time AS createTime,
			uii.province AS userProvince,
			oi.licence_province AS orgProvince,
			u.head_portrait_url AS personPortraitUrl,
			oinfo.logo_url AS logoUrl,
			adc.achievement_count AS achievementNum,
			adc.demand_count AS demandNum
	FROM
		user_interest ui
	LEFT JOIN user_identity uii ON uii.uid = ui.to_uid
	LEFT JOIN organization_identity oi ON oi.uid = ui.to_uid
	LEFT JOIN `user` u ON u.id = ui.to_uid
	<!-- LEFT JOIN user_info uinfo ON uinfo.uid = ui.to_uid -->
	LEFT JOIN organization_info oinfo ON oinfo.uid = ui.to_uid
	LEFT JOIN achievement_demand_count adc ON adc.uid = ui.to_uid
	WHERE
		ui.from_uid = #{fromUid,jdbcType=VARCHAR}
		<if test="userType != null" >
        and u.type = #{userType}
      	</if>
	ORDER BY
		ui.create_time DESC
	<if test="page_sql!=null">
			${page_sql}
	</if>
  </select>
  
  <select id="findInterestUserCount" resultType="java.lang.Integer" parameterType="java.lang.String">
  	SELECT
	  count(1)
    FROM
	 user_interest ui
	 LEFT JOIN `user` u ON u.id = ui.to_uid
    WHERE
	 ui.from_uid = #{fromUid,jdbcType=VARCHAR}
	 <if test="userType != null" >
       and u.type = #{userType}
   	 </if>
  </select>
  
  <select id="countByToUid" parameterType="java.lang.String" resultType="java.lang.Integer">
  	select 
  		count(1) 
  	from user_interest 
  	where to_uid = #{toUid,jdbcType=VARCHAR} 
  </select>
  
  <delete id="deleteInterest" parameterType="java.lang.String" >
    delete from user_interest
    where from_uid = #{uid,jdbcType=VARCHAR}
    and to_uid=#{objectId,jdbcType=VARCHAR}
    </delete>
    <select id="checkUidAndDid" resultType="Integer">
  		select count(0) from user_interest where 1=1
  	    <if test="id != null">
  	       and from_uid = #{id,jdbcType=VARCHAR}
  	    </if>
  		<if test="uid != null">
  	       and to_uid = #{uid,jdbcType=VARCHAR}
  	    </if>
  </select>
  
   
   <select id="countInterest" resultType="java.lang.Integer">
  	select count(0) from user_interest
	where 
  	to_uid = #{uid,jdbcType=VARCHAR}
  </select>
   
   
    
</mapper>