<?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.lightinit.hsdataportal.mapper.ResourceFollowMapper">
  <resultMap id="BaseResultMap" type="com.lightinit.hsdataportal.entity.ResourceFollow">
    <id column="id" jdbcType="BIGINT" property="id" />
    <result column="platform_type" jdbcType="VARCHAR" property="platformType" />
    <result column="follower_id" jdbcType="BIGINT" property="followerId" />
    <result column="publish_setting_id" jdbcType="BIGINT" property="publishSettingId" />
    <result column="created_at" jdbcType="TIMESTAMP" property="createdAt" />
  </resultMap>
  <sql id="Example_Where_Clause">
    <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">
    <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">
    id, platform_type, follower_id, publish_setting_id, created_at
  </sql>
  <select id="selectByExample" parameterType="com.lightinit.hsdataportal.entity.ResourceFollowExample" resultMap="BaseResultMap">
    select
    <if test="distinct">
      distinct
    </if>
    <include refid="Base_Column_List" />
    from resource_follow
    <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.Long" resultMap="BaseResultMap">
    select 
    <include refid="Base_Column_List" />
    from resource_follow
    where id = #{id,jdbcType=BIGINT}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Long">
    delete from resource_follow
    where id = #{id,jdbcType=BIGINT}
  </delete>
  <delete id="deleteByExample" parameterType="com.lightinit.hsdataportal.entity.ResourceFollowExample">
    delete from resource_follow
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </delete>
  <insert id="insert" parameterType="com.lightinit.hsdataportal.entity.ResourceFollow">
    <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Long">
      SELECT LAST_INSERT_ID()
    </selectKey>
    insert into resource_follow (platform_type, follower_id, publish_setting_id, 
      created_at)
    values (#{platformType,jdbcType=VARCHAR}, #{followerId,jdbcType=BIGINT}, #{publishSettingId,jdbcType=BIGINT}, 
      #{createdAt,jdbcType=TIMESTAMP})
  </insert>
  <insert id="insertSelective" parameterType="com.lightinit.hsdataportal.entity.ResourceFollow">
    <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Long">
      SELECT LAST_INSERT_ID()
    </selectKey>
    insert into resource_follow
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="platformType != null">
        platform_type,
      </if>
      <if test="followerId != null">
        follower_id,
      </if>
      <if test="publishSettingId != null">
        publish_setting_id,
      </if>
      <if test="createdAt != null">
        created_at,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="platformType != null">
        #{platformType,jdbcType=VARCHAR},
      </if>
      <if test="followerId != null">
        #{followerId,jdbcType=BIGINT},
      </if>
      <if test="publishSettingId != null">
        #{publishSettingId,jdbcType=BIGINT},
      </if>
      <if test="createdAt != null">
        #{createdAt,jdbcType=TIMESTAMP},
      </if>
    </trim>
  </insert>
  <select id="countByExample" parameterType="com.lightinit.hsdataportal.entity.ResourceFollowExample" resultType="java.lang.Long">
    select count(*) from resource_follow
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </select>
  <update id="updateByExampleSelective" parameterType="map">
    update resource_follow
    <set>
      <if test="record.id != null">
        id = #{record.id,jdbcType=BIGINT},
      </if>
      <if test="record.platformType != null">
        platform_type = #{record.platformType,jdbcType=VARCHAR},
      </if>
      <if test="record.followerId != null">
        follower_id = #{record.followerId,jdbcType=BIGINT},
      </if>
      <if test="record.publishSettingId != null">
        publish_setting_id = #{record.publishSettingId,jdbcType=BIGINT},
      </if>
      <if test="record.createdAt != null">
        created_at = #{record.createdAt,jdbcType=TIMESTAMP},
      </if>
    </set>
    <if test="_parameter != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByExample" parameterType="map">
    update resource_follow
    set id = #{record.id,jdbcType=BIGINT},
      platform_type = #{record.platformType,jdbcType=VARCHAR},
      follower_id = #{record.followerId,jdbcType=BIGINT},
      publish_setting_id = #{record.publishSettingId,jdbcType=BIGINT},
      created_at = #{record.createdAt,jdbcType=TIMESTAMP}
    <if test="_parameter != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByPrimaryKeySelective" parameterType="com.lightinit.hsdataportal.entity.ResourceFollow">
    update resource_follow
    <set>
      <if test="platformType != null">
        platform_type = #{platformType,jdbcType=VARCHAR},
      </if>
      <if test="followerId != null">
        follower_id = #{followerId,jdbcType=BIGINT},
      </if>
      <if test="publishSettingId != null">
        publish_setting_id = #{publishSettingId,jdbcType=BIGINT},
      </if>
      <if test="createdAt != null">
        created_at = #{createdAt,jdbcType=TIMESTAMP},
      </if>
    </set>
    where id = #{id,jdbcType=BIGINT}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.lightinit.hsdataportal.entity.ResourceFollow">
    update resource_follow
    set platform_type = #{platformType,jdbcType=VARCHAR},
      follower_id = #{followerId,jdbcType=BIGINT},
      publish_setting_id = #{publishSettingId,jdbcType=BIGINT},
      created_at = #{createdAt,jdbcType=TIMESTAMP}
    where id = #{id,jdbcType=BIGINT}
  </update>
  <select id="queryOpenList" parameterType="com.lightinit.hsdataportal.model.MyAPIFollowSearchModel" resultType="com.lightinit.hsdataportal.model.MyAPIOpenFollowListModel">
        select r.resource_name,r.resource_summary,rf.id,pi.audited_at,rf.created_at
        from `resource_follow` rf
        join publish_setting ps
        on rf.`publish_setting_id` = ps.id
        left join publish_info pi
        on pi.id = ps.publish_info_id
        left join resource r
        on r.id = pi.`resource_id`
        <if test="dicPlatformType == '开放平台'.toString()">
        LEFT JOIN `user` u
        ON u.id = rf.follower_id
        WHERE u.id = #{followId} AND r.resource_type = #{resourceType}
        </if>
        <if test="dicPlatformType == '共享平台'.toString()">
          LEFT JOIN `admin` a
          ON a.id = rf.follower_id
            WHERE a.id = #{followId} AND r.resource_type = #{resourceType}
        </if>
        ORDER BY rf.id DESC
        <if test="page != null">
          LIMIT #{page.beginIndex},#{page.pageSize}
        </if>
  </select>
  <select id="queryOpenCount" parameterType="com.lightinit.hsdataportal.model.MyAPIApplySearchModel" resultType="java.lang.Long">
    select count(*)
    from `resource_follow` rf
    join publish_setting ps
    on rf.`publish_setting_id` = ps.id
    left join publish_info pi
    on pi.id = ps.publish_info_id
    left join resource r
    on r.id = pi.`resource_id`
      <if test="dicPlatformType == '开放平台'.toString()">
          LEFT JOIN `user` u
          ON u.id = rf.follower_id
          WHERE u.id = #{followId} AND r.resource_type = #{resourceType}
      </if>
      <if test="dicPlatformType == '共享平台'.toString()">
          LEFT JOIN `admin` a
          ON a.id = rf.follower_id
          WHERE a.id = #{followId} AND r.resource_type = #{resourceType}
      </if>
  </select>
  <select id="selectFollowOpenList" parameterType="com.lightinit.hsdataportal.model.MyMicroappFollowSearchModel" resultType="com.lightinit.hsdataportal.model.MyMicroappFollowListModel">
    select r.resource_name,r.resource_summary,pi.created_at auditedAt,rf.id,rf.created_at
    from `resource_follow` rf
    join publish_setting ps
    on rf.`publish_setting_id` = ps.id
    left join publish_info pi
    on pi.id = ps.publish_info_id
    left join resource r
    on r.id = pi.`resource_id`
    <if test="platformType == '开放平台'.toString()">
      LEFT JOIN `user` u
      ON u.id = rf.follower_id
    </if>
    <if test="platformType == '共享平台'.toString()">
      LEFT JOIN `admin` a
      ON a.id = rf.follower_id
    </if>
    <where>
      r.resource_type = #{resourceType}
      and rf.platform_type =#{platformType}
      and rf.follower_id = #{followId}
    </where>
    ORDER BY rf.id DESC
    <if test="page != null">
      LIMIT #{page.beginIndex},#{page.pageSize}
    </if>
  </select>
  <select id="selectFollowOpenCount" parameterType="com.lightinit.hsdataportal.model.MyMicroappFollowSearchModel" resultType="java.lang.Long">
    select count(*)
    from `resource_follow` rf
    join publish_setting ps
    on rf.`publish_setting_id` = ps.id
    left join publish_info pi
    on pi.id = ps.publish_info_id
    left join resource r
    on r.id = pi.`resource_id`
    <if test="platformType == '开放平台'.toString()">
      LEFT JOIN `user` u
      ON u.id = rf.follower_id
    </if>
    <if test="platformType == '共享平台'.toString()">
      LEFT JOIN `admin` a
      ON a.id = rf.follower_id
    </if>
    <where>
      r.resource_type = #{resourceType}
      and rf.follower_id = #{followId}
    </where>
    ORDER BY rf.id DESC
  </select>

  <!--sun:-->
    <select id="getFollowDateList" parameterType="com.lightinit.hsdataportal.model.ResourceAuthMapperDemo" resultType="com.lightinit.hsdataportal.model.FollowDataListModel">
      SELECT rf.id AS follow_id,ps.id AS publish_settingId,r.resource_name,r.resource_summary,s.sector_name,pu.audited_at,rf.created_at,u.id AS user_id
      FROM resource_follow rf
      INNER JOIN publish_setting  ps ON ps.id=rf.publish_setting_id
      LEFT JOIN publish_info pu ON pu.id=ps.publish_info_id
      INNER JOIN resource r ON r.id=pu.resource_id
      LEFT JOIN resource_data rd ON rd.id=r.id
      LEFT JOIN sector s ON rd.belong_to=s.id
      <if test="openType !=null and openType!=''">
        INNER JOIN USER u ON u.id=rf.follower_id
        where
        r.open_type !=#{shareOrOpenType} AND r.open_type IS NOT NULL AND r.resource_type=#{resourceType} AND rf.platform_type=#{platformType} AND u.id=#{applierId}
      </if>
      <if test="shareType !=null and shareType!=''">
        INNER JOIN admin u ON u.id=rf.follower_id
        where
        r.share_type !=#{shareOrOpenType} AND r.share_type IS NOT NULL AND r.resource_type=#{resourceType} AND rf.platform_type=#{platformType} AND u.id=#{applierId}
      </if>
  </select>

  <select id="selectFollowApiOrMicCount" resultType="java.lang.Integer">
    SELECT count(*)
    FROM resource_follow rf
    LEFT JOIN publish_setting ps
    ON rf.publish_setting_id = ps.id
    LEFT JOIN publish_info pi
    ON ps.publish_info_id = pi.id
    LEFT JOIN resource r
    ON r.id = pi.resource_id
    WHERE rf.follower_id = #{userId}
    AND rf.platform_type = #{platformType}
    AND r.resource_type = #{resourceType}
  </select>

  <select id="selectFollowCount" resultType="java.lang.Integer">
    select count(*)
    FROM resource_follow
    WHERE platform_type = #{platformType}
    AND publish_setting_id = #{settingId}
  </select>
</mapper>