<?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.ResourceMicroappMapper">
  <resultMap id="BaseResultMap" type="com.lightinit.hsdataportal.entity.ResourceMicroapp">
    <id column="id" jdbcType="BIGINT" property="id" />
    <result column="provider" jdbcType="BIGINT" property="provider" />
    <result column="service_method" jdbcType="VARCHAR" property="serviceMethod" />
    <result column="service_channel" jdbcType="VARCHAR" property="serviceChannel" />
    <result column="service_tag" jdbcType="VARCHAR" property="serviceTag" />
    <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>
  <resultMap extends="BaseResultMap" id="ResultMapWithBLOBs" type="com.lightinit.hsdataportal.entity.ResourceMicroappWithBLOBs">
    <result column="demo_url" jdbcType="LONGVARCHAR" property="demoUrl" />
    <result column="url" jdbcType="LONGVARCHAR" property="url" />
    <result column="screenshot" jdbcType="LONGVARCHAR" property="screenshot" />
  </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, provider, service_method, service_channel, service_tag, created_at, created_by, 
    deleted_at, deleted_by
  </sql>
  <sql id="Blob_Column_List">
    demo_url, url, screenshot
  </sql>
  <select id="selectByExampleWithBLOBs" parameterType="com.lightinit.hsdataportal.entity.ResourceMicroappExample" resultMap="ResultMapWithBLOBs">
    select
    <if test="distinct">
      distinct
    </if>
    <include refid="Base_Column_List" />
    ,
    <include refid="Blob_Column_List" />
    from resource_microapp
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
    <if test="orderByClause != null">
      order by ${orderByClause}
    </if>
  </select>
  <select id="selectByExample" parameterType="com.lightinit.hsdataportal.entity.ResourceMicroappExample" resultMap="BaseResultMap">
    select
    <if test="distinct">
      distinct
    </if>
    <include refid="Base_Column_List" />
    from resource_microapp
    <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="ResultMapWithBLOBs">
    select 
    <include refid="Base_Column_List" />
    ,
    <include refid="Blob_Column_List" />
    from resource_microapp
    where id = #{id,jdbcType=BIGINT}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Long">
    delete from resource_microapp
    where id = #{id,jdbcType=BIGINT}
  </delete>
  <delete id="deleteByExample" parameterType="com.lightinit.hsdataportal.entity.ResourceMicroappExample">
    delete from resource_microapp
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </delete>
  <insert id="insert" parameterType="com.lightinit.hsdataportal.entity.ResourceMicroappWithBLOBs">
    <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Long">
      SELECT LAST_INSERT_ID()
    </selectKey>
    insert into resource_microapp (provider, service_method, service_channel, 
      service_tag, created_at, created_by, 
      deleted_at, deleted_by, demo_url, 
      url, screenshot)
    values (#{provider,jdbcType=BIGINT}, #{serviceMethod,jdbcType=VARCHAR}, #{serviceChannel,jdbcType=VARCHAR}, 
      #{serviceTag,jdbcType=VARCHAR}, #{createdAt,jdbcType=TIMESTAMP}, #{createdBy,jdbcType=BIGINT}, 
      #{deletedAt,jdbcType=TIMESTAMP}, #{deletedBy,jdbcType=BIGINT}, #{demoUrl,jdbcType=LONGVARCHAR}, 
      #{url,jdbcType=LONGVARCHAR}, #{screenshot,jdbcType=LONGVARCHAR})
  </insert>
  <insert id="insertSelective" parameterType="com.lightinit.hsdataportal.entity.ResourceMicroappWithBLOBs">
    <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Long">
      SELECT LAST_INSERT_ID()
    </selectKey>
    insert into resource_microapp
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="provider != null">
        provider,
      </if>
      <if test="serviceMethod != null">
        service_method,
      </if>
      <if test="serviceChannel != null">
        service_channel,
      </if>
      <if test="serviceTag != null">
        service_tag,
      </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>
      <if test="demoUrl != null">
        demo_url,
      </if>
      <if test="url != null">
        url,
      </if>
      <if test="screenshot != null">
        screenshot,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="provider != null">
        #{provider,jdbcType=BIGINT},
      </if>
      <if test="serviceMethod != null">
        #{serviceMethod,jdbcType=VARCHAR},
      </if>
      <if test="serviceChannel != null">
        #{serviceChannel,jdbcType=VARCHAR},
      </if>
      <if test="serviceTag != null">
        #{serviceTag,jdbcType=VARCHAR},
      </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>
      <if test="demoUrl != null">
        #{demoUrl,jdbcType=LONGVARCHAR},
      </if>
      <if test="url != null">
        #{url,jdbcType=LONGVARCHAR},
      </if>
      <if test="screenshot != null">
        #{screenshot,jdbcType=LONGVARCHAR},
      </if>
    </trim>
  </insert>
  <select id="countByExample" parameterType="com.lightinit.hsdataportal.entity.ResourceMicroappExample" resultType="java.lang.Long">
    select count(*) from resource_microapp
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </select>
  <update id="updateByExampleSelective" parameterType="map">
    update resource_microapp
    <set>
      <if test="record.id != null">
        id = #{record.id,jdbcType=BIGINT},
      </if>
      <if test="record.provider != null">
        provider = #{record.provider,jdbcType=BIGINT},
      </if>
      <if test="record.serviceMethod != null">
        service_method = #{record.serviceMethod,jdbcType=VARCHAR},
      </if>
      <if test="record.serviceChannel != null">
        service_channel = #{record.serviceChannel,jdbcType=VARCHAR},
      </if>
      <if test="record.serviceTag != null">
        service_tag = #{record.serviceTag,jdbcType=VARCHAR},
      </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>
      <if test="record.demoUrl != null">
        demo_url = #{record.demoUrl,jdbcType=LONGVARCHAR},
      </if>
      <if test="record.url != null">
        url = #{record.url,jdbcType=LONGVARCHAR},
      </if>
      <if test="record.screenshot != null">
        screenshot = #{record.screenshot,jdbcType=LONGVARCHAR},
      </if>
    </set>
    <if test="_parameter != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByExampleWithBLOBs" parameterType="map">
    update resource_microapp
    set id = #{record.id,jdbcType=BIGINT},
      provider = #{record.provider,jdbcType=BIGINT},
      service_method = #{record.serviceMethod,jdbcType=VARCHAR},
      service_channel = #{record.serviceChannel,jdbcType=VARCHAR},
      service_tag = #{record.serviceTag,jdbcType=VARCHAR},
      created_at = #{record.createdAt,jdbcType=TIMESTAMP},
      created_by = #{record.createdBy,jdbcType=BIGINT},
      deleted_at = #{record.deletedAt,jdbcType=TIMESTAMP},
      deleted_by = #{record.deletedBy,jdbcType=BIGINT},
      demo_url = #{record.demoUrl,jdbcType=LONGVARCHAR},
      url = #{record.url,jdbcType=LONGVARCHAR},
      screenshot = #{record.screenshot,jdbcType=LONGVARCHAR}
    <if test="_parameter != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByExample" parameterType="map">
    update resource_microapp
    set id = #{record.id,jdbcType=BIGINT},
      provider = #{record.provider,jdbcType=BIGINT},
      service_method = #{record.serviceMethod,jdbcType=VARCHAR},
      service_channel = #{record.serviceChannel,jdbcType=VARCHAR},
      service_tag = #{record.serviceTag,jdbcType=VARCHAR},
      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.hsdataportal.entity.ResourceMicroappWithBLOBs">
    update resource_microapp
    <set>
      <if test="provider != null">
        provider = #{provider,jdbcType=BIGINT},
      </if>
      <if test="serviceMethod != null">
        service_method = #{serviceMethod,jdbcType=VARCHAR},
      </if>
      <if test="serviceChannel != null">
        service_channel = #{serviceChannel,jdbcType=VARCHAR},
      </if>
      <if test="serviceTag != null">
        service_tag = #{serviceTag,jdbcType=VARCHAR},
      </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>
      <if test="demoUrl != null">
        demo_url = #{demoUrl,jdbcType=LONGVARCHAR},
      </if>
      <if test="url != null">
        url = #{url,jdbcType=LONGVARCHAR},
      </if>
      <if test="screenshot != null">
        screenshot = #{screenshot,jdbcType=LONGVARCHAR},
      </if>
    </set>
    where id = #{id,jdbcType=BIGINT}
  </update>
  <update id="updateByPrimaryKeyWithBLOBs" parameterType="com.lightinit.hsdataportal.entity.ResourceMicroappWithBLOBs">
    update resource_microapp
    set provider = #{provider,jdbcType=BIGINT},
      service_method = #{serviceMethod,jdbcType=VARCHAR},
      service_channel = #{serviceChannel,jdbcType=VARCHAR},
      service_tag = #{serviceTag,jdbcType=VARCHAR},
      created_at = #{createdAt,jdbcType=TIMESTAMP},
      created_by = #{createdBy,jdbcType=BIGINT},
      deleted_at = #{deletedAt,jdbcType=TIMESTAMP},
      deleted_by = #{deletedBy,jdbcType=BIGINT},
      demo_url = #{demoUrl,jdbcType=LONGVARCHAR},
      url = #{url,jdbcType=LONGVARCHAR},
      screenshot = #{screenshot,jdbcType=LONGVARCHAR}
    where id = #{id,jdbcType=BIGINT}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.lightinit.hsdataportal.entity.ResourceMicroapp">
    update resource_microapp
    set provider = #{provider,jdbcType=BIGINT},
      service_method = #{serviceMethod,jdbcType=VARCHAR},
      service_channel = #{serviceChannel,jdbcType=VARCHAR},
      service_tag = #{serviceTag,jdbcType=VARCHAR},
      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="selectOpenList" resultType="com.lightinit.hsdataportal.model.MicroappListModel" parameterType="com.lightinit.hsdataportal.model.MicroappSearchModel">
    <if test="resourceName != null and resourceName.length() > 0">
      <bind name="resourceName" value="'%' + _parameter.resourceName + '%'" />
    </if>
    <if test="catelogThemeCode != null and catelogThemeCode.length() > 0">
      <bind name="catelogThemeCode" value="_parameter.catelogThemeCode + '%'" />
    </if>
    <if test="sectorCode != null and sectorCode.length() > 0">
      <bind name="sectorCode" value="_parameter.sectorCode + '%'" />
    </if>
    <if test="resourceIdentity != null and resourceIdentity.length() > 0">
      <bind name="resourceIdentity" value="'%'+_parameter.resourceIdentity + '%'" />
    </if>
    <if test="serviceTag != null and serviceTag.length() > 0">
      <bind name="serviceTag" value="'%'+_parameter.serviceTag + '%'" />
    </if>
    SELECT
      ps.id settingId,
    r.`resource_name`,
    rv.`view_times`,
    ps.`package_name`,
    ps.`charge_method`,
    r.`open_type`,
    p.resource_img,
    (SELECT COUNT(*) FROM `resource_auth` ra WHERE ra.publish_setting_id =ps.`id` )apply
    FROM
    `resource` r
    JOIN `resource_microapp` rm
    ON r.`id` = rm.`id`
    JOIN `publish_info` p
    ON r.`id` = p.`resource_id`
    JOIN `publish_setting` ps ON p.`id` =ps.`publish_info_id` and ps.deleted_at is null
    LEFT JOIN `resource_view_times` rv
    ON r.`id` = rv.`id`
    left join sector s
    on s.id = rm.provider
    left join resource_catelog rc
    on rc.id = r.catelog_theme
    left join resource_sort rs on r.id = rs.resource_id and rs.sort_category = #{sortCategory}
    <where>
      p.publish_state = #{publishState}
      AND r.open_type !=""
      AND r.open_type is not null
      AND r.open_type != '不开放'

      <if test="chargeMethod != null and chargeMethod.length()>0">
        AND ps.charge_method = #{chargeMethod}
      </if>
      <if test="catelogThemeCode != null and catelogThemeCode.length() > 0">
        AND rc.catelog_code LIKE #{catelogThemeCode}
      </if>
      <if test="sectorCode != null and sectorCode.length() > 0">
        AND s.`sector_code` LIKE #{sectorCode}
      </if>
      <if test="openType != null and openType.length() > 0">
        AND r.open_type = #{openType}
      </if>
      <if test="resourceName != null and resourceName.length() > 0">
        AND r.resource_name like #{resourceName}
      </if>
      <if test="catelogTheme != null">
        AND r.catelog_theme = #{catelogTheme}
      </if>
      <if test="catelogSector != null">
        AND r.catelog_sector = #{catelogSector}
      </if>
      <if test="resourceIdentity != null and resourceIdentity.length() > 0">
        AND r.resource_identity like #{resourceIdentity}
      </if>
      <if test="serviceTag != null and serviceTag.length() > 0">
        AND rm.service_tag like #{serviceTag}
      </if>
      <!--<if test="flag !=null">-->
        <!--<choose>-->
          <!--<when test="flag==0">-->
            <!--ORDER BY r.id DESC-->
          <!--</when>-->
          <!--<when test="flag==1">-->
            <!--ORDER BY p.created_at DESC-->
          <!--</when>-->
        <!--</choose>-->
      <!--</if>-->
      <if test="flag ==null">
        ORDER BY rs.sort_value DESC
      </if>
      <if test="flag==0">
        ORDER BY rs.sort_value DESC
      </if>
      <if test="flag==1">
        ORDER BY p.created_at DESC
      </if>
      <if test="flag ==2">  /*浏览次数还是关注次数*/
        ORDER BY rv.view_times DESC
      </if>
      <!--<if test="flag ==3">  /*浏览次数还是关注次数*/-->
        <!--ORDER BY apply DESC-->
      <!--</if>-->
    </where>

    <if test="page != null" >
      LIMIT #{page.beginIndex},#{page.pageSize}
    </if>
  </select>

  <select id="selectOpenCount" resultType="java.lang.Long" parameterType="com.lightinit.hsdataportal.model.MicroappSearchModel">
    <if test="resourceName != null and resourceName.length() > 0">
      <bind name="resourceName" value="'%' + _parameter.resourceName + '%'" />
    </if>
    <if test="catelogThemeCode != null and catelogThemeCode.length() > 0">
      <bind name="catelogThemeCode" value="_parameter.catelogThemeCode + '%'" />
    </if>
    <if test="sectorCode != null and sectorCode.length() > 0">
      <bind name="sectorCode" value="_parameter.sectorCode + '%'" />
    </if>
    SELECT
    count(*)
    FROM
    `resource` r
    JOIN `resource_microapp` rm
    ON r.`id` = rm.`id`
    JOIN `publish_info` p
    ON r.`id` = p.`resource_id`
    JOIN `publish_setting` ps ON p.`id` =ps.`publish_info_id` and ps.deleted_at is null
    LEFT JOIN `resource_view_times` rv
    ON r.`id` = rv.`id`
    left join sector s
    on s.id = rm.provider
    left join resource_catelog rc
    on rc.id = r.catelog_theme
    left join resource_sort rs on r.id = rs.resource_id and rs.sort_category = #{sortCategory}
    <where>
      p.publish_state = #{publishState}
      AND r.open_type !=""
      AND r.open_type is not null
      AND r.open_type != '不开放'
      <if test="chargeMethod != null and chargeMethod.length()>0">
        AND ps.charge_method = #{chargeMethod}
      </if>
      <if test="catelogThemeCode != null and catelogThemeCode.length() > 0">
        AND rc.catelog_code LIKE #{catelogThemeCode}
      </if>
      <if test="sectorCode != null and sectorCode.length() > 0">
        AND s.`sector_code` LIKE #{sectorCode}
      </if>
      <if test="openType != null and openType.length() > 0">
        AND r.open_type = #{openType}
      </if>
      <if test="resourceName != null and resourceName.length() > 0">
        AND r.resource_name like #{resourceName}
      </if>
      <!--<if test="flag !=null">-->
        <!--<choose>-->
          <!--<when test="flag==0">-->
            <!--ORDER BY r.id DESC-->
          <!--</when>-->
          <!--<when test="flag==1">-->
            <!--ORDER BY p.created_at DESC-->
          <!--</when>-->

        <!--</choose>-->
      <!--</if>-->
      <if test="flag ==null">
        ORDER BY rs.sort_value DESC
      </if>
      <if test="flag==0">
        ORDER BY rs.sort_value DESC
      </if>
      <if test="flag==1">
        ORDER BY p.created_at DESC
      </if>
      <if test="flag ==2">  /*浏览次数还是关注次数*/
        ORDER BY rv.view_times DESC
      </if>
      <!--<if test="flag ==3">  /*浏览次数还是关注次数*/-->
        <!--ORDER BY apply DESC-->
      <!--</if>-->
    </where>
  </select>

  <!--共享列表-->
  <select id="selectShareList" resultType="com.lightinit.hsdataportal.model.MicroappListModel" parameterType="com.lightinit.hsdataportal.model.MicroappSearchModel">
      <if test="resourceName != null and resourceName.length() > 0">
          <bind name="resourceName" value="'%' + _parameter.resourceName + '%'" />
      </if>
      <if test="catelogThemeCode != null and catelogThemeCode.length() > 0">
          <bind name="catelogThemeCode" value="_parameter.catelogThemeCode + '%'" />
      </if>
      <if test="sectorCode != null and sectorCode.length() > 0">
          <bind name="sectorCode" value="_parameter.sectorCode + '%'" />
      </if>
      SELECT
      ps.id settingId,
      r.`resource_name`,
      rv.`view_times`,
      ps.`package_name`,
      ps.`charge_method`,
      r.`share_type`,
      p.resource_img
      FROM
      `resource` r
      JOIN `resource_microapp` rm
      ON r.`id` = rm.`id`
      JOIN `publish_info` p
      ON r.`id` = p.`resource_id`
      JOIN `publish_setting` ps ON p.`id` =ps.`publish_info_id` and ps.deleted_at is null
      LEFT JOIN `resource_view_times` rv
      ON r.`id` = rv.`id`
      left join sector s
      on s.id = rm.provider
      left join resource_catelog rc
      on rc.id = r.catelog_theme
    left join resource_sort rs on r.id = rs.resource_id and rs.sort_category = #{sortCategory}
      <where>
          p.publish_state = #{publishState}
          AND r.share_type !=""
          AND r.share_type is not null
            AND r.share_type != '不予共享'

          <if test="chargeMethod != null and chargeMethod.length()>0">
              AND ps.charge_method = #{chargeMethod}
          </if>
          <if test="catelogThemeCode != null and catelogThemeCode.length() > 0">
              AND rc.catelog_code LIKE #{catelogThemeCode}
          </if>
          <if test="sectorCode != null and sectorCode.length() > 0">
              AND s.`sector_code` LIKE #{sectorCode}
          </if>
          <if test="shareType != null and shareType.length() > 0">
              AND r.share_type = #{shareType}
          </if>
          <if test="resourceName != null and resourceName.length() > 0">
              AND r.resource_name like #{resourceName}
          </if>

        <if test="flag ==null">
          ORDER BY rs.sort_value DESC
        </if>
        <if test="flag==0">
          ORDER BY rs.sort_value DESC
        </if>
        <if test="flag==1">
          ORDER BY p.created_at DESC
        </if>

      </where>
      <if test="page != null" >
          LIMIT #{page.beginIndex},#{page.pageSize}
      </if>
  </select>
  <select id="selectShareCount" resultType="java.lang.Long" parameterType="com.lightinit.hsdataportal.model.MicroappSearchModel">
      <if test="resourceName != null and resourceName.length() > 0">
          <bind name="resourceName" value="'%' + _parameter.resourceName + '%'" />
      </if>
      <if test="catelogThemeCode != null and catelogThemeCode.length() > 0">
          <bind name="catelogThemeCode" value="_parameter.catelogThemeCode + '%'" />
      </if>
      <if test="sectorCode != null and sectorCode.length() > 0">
          <bind name="sectorCode" value="_parameter.sectorCode + '%'" />
      </if>
      SELECT
      count(*)
      FROM
      `resource` r
      JOIN `resource_microapp` rm
      ON r.`id` = rm.`id`
      JOIN `publish_info` p
      ON r.`id` = p.`resource_id`
      JOIN `publish_setting` ps ON p.`id` =ps.`publish_info_id` and ps.deleted_at is null
      LEFT JOIN `resource_view_times` rv
      ON r.`id` = rv.`id`
      left join sector s
      on s.id = rm.provider
      left join resource_catelog rc
      on rc.id = r.catelog_theme
    left join resource_sort rs on r.id = rs.resource_id and rs.sort_category = #{sortCategory}
      <where>
          p.publish_state = #{publishState}
          AND r.share_type !=""
          AND r.share_type is not null
           AND r.share_type != '不予共享'
          <if test="chargeMethod != null and chargeMethod.length()>0">
              AND ps.charge_method = #{chargeMethod}
          </if>
          <if test="catelogThemeCode != null and catelogThemeCode.length() > 0">
              AND rc.catelog_code LIKE #{catelogThemeCode}
          </if>
          <if test="sectorCode != null and sectorCode.length() > 0">
              AND s.`sector_code` LIKE #{sectorCode}
          </if>
          <if test="shareType != null and shareType.length() > 0">
              AND r.share_type = #{shareType}
          </if>
          <if test="resourceName != null and resourceName.length() > 0">
              AND r.resource_name like #{resourceName}
          </if>
        <if test="flag ==null">
          ORDER BY rs.sort_value DESC
        </if>
        <if test="flag==0">
          ORDER BY rs.sort_value DESC
        </if>
        <if test="flag==1">
          ORDER BY p.created_at DESC
        </if>
      </where>
  </select>
  <select id="queryView"  resultType="com.lightinit.hsdataportal.model.MicroappViewModel">
  SELECT

  ps.id,s.sector_name provider,rv.view_times,rm.screenshot,p.created_at,r.`catelog_type`,r.`resource_name`,ps.`package_price`,ps.package_months,r.open_type,rm.url,p.open_condition,r.share_type,ps.charge_method,
  cc.`catelog_name` basicName,c1.`catelog_name` customName,c2.`catelog_name` sectorName,c3.`catelog_name` themeName,p.resource_img,p.open_condition,rm.id microappId,p.id publishInfoId,r.resource_summary,
  ( SELECT COUNT(*) FROM resource_follow f WHERE f.platform_type = #{platformType,jdbcType=VARCHAR} and f.`publish_setting_id` = #{id,jdbcType=BIGINT})coun,f.`publish_setting_id` settingId,ps.id publishSettingId,f.id followId,p.data_tag,rm.demo_url demoUrl
    FROM
    `resource` r
    JOIN `resource_microapp` rm
    ON r.`id` = rm.`id`
    JOIN `publish_info` p
    ON r.`id` = p.`resource_id`
    LEFT JOIN `resource_view_times` rv   ON r.`id` = rv.`id`
    JOIN `publish_setting` ps ON p.`id` =ps.`publish_info_id`
    LEFT JOIN sector s ON rm.provider = s.id
    LEFT JOIN resource_catelog cc ON cc.`id` = r.`catelog_basic`
     LEFT JOIN resource_catelog c1 ON c1.`id` = r.`catelog_custom`
      LEFT JOIN resource_catelog c2 ON c2.`id` = r.`catelog_sector`
       LEFT JOIN resource_catelog c3 ON c3.`id` = r.`catelog_theme`
          LEFT JOIN `resource_follow` f ON  f.`publish_setting_id` = ps.`id`
     WHERE ps.id = #{id,jdbcType=BIGINT}
  </select>
  <select id="demo" parameterType="java.lang.Long" resultType="com.lightinit.hsdataportal.entity.ResourceMicroappWithBLOBs">
    select demo_url,url from resource_microapp
    where id = #{id}
  </select>

  <select id="selectSearchChildList" resultType="com.lightinit.hsdataportal.model.TagsSearchChildListModel">
    <if test="catelogCode != null and catelogCode.length() > 0">
      <bind name="catelogCode" value="_parameter.catelogCode + '%'" />
    </if>
      SELECT r.id,r.resource_name,ps.id settingId
    FROM resource r
    JOIN resource_microapp ra
    ON r.id = ra.id
    JOIN publish_info ri
    ON ri.resource_id = r.id
    join publish_setting ps
    on ps.publish_info_id = ri.id and ps.deleted_at is null
    left join resource_catelog rc
    on rc.id = r.catelog_theme
    where
     ri.publish_state = '已发布'
   	  AND r.open_type !=""
      AND r.open_type is not null
      AND r.open_type != '不予共享'
    <if test="catelogCode != null and catelogCode.length() > 0">
      AND rc.catelog_code LIKE #{catelogCode}
    </if>
  </select>
</mapper>