<?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">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Fri Jun 15 09:33:14 CST 2018.
    -->
    <id column="id" jdbcType="VARCHAR" property="id" />
    <result column="from_uid" jdbcType="VARCHAR" property="fromUid" />
    <result column="to_uid" jdbcType="VARCHAR" property="toUid" />
    <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
  </resultMap>
  <sql id="Example_Where_Clause">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Fri Jun 15 09:33:14 CST 2018.
    -->
    <where>
      <foreach collection="oredCriteria" item="criteria" separator="or">
        <if test="criteria.valid">
          <trim prefix="(" prefixOverrides="and" suffix=")">
            <foreach collection="criteria.criteria" item="criterion">
              <choose>
                <when test="criterion.noValue">
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue">
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue">
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue">
                  and ${criterion.condition}
                  <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
  <sql id="Update_By_Example_Where_Clause">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Fri Jun 15 09:33:14 CST 2018.
    -->
    <where>
      <foreach collection="example.oredCriteria" item="criteria" separator="or">
        <if test="criteria.valid">
          <trim prefix="(" prefixOverrides="and" suffix=")">
            <foreach collection="criteria.criteria" item="criterion">
              <choose>
                <when test="criterion.noValue">
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue">
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue">
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue">
                  and ${criterion.condition}
                  <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
  <sql id="Base_Column_List">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Fri Jun 15 09:33:14 CST 2018.
    -->
    id, from_uid, to_uid, create_time
  </sql>
  <select id="selectByExample" parameterType="com.goafanti.common.model.UserInterestExample" resultMap="BaseResultMap">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Fri Jun 15 09:33:14 CST 2018.
    -->
    select
    <if test="distinct">
      distinct
    </if>
    <include refid="Base_Column_List" />
    from user_interest
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
    <if test="orderByClause != null">
      order by ${orderByClause}
    </if>
  </select>
  <select id="selectByPrimaryKey" parameterType="java.lang.String" resultMap="BaseResultMap">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Fri Jun 15 09:33:14 CST 2018.
    -->
    select 
    <include refid="Base_Column_List" />
    from user_interest
    where id = #{id,jdbcType=VARCHAR}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.String">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Fri Jun 15 09:33:14 CST 2018.
    -->
    delete from user_interest
    where id = #{id,jdbcType=VARCHAR}
  </delete>
  <delete id="deleteByExample" parameterType="com.goafanti.common.model.UserInterestExample">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Fri Jun 15 09:33:14 CST 2018.
    -->
    delete from user_interest
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </delete>
  <insert id="insert" parameterType="com.goafanti.common.model.UserInterest">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Fri Jun 15 09:33:14 CST 2018.
    -->
    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">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Fri Jun 15 09:33:14 CST 2018.
    -->
    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>
  <select id="countByExample" parameterType="com.goafanti.common.model.UserInterestExample" resultType="java.lang.Long">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Fri Jun 15 09:33:14 CST 2018.
    -->
    select count(*) from user_interest
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </select>
  <update id="updateByExampleSelective" parameterType="map">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Fri Jun 15 09:33:14 CST 2018.
    -->
    update user_interest
    <set>
      <if test="record.id != null">
        id = #{record.id,jdbcType=VARCHAR},
      </if>
      <if test="record.fromUid != null">
        from_uid = #{record.fromUid,jdbcType=VARCHAR},
      </if>
      <if test="record.toUid != null">
        to_uid = #{record.toUid,jdbcType=VARCHAR},
      </if>
      <if test="record.createTime != null">
        create_time = #{record.createTime,jdbcType=TIMESTAMP},
      </if>
    </set>
    <if test="_parameter != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByExample" parameterType="map">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Fri Jun 15 09:33:14 CST 2018.
    -->
    update user_interest
    set id = #{record.id,jdbcType=VARCHAR},
      from_uid = #{record.fromUid,jdbcType=VARCHAR},
      to_uid = #{record.toUid,jdbcType=VARCHAR},
      create_time = #{record.createTime,jdbcType=TIMESTAMP}
    <if test="_parameter != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByPrimaryKeySelective" parameterType="com.goafanti.common.model.UserInterest">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Fri Jun 15 09:33:14 CST 2018.
    -->
    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">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Fri Jun 15 09:33:14 CST 2018.
    -->
    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" parameterType="java.lang.String" resultType="java.lang.Integer">
  	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>
  
    <select id="findAppUserInterestByPage" parameterType="String" resultType="com.goafanti.demand.bo.ObjectInterestListBo">
  	select y.uid as id,y.username as name ,t.create_time as createTime
	from user_interest t
	left join  user_identity y on t.to_uid=y.uid
	where expert=1  
	and from_uid=#{uid,jdbcType=VARCHAR}
  	<if test="page_sql!=null">
		${page_sql}
	</if>
  </select>
  
  <select id="findAppUserInterestCount" parameterType="String" resultType="java.lang.Integer">
  	select count(1)
	from user_interest t
	left join  user_identity y on t.to_uid=y.uid
	where expert=1  
	and  from_uid=#{uid,jdbcType=VARCHAR}
	</select>
	
	<select id="findAppCounselorInterestByPage" parameterType="String" resultType="com.goafanti.demand.bo.ObjectInterestListBo">
  	select y.uid as id,y.username as name ,t.create_time as createTime
	from user_interest t
	left join  user_identity y on t.to_uid=y.uid
	where expert=2  
	and from_uid=#{uid,jdbcType=VARCHAR}
  	<if test="page_sql!=null">
		${page_sql}
	</if>
  </select>
  
  <select id="findAppCounselorInterestCount" parameterType="String" resultType="java.lang.Integer">
  	select count(1)
	from user_interest t
	left join  user_identity y on t.to_uid=y.uid
	where  expert=2  
	and from_uid=#{uid,jdbcType=VARCHAR}
	</select>
</mapper>