<?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="io.renren.modules.resource.dao.ResourceDao">

    <resultMap type="io.renren.modules.resource.entity.ResourceEntity" id="resourceMap">
        <result property="id" column="id"/>
        <result property="type" column="type"/>
        <result property="name" column="name"/>
        <result property="description" column="description"/>
        <result property="link" column="link"/>
        <result property="apiMethodType" column="api_method_type"/>
        <result property="apiUrl" column="api_url"/>
        <result property="groupId" column="group_id"/>
        <result property="deptId" column="dept_id"/>
        <result property="deptContacts" column="dept_contacts"/>
        <result property="deptPhone" column="dept_phone"/>
        <result property="shareType" column="share_type"/>
        <result property="shareMode" column="share_mode"/>
        <result property="shareCondition" column="share_condition"/>
        <result property="districtId" column="district_id"/>
        <result property="visits" column="visits"/>
        <result property="delFlag" column="del_flag"/>
        <result property="creator" column="creator"/>
        <result property="createDate" column="create_date"/>
        <result property="updater" column="updater"/>
        <result property="updateDate" column="update_date"/>
        <result property="note1" column="note1"/>
        <result property="note2" column="note2"/>
        <result property="note3" column="note3"/>
        <result property="note4" column="note4"/>
        <result property="note5" column="note5"/>
        <result property="enclosure" column="enclosure"/>
        <result property="undercarriageReason" column="undercarriage_reason"/>
        <result property="undercarriageUserName" column="undercarriage_user_name"/>
        <result property="undercarriageEnclosure" column="undercarriage_enclosure"/>
        <result property="infoList" column="info_list"
                typeHandler="com.baomidou.mybatisplus.extension.handlers.FastjsonTypeHandler"/>
        <result property="applyNumber" column="apply_number"/>
        <result property="pinTop" column="pin_top"/>
        <result property="pinTopTime" column="pin_top_time"/>
        <result property="undercarriageTitle" column="undercarriage_title"/>
        <result property="undercarriageUserPhone" column="undercarriage_phone"/>
        <result property="undercarriageEnclosureName" column="undercarriage_enclosure_name"/>
    </resultMap>

    <resultMap id="resourceDTO" type="io.renren.modules.resource.dto.ResourceDTO">
        <result property="id" column="id"/>
        <result property="type" column="type"/>
        <result property="name" column="name"/>
        <result property="description" column="description"/>
        <result property="link" column="link"/>
        <result property="apiMethodType" column="api_method_type"/>
        <result property="apiUrl" column="api_url"/>
        <result property="groupId" column="group_id"/>
        <result property="deptId" column="dept_id"/>
        <result property="deptContacts" column="dept_contacts"/>
        <result property="deptPhone" column="dept_phone"/>
        <result property="shareType" column="share_type"/>
        <result property="shareMode" column="share_mode"/>
        <result property="shareCondition" column="share_condition"/>
        <result property="districtId" column="district_id"/>
        <result property="visits" column="visits"/>
        <result property="delFlag" column="del_flag"/>
        <result property="creator" column="creator"/>
        <result property="createDate" column="create_date"/>
        <result property="updater" column="updater"/>
        <result property="updateDate" column="update_date"/>
        <result property="note1" column="note1"/>
        <result property="note2" column="note2"/>
        <result property="note3" column="note3"/>
        <result property="note4" column="note4"/>
        <result property="note5" column="note5"/>
        <result property="enclosure" column="enclosure"/>
        <result property="undercarriageReason" column="undercarriage_reason"/>
        <result property="undercarriageUserName" column="undercarriage_user_name"/>
        <result property="undercarriageEnclosure" column="undercarriage_enclosure"/>
        <!--  自定义字段  -->
        <result property="deptName" column="deptName"/>
        <result property="isCollect" column="isCollect"/>
        <result property="collectCount" column="collectCount"/>
        <result property="score" column="score"/>
        <result property="applyState" column="applyState"/>
        <result property="applyCount" column="applyCount"/>
        <result property="infoList" column="info_list"
                typeHandler="com.baomidou.mybatisplus.extension.handlers.FastjsonTypeHandler"/>
        <result property="total" column="total"/>
        <result property="applyNumber" column="apply_number"/>
        <result property="pinTop" column="pin_top"/>
        <result property="pinTopTime" column="pin_top_time"/>
        <result property="undercarriageTitle" column="undercarriage_title"/>
        <result property="undercarriageUserPhone" column="undercarriage_phone"/>
        <result property="undercarriageEnclosureName" column="undercarriage_enclosure_name"/>
    </resultMap>

    <update id="deleteByIds">
        UPDATE tb_data_resource
        SET del_flag = 1,
        update_date = NOW()
        WHERE 1 = 1
        AND id IN
        <foreach collection="ids" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </update>

    <select id="selectWithAttrs" resultMap="resourceDTO">
        SELECT
        DISTINCT tdr.id,
        tdr.create_date,
        tdr.visits,
        tdr.visitor,
        tdr.pin_top,
        tdr.pin_top_time,
        tdr.dept_id,
        sd.type AS "deptType",
        sd.sort AS "deptSort",
        sr.sort AS "regionSort",
        IF( IFNULL(taa2.approve_status, 0) &lt; 1, '未申请', '已申请' ) AS "applyState",
        IFNULL(trs.score, 0 ) AS "score",
        IFNULL(taa.applyCount, 0 ) AS "applyCount",
        IFNULL(trc.collectCount, 0) AS "collectCount",
        IFNULL(sd.name, '暂无部门单位信息') AS "deptName",
        IFNULL(trc2.isCollect, 'false') AS "isCollect",
        (IFNULL(tdr.visits / 100, 0) + IFNULL(trs.score, 0) + IFNULL(taa.applyCount, 0)+ IFNULL(trc.collectCount, 0)) AS
        total,
        IF ( IFNULL( trcar.car, 0 ) &gt; 0, TRUE, FALSE ) AS "isInShoppingCart",
        IF( IFNULL(taa3.approve_status, 0) &lt; 1, '未通过', '通过' ) AS "approveStatus"
        FROM
        tb_data_resource tdr
        LEFT JOIN tb_data_attr tda ON tdr.id = tda.data_resource_id
        LEFT JOIN ( SELECT resource_id, AVG(score) AS "score" FROM tb_resource_score WHERE 1 = 1 AND del_flag = 0 GROUP
        BY resource_id ) trs ON tdr.id = trs.resource_id
        LEFT JOIN ( SELECT resource_id, COUNT( id ) AS "car" FROM tb_resource_car WHERE 1 = 1 AND del_flag = 0 AND
        user_id = #{dto.creator} GROUP BY resource_id ) trcar
        ON tdr.id = trcar.resource_id
        LEFT JOIN ( SELECT resource_id, COUNT(id) AS "applyCount" FROM t_ability_application WHERE 1 = 1 AND del_flag =
        0 GROUP BY resource_id ) taa ON tdr.id = taa.resource_id
        LEFT JOIN ( SELECT resource_id, COUNT(id) AS "collectCount" FROM tb_resource_collection WHERE 1 = 1 AND del_flag
        = 0 GROUP BY resource_id ) trc ON tdr.id = trc.resource_id
        LEFT JOIN ( SELECT resource_id, user_id, ( IF(COUNT( id ) = 1, 'true', 'false') ) AS "isCollect"
        FROM tb_resource_collection WHERE 1 = 1 AND del_flag = 0 AND user_id = #{dto.creator} GROUP BY resource_id
        ) trc2 ON tdr.id = trc2.resource_id
        LEFT JOIN ( SELECT resource_id, COUNT( approve_status ) approve_status FROM t_ability_application WHERE 1 = 1
        AND del_flag = 0 AND user_id = #{dto.creator} GROUP BY resource_id) taa2 ON tdr.id = taa2.resource_id
        LEFT JOIN ( SELECT resource_id, COUNT( approve_status ) approve_status FROM t_ability_application WHERE 1 = 1
        AND del_flag = 0 AND user_id = #{dto.creator} AND approve_status = '通过' GROUP BY resource_id ) taa3 ON tdr.id
        =
        taa3.resource_id
        LEFT JOIN sys_dept sd ON tdr.dept_id = sd.id
        LEFT JOIN sys_region sr ON sd.district = sr.id
        WHERE 1 = 1
        AND tdr.del_flag = 0
        <if test="dto.type != null and dto.type != '赋能案例'">
            AND tdr.type != '赋能案例'
        </if>
        <if test="dto.type != null and dto.type != ''">
            AND MATCH (tdr.type) AGAINST ( #{dto.type} IN BOOLEAN MODE)
        </if>
        <if test="dto.name != null and dto.name != '' and nonChinese == false">
            AND MATCH (tdr.name) AGAINST ( #{dto.name} IN BOOLEAN MODE)
        </if>
        <if test="dto.name != null and dto.name != '' and nonChinese == true">
            AND tdr.name LIKE CONCAT( '%', #{dto.name}, '%' )
        </if>
        <if test="dto.districtId != null and dto.districtId != ''">
            AND tdr.district_id = #{dto.districtId}
        </if>
        <if test="null != dto.deptIds and dto.deptIds.size > 0">
            AND tdr.dept_id IN
            <foreach collection="dto.deptIds" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="dto.shareCondition != null and dto.shareCondition != ''">
            AND tdr.share_condition = #{dto.shareCondition}
        </if>
        <if test="dto.deptId != null and dto.deptId != ''">
            AND tdr.dept_id = #{dto.deptId}
        </if>

        <!-- 按申请量和收藏量查询时排除知识库数据-->
        <if test="orderField != null and orderField != '' and (orderField == 'applyCount' or orderField == 'collectCount')">
            AND tdr.type != '知识库'
        </if>


        <if test="dto.infoList.size > 0">
            AND
            tda.data_resource_id IN (
            SELECT data_resource_id
            FROM (
            SELECT tb.data_resource_id
            FROM (
            <foreach collection="dto.infoList" item="item" separator="union all">
                SELECT data_resource_id FROM tb_data_attr
                WHERE attr_type = #{item.attrType}
                AND MATCH ( attr_value) AGAINST ( #{item.attrValue} IN BOOLEAN MODE)
                AND del_flag = 0
            </foreach>) tb
            GROUP BY tb.data_resource_id
            HAVING COUNT( tb.data_resource_id ) = ${dto.infoList.size}
            ) tmp
            )
        </if>
        <if test="orderField != null and orderField !=''">
            <if test="orderField == 'pin_top'">
                ORDER BY ${orderField} ${orderType}, pin_top_time DESC
            </if>
            <if test="orderField == 'deptSort'">
                ORDER BY sd.type, sr.sort, sd.sort, tdr.dept_id, tdr.visitor DESC, tdr.id DESC
            </if>
            <if test="orderField != 'pin_top' and orderField != 'deptSort'">
                ORDER BY ${orderField} ${orderType}
            </if>
        </if>
    </select>

    <select id="selectTypeCount" resultType="java.util.Map">
        SELECT
        type,
        count(id) AS "count"
        FROM tb_data_resource
        WHERE 1 = 1
        AND del_flag = 0
        <if test="type != null and type != ''">
            AND MATCH (type) AGAINST ( #{type} IN BOOLEAN MODE)
        </if>
        AND type != '赋能案例'
        GROUP BY type
        ORDER BY type
    </select>

    <select id="selectMostPopular" resultType="io.renren.modules.resource.dto.ResourceDTO">
        SELECT
        tdr.id,
        tdr.`type`,
        tdr.`name`,
        tdr.description,
        tdr.link,
        tdr.api_method_type,
        tdr.api_url,
        tdr.group_id,
        tdr.dept_id,
        tdr.dept_contacts,
        tdr.dept_phone,
        tdr.share_type,
        tdr.share_mode,
        tdr.share_condition,
        tdr.district_id,
        tdr.visits,
        tdr.del_flag,
        tdr.creator,
        tdr.create_date,
        tdr.updater,
        tdr.update_date,
        tdr.note1,
        tdr.note2,
        tdr.note3,
        tdr.note4,
        tdr.note5,
        tdr.enclosure,
        tdr.undercarriage_reason,
        tdr.undercarriage_user_name,
        tdr.info_list,
        tdr.total AS total,
        IFNULL(trs.score, 0 ) AS "score",
        IFNULL(taa.applyCount, 0 ) AS "applyCount",
        IFNULL(trc.collectCount, 0) AS "collectCount"
        <!--        (IFNULL(visits / 100, 0) + IFNULL(trs.score, 0) + IFNULL(applyCount,0)+ IFNULL(collectCount,0)) AS total-->
        FROM
        tb_data_resource tdr
        LEFT JOIN ( SELECT resource_id, SUM(score) AS "score" FROM tb_resource_score WHERE 1 = 1 AND del_flag = 0 GROUP
        BY resource_id ) trs ON tdr.id = trs.resource_id
        LEFT JOIN ( SELECT resource_id, COUNT(id) AS "applyCount" FROM t_ability_application WHERE 1 = 1 AND del_flag =
        0 GROUP BY resource_id ) taa ON tdr.id = taa.resource_id
        LEFT JOIN ( SELECT resource_id, COUNT(id) AS "collectCount" FROM tb_resource_collection WHERE 1 = 1 AND del_flag
        = 0 GROUP BY resource_id ) trc ON tdr.id = trc.resource_id
        WHERE
        1 = 1
        AND tdr.del_flag = 0
        AND tdr.type != '知识库'
        AND tdr.type != '赋能案例'
        <if test="type != null and type != ''">
            AND tdr.type = #{type}
        </if>
        <if test="name != null and name != ''">
            AND MATCH (tdr.name) AGAINST ( #{name} IN BOOLEAN MODE)
        </if>
        ORDER BY ${orderFiled} ${orderType}
        LIMIT ${pageNum}, ${pageSize}
    </select>

    <select id="selectDTOById" resultMap="resourceDTO">
        SELECT
        tdr.id,
        tdr.`type`,
        tdr.`name`,
        tdr.description,
        tdr.link,
        tdr.api_method_type,
        tdr.api_url,
        tdr.group_id,
        tdr.dept_id,
        tdr.dept_contacts,
        tdr.dept_phone,
        tdr.share_type,
        tdr.share_mode,
        tdr.share_condition,
        tdr.district_id,
        tdr.visits,
        tdr.del_flag,
        tdr.creator,
        tdr.create_date,
        tdr.updater,
        tdr.update_date,
        tdr.note1,
        tdr.note2,
        tdr.note3,
        tdr.note4,
        tdr.note5,
        tdr.enclosure,
        tdr.undercarriage_reason,
        tdr.undercarriage_user_name,
        tdr.info_list,
        tdr.pin_top,
        tdr.pin_top_time,
        IFNULL( trs.score, 0 ) AS "score",
        IFNULL( taa.applyCount, 0 ) AS "applyCount",
        IFNULL( trc.collectCount, 0 ) AS "collectCount",
        sd.NAME AS "deptName",
        IFNULL( trc2.isCollect, 'false' ) AS "isCollect",
        IF( IFNULL(taa2.approve_status, 0) &lt; 1, '未申请', '已申请' ) AS "applyState",
        IF ( IFNULL( trcar.car, 0 ) &gt; 0, TRUE, FALSE ) AS "isInShoppingCart",
        IF( IFNULL(taa3.approve_status, 0) &lt; 1, '未通过', '通过' ) AS "approveStatus"
        FROM
        tb_data_resource tdr
        LEFT JOIN ( SELECT resource_id, AVG( score ) AS "score" FROM tb_resource_score WHERE 1 = 1 AND del_flag = 0
        GROUP BY resource_id ) trs ON tdr.id = trs.resource_id
        LEFT JOIN ( SELECT resource_id, COUNT( id ) AS "applyCount" FROM t_ability_application WHERE 1 = 1 AND del_flag
        = 0 GROUP BY resource_id ) taa ON tdr.id = taa.resource_id
        LEFT JOIN ( SELECT resource_id, COUNT( id ) AS "collectCount" FROM tb_resource_collection WHERE 1 = 1 AND
        del_flag = 0 GROUP BY resource_id ) trc ON tdr.id = trc.resource_id
        LEFT JOIN ( SELECT resource_id, COUNT( id ) AS "car" FROM tb_resource_car WHERE 1 = 1 AND del_flag = 0 AND
        user_id = #{userId} GROUP BY resource_id ) trcar
        ON tdr.id = trcar.resource_id
        LEFT JOIN (
        SELECT
        resource_id,
        user_id,
        ( IF(COUNT( id ) = 1, 'true', 'false') ) AS "isCollect"
        FROM
        tb_resource_collection
        WHERE
        1 = 1
        AND del_flag = 0
        AND user_id = #{userId}
        GROUP BY
        resource_id
        ) trc2 ON tdr.id = trc2.resource_id
        LEFT JOIN ( SELECT resource_id, COUNT( approve_status ) approve_status FROM t_ability_application WHERE 1 = 1
        AND del_flag = 0 AND user_id = #{userId} GROUP BY resource_id ) taa2 ON tdr.id = taa2.resource_id
        LEFT JOIN ( SELECT resource_id, COUNT( approve_status ) approve_status FROM t_ability_application WHERE 1 = 1
        AND del_flag = 0 AND user_id = #{userId} AND approve_status = '通过' GROUP BY resource_id ) taa3 ON tdr.id =
        taa3.resource_id
        LEFT JOIN sys_dept sd ON tdr.dept_id = sd.id
        WHERE
        1 = 1
        AND tdr.id = #{id}
    </select>

    <select id="selectDTOPage" resultMap="resourceDTO">
        SELECT
        tdr.id,
        tdr.`type`,
        tdr.`name`,
        tdr.description,
        tdr.link,
        tdr.api_method_type,
        tdr.api_url,
        tdr.group_id,
        tdr.dept_id,
        tdr.dept_contacts,
        tdr.dept_phone,
        tdr.share_type,
        tdr.share_mode,
        tdr.share_condition,
        tdr.district_id,
        tdr.visits,
        tdr.del_flag,
        tdr.creator,
        tdr.create_date,
        tdr.updater,
        tdr.update_date,
        tdr.note1,
        tdr.note2,
        tdr.note3,
        tdr.note4,
        tdr.note5,
        tdr.enclosure,
        tdr.undercarriage_reason,
        tdr.undercarriage_user_name,
        tdr.info_list,
        tdr.total AS total,
        tdr.pin_top,
        tdr.pin_top_time,
        tdr.visitor,
        IFNULL(trs.score, 0 ) AS "score",
        IFNULL(taa.applyCount, 0 ) AS "applyCount",
        IFNULL(trc.collectCount, 0) AS "collectCount",
        sd.name AS "deptName",
        sd.type AS "deptType",
        sd.sort AS "deptSort",
        sr.sort AS "regionSort",
        IFNULL(trc2.isCollect, 'false') AS "isCollect",
        IF( IFNULL(taa2.approve_status, 0) &lt; 1, '未申请', '已申请' ) AS "applyState",
        IF ( IFNULL( trcar.car, 0 ) &gt; 0, TRUE, FALSE ) AS "isInShoppingCart",
        IF( IFNULL(taa3.approve_status, 0) &lt; 1, '未通过', '通过' ) AS "approveStatus"
        FROM
        tb_data_resource tdr
        LEFT JOIN ( SELECT resource_id, AVG(score) AS "score" FROM tb_resource_score WHERE 1 = 1 AND del_flag = 0 GROUP
        BY resource_id ) trs ON tdr.id = trs.resource_id
        LEFT JOIN ( SELECT resource_id, COUNT( id ) AS "car" FROM tb_resource_car WHERE 1 = 1 AND del_flag = 0 AND
        user_id = #{dto.creator} GROUP BY resource_id ) trcar
        ON tdr.id = trcar.resource_id
        LEFT JOIN ( SELECT resource_id, COUNT(id) AS "applyCount" FROM t_ability_application WHERE 1 = 1 AND del_flag =
        0 GROUP BY resource_id ) taa ON tdr.id = taa.resource_id
        LEFT JOIN ( SELECT resource_id, COUNT(id) AS "collectCount" FROM tb_resource_collection WHERE 1 = 1 AND del_flag
        = 0 GROUP BY resource_id ) trc ON tdr.id = trc.resource_id
        LEFT JOIN ( SELECT resource_id, user_id, ( IF(COUNT( id ) = 1, 'true', 'false') ) AS "isCollect"
        FROM tb_resource_collection WHERE 1 = 1 AND del_flag = 0 AND user_id = #{dto.creator}
        GROUP BY resource_id) trc2 ON tdr.id = trc2.resource_id
        LEFT JOIN ( SELECT resource_id, COUNT( approve_status ) approve_status FROM t_ability_application WHERE 1 = 1
        AND del_flag = 0 AND user_id = #{dto.creator} GROUP BY resource_id ) taa2 ON tdr.id = taa2.resource_id
        LEFT JOIN ( SELECT resource_id, COUNT( approve_status ) approve_status FROM t_ability_application WHERE 1 = 1
        AND del_flag = 0 AND user_id = #{dto.creator} AND approve_status = '通过' GROUP BY resource_id ) taa3 ON tdr.id
        =
        taa3.resource_id
        LEFT JOIN sys_dept sd ON tdr.dept_id = sd.id
        LEFT JOIN sys_region sr ON sd.district = sr.id
        WHERE 1 = 1
        AND tdr.del_flag = 0

        <if test="dto.type != '赋能案例'">
            AND tdr.type != '赋能案例'
        </if>
        <if test="dto.name != null and dto.name != '' and nonChinese == false">
            AND MATCH (tdr.name) AGAINST ( #{dto.name} IN BOOLEAN MODE)
        </if>
        <if test="dto.name != null and dto.name != '' and nonChinese == true">
            AND tdr.name LIKE CONCAT( '%', #{dto.name}, '%' )
        </if>
        <if test="dto.type != null and dto.type != '' ">
            AND tdr.type = #{dto.type}
        </if>


        <!-- 按申请量和收藏量查询时排除知识库数据-->
        <if test="orderField != null and orderField != '' and (orderField == 'applyCount' or orderField == 'collectCount')">
            AND tdr.type != '知识库'
        </if>


        <if test="dto.districtId != null and dto.districtId != ''">
            AND tdr.district_id = #{dto.districtId}
        </if>
        <if test="null != dto.deptIds and dto.deptIds.size > 0">
            AND tdr.dept_id IN
            <foreach collection="dto.deptIds" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="dto.shareCondition != null and dto.shareCondition != ''">
            AND tdr.share_condition = #{dto.shareCondition}
        </if>
        <if test="dto.deptId != null and dto.deptId != ''">
            AND tdr.dept_id = #{dto.deptId}
        </if>
        <if test="ids != null and ids.size > 0">
            AND tdr.id IN
            <foreach collection="ids" item="item_" open="(" separator="," close=")">
                #{item_}
            </foreach>
        </if>
        <if test="orderField != null and orderField !=''">
            <if test="orderField == 'pin_top'">
                ORDER BY ${orderField} ${orderType}, pin_top_time DESC
            </if>
            <if test="orderField == 'deptSort'">
                ORDER BY sd.type, sr.sort, sd.sort, tdr.dept_id, tdr.visitor DESC, tdr.id DESC
            </if>
            <if test="orderField != 'pin_top' and orderField != 'deptSort'">
                ORDER BY ${orderField} ${orderType}
            </if>
            LIMIT ${pageNum}, ${pageSize}
        </if>
    </select>

    <select id="selectDTOPageSpecilTotal" resultType="java.util.Map">
        SELECT
        tdr.id,
        IFNULL(tdr.total,0) AS total,
        tdr.pin_top,
        UNIX_TIMESTAMP(tdr.pin_top_time) AS pin_top_time
        FROM
        tb_data_resource tdr
        WHERE
        1 = 1
        AND tdr.del_flag = 0
        <if test="dto.name != null and dto.name != ''">
            AND MATCH (tdr.name) AGAINST ( #{dto.name} IN BOOLEAN MODE)
        </if>
        <if test="dto.type != null and dto.type != ''">
            AND tdr.type = #{dto.type}
        </if>
        <if test="dto.districtId != null and dto.districtId != ''">
            AND tdr.district_id = #{dto.districtId}
        </if>
        <if test="null != dto.deptIds and dto.deptIds.size > 0">
            AND tdr.dept_id IN
            <foreach collection="dto.deptIds" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="dto.shareCondition != null and dto.shareCondition != ''">
            AND tdr.share_condition = #{dto.shareCondition}
        </if>
        <if test="dto.deptId != null and dto.deptId != ''">
            AND tdr.dept_id = #{dto.deptId}
        </if>
        ORDER BY create_date DESC
    </select>

    <select id="selectDTOPageCount" resultType="java.lang.Long">
        SELECT
        COUNT(DISTINCT tdr.id)
        FROM
        tb_data_resource tdr
        WHERE
        1 = 1
        AND tdr.del_flag = 0
        <if test="dto.name != null and dto.name != '' and nonChinese == false">
            AND MATCH (tdr.name) AGAINST ( #{dto.name} IN BOOLEAN MODE)
        </if>
        <if test="dto.name != null and dto.name != '' and nonChinese == true">
            AND tdr.name LIKE CONCAT( '%', #{dto.name}, '%' )
        </if>
        <if test="dto.type != null and dto.type != ''">
            AND tdr.type = #{dto.type}
        </if>
        <if test="dto.districtId != null and dto.districtId != ''">
            AND tdr.district_id = #{dto.districtId}
        </if>
        <if test="null != dto.deptIds and dto.deptIds.size > 0">
            AND tdr.dept_id IN
            <foreach collection="dto.deptIds" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="dto.shareCondition != null and dto.shareCondition != ''">
            AND tdr.share_condition = #{dto.shareCondition}
        </if>
        <if test="dto.deptId != null and dto.deptId != ''">
            AND tdr.dept_id = #{dto.deptId}
        </if>
    </select>

    <select id="selectApplyArea" resultType="java.util.Map">
        SELECT
        *
        FROM
        (
        SELECT
        attr_value,
        IFNULL( COUNT( trc.id ), 0 ) AS "colCount",
        IFNULL( COUNT( taa.id ), 0 ) AS "aplCount",
        (
        IFNULL( COUNT( trc.id ), 0 ) + IFNULL( COUNT( taa.id ), 0 )) AS total
        FROM
        tb_data_attr tda
        LEFT JOIN tb_resource_collection trc ON tda.data_resource_id = trc.resource_id
        AND trc.del_flag = 0
        AND trc.user_id = #{userId}
        LEFT JOIN t_ability_application taa ON tda.data_resource_id = taa.resource_id
        AND taa.del_flag = 0
        AND taa.user_id = #{userId}
        WHERE
        1 = 1
        AND tda.attr_type = '应用领域'
        AND tda.del_flag = 0
        AND ( attr_value IS NOT NULL AND attr_value != '')
        GROUP BY
        attr_value
        ORDER BY
        total DESC
        ) temp
        WHERE
        temp.total != 0
    </select>

    <select id="getAmountGroupByType" resultType="java.util.Map">
        SELECT
        COUNT( id ) AS amount,
        type AS type
        FROM
        tb_data_resource
        WHERE
        del_flag = 0
        AND type != '赋能案例'
        GROUP BY
        type
    </select>

    <select id="countAllDept" resultType="java.lang.Long">
        SELECT
        COUNT(id)
        FROM
        sys_dept
    </select>

    <select id="selectGroupByDeptId" resultType="java.util.Map">
        SELECT
        *
        FROM
        (
        SELECT
        ( CASE sd.type WHEN 1 THEN '省级' WHEN 2 THEN '市级' WHEN 3 THEN '区级' WHEN 4 THEN '企业' ELSE '其他' END ) AS
        "type",
        sd.NAME AS "deptName",
        IFNULL( tdr.deptCount, 0 ) AS "deptCount",
        sd.id AS "deptId",
        sr.name AS "districtName",
        sr.id AS "districtId",
        sr.sort AS "regionSort"
        FROM
        sys_dept sd
        LEFT JOIN ( SELECT dept_id, COUNT( id ) AS "deptCount" FROM tb_data_resource WHERE 1 = 1 AND del_flag = 0
        <if test="type != null and type != ''">
            AND type = #{type}
        </if>
        GROUP BY dept_id ) tdr ON sd.id = tdr.dept_id
        LEFT JOIN sys_region sr ON sd.district = sr.id
        ORDER BY sd.type, sr.sort, sd.sort
        ) temp1
        WHERE
        1 = 1
        AND temp1.deptCount != 0

    </select>

    <select id="selectTypeCountByDept" resultType="java.lang.Integer">
        SELECT
        SUM( deptCount )
        FROM
        (
        SELECT
        type,
        deptCount
        FROM
        (
        SELECT
        ( CASE sd.type WHEN 1 THEN '省级' WHEN 2 THEN '市级' WHEN 3 THEN '区级' WHEN 4 THEN '企业' ELSE '其他' END ) AS
        "type",
        IFNULL( tdr.deptCount, 0 ) AS "deptCount"
        FROM
        sys_dept sd
        LEFT JOIN ( SELECT dept_id, COUNT( id ) AS "deptCount" FROM tb_data_resource WHERE 1 = 1 AND del_flag = 0
        <if test="resourceType != null and resourceType != ''">
            AND type = #{resourceType}
        </if>
        GROUP BY dept_id ) tdr ON sd.id = tdr.dept_id
        ) temp1
        WHERE
        1 = 1
        AND temp1.deptCount != 0
        ) temp2
        WHERE 1 = 1
        AND temp2.type = #{deptType}
        GROUP BY
        temp2.type
    </select>

    <select id="selectAlgorithmPage" resultType="io.renren.modules.resource.dto.ResourceDTO">
        SELECT *
        FROM tb_data_resource
        WHERE 1 = 1
        ORDER BY ${orderField} ${orderType}
        LIMIT ${pageNum}, ${pageSize}
    </select>

    <select id="selectTypeCountByDist" resultType="java.lang.Integer">
        SELECT
        SUM( deptCount )
        FROM
        (
        SELECT
        type, deptCount, districtName
        FROM
        (
        SELECT
        ( CASE sd.type WHEN 1 THEN '省级' WHEN 2 THEN '市级' WHEN 3 THEN '区级' WHEN 4 THEN '企业' ELSE '其他' END ) AS
        "type",
        IFNULL( tdr.deptCount, 0) AS "deptCount",
        IFNULL(sr.name, '暂无该地区') AS "districtName"
        FROM
        sys_dept sd
        LEFT JOIN ( SELECT dept_id, COUNT( id ) AS "deptCount" FROM tb_data_resource
        WHERE 1 = 1 AND del_flag = 0
        <if test="resourceType != null and resourceType != ''">
            AND type = #{resourceType}
        </if>
        GROUP BY dept_id ) tdr ON sd.id = tdr.dept_id
        LEFT JOIN sys_region sr ON sd.district = sr.id
        ) temp1
        WHERE
        1 = 1
        AND temp1.deptCount != 0
        ) temp2
        WHERE 1 = 1
        AND temp2.districtName = #{districtName}
        GROUP BY
        temp2.type
    </select>

    <select id="countAllVisits" resultType="java.lang.Long">
        SELECT
        SUM( visits )
        FROM
        tb_data_resource
    </select>

    <select id="selectDeptCount" resultType="java.lang.Integer">
        <!-- SELECT
         COUNT( 1 )
         FROM
         ( SELECT DISTINCT dept_id FROM tb_data_resource WHERE 1 = 1 AND del_flag = 0 AND dept_id IS NOT NULL ) temp -->
        SELECT COUNT(id) FROM sys_dept WHERE name != '访客部门'
    </select>

    <select id="selectDeptTypeCount" resultType="java.util.Map">
        <!-- SELECT IFNULL(COUNT(deptId),0) AS "count",
        type
        FROM (
        SELECT DISTINCT
        tbr.dept_id AS "deptId",
        (CASE sd.type
        WHEN 1 THEN
        '省级'
        WHEN 2 THEN
        '市级'
        WHEN 3 THEN
        '区级'
        WHEN 4 THEN
        '企业' ELSE '其他'
        END ) AS "type"
        FROM
        sys_dept sd
        RIGHT JOIN tb_data_resource tbr ON tbr.dept_id = sd.id
        WHERE
        1 = 1
        AND del_flag = 0
        AND dept_id IS NOT NULL) temp GROUP BY type -->
        SELECT
        IFNULL( COUNT( deptId ), 0 ) AS "count",
        type
        FROM
        (
        SELECT DISTINCT
        sd.id AS "deptId",
        ( CASE sd.type WHEN 1 THEN '省级' WHEN 2 THEN '市级' WHEN 3 THEN '区级' WHEN 4 THEN '企业' ELSE '其他' END ) AS
        "type"
        FROM
        sys_dept sd
        WHERE
        `name` != '访客部门'
        ) temp
        GROUP BY
        type
    </select>

    <select id="selectDeptTotalCount" resultType="java.util.Map">
        SELECT
        COUNT( type ) AS "total",
        type
        FROM
        (
        SELECT
        (
        CASE
        WHEN ( COUNT( id ) BETWEEN 0 AND 5 ) THEN
        '0'
        WHEN ( COUNT( id ) BETWEEN 5 AND 10 ) THEN
        '5'
        WHEN ( COUNT( id ) BETWEEN 10 AND 15 ) THEN
        '10'
        WHEN ( COUNT( id ) BETWEEN 15 AND 20 ) THEN
        '15' ELSE '20'
        END
        ) AS "type"
        FROM
        tb_data_resource
        WHERE
        del_flag = 0
        AND dept_id IS NOT NULL
        GROUP BY
        dept_id
        ) temp
        GROUP BY
        type
    </select>
    <select id="selectDeptProvideCount" resultType="java.util.Map">
        SELECT
        COUNT( DISTINCT temp.value_ ) count,
        any_value ( tdr.dept_id ) dept_id
        FROM
        (
        SELECT
        SUBSTRING_INDEX( SUBSTRING_INDEX( tdav.attr_value, ';', b.help_topic_id + 1 ), ';',- 1 ) AS value_,
        tdav.data_resource_id AS resourceId
        FROM
        tb_data_attr tdav
        JOIN mysql.help_topic b ON b.help_topic_id &lt; ( LENGTH( tdav.attr_value ) - LENGTH( REPLACE ( tdav.attr_value,
        ';', '' ) ) + 1 )
        WHERE
        1 = 1
        AND tdav.attr_type = '应用领域'
        AND tdav.del_flag = 0
        ) temp
        INNER JOIN tb_data_resource tdr ON temp.resourceId = tdr.id
        AND tdr.dept_id IS NOT NULL
        GROUP BY
        tdr.dept_id
        ORDER BY
        count DESC
        LIMIT #{n}
    </select>
    <select id="selectDeptProvide" resultType="java.lang.String">
        SELECT DISTINCT
        tda.attr_value
        FROM
        tb_data_attr tda
        INNER JOIN tb_data_resource tdr ON tda.data_resource_id = tdr.id
        AND tdr.dept_id = #{deptId}
        WHERE
        tda.attr_type = '应用领域'
    </select>

    <select id="selectMaxDeptIds" resultType="java.lang.Long">
        SELECT
        dept_id
        FROM
        (
        SELECT
        COUNT( id ) AS "count",
        dept_id
        FROM
        tb_data_resource
        WHERE
        1 = 1
        AND del_flag = 0
        AND dept_id IS NOT NULL
        GROUP BY
        dept_id
        ) temp1
        WHERE
        temp1.count = (
        SELECT
        MAX( count ) AS "total"
        FROM
        (
        SELECT
        COUNT( id ) AS "count"
        FROM
        tb_data_resource
        WHERE
        1 = 1
        AND del_flag = 0
        AND dept_id IS NOT NULL
        GROUP BY
        dept_id
        ) temp2
        )
    </select>

    <select id="selectByDeptId" resultType="java.util.Map">
        SELECT id,
        name
        FROM tb_data_resource
        WHERE 1 = 1
        AND del_flag = 0
        AND dept_id = #{deptId}
        LIMIT 30
    </select>

    <select id="selectDeptCountList" resultType="java.util.Map">
        SELECT
        sd.id AS "deptId",
        sd.NAME AS "deptName",
        COUNT( tdr.id ) AS "total"
        FROM
        tb_data_resource tdr,
        sys_dept sd
        WHERE
        1 = 1
        AND tdr.del_flag = 0
        AND tdr.dept_id = sd.id
        GROUP BY tdr.dept_id
        ORDER BY total DESC
    </select>

    <select id="selectMaxApplyDeptIds" resultType="java.lang.Long">
        SELECT
        dept_id
        FROM
        (
        SELECT
        COUNT( taa.id ) AS "count",
        su.dept_id
        FROM
        t_ability_application taa,
        sys_user su
        WHERE
        1 = 1
        AND taa.user_id = su.id
        AND taa.user_id IS NOT NULL
        GROUP BY
        su.dept_id
        ) temp1
        WHERE
        temp1.count = (
        SELECT
        MAX( count ) AS "total"
        FROM
        (
        SELECT
        COUNT( taa.id ) AS "count"
        FROM
        t_ability_application taa,
        sys_user su
        WHERE
        1 = 1
        AND taa.user_id = su.id
        AND taa.user_id IS NOT NULL
        GROUP BY
        su.dept_id
        ) temp2
        )
    </select>

    <select id="selectByApplyDeptId" resultType="java.util.Map">
        SELECT tdr.id,
        tdr.name
        FROM t_ability_application taa, tb_data_resource tdr, sys_user su
        WHERE 1 = 1
        AND tdr.del_flag = 0
        AND su.id = taa.user_id
        AND taa.resource_id = tdr.id
        AND su.dept_id = #{deptId}
        LIMIT 30
    </select>

    <select id="selectApplyDeptCountList" resultType="java.util.Map">
        SELECT
        sd.id AS "deptId",
        sd.NAME AS "deptName",
        COUNT( taa.resource_id ) AS "total"
        FROM
        t_ability_application taa,
        sys_user su,
        sys_dept sd
        WHERE
        1 = 1
        AND su.id = taa.user_id
        AND su.dept_id = sd.id
        AND EXISTS ( SELECT 1 FROM tb_data_resource WHERE tb_data_resource.id = taa.resource_id AND
        tb_data_resource.del_flag = 0 )
        GROUP BY
        sd.id
        ORDER BY
        total DESC
    </select>

    <select id="selectDeptTypeCountList" resultType="java.util.Map">
        SELECT
        COUNT( tdr.id ) AS "count",
        sd.NAME AS "deptName",
        (CASE tdr.type
        WHEN '应用资源' THEN 'yyzy'
        WHEN '组件服务' THEN 'zjfw'
        WHEN '基础设施' THEN 'jcss'
        WHEN '知识库' THEN 'zsk'
        WHEN '数据资源' THEN 'sjzy'
        ELSE 'yyzy' END)AS "type"
        FROM
        tb_data_resource tdr,
        sys_dept sd
        WHERE
        1 = 1
        AND tdr.del_flag = 0
        AND tdr.dept_id = sd.id
        GROUP BY
        tdr.dept_id,
        tdr.type
    </select>


    <select id="selectApplyDeptTypeCountList" resultType="java.util.Map">
        SELECT
        COUNT( taa.resource_id ) AS "count",
        sd.NAME AS "deptName",
        (
        CASE
        tdr.type
        WHEN '应用资源' THEN
        'yyzy'
        WHEN '组件服务' THEN
        'zjfw'
        WHEN '基础设施' THEN
        'jcss'
        WHEN '知识库' THEN
        'zsk'
        WHEN '数据资源' THEN
        'sjzy' ELSE 'yyzy'
        END
        ) AS "type"
        FROM
        tb_data_resource tdr,
        sys_dept sd,
        sys_user su,
        t_ability_application taa
        WHERE
        1 = 1
        AND tdr.del_flag = 0
        AND taa.user_id = su.id
        AND su.dept_id = sd.id
        AND taa.resource_id = tdr.id
        GROUP BY
        sd.id,
        tdr.type

        UNION

        SELECT
        COUNT( taa.apply_flag ) AS 'count',

        sd.NAME AS "deptName",
        'jcss' AS 'type'
        FROM
        t_ability_application taa,
        sys_dept sd,
        sys_user su
        WHERE
        1 = 1
        AND taa.user_id = su.id
        AND su.dept_id = sd.id
        AND taa.camera_list != ''
        AND taa.camera_list IS NOT NULL
        GROUP BY
        sd.id
    </select>
    <!--
    <select id="selectMaxType" resultType="java.lang.String">
        SELECT
        type
        FROM
        ( SELECT COUNT( id ) AS "count", type FROM tb_data_resource WHERE 1 = 1 AND type != '赋能案例' AND del_flag = 0 GROUP BY type ) temp1
        WHERE
        temp1.count = (
        SELECT
        MAX( count ) AS "total"
        FROM
        ( SELECT COUNT( id ) AS "count" FROM tb_data_resource WHERE 1 = 1 AND type != '赋能案例' AND del_flag = 0 GROUP BY type ) temp2
        )
    </select>
    -->
    <select id="selectByType" resultType="java.util.Map">
        SELECT id,
        name
        FROM tb_data_resource
        WHERE 1 = 1
        AND del_flag = 0
        AND type = #{type}
        AND type != '赋能案例'
    </select>

    <select id="selectTypeCountList" resultType="java.util.Map">
        SELECT
        tdr.type,
        COUNT( tdr.id ) AS "total"
        FROM
        tb_data_resource tdr
        WHERE
        1 = 1
        AND tdr.del_flag = 0
        GROUP BY
        tdr.type
        ORDER BY
        total DESC
    </select>

    <select id="selectMaxAppArea" resultType="java.lang.String">
        SELECT
        type
        FROM
        (
        SELECT
        SUBSTRING_INDEX( SUBSTRING_INDEX( tdav.attr_value, ';', b.help_topic_id + 1 ), ';',- 1 ) AS "type",
        COUNT( tdav.data_resource_id ) AS "count"
        FROM
        tb_data_attr tdav
        JOIN mysql.help_topic b ON b.help_topic_id &lt; ( LENGTH( tdav.attr_value ) - LENGTH( REPLACE ( tdav.attr_value,
        ';', '' ) ) + 1 )
        WHERE
        1 = 1
        AND tdav.attr_type = '应用领域'
        AND tdav.del_flag = 0
        AND SUBSTRING_INDEX( SUBSTRING_INDEX( tdav.attr_value, ';', b.help_topic_id + 1 ), ';',- 1 ) != ''
        GROUP BY
        type
        ) temp1
        WHERE
        temp1.count = (
        SELECT
        MAX( count ) AS "total"
        FROM
        (
        SELECT
        SUBSTRING_INDEX( SUBSTRING_INDEX( tdav.attr_value, ';', b.help_topic_id + 1 ), ';',- 1 ) AS "type",
        COUNT( tdav.data_resource_id ) AS "count"
        FROM
        tb_data_attr tdav
        JOIN mysql.help_topic b ON b.help_topic_id &lt; ( LENGTH( tdav.attr_value ) - LENGTH( REPLACE ( tdav.attr_value,
        ';', '' ) ) + 1 )
        WHERE
        1 = 1
        AND tdav.attr_type = '应用领域'
        AND tdav.del_flag = 0
        AND SUBSTRING_INDEX( SUBSTRING_INDEX( tdav.attr_value, ';', b.help_topic_id + 1 ), ';',- 1 ) != ''
        GROUP BY
        type
        ) temp2
        )
    </select>

    <select id="selectByAppArea" resultType="java.util.Map">
        SELECT
        tdr.id,
        tdr.name
        FROM
        tb_data_resource tdr,
        tb_data_attr tda
        WHERE
        1 = 1
        AND tdr.del_flag = 0
        AND tda.del_flag = 0
        AND tdr.id = tda.data_resource_id
        AND tda.attr_type = '应用领域'
        AND MATCH (attr_value) AGAINST ( #{type} IN BOOLEAN MODE)
    </select>

    <select id="selectAppAreaCountList" resultType="java.util.Map">
        SELECT
        SUBSTRING_INDEX( SUBSTRING_INDEX( tdav.attr_value, ';', b.help_topic_id + 1 ), ';',- 1 ) AS "type",
        COUNT( tdav.data_resource_id ) AS "total"
        FROM
        tb_data_attr tdav
        JOIN mysql.help_topic b ON b.help_topic_id &lt; ( LENGTH( tdav.attr_value ) - LENGTH( REPLACE ( tdav.attr_value,
        ';', '' ) ) + 1 )
        WHERE
        1 = 1
        AND tdav.attr_type = '应用领域'
        AND tdav.del_flag = 0
        AND SUBSTRING_INDEX( SUBSTRING_INDEX( tdav.attr_value, ';', b.help_topic_id + 1 ), ';',- 1 ) != ''
        GROUP BY
        type
    </select>
    <select id="applicationAreaCapabilitySet" parameterType="java.util.Map" resultType="java.util.Map">
        SELECT
        SUBSTRING_INDEX( SUBSTRING_INDEX( tdav.attr_value, ';', b.help_topic_id + 1 ), ';',- 1 ) AS type ,
        COUNT( tdav.data_resource_id ) AS total
        FROM
        tb_data_attr tdav
        JOIN mysql.help_topic b ON b.help_topic_id &lt; ( LENGTH( tdav.attr_value ) - LENGTH( REPLACE ( tdav.attr_value,
        ';', '' ) ) + 1 )
        WHERE
        1 = 1
        AND tdav.attr_type = '应用领域'
        AND tdav.del_flag = 0
        AND SUBSTRING_INDEX( SUBSTRING_INDEX( tdav.attr_value, ';', b.help_topic_id + 1 ), ';',- 1 ) != ''
        GROUP BY
        type
    </select>
    <select id="applicationAreaCapabilityList" parameterType="java.util.Map" resultType="java.util.Map">
        SELECT
        SUBSTRING_INDEX( SUBSTRING_INDEX( tdav.attr_value, ';', b.help_topic_id + 1 ), ';',- 1 ) AS type ,
        COUNT( tdav.data_resource_id ) AS total
        FROM
        (SELECT a.* FROM tb_data_attr a INNER JOIN tb_data_resource c ON a.data_resource_id = c.id
        WHERE
        MATCH (c.type) AGAINST ( #{resourceType} IN BOOLEAN MODE)
        AND c.dept_id = #{id} AND c.del_flag = 0) tdav
        JOIN mysql.help_topic b ON b.help_topic_id &lt; ( LENGTH( tdav.attr_value ) - LENGTH( REPLACE ( tdav.attr_value,
        ';', '' ) ) + 1 )
        WHERE
        1 = 1
        AND tdav.attr_type = '应用领域'
        AND tdav.del_flag = 0
        AND SUBSTRING_INDEX( SUBSTRING_INDEX( tdav.attr_value, ';', b.help_topic_id + 1 ), ';',- 1 ) != ''
        GROUP BY
        type
    </select>
    <select id="applicationAreaCapabilityUseList" parameterType="java.util.Map" resultType="java.util.Map">
        SELECT
        SUBSTRING_INDEX( SUBSTRING_INDEX( tdav.attr_value, ';', b.help_topic_id + 1 ), ';',- 1 ) AS type ,
        COUNT( tdav.data_resource_id ) AS total
        FROM
        (
        SELECT b.* FROM
        (
        SELECT DISTINCT(c.id) AS id FROM
        (
        SELECT a.id FROM tb_data_resource a WHERE a.del_flag = 0 AND a.type='应用资源' AND a.dept_id = #{id}
        ) a
        INNER JOIN tb_data_resource_rel b ON a.id = b.key_id INNER JOIN tb_data_resource c ON b.reference_id = c.id
        WHERE b.del_flag = 0 AND
        MATCH (c.type) AGAINST ( #{resourceType} IN BOOLEAN MODE)
        AND c.del_flag = 0
        ) a INNER JOIN tb_data_attr b ON a.id = b.data_resource_id AND b.del_flag = 0
        ) tdav
        JOIN mysql.help_topic b ON b.help_topic_id &lt; ( LENGTH( tdav.attr_value ) - LENGTH( REPLACE ( tdav.attr_value,
        ';', '' ) ) + 1 )
        WHERE
        1 = 1
        AND tdav.attr_type = '应用领域'
        AND tdav.del_flag = 0
        AND SUBSTRING_INDEX( SUBSTRING_INDEX( tdav.attr_value, ';', b.help_topic_id + 1 ), ';',- 1 ) != ''
        GROUP BY
        type
    </select>

    <select id="selectTopFiveComponentServiceApplications" resultType="java.util.Map">
        SELECT
        tdr.id AS "resourceId",
        tdr.NAME AS "name",
        IFNULL(COUNT( taa.id ), 0) AS "count"
        FROM
        tb_data_resource tdr
        LEFT JOIN t_ability_application taa ON tdr.id = taa.resource_id AND taa.del_flag = 0
        WHERE
        1 = 1
        AND tdr.del_flag = 0
        AND MATCH (tdr.type) AGAINST ( '组件服务' IN BOOLEAN MODE)
        GROUP BY
        tdr.id
        ORDER BY
        count DESC
        LIMIT 5
    </select>

    <select id="selectTopFiveComponentServiceScore" resultType="java.util.Map">
        SELECT
        tdr.id AS "resourceId",
        tdr.NAME AS "name",
        IFNULL( SUM( trs.score ), 0 ) AS "score"
        FROM
        tb_data_resource tdr
        LEFT JOIN tb_resource_score trs ON tdr.id = trs.resource_id
        AND trs.del_flag = 0
        WHERE
        1 = 1
        AND tdr.del_flag = 0
        AND MATCH (tdr.type) AGAINST ( '组件服务' IN BOOLEAN MODE)
        GROUP BY
        tdr.id
        ORDER BY
        score DESC
        LIMIT 5
    </select>

    <select id="selectResourceNumAsType" parameterType="java.util.Map" resultType="java.util.Map">
        SELECT COUNT(b.id) AS resourceNum,
        a.id AS deptId,
        a.name AS deptName,
        SUM(b.visits) AS visits
        FROM
        sys_dept a
        INNER JOIN
        tb_data_resource b
        ON
        a.id = b.dept_id
        WHERE
        b.del_flag = 0
        AND
        AND MATCH (b.type) AGAINST ( '组件服务' IN BOOLEAN MODE)
        <if test="id != null and id != ''">
            AND (a.pids LIKE concat(concat('%',#{id}),'%') OR a.id = #{id})
        </if>
        GROUP BY
        a.name,a.id
        ORDER BY
        a.name,a.id
        LIMIT ${pageNum}, ${pageSize}
    </select>
    <select id="selectResourceCarNum" parameterType="java.util.Map" resultType="integer">
        SELECT COUNT(a.id) as resourceCarNum
        FROM
        tb_resource_car a
        INNER JOIN
        tb_data_resource b
        ON
        a.resource_id = b.id
        INNER JOIN sys_dept c
        ON b.dept_id = c.id
        WHERE
        a.del_flag = 0 AND b.del_flag = 0
        <if test="id != null and id != ''">
            and (c.id= #{id} OR INSTR(c.pids,#{id}))
        </if>
        <if test="resourceType != null and resourceType != ''">
            AND MATCH (b.type) AGAINST ( #{resourceType} IN BOOLEAN MODE)
        </if>

    </select>
    <select id="selectResourceCollectionNum" parameterType="java.util.Map" resultType="integer">
        SELECT COUNT(a.id) as resourceCollectionNum
        FROM
        tb_resource_collection a
        INNER JOIN
        tb_data_resource b
        ON
        a.resource_id = b.id
        INNER JOIN sys_dept c
        ON b.dept_id = c.id
        WHERE
        a.del_flag = 0 AND b.del_flag = 0
        <if test="id != null and id != ''">
            and (c.id = #{id} OR INSTR(c.pids,#{id}))
        </if>
        <if test="resourceType != null and resourceType != ''">
            AND MATCH (b.type) AGAINST ( #{resourceType} IN BOOLEAN MODE)
        </if>
    </select>
    <select id="selectResourceBrowseNum" parameterType="java.util.Map" resultType="integer">
        SELECT COUNT(a.id) as resourceBrowseNum
        FROM
        tb_resource_browse a
        INNER JOIN
        tb_data_resource b
        ON
        a.resource_id = b.id
        INNER JOIN sys_dept c
        ON b.dept_id = c.id
        WHERE
        a.state = 0 AND b.del_flag = 0
        <if test="id != null and id != ''">
            and (c.id = #{id} OR INSTR(c.pids,#{id}))
        </if>
        <if test="resourceType != null and resourceType != ''">
            AND MATCH (b.type) AGAINST ( #{resourceType} IN BOOLEAN MODE)
        </if>
    </select>
    <select id="selectResourceRelNum" parameterType="java.util.Map" resultType="integer">
        SELECT COUNT(a.key_id) as resourceBrowseNum
        FROM
        tb_data_resource_rel a
        INNER JOIN
        tb_data_resource b
        ON
        a.reference_id = b.id
        INNER JOIN sys_dept c
        ON b.dept_id = c.id
        WHERE
        a.del_flag = 0 AND b.del_flag = 0
        <if test="id != null and id != ''">
            and (c.id = #{id} OR INSTR(c.pids,#{id}))
        </if>
        <if test="resourceType != null and resourceType != ''">
            AND MATCH (b.type) AGAINST ( #{resourceType} IN BOOLEAN MODE)
        </if>
    </select>
    <select id="selectResourceShare" parameterType="java.util.Map" resultType="integer">
        SELECT COUNT(a.id) AS nums
        FROM
        tb_data_resource a
        INNER JOIN sys_dept b
        ON a.dept_id = b.id
        WHERE
        a.del_flag = 0
        <if test="id != null and id != ''">
            and (b.id = #{id} OR INSTR(b.pids,#{id}))
        </if>
        <if test="resourceType != null and resourceType != ''">
            AND MATCH (a.type) AGAINST ( #{resourceType} IN BOOLEAN MODE)
        </if>
        <if test="shareCondition != null and shareCondition != ''">
            AND
            a.share_condition = #{shareCondition}
        </if>

    </select>

    <select id="selectResurceCarDetails" parameterType="java.util.Map" resultType="java.util.Map">
        SELECT COUNT(a.id) AS resourceCarNum,
        c.name as deptName,c.id,COUNT(e.id) as useNum
        FROM
        tb_resource_car a
        INNER JOIN
        sys_user b
        ON
        a.user_id = b.id
        INNER JOIN
        sys_dept c
        ON
        b.dept_id = c.id
        INNER JOIN
        tb_data_resource d
        ON
        a.resource_id = d.id
        INNER JOIN tb_data_resource_rel e
        ON
        b.id = e.creator
        where
        (c.id = #{id} or c.pids LIKE CONCAT('%',CONCAT(#{id},'%')))
        AND MATCH (d.type) AGAINST ( #{resourceType} IN BOOLEAN MODE)
        AND
        a.del_flag = 0
        AND
        d.del_flag = 0
        GROUP BY
        c.name,c.id
        ORDER BY
        c.name,c.id
        LIMIT ${pageNum}, ${pageSize}
    </select>

    <!-- 根据组件类型查询部门申请组件的数量-->
    <select id="assemblyCarByDept" parameterType="java.util.Map" resultType="java.util.Map">
        SELECT COUNT(a.id) as carNum FROM tb_resource_car a INNER JOIN sys_user b ON a.user_id = b.id INNER JOIN
        sys_dept c
        ON b.dept_id = c.id INNER JOIN tb_data_resource d ON a.resource_id = d.id
        WHERE a.del_flag = 0 AND (c.id = #{id} OR INSTR(c.pids,#{id})) AND MATCH (d.type) AGAINST ( #{resourceType} IN
        BOOLEAN MODE) AND d.del_flag = 0
    </select>

    <!--根据组件类型查询部门使用组件的数量-->
    <select id="assemblyUseByDept" parameterType="java.util.Map" resultType="java.util.Map">
        SELECT COUNT(DISTINCT(b.reference_id)) AS useNum FROM
        (
        SELECT a.id FROM tb_data_resource a INNER JOIN sys_dept b ON a.dept_id = b.id
        WHERE a.del_flag = 0 AND a.type='应用资源' AND (b.id = #{id} OR INSTR(b.pids,#{id}))
        ) a
        INNER JOIN tb_data_resource_rel b ON a.id = b.key_id WHERE b.del_flag = 0
    </select>

    <select id="selectCountByDepts" resultType="java.util.Map">
        SELECT
        type,
        count(id) AS "count"
        FROM tb_data_resource
        WHERE 1=1
        AND del_flag = 0
        <if test="null != list">
            AND dept_id in
            <foreach item="deptId" collection="list" open="(" separator="," close=")">
                #{deptId}
            </foreach>
        </if>
        GROUP BY type
        ORDER BY type
    </select>

    <select id="selectCountByIds" resultType="java.util.Map">
        SELECT
        type,
        count(id) AS "count"
        FROM tb_data_resource
        WHERE 1=1
        AND del_flag = 0
        AND id in
        <foreach item="resourceId" collection="list" open="(" separator="," close=")">
            #{resourceId}
        </foreach>
        GROUP BY type
        ORDER BY type
    </select>

    <select id="selectDeptResourceByApplyNum" resultType="java.util.Map">
        SELECT tdr.name, count(taa.id) as count
        FROM tb_data_resource tdr left join t_ability_application taa on tdr.id=taa.resource_id
        WHERE
        1 = 1
        AND tdr.del_flag = 0
        <if test="null != list">
            and tdr.dept_id in
            <foreach item="deptId" collection="list" open="(" separator="," close=")">
                #{deptId}
            </foreach>
        </if>
        GROUP BY tdr.id ORDER BY count desc
    </select>

    <select id="selectUsersApplyAndCount" resultMap="resourceDTO">
        SELECT tdr.*, count(taa.id) as applyCount
        FROM tb_data_resource tdr left join t_ability_application taa on tdr.id=taa.resource_id
        WHERE
        1 = 1
        AND tdr.del_flag = 0
        <if test="null != userIds and userIds.size > 0">
            and taa.user_id in
            <foreach item="userId" collection="userIds" open="(" separator="," close=")">
                #{userId}
            </foreach>
        </if>
        GROUP BY tdr.id ORDER BY applyCount desc
    </select>

    <select id="trafficDeptResource" resultType="java.util.Map">
        SELECT
        SUBSTR(create_date, 1, 10) AS "date",
        COUNT( id ) AS "count"
        FROM
        tb_data_resource tdr
        WHERE
        1 = 1
        AND del_flag = 0
        <if test="null != deptIds">
            AND tdr.dept_id IN
            <foreach item="deptId" collection="deptIds" open="(" separator="," close=")">
                #{deptId}
            </foreach>
        </if>
        AND SUBSTR(create_date, 1, 10) BETWEEN #{startDate} AND #{endDate}
        GROUP BY
        SUBSTR(create_date, 1, 10)
    </select>

    <select id="selectDayAvg" resultType="java.lang.Long">
        SELECT IFNULL(COUNT(id), 0) / ${days}
        FROM tb_data_resource
        WHERE 1 = 1
        AND del_flag=0
    </select>

    <select id="selectDayMax" resultType="java.lang.Integer">
        SELECT IFNULL(MAX(count), 0)
        FROM
        (SELECT
        COUNT( id ) AS "count"
        FROM
        tb_data_resource
        WHERE
        1 = 1
        AND del_flag = 0
        GROUP BY
        SUBSTR(create_date, 1, 10)) temp
    </select>

    <select id="selectCameraDTOById" resultMap="resourceDTO">
        SELECT tdr.*, tda.* FROM tb_data_resource tdr, tb_data_attr tda WHERE tdr.id = tda.data_resource_id AND
        tdr.del_flag = 0 AND tdr.id = #{id}
    </select>

    <select id="selectDevelopDocResource" resultType="java.util.Map">
        SELECT DISTINCT
        tdr.id,
        tdr.NAME AS title,
        tdr.type,
        tda.attr_value AS doc
        FROM
        tb_data_resource tdr
        RIGHT JOIN tb_data_attr tda ON tdr.id = tda.data_resource_id
        WHERE
        tdr.del_flag != 1
        AND tda.del_flag = 0
        AND tda.attr_type = '技术文档'
    </select>

    <select id="resourceInstallationOrDataResourceDetails" parameterType="java.util.Map" resultType="java.util.Map">
        SELECT COUNT(a.id) AS resourceNum,a.dept_id AS deptId,b.name AS deptName
        FROM tb_data_resource a INNER JOIN sys_dept b ON a.dept_id = b.id
        WHERE
        MATCH (a.type) AGAINST ( #{resourceType} IN BOOLEAN MODE)
        AND a.del_flag = 0
        <if test="id != '0'.toString()">
            AND (b.ID = #{id} OR INSTR(b.pids,#{id}))
        </if>
        GROUP BY a.dept_id,b.name
        ORDER BY a.dept_id,b.name
    </select>
    <select id="selectAppList" resultType="java.util.Map">
        SELECT
        tdr.id,
        tdr.`name`,
        tdr.dept_id,
        sd.name AS "deptName",
        sd.sort AS "deptSort",
        sd.type AS "deptType",
        sr.sort AS "regionSort",
        IFNULL( tdr.visitor, 0 ) AS visitor
        FROM
        tb_data_resource tdr
        LEFT JOIN sys_dept sd ON tdr.dept_id = sd.id
        LEFT JOIN sys_region sr ON sd.district = sr.id
        WHERE
        1 = 1
        AND MATCH (tdr.type) AGAINST ( '应用资源' IN BOOLEAN MODE)
        AND tdr.del_flag = 0
        <if test=" type != null and type != ''">
            AND sd.type = #{type}
        </if>
        <if test=" area != null and area != ''">
            AND JSON_EXTRACT( info_list, JSON_UNQUOTE( REPLACE ( JSON_SEARCH( info_list, 'all', '应用领域', NULL,
            '$[*].attrType' ), 'attrType', 'attrValue' ) ) ) LIKE CONCAT( '%', #{area}, '%' )
        </if>
        ORDER BY
        sd.type, sr.sort, sd.sort, tdr.dept_id, tdr.visitor DESC, tdr.id DESC
        LIMIT ${pageNum}, 9

    </select>
    <select id="selectPicByResId" resultType="java.lang.String">
        SELECT
        tda.attr_value
        FROM
        tb_data_attr tda
        LEFT JOIN tb_data_resource tdr ON tda.data_resource_id = tdr.id
        WHERE 1 = 1
        <if test="resourceType == '应用资源'">
            AND tda.attr_type = '应用图片'
        </if>
        <if test="resourceType == '图层服务'">
            AND tda.attr_type = '图层缩略图'
        </if>
        <if test="resourceType == '智能算法'">
            AND tda.attr_type = '应用场景'
        </if>
        AND tdr.id = #{id}
        AND tda.del_flag = 0
    </select>
    <select id="selectTypeCountByName" resultType="java.util.Map">
        SELECT
        type,
        count(id) AS "count"
        FROM tb_data_resource
        WHERE 1 = 1
        AND del_flag = 0
        <if test="keyWorld != null and keyWorld != '' and nonChinese == false">
            AND MATCH (name) AGAINST ( #{keyWorld} IN BOOLEAN MODE)
        </if>
        <if test="keyWorld != null and keyWorld != '' and nonChinese == true">
            AND name LIKE CONCAT( '%', #{keyWorld}, '%' )
        </if>
        AND type != '赋能案例'
        GROUP BY type
        ORDER BY type
    </select>
    <select id="countByCIMType" resultType="java.util.Map">
        SELECT
        '平台' AS type,
        COUNT( id ) AS "count"
        FROM
        tb_data_resource tdr
        WHERE
        del_flag = 0
        AND dept_id = #{deptId}
        AND
        MATCH (type) AGAINST ( '应用资源' IN BOOLEAN MODE)
        UNION
        SELECT
        ( CASE tda.attr_value WHEN '开发组件' THEN '组件' WHEN '图层服务' THEN '图层' ELSE '其他' END ) AS "type",
        COUNT( tdr.id ) AS "count"
        FROM
        tb_data_resource tdr,
        tb_data_attr tda
        WHERE
        tdr.del_flag = 0
        AND tda.del_flag = 0
        AND tdr.id = tda.data_resource_id
        AND tdr.dept_id = #{deptId}
        AND MATCH (tdr.type) AGAINST ( '组件服务' IN BOOLEAN MODE)
        AND tda.attr_type = '组件类型'
        AND tda.attr_value IN ( '开发组件', '图层服务' )
        GROUP BY
        tda.attr_value
    </select>
    <select id="getCIMApplyApplicationList" resultType="java.util.Map">
        SELECT
        tdr.id,
        tdr.name,
        tdr.description,
        tda.attr_value AS "pic",
        tdr.create_date
        FROM
        tb_data_resource tdr,
        tb_data_attr tda
        WHERE
        tdr.id = tda.data_resource_id
        AND tdr.del_flag = 0
        AND tda.del_flag = 0
        AND MATCH (tdr.type) AGAINST ( '应用资源' IN BOOLEAN MODE)
        AND tda.attr_type = '应用图片'
        AND tdr.dept_id = #{deptId}
        ORDER BY
        tdr.create_date
    </select>

    <select id="getImgServices" resultType="java.util.Map">
        SELECT A.*,IFNULL(c.attr_value,'') as link
        FROM
        (
        SELECT m.* FROM
        (
        SELECT a.name,a.id,a.info_list FROM tb_data_resource a INNER JOIN tb_data_attr b ON a.id = b.data_resource_id
        WHERE a.dept_id = #{deptid} AND a.del_flag = 0 and MATCH (a.type) AGAINST ( '组件服务' IN BOOLEAN MODE)
        AND (b.attr_type = '组件类型' AND MATCH (b.attr_value) AGAINST ( '图层服务' IN BOOLEAN MODE)) AND b.del_flag = 0
        ) m
        INNER JOIN tb_data_attr b ON m.id = b.data_resource_id
        AND b.attr_type = '分类' AND LOCATE(#{type},b.attr_value) AND b.del_flag = 0
        ) A

        LEFT JOIN tb_data_attr c
        ON A.id = c.data_resource_id
        AND c.attr_type = '图层缩略图'
        AND c.del_flag = 0
    </select>

    <select id="cimAssemblyResources" resultType="java.util.Map">
        SELECT a.name,a.id
        FROM tb_data_resource a INNER JOIN tb_data_attr b ON a.id = b.data_resource_id
        WHERE
        a.dept_id = #{deptid}
        AND MATCH (a.type) AGAINST ( '组件服务' IN BOOLEAN MODE)
        AND a.del_flag = 0
        AND (b.attr_type = '组件类型' AND MATCH (b.attr_value) AGAINST ( '开发组件' IN BOOLEAN MODE))
        AND b.del_flag = 0
    </select>

    <select id="selectApplyDeptDetailTypeCountList" resultType="java.util.Map">
        SELECT
        COUNT( taa.resource_id ) AS "count",
        sd.NAME AS "deptName",
        sd.id AS "dept_id",
        sd.type AS "deptType",
        sd.district AS "district",
        (
        CASE
        tdr.type
        WHEN '应用资源' THEN
        'yyzy'
        WHEN '智能算法' THEN
        'znsf'
        WHEN '图层服务' THEN
        'tcfw'
        WHEN '开发组件' THEN
        'kfzj'
        WHEN '业务组件' THEN
        'ywzj'
        WHEN '基础设施' THEN
        'jcss'
        WHEN '知识库' THEN
        'zsk'
        WHEN '数据资源' THEN
        'sjzy' ELSE 'yyzy'
        END
        ) AS "type"
        FROM
        (SELECT IF(d.type='组件服务', A.attr_value, d.type) AS type, d.id, d.dept_id, d.del_flag FROM tb_data_resource d
        LEFT JOIN
        tb_data_attr a ON d.id=a.data_resource_id AND a.attr_type='组件类型' AND a.del_flag=0) tdr,
        sys_dept sd,
        sys_user su,
        t_ability_application taa,
        sys_dept dept
        WHERE
        1 = 1
        AND tdr.del_flag = 0
        AND taa.user_id = su.id
        AND su.dept_id = sd.id
        AND taa.resource_id = tdr.id
        AND dept.id = tdr.dept_id
        AND (tdr.type = '应用资源' OR tdr.type = '智能算法' OR tdr.type = '图层服务' OR tdr.type = '开发组件' OR
        tdr.type = '业务组件')
        <if test="approveStatus != null and approveStatus != ''">
            AND taa.approve_status = #{approveStatus}
        </if>
        <if test="deptId != null and deptId != ''">
            AND sd.id = #{deptId}
        </if>
        <if test="type != null and type != ''">
            AND tdr.type = #{type}
        </if>
        <if test="region != null and region != ''">
            AND sd.district = #{region}
        </if>
        <if test="startDate != null and startDate != '' and endDate != null and endDate != ''">
            AND SUBSTR(taa.create_date, 1, 10) BETWEEN #{startDate} AND #{endDate}
        </if>
        GROUP BY
        sd.id,
        tdr.type
        <if test="type == null or type == '' or type == '会议室'">
            UNION

            SELECT
            COUNT( id ) AS 'count',
            dept AS 'deptName',
            NULL AS "dept_id",
            99 AS "deptType",
            NULL AS "district",
            'hys' AS 'type'
            FROM
            t_meetingroom_book tmb
            where 1=1
            <choose>
                <when test="approveStatus != null and approveStatus == '通过'">
                    AND tmb.state=2
                </when>
                <when test="approveStatus != null and approveStatus == '不通过'">
                    AND tmb.state=3
                </when>
                <when test="approveStatus != null and approveStatus == '审核中'">
                    AND tmb.state not in (2, 3)
                </when>
            </choose>
            <if test="deptId != null and deptId != ''">
                AND tmb.dept = #{deptId}
            </if>
            <if test="startDate != null and startDate != '' and endDate != null and endDate != ''">
                AND SUBSTR(tmb.create_date, 1, 10) BETWEEN #{startDate} AND #{endDate}
            </if>
            GROUP BY
            deptName
        </if>
    </select>

    <select id="selectDeptDetailTypeCountList" resultType="java.util.Map">
        SELECT
        COUNT( tdr.id ) AS "count",
        sd.NAME AS "deptName",
        sd.id AS "dept_id",
        sd.type AS "deptType",
        sd.district AS "district",
        (CASE tdr.type
        WHEN '应用资源' THEN 'yyzy'
        WHEN '智能算法' THEN 'znsf'
        WHEN '图层服务' THEN 'tcfw'
        WHEN '开发组件' THEN 'kfzj'
        WHEN '业务组件' THEN 'ywzj'
        WHEN '基础设施' THEN 'jcss'
        WHEN '知识库' THEN 'zsk'
        WHEN '数据资源' THEN 'sjzy'
        ELSE 'yyzy' END)AS "type"
        FROM
        (SELECT IF(d.type='组件服务', A.attr_value, d.type) AS type, d.id, d.del_flag, d.dept_id, d.create_date FROM
        tb_data_resource d LEFT JOIN tb_data_attr a ON d.id=a.data_resource_id AND a.attr_type='组件类型' AND
        a.del_flag=0) tdr,
        sys_dept sd
        WHERE
        1 = 1
        AND tdr.dept_id = sd.id
        AND (tdr.type = '应用资源' OR tdr.type = '智能算法' OR tdr.type = '图层服务' OR tdr.type = '开发组件' OR
        tdr.type = '业务组件')
        <choose>
            <when test="approveStatus != null and approveStatus == '通过'">
                AND tdr.del_flag=0
            </when>
            <when test="approveStatus != null and approveStatus == '审核中'">
                AND tdr.del_flag in (2, 3, 4)
            </when>
            <when test="approveStatus != null and approveStatus == '不通过'">
                AND tdr.del_flag=6
            </when>
            <otherwise>
                AND tdr.del_flag in (0, 2, 3, 4, 6)
            </otherwise>
        </choose>
        <if test="deptId != null and deptId != ''">
            AND sd.id = #{deptId}
        </if>
        <if test="region != null and region != ''">
            AND sd.district = #{region}
        </if>
        <if test="type != null and type != ''">
            AND tdr.type = #{type}
        </if>
        <if test="startDate != null and startDate != '' and endDate != null and endDate != ''">
            AND SUBSTR(tdr.create_date, 1, 10) BETWEEN #{startDate} AND #{endDate}
        </if>
        GROUP BY
        tdr.dept_id,
        tdr.type
    </select>

    <select id="selectCensusResourceTable" resultType="java.util.Map">
        SELECT sd.name AS deptName, tdr.name AS resourceName, tdr.type, DATE_FORMAT(tdr.create_date,'%Y-%m-%d %T') AS
        createDate,
        CASE WHEN (tdr.del_flag=0 OR tdr.del_flag=5) THEN '通过' WHEN tdr.del_flag=6 THEN '不通过' ELSE '审核中' END AS
        approveStatus, trma.instance_id AS applyNumber
        FROM (SELECT IF(d.type='组件服务', a.attr_value, d.type) AS type, d.id, d.del_flag, d.dept_id, d.create_date,
        d.name
        FROM tb_data_resource d LEFT JOIN tb_data_attr a ON d.id=a.data_resource_id AND a.attr_type='组件类型' AND
        a.del_flag=0
        WHERE d.type NOT IN ('知识库','基础设施','数据资源')) tdr
        LEFT JOIN t_resource_mount_apply trma on tdr.id=trma.resource_id,
        sys_dept sd
        WHERE 1=1 AND tdr.dept_id=sd.id
        <choose>
            <when test="approveStatus != null and approveStatus == '通过'">
                AND tdr.del_flag=0
            </when>
            <when test="approveStatus != null and approveStatus == '审核中'">
                AND tdr.del_flag in (2, 3, 4)
            </when>
            <when test="approveStatus != null and approveStatus == '不通过'">
                AND tdr.del_flag=6
            </when>
            <otherwise>
                AND tdr.del_flag in (0, 2, 3, 4, 6)
            </otherwise>
        </choose>
        <if test="deptId != null and deptId != ''">
            AND sd.id = #{deptId}
        </if>
        <if test="type != null and type != ''">
            AND tdr.type = #{type}
        </if>
        <if test="region != null and region != ''">
            AND sd.district = #{region}
        </if>
        <if test="startDate != null and startDate != '' and endDate != null and endDate != ''">
            AND SUBSTR(tdr.create_date, 1, 10) BETWEEN #{startDate} AND #{endDate}
        </if>
        ORDER BY tdr.create_date DESC
    </select>

    <select id="selectCensusApplyTable" resultType="java.util.Map">
        SELECT
        sd.NAME AS deptName,
        tdr.NAME AS resourceName,
        dept.`name` AS resourceDeptName,
        tdr.type,
        DATE_FORMAT(taa.create_date,'%Y-%m-%d %T') AS createDate,
        taa.approve_status AS approveStatus,
        taa.apply_flag AS applyNumber
        FROM
        (
        SELECT
        IF
        ( d.type = '组件服务', A.attr_value, d.type ) AS type,
        d.id,
        d.del_flag,
        d.dept_id,
        d.create_date,
        d.NAME
        FROM
        tb_data_resource d
        LEFT JOIN tb_data_attr a ON d.id = a.data_resource_id
        AND a.attr_type = '组件类型'
        AND a.del_flag = 0
        WHERE
        d.type NOT IN ( '知识库', '基础设施', '数据资源' )
        ) tdr,
        sys_dept sd,
        sys_user su,
        t_ability_application taa,
        sys_dept dept
        WHERE
        1 = 1
        AND tdr.del_flag = 0
        AND taa.user_id = su.id
        AND su.dept_id = sd.id
        AND taa.resource_id = tdr.id
        AND dept.id = tdr.dept_id
        <if test="approveStatus != null and approveStatus != ''">
            AND taa.approve_status = #{approveStatus}
        </if>
        <if test="deptId != null and deptId != ''">
            AND sd.id = #{deptId}
        </if>
        <if test="type != null and type != ''">
            AND tdr.type = #{type}
        </if>
        <if test="region != null and region != ''">
            AND sd.district = #{region}
        </if>
        <if test="startDate != null and startDate != '' and endDate != null and endDate != ''">
            AND SUBSTR(taa.create_date, 1, 10) BETWEEN #{startDate} AND #{endDate}
        </if>

        <if test="type == null or type == '' or type == '会议室'">
            UNION ALL

            SELECT
            tmb.dept AS 'deptName',
            tm.`name` AS 'resourceName',
            '青岛市大数据发展管理局' AS 'resourceDeptName',
            '会议室' AS 'type',
            tmb.create_date AS 'createDate',
            CASE tmb.state WHEN 1 THEN '审核中' WHEN 2 THEN '通过' WHEN 3 THEN '不通过' ELSE '审核中' END AS
            'approveStatus',
            '' AS 'applyNumber'
            FROM
            t_meetingroom_book tmb,
            t_meetingroom tm
            WHERE
            tmb.room_id = tm.id
            <choose>
                <when test="approveStatus != null and approveStatus == '通过'">
                    AND tmb.state=2
                </when>
                <when test="approveStatus != null and approveStatus == '不通过'">
                    AND tmb.state=3
                </when>
                <when test="approveStatus != null and approveStatus == '审核中'">
                    AND tmb.state not in (2, 3)
                </when>
            </choose>
            <if test="deptId != null and deptId != ''">
                AND tmb.dept = #{deptId}
            </if>
            <if test="startDate != null and startDate != '' and endDate != null and endDate != ''">
                AND SUBSTR(tmb.create_date, 1, 10) BETWEEN #{startDate} AND #{endDate}
            </if>
        </if>

        ORDER BY createDate DESC
    </select>

    <select id="selectCollectList" resultType="java.util.Map">
        SELECT tda.attr_value as type, count(*) as count FROM tb_data_resource tdr
        LEFT JOIN tb_data_attr tda ON tdr.id=tda.data_resource_id AND tda.attr_type=#{attrType} AND tda.del_flag=0
        WHERE 1=1 AND tdr.del_flag=0 AND tdr.type=#{resourceType}
        GROUP BY tda.attr_value
        ORDER BY count DESC
    </select>

    <select id="selectCollectResourceList" resultType="java.util.Map">
        SELECT tda.attr_value as type, count(*) as count FROM tb_data_resource tdr LEFT JOIN
        (SELECT a.data_resource_id, a.id, a.attr_type, substring_index(substring_index(a.attr_value, ';',
        b.help_topic_id + 1), ';', - 1) AS attr_value
        FROM tb_data_attr a INNER JOIN mysql.help_topic b ON a.attr_type='应用领域' AND a.del_flag=0 AND b.help_topic_id
        <![CDATA[ < ]]> (length(a.attr_value) - length(REPLACE(a.attr_value, ';', '')) + 1)) tda
        ON tdr.id=tda.data_resource_id
        WHERE 1=1 AND tdr.del_flag=0 AND tdr.type='应用资源'
        GROUP BY tda.attr_value
        ORDER BY count DESC
    </select>

    <select id="getPraiseList" resultType="java.util.Map">
        SELECT
        tdr.id AS "resourceId",
        tdr.NAME AS "resourceName",
        IFNULL( AVG( trs.score ), 3 ) AS "score",
        tdr.create_date AS "createDate"
        FROM
        tb_data_resource tdr LEFT JOIN
        tb_resource_score trs ON tdr.id = trs.resource_id
        WHERE
        1 = 1
        AND tdr.del_flag = 0
        AND tdr.type IN('组件服务', '应用资源')
        GROUP BY
        resourceId
        ORDER BY
        score DESC, createDate
        LIMIT 10
    </select>

    <select id="getPopularList" resultType="java.util.Map">
        SELECT
        tdr.id AS "resourceId",
        tdr.NAME AS "resourceName",
        IFNULL(taa.applyCount, 0) AS "applyCount",
        IFNULL(trc.collectCount, 0) AS "collectCount",
        IFNULL(trb.browseCount, 0) AS "browseCount",
        tdr.create_date
        FROM
        tb_data_resource tdr
        LEFT JOIN ( SELECT resource_id, COUNT( id ) AS "applyCount" FROM t_ability_application WHERE 1 = 1 AND del_flag
        = 0 AND create_date > #{startDate} GROUP BY resource_id ) taa ON tdr.id = taa.resource_id
        LEFT JOIN ( SELECT resource_id, COUNT( id ) AS "collectCount" FROM tb_resource_collection WHERE 1 = 1 AND
        del_flag = 0 AND create_date > #{startDate} GROUP BY resource_id ) trc ON tdr.id = trc.resource_id
        LEFT JOIN (SELECT resource_id, COUNT(id) AS "browseCount" FROM tb_resource_browse WHERE 1 = 1
        AND create_date > #{startDate} GROUP BY resource_id) trb ON tdr.id = trb.resource_id
        WHERE
        1 = 1
        AND tdr.del_flag = 0
        AND tdr.type IN('组件服务', '应用资源')
        ORDER BY
        applyCount DESC,
        collectCount DESC,
        browseCount DESC,
        tdr.create_date
        LIMIT 10
    </select>


    <select id="getPotentialList" resultType="java.util.Map">
        SELECT
        tdr.id AS "resourceId",
        tdr.`name` AS "resourceName",
        IF(temp2.oneCount IS NULL, '1' , '0') AS "isNew",
        IFNULL(temp1.halfCount, 0) AS "halfMonthCount",
        IFNULL(temp2.oneCount, 0) AS "lastMonthCount",
        (IFNULL(temp1.halfCount, 0) - IFNULL(temp2.oneCount, 0)) / IFNULL(temp2.oneCount, 0) AS "growthRate",
        tdr.create_date
        FROM
        tb_data_resource tdr
        LEFT JOIN (
        SELECT
        COUNT( id ) AS "halfCount",
        resource_id
        FROM
        t_ability_application taa
        WHERE
        1 = 1
        AND del_flag = 0
        AND camera_list IS NULL
        AND create_date >= #{halfMonthDay}
        GROUP BY
        resource_id
        ) temp1 ON tdr.id = temp1.resource_id
        LEFT JOIN (
        SELECT
        COUNT( id ) AS "oneCount",
        resource_id
        FROM
        t_ability_application taa
        WHERE
        1 = 1
        AND del_flag = 0
        AND camera_list IS NULL
        AND #{halfMonthDay} > create_date AND create_date >= #{monthDay}
        GROUP BY
        resource_id
        ) temp2 ON tdr.id = temp2.resource_id
        WHERE
        1 = 1
        AND tdr.del_flag = 0
        AND tdr.type IN ('组件服务', '应用资源')
        ORDER BY isNew DESC, halfMonthCount DESC, create_date DESC, growthRate DESC
        LIMIT 10
    </select>

    <select id="getStarList" resultType="java.util.Map">
        SELECT list.resourceId,
        list.resourceName,
        (list.applyNum + list.browseNum + list.collectNum + list.scoreNum) AS "fireNum",
        list.create_date AS "createDate"
        FROM (

        SELECT
        tdr.id AS "resourceId",
        tdr.name AS "resourceName",
        IFNULL((taa.count / temp1.maxApplyCount), 0) * 1.5 AS "applyNum",
        IFNULL((trc.count / temp2.maxCopllectCount), 0) * 1 AS "collectNum",
        IFNULL((trb.count / temp3.maxBrowseCount), 0) * 0.5 AS "browseNum",
        IFNULL((trs.score / temp4.maxScore), 0 ) * 2 AS "scoreNum",
        tdr.create_date
        FROM
        tb_data_resource tdr

        LEFT JOIN (SELECT COUNT(id) AS "count", resource_id FROM t_ability_application WHERE 1 = 1 AND del_flag = 0 AND
        camera_list IS NULL GROUP BY resource_id ) taa ON tdr.id = taa.resource_id

        LEFT JOIN (SELECT COUNT(id) AS "count", resource_id FROM tb_resource_collection WHERE 1 = 1 AND del_flag = 0
        GROUP BY resource_id) trc
        ON tdr.id = trc.resource_id

        LEFT JOIN (SELECT COUNT(id) AS "count", resource_id FROM tb_resource_browse WHERE 1 = 1 GROUP BY resource_id)
        trb
        ON tdr.id = trb.resource_id

        LEFT JOIN (SELECT AVG(score) AS "score", resource_id FROM tb_resource_score WHERE 1 = 1 GROUP BY resource_id)
        trs
        ON tdr.id = trs.resource_id

        JOIN (SELECT IFNULL(MAX(count), 0) AS "maxApplyCount" FROM (SELECT COUNT(id) AS "count" FROM
        t_ability_application WHERE 1 = 1 AND del_flag = 0 AND camera_list IS NULL GROUP BY resource_id ) taa) temp1

        JOIN (SELECT IFNULL(MAX(count), 0) AS "maxCopllectCount" FROM(SELECT COUNT(id) AS "count" FROM
        tb_resource_collection WHERE 1 = 1 AND del_flag = 0 GROUP BY resource_id)trc) temp2

        JOIN (SELECT IFNULL(MAX(count), 0) AS "maxBrowseCount" FROM(SELECT COUNT(id) AS "count" FROM tb_resource_browse
        WHERE 1 = 1 GROUP BY resource_id) trb) temp3

        JOIN (SELECT IFNULL(MAX(score), 0) AS "maxScore" FROM(SELECT AVG(score) AS "score" FROM tb_resource_score WHERE
        1 = 1 GROUP BY resource_id) trs) temp4

        WHERE 1 = 1
        AND tdr.del_flag = 0
        AND tdr.type IN ('组件服务', '应用资源')

        )list

        ORDER BY fireNum DESC, create_date DESC LIMIT 10
    </select>

    <select id="selectAppListCount" resultType="java.lang.Integer">
        SELECT IFNULL(COUNT(1), 0)
        FROM
        tb_data_resource tdr
        LEFT JOIN sys_dept sd ON tdr.dept_id = sd.id
        LEFT JOIN sys_region sr ON sd.district = sr.id
        WHERE
        1 = 1
        AND MATCH (tdr.type) AGAINST ( '应用资源' IN BOOLEAN MODE)
        AND tdr.del_flag = 0
        <if test=" type != null and type != ''">
            AND sd.type = #{type}
        </if>
        <if test=" area != null and area != ''">
            AND JSON_EXTRACT( info_list, JSON_UNQUOTE( REPLACE ( JSON_SEARCH( info_list, 'all', '应用领域', NULL,
            '$[*].attrType' ), 'attrType', 'attrValue' ) ) ) LIKE CONCAT( '%', #{area}, '%' )
        </if>
    </select>

    <select id="getSquareList" resultType="java.util.Map">
        SELECT
        tdr.id,
        tdr.`name`,
        tdr.dept_id,
        sd.NAME AS "deptName",
        sd.sort AS "deptSort",
        sd.type AS "deptType",
        sr.sort AS "regionSort",
        IFNULL( tdr.visitor, 0 ) AS visitor
        FROM
        tb_data_resource tdr
        LEFT JOIN sys_dept sd ON tdr.dept_id = sd.id
        LEFT JOIN sys_region sr ON sd.district = sr.id
        WHERE
        1 = 1
        AND MATCH ( tdr.type ) AGAINST ( '组件服务' IN BOOLEAN MODE )
        AND tdr.del_flag = 0
        AND tdr.id IN ( SELECT data_resource_id FROM tb_data_attr WHERE attr_type = '组件类型' AND attr_value = #{type}
        AND del_flag = 0 )
        <if test=" deptType != null and deptType != ''">
            AND sd.type = #{deptType}
        </if>
        <if test=" area != null and area != ''">
            AND JSON_EXTRACT( info_list, JSON_UNQUOTE( REPLACE ( JSON_SEARCH( info_list, 'all', '应用领域', NULL,
            '$[*].attrType' ), 'attrType', 'attrValue' ) ) ) LIKE CONCAT( '%', #{area}, '%')
        </if>
        ORDER BY
        sd.type,
        sr.sort,
        sd.sort,
        tdr.dept_id,
        tdr.visitor DESC,
        tdr.id DESC
        LIMIT ${pageNum}, 9
    </select>

    <select id="getSquareListCount" resultType="java.lang.Integer">
        SELECT IFNULL(COUNT(1), 0) FROM tb_data_resource tdr
        LEFT JOIN sys_dept sd ON tdr.dept_id = sd.id
        LEFT JOIN sys_region sr ON sd.district = sr.id
        WHERE
        1 = 1
        AND MATCH ( tdr.type ) AGAINST ( '组件服务' IN BOOLEAN MODE )
        AND tdr.del_flag = 0
        AND tdr.id IN ( SELECT data_resource_id FROM tb_data_attr WHERE attr_type = '组件类型' AND attr_value = #{type}
        AND del_flag = 0 )
        <if test=" deptType != null and deptType != ''">
            AND sd.type = #{deptType}
        </if>
        <if test=" area != null and area != ''">
            AND JSON_EXTRACT( info_list, JSON_UNQUOTE( REPLACE ( JSON_SEARCH( info_list, 'all', '应用领域', NULL,
            '$[*].attrType' ), 'attrType', 'attrValue' ) ) ) LIKE CONCAT( '%', #{area}, '%')
        </if>
    </select>

    <select id="getAppListByDept" resultType="java.util.Map">
        SELECT
            tdr.id,
            tdr.NAME,
            tdr.create_date
        FROM
            tb_data_resource tdr
        WHERE
            1 = 1
            AND tdr.del_flag = 0
            AND tdr.dept_id = #{deptId}
            <if test="key != null and key != ''">
                AND tdr.name like CONCAT('%', #{key}, '%')
            </if>
        ORDER BY create_date DESC
    </select>

</mapper>