<?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.hsdataplatform.mapper.ResourceSortMapper">
  <resultMap id="BaseResultMap" type="com.lightinit.hsdataplatform.entity.ResourceSort">
    <id column="id" jdbcType="BIGINT" property="id" />
    <result column="resource_id" jdbcType="BIGINT" property="resourceId" />
    <result column="sort_category" jdbcType="VARCHAR" property="sortCategory" />
    <result column="sort_value" jdbcType="INTEGER" property="sortValue" />
    <result column="created_at" jdbcType="TIMESTAMP" property="createdAt" />
    <result column="created_by" jdbcType="BIGINT" property="createdBy" />
    <result column="deleted_at" jdbcType="TIMESTAMP" property="deletedAt" />
    <result column="deleted_by" jdbcType="BIGINT" property="deletedBy" />
  </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, resource_id, sort_category, sort_value, created_at, created_by, deleted_at, deleted_by
  </sql>
  <select id="selectByExample" parameterType="com.lightinit.hsdataplatform.entity.ResourceSortExample" resultMap="BaseResultMap">
    select
    <if test="distinct">
      distinct
    </if>
    <include refid="Base_Column_List" />
    from resource_sort
    <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_sort
    where id = #{id,jdbcType=BIGINT}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Long">
    delete from resource_sort
    where id = #{id,jdbcType=BIGINT}
  </delete>
  <delete id="deleteByExample" parameterType="com.lightinit.hsdataplatform.entity.ResourceSortExample">
    delete from resource_sort
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </delete>
  <insert id="insert" parameterType="com.lightinit.hsdataplatform.entity.ResourceSort">
    <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Long">
      SELECT LAST_INSERT_ID()
    </selectKey>
    insert into resource_sort (resource_id, sort_category, sort_value, 
      created_at, created_by, deleted_at, 
      deleted_by)
    values (#{resourceId,jdbcType=BIGINT}, #{sortCategory,jdbcType=VARCHAR}, #{sortValue,jdbcType=INTEGER}, 
      #{createdAt,jdbcType=TIMESTAMP}, #{createdBy,jdbcType=BIGINT}, #{deletedAt,jdbcType=TIMESTAMP}, 
      #{deletedBy,jdbcType=BIGINT})
  </insert>
  <insert id="insertSelective" parameterType="com.lightinit.hsdataplatform.entity.ResourceSort">
    <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Long">
      SELECT LAST_INSERT_ID()
    </selectKey>
    insert into resource_sort
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="resourceId != null">
        resource_id,
      </if>
      <if test="sortCategory != null">
        sort_category,
      </if>
      <if test="sortValue != null">
        sort_value,
      </if>
      <if test="createdAt != null">
        created_at,
      </if>
      <if test="createdBy != null">
        created_by,
      </if>
      <if test="deletedAt != null">
        deleted_at,
      </if>
      <if test="deletedBy != null">
        deleted_by,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="resourceId != null">
        #{resourceId,jdbcType=BIGINT},
      </if>
      <if test="sortCategory != null">
        #{sortCategory,jdbcType=VARCHAR},
      </if>
      <if test="sortValue != null">
        #{sortValue,jdbcType=INTEGER},
      </if>
      <if test="createdAt != null">
        #{createdAt,jdbcType=TIMESTAMP},
      </if>
      <if test="createdBy != null">
        #{createdBy,jdbcType=BIGINT},
      </if>
      <if test="deletedAt != null">
        #{deletedAt,jdbcType=TIMESTAMP},
      </if>
      <if test="deletedBy != null">
        #{deletedBy,jdbcType=BIGINT},
      </if>
    </trim>
  </insert>
  <select id="countByExample" parameterType="com.lightinit.hsdataplatform.entity.ResourceSortExample" resultType="java.lang.Long">
    select count(*) from resource_sort
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </select>
  <update id="updateByExampleSelective" parameterType="map">
    update resource_sort
    <set>
      <if test="record.id != null">
        id = #{record.id,jdbcType=BIGINT},
      </if>
      <if test="record.resourceId != null">
        resource_id = #{record.resourceId,jdbcType=BIGINT},
      </if>
      <if test="record.sortCategory != null">
        sort_category = #{record.sortCategory,jdbcType=VARCHAR},
      </if>
      <if test="record.sortValue != null">
        sort_value = #{record.sortValue,jdbcType=INTEGER},
      </if>
      <if test="record.createdAt != null">
        created_at = #{record.createdAt,jdbcType=TIMESTAMP},
      </if>
      <if test="record.createdBy != null">
        created_by = #{record.createdBy,jdbcType=BIGINT},
      </if>
      <if test="record.deletedAt != null">
        deleted_at = #{record.deletedAt,jdbcType=TIMESTAMP},
      </if>
      <if test="record.deletedBy != null">
        deleted_by = #{record.deletedBy,jdbcType=BIGINT},
      </if>
    </set>
    <if test="_parameter != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByExample" parameterType="map">
    update resource_sort
    set id = #{record.id,jdbcType=BIGINT},
      resource_id = #{record.resourceId,jdbcType=BIGINT},
      sort_category = #{record.sortCategory,jdbcType=VARCHAR},
      sort_value = #{record.sortValue,jdbcType=INTEGER},
      created_at = #{record.createdAt,jdbcType=TIMESTAMP},
      created_by = #{record.createdBy,jdbcType=BIGINT},
      deleted_at = #{record.deletedAt,jdbcType=TIMESTAMP},
      deleted_by = #{record.deletedBy,jdbcType=BIGINT}
    <if test="_parameter != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByPrimaryKeySelective" parameterType="com.lightinit.hsdataplatform.entity.ResourceSort">
    update resource_sort
    <set>
      <if test="resourceId != null">
        resource_id = #{resourceId,jdbcType=BIGINT},
      </if>
      <if test="sortCategory != null">
        sort_category = #{sortCategory,jdbcType=VARCHAR},
      </if>
      <if test="sortValue != null">
        sort_value = #{sortValue,jdbcType=INTEGER},
      </if>
      <if test="createdAt != null">
        created_at = #{createdAt,jdbcType=TIMESTAMP},
      </if>
      <if test="createdBy != null">
        created_by = #{createdBy,jdbcType=BIGINT},
      </if>
      <if test="deletedAt != null">
        deleted_at = #{deletedAt,jdbcType=TIMESTAMP},
      </if>
      <if test="deletedBy != null">
        deleted_by = #{deletedBy,jdbcType=BIGINT},
      </if>
    </set>
    where id = #{id,jdbcType=BIGINT}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.lightinit.hsdataplatform.entity.ResourceSort">
    update resource_sort
    set resource_id = #{resourceId,jdbcType=BIGINT},
      sort_category = #{sortCategory,jdbcType=VARCHAR},
      sort_value = #{sortValue,jdbcType=INTEGER},
      created_at = #{createdAt,jdbcType=TIMESTAMP},
      created_by = #{createdBy,jdbcType=BIGINT},
      deleted_at = #{deletedAt,jdbcType=TIMESTAMP},
      deleted_by = #{deletedBy,jdbcType=BIGINT}
    where id = #{id,jdbcType=BIGINT}
  </update>
  <select id="queryList" parameterType="com.lightinit.hsdataplatform.model.admin.DataRecommendationSearchModel" resultType="com.lightinit.hsdataplatform.model.admin.DataRecommendationListModel">
    SELECT r.`resource_name`,s.sector_name provider,rs.id ,rs.sort_value  FROM resource r
    <if test="resourceType=='数据'">
      JOIN `resource_data` rd ON r.`id` = rd.`id`
      left join sector s on rd.belong_to = s.id
    </if>
    <if test="resourceType=='API'">
      JOIN `resource_api` ra ON r.`id` = ra.`id`
      left join sector s on ra.provider = s.id
    </if>
    <if test="resourceType=='微应用'">
      JOIN `resource_microapp` rm ON r.`id` = rm.`id`
      left join sector s on rm.provider = s.id
    </if>
    JOIN `resource_sort` rs ON r.`id` = rs.`resource_id`
    WHERE rs.`sort_category` = #{sortCategory}
    ORDER BY rs.`sort_value` desc
  </select>
  <select id="queryCount" parameterType="com.lightinit.hsdataplatform.model.admin.DataRecommendationSearchModel" resultType="java.lang.Long">
    SELECT count(*) FROM resource r
    <if test="resourceType=='数据'">
      JOIN `resource_data` rd ON r.`id` = rd.`id`
      left join sector s on rd.belong_to = s.id
    </if>
    <if test="resourceType=='API'">
      JOIN `resource_api` ra ON r.`id` = ra.`id`
      left join sector s on ra.provider = s.id
    </if>
    <if test="resourceType=='微应用'">
      JOIN `resource_microapp` rm ON r.`id` = rm.`id`
      left join sector s on rm.provider = s.id
    </if>
    JOIN `resource_sort` rs ON r.`id` = rs.`resource_id`
    WHERE rs.`sort_category` = #{sortCategory}
    ORDER BY rs.`sort_value` desc
  </select>
  <select id="queryResourceList" resultType="com.lightinit.hsdataplatform.model.admin.DataRecommendationListModel" parameterType="com.lightinit.hsdataplatform.model.admin.DataRecommendationSearchModel">
    <if test="resourceName != null and resourceName.length() > 0">
      <bind name="resourceName" value="'%' + _parameter.resourceName + '%'" />
    </if>
    SELECT r.`resource_name`,r.id resourceId,r.resource_type FROM `resource` r

    <where>
      <if test="resourceName != null and resourceName.length() > 0">
         r.resource_name like #{resourceName}
      </if>
      <if test="resourceType != null and resourceType.length() > 0 ">
        AND r.resource_type = #{resourceType}
      </if>
    </where>

      ORDER BY r.id desc
    <if test="page != null" >
      limit ${page.beginIndex},${page.pageSize}
    </if>
  </select>
  <select id="queryResourceCount" resultType="java.lang.Long" parameterType="com.lightinit.hsdataplatform.model.admin.DataRecommendationSearchModel">
    <if test="resourceName != null and resourceName.length() > 0">
      <bind name="resourceName" value="'%' + _parameter.resourceName + '%'" />
    </if>
    SELECT count(*) FROM `resource` r

    <where>
      <if test="resourceName != null and resourceName.length() > 0">
         r.resource_name like #{resourceName}
      </if>
      <if test="resourceType != null and resourceType.length() > 0 ">
        AND r.resource_type = #{resourceType}
      </if>
    </where>
    ORDER BY r.id desc
  </select>
    <select id="queryDefaultList" parameterType="com.lightinit.hsdataplatform.model.admin.DataRecommendationSearchModel" resultType="com.lightinit.hsdataplatform.model.admin.DataRecommendationListModel">
        SELECT r.`resource_name`,s.sector_name provider,rs.id,p.audited_at,r.id resourceId ,rs.sort_value FROM resource r
        <if test="resourceType=='数据'">
            JOIN `resource_data` rd ON r.`id` = rd.`id`
            left join sector s on rd.belong_to = s.id
        </if>
        <if test="resourceType=='API'">
            JOIN `resource_api` ra ON r.`id` = ra.`id`
            left join sector s on ra.provider = s.id
        </if>
        <if test="resourceType=='微应用'">
            JOIN `resource_microapp` rm ON r.`id` = rm.`id`
            left join sector s on rm.provider = s.id
        </if>
        join publish_info p on r.id = p .resource_id
        LEFT JOIN `resource_sort` rs ON r.`id` = rs.`resource_id`
        <where>
            <if test="sortCategory != null and sortCategory.length() > 0 ">
                 rs.sort_category = #{sortCategory}
            </if>
        </where>

        ORDER BY rs.`sort_value` desc
      <if test="page != null" >
        limit ${page.beginIndex},${page.pageSize}
      </if>
    </select>
    <select id="queryDefaultCount" resultType="java.lang.Long" parameterType="com.lightinit.hsdataplatform.model.admin.DataRecommendationSearchModel">
        SELECT count(*) FROM resource r
        <if test="resourceType=='数据'">
            JOIN `resource_data` rd ON r.`id` = rd.`id`
            left join sector s on rd.belong_to = s.id
        </if>
        <if test="resourceType=='API'">
            JOIN `resource_api` ra ON r.`id` = ra.`id`
            left join sector s on ra.provider = s.id
        </if>
        <if test="resourceType=='微应用'">
            JOIN `resource_microapp` rm ON r.`id` = rm.`id`
            left join sector s on rm.provider = s.id
        </if>
        join publish_info p on r.id = p .resource_id
        LEFT  JOIN `resource_sort` rs ON r.`id` = rs.`resource_id`
        <where>
            <if test="sortCategory != null and sortCategory.length() > 0 ">
                rs.sort_category = #{sortCategory}
            </if>
        </where>
        ORDER BY rs.`sort_value` desc
    </select>
</mapper>