<?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.gxwebsoft.app.mapper.AppResourceMapper">

    <!-- 关联查询 SQL -->
    <sql id="selectSql">
        SELECT a.*, w.website_name
        FROM app_resource a
        LEFT JOIN cms_website w ON a.website_id = w.website_id AND w.deleted = 0
        <where>
            a.deleted = 0
            <if test="param.resourceId != null">
                AND a.resource_id = #{param.resourceId}
            </if>
            <if test="param.resourceType != null and param.resourceType != ''">
                AND a.resource_type = #{param.resourceType}
            </if>
            <if test="param.websiteId != null">
                AND a.website_id = #{param.websiteId}
            </if>
            <if test="param.provider != null and param.provider != ''">
                AND a.provider = #{param.provider}
            </if>
            <if test="param.status != null and param.status != ''">
                AND a.status = #{param.status}
            </if>
            <if test="param.userId != null">
                AND a.user_id = #{param.userId}
            </if>
            <if test="param.tenantId != null">
                AND a.tenant_id = #{param.tenantId}
            </if>
            <if test="param.keywords != null and param.keywords != ''">
                AND (
                    a.name        LIKE CONCAT('%', #{param.keywords}, '%')
                 OR a.ip          LIKE CONCAT('%', #{param.keywords}, '%')
                 OR a.domain      LIKE CONCAT('%', #{param.keywords}, '%')
                 OR a.host        LIKE CONCAT('%', #{param.keywords}, '%')
                )
            </if>
            <if test="param.createTimeStart != null">
                AND a.create_time &gt;= #{param.createTimeStart}
            </if>
            <if test="param.createTimeEnd != null">
                AND a.create_time &lt;= #{param.createTimeEnd}
            </if>
        </where>
        ORDER BY a.create_time DESC
    </sql>

    <!-- 分页查询 -->
    <select id="selectPageRel" resultType="com.gxwebsoft.app.entity.AppResource">
        <include refid="selectSql"/>
    </select>

    <!-- 查询全部 -->
    <select id="selectListRel" resultType="com.gxwebsoft.app.entity.AppResource">
        <include refid="selectSql"/>
    </select>

    <!-- 按类型统计数量 -->
    <select id="countByType" resultType="java.util.Map">
        SELECT resource_type AS resourceType, COUNT(*) AS cnt
        FROM app_resource
        WHERE deleted = 0
        <if test="userId != null">
            AND user_id = #{userId}
        </if>
        <if test="tenantId != null">
            AND tenant_id = #{tenantId}
        </if>
        GROUP BY resource_type
    </select>

</mapper>
