<?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="org.jeecg.modules.adweb.seo.mapper.SeoKeywordsMapper">
    <select id="getKeywordsLastSevenDays" resultType="java.lang.String">
        SELECT t1.se_date
        FROM adweb_seo_keywords_serp t1,
        adweb_seo_keywords t2
        WHERE t2.`status` = 1
        AND t1.keywords_id = t2.id
        AND t2.app_key = 'adweb'
        AND t2.site_code = #{siteCode}
        AND t2.keyword_type = #{keywordType}
        GROUP BY t1.se_date
        ORDER BY t1.se_date DESC
        LIMIT 7
    </select>
    <select id="queryKeywordNumByRankRange" resultType="org.jeecg.modules.adweb.seo.vo.RankInfoVO">
        SELECT
        t.type keywordType,
        count(t.id) keywordNum
        FROM
        (
        SELECT
        t1.id,
        t1.keyword_type type
        FROM
        adweb_seo_keywords t1
        WHERE
        t1.site_code = #{siteCode}
        AND `status` = 1
        AND keyword_type = #{keywordType}
        <if test="rankStart != null">
            AND t1.last_rank >= #{rankStart}
        </if>
        <if test="rankEnd != null">
            AND #{rankEnd} >= t1.last_rank
        </if>
        AND t1.app_key = 'adweb'
        AND
        (
        id IN
        (
        SELECT
        t3.keywords_id
        FROM
        (
        SELECT
        keywords_id,MAX(t1.rank_absolute) max_rank
        FROM
        adweb_seo_keywords_serp t1,
        adweb_seo_keywords t2
        WHERE
        t2.`status` = 1
        AND t1.keywords_id = t2.id
        AND t2.app_key = 'adweb'
        <if test="siteCode != null and siteCode != ''">
            AND t2.site_code = #{siteCode}
        </if>
        <if test="keywordType != null">
            AND t2.keyword_type = #{keywordType}
        </if>
        AND se_date IN ${dateStr}
        GROUP BY keywords_id
        ) t3
        WHERE t3.max_rank <![CDATA[ <>]]> 0
        )
        )
        ) t
        GROUP BY
        t.type
    </select>
    <select id="getKeywordStandard" resultType="java.lang.Integer">
        SELECT
        IF(COUNT(*) = 0 OR t1.target IS NULL, 10, t1.target)
        FROM
        adweb_subscribe_plan t1,
        adweb_sysuser_plan_subscription t2,
        adweb_site t3
        WHERE
        t2.`status` = 1
        AND t1.id = t2.plan_id
        AND t2.site_id = t3.id
        AND t3.`code` = #{siteCode}
    </select>
    <select id="getKeywordList" resultType="org.jeecg.modules.adweb.seo.entity.SeoKeywords">
        SELECT
        DISTINCT t1.*,CHAR_LENGTH( keywords ) keywordsLength
        FROM
        adweb_seo_keywords t1
        <where>

            <if test="keyword != null and keyword != ''">
                AND t1.keywords LIKE CONCAT('%', #{keyword}, '%')
            </if>
            <if test="siteCode != null and siteCode != ''">
                AND t1.site_code = #{siteCode}
            </if>
            <if test="keywordType != null">
                AND t1.keyword_type = #{keywordType}
            </if>
            <if test="rankStart != null">
                AND t1.last_rank >= #{rankStart}
            </if>
            <if test="rankEnd != null">
                AND #{rankEnd} >= t1.last_rank
            </if>
            AND t1.app_key = 'adweb'
            AND t1.`status` = 1
            AND
            (
            id IN
            (
            SELECT
            t3.keywords_id
            FROM
            (
            SELECT
            keywords_id,MAX(t1.rank_absolute) max_rank
            FROM
            adweb_seo_keywords_serp t1,
            adweb_seo_keywords t2
            WHERE
            t2.`status` = 1
            AND t1.keywords_id = t2.id
            AND t2.app_key = 'adweb'
            <if test="siteCode != null and siteCode != ''">
                AND t2.site_code = #{siteCode}
            </if>
            <if test="keywordType != null">
                AND t2.keyword_type = #{keywordType}
            </if>
            AND se_date IN ${datesStr}
            GROUP BY keywords_id
            ) t3
            WHERE t3.max_rank <![CDATA[ <>]]> 0
            )
            <if test="!isCustomer">
                OR t1.`status` = 1
            </if>
            )
        </where>
        ORDER BY
        last_rank + IF(t1.status = 0, 10000, 0) + IF(t1.last_rank = 0, 1000, 0)+ IF(t1.keyword_type = 2, 100, 0)
        <if test="buttonColumn != null and buttonColumn == 'ranking'">
            ${buttonSort}
        </if>
        <if test="buttonColumn != null and buttonColumn == 'words'">
            ,CHAR_LENGTH(keywords) ${buttonSort}
        </if>
        ,create_time
    </select>

    <select id="getKeywordsToSerp" resultType="org.jeecg.modules.adweb.seo.entity.SeoKeywords">
        SELECT
        *
        FROM
        adweb_seo_keywords
        WHERE
        status = 1
        AND
        (search_status != 1 or search_status IS NULL)
        AND site_code IN
        <foreach collection="sideCodes" item="sideCode" open="(" separator="," close=")">
            #{sideCode}
        </foreach>
        <if test="keywordType != null">
            AND keyword_type = #{keywordType}
        </if>
        AND (last_search_time <![CDATA[ <]]> CURDATE() OR last_search_time IS NULL)
        LIMIT #{limit}
    </select>

    <select id="getPageList" resultType="org.jeecg.modules.adweb.seo.entity.SeoKeywords">
        (
        SELECT
        *,CHAR_LENGTH( t1.keywords ) keywordsLength
        FROM
        adweb_seo_keywords t1
        WHERE
        t1.`status` = 1
        AND t1.app_key = 'adweb'
        AND t1.last_rank <![CDATA[ <>]]> 0
        <if test="siteCode != null and siteCode != ''">
            AND t1.site_code = #{siteCode}
        </if>
        <if test="keywordType != null and keywordType != ''">
            AND t1.keyword_type = #{keywordType}
        </if>
        <if test="keywords != null and keywords != ''">
            AND t1.keywords LIKE CONCAT('%', #{keywords}, '%')
        </if>
        <if test="createTime != null and createTime != ''">
            AND t1.create_time LIKE CONCAT(#{createTime}, '%')
        </if>
        <if test="lastSearchTime != null and lastSearchTime != ''">
            AND t1.last_search_time LIKE CONCAT(#{lastSearchTime}, '%')
        </if>
        <if test="keywordRank != null and keywordRank != ''">
            AND t1.last_rank >= 1
            AND #{keywordRank} * 10 >= t1.last_rank
        </if>
        ORDER BY
        <if test="column != null and column != '' and column != 'keywords'">
            ${column} ${order}
        </if>
        <if test="column != null and column != '' and column == 'keywords'">
            CHAR_LENGTH(t1.keywords) ${order}
        </if>
        <if test="keywordRank != null and keywordRank != ''">
            ,last_rank ASC
        </if>
        <if test="column == null">
            ,priority ASC,optimize_process DESC,create_time DESC
        </if>
        LIMIT 99999999999
        )
        UNION ALL
        (
        SELECT
        *,CHAR_LENGTH( t1.keywords ) keywordsLength
        FROM
        adweb_seo_keywords t1
        WHERE
        t1.`status` = 1
        AND t1.app_key = 'adweb'
        AND t1.last_rank = 0
        <if test="siteCode != null and siteCode != ''">
            AND t1.site_code = #{siteCode}
        </if>
        <if test="keywordType != null and keywordType != ''">
            AND t1.keyword_type = #{keywordType}
        </if>
        <if test="keywords != null and keywords != ''">
            AND t1.keywords LIKE CONCAT('%', #{keywords}, '%')
        </if>
        <if test="createTime != null and createTime != ''">
            AND t1.create_time LIKE CONCAT(#{createTime}, '%')
        </if>
        <if test="lastSearchTime != null and lastSearchTime != ''">
            AND t1.last_search_time LIKE CONCAT(#{lastSearchTime}, '%')
        </if>
        <if test="keywordRank != null and keywordRank != ''">
            AND t1.last_rank >= 1
            AND #{keywordRank} * 10 >= t1.last_rank
        </if>
        ORDER BY
        <if test="column != null and column != '' and column != 'keywords'">
            ${column} ${order}
        </if>
        <if test="column != null and column != '' and column == 'keywords'">
            CHAR_LENGTH(t1.keywords) ${order}
        </if>
        <if test="keywordRank != null and keywordRank != ''">
            ,last_rank ASC
        </if>
        <if test="column == null">
            ,priority ASC,optimize_process DESC,create_time DESC
        </if>
        LIMIT 99999999999
        )
    </select>

    <select id="getPageListByCreateTime" resultType="org.jeecg.modules.adweb.seo.entity.SeoKeywords">
        SELECT
        *
        FROM
        adweb_seo_keywords t1
        WHERE
        t1.`status` = 1
        AND t1.app_key = 'adweb'
        <if test="siteCode != null and siteCode != ''">
            AND t1.site_code = #{siteCode}
        </if>
        <if test="keywordType != null and keywordType != ''">
            AND t1.keyword_type = #{keywordType}
        </if>
        <if test="keywords != null and keywords != ''">
            AND t1.keywords LIKE CONCAT('%', #{keywords}, '%')
        </if>
        <if test="createTime != null and createTime != ''">
            AND t1.create_time LIKE CONCAT(#{createTime}, '%')
        </if>
        <if test="lastSearchTime != null and lastSearchTime != ''">
            AND t1.last_search_time LIKE CONCAT(#{lastSearchTime}, '%')
        </if>
        <if test="keywordRank != null and keywordRank != ''">
            AND t1.last_rank >= 1
            AND #{keywordRank} * 10 >= t1.last_rank
        </if>
        ORDER BY
        <if test="column != null and column != ''">
            ${column} ${order}
        </if>
        <if test="keywordRank != null and keywordRank != ''">
            ,last_rank ASC
        </if>
    </select>

    <select id="getSubscriptionIdBySiteCode"
            resultType="org.jeecg.modules.adweb.subscription.entity.UserPlanSubscription">
        SELECT
        t1.id,
        t1.plan_id
        FROM
        adweb_sysuser_plan_subscription t1,
        adweb_site t2
        WHERE
        t1.site_id = t2.id
        AND
        t1.`status` = 1
        AND
        t2.`code` = #{siteCode}
    </select>

    <select id="getKeywordsRankStats"
            resultType="org.jeecg.modules.adweb.seo.dto.ComprehensiveStatistics$KeywordRankStats">
        SELECT keyword_type, COUNT(*) AS keyword_num,
        SUM(CASE WHEN last_rank BETWEEN 1 AND #{targetRank} THEN 1 ELSE 0 END) AS achieved_keyword_num
        FROM adweb_seo_keywords
        WHERE site_code = #{siteCode} AND status = 1
        GROUP BY keyword_type
    </select>

</mapper>