<?xml version="1.0" encoding="UTF-8"?>
<!--
  ~ Licensed to the Apache Software Foundation (ASF) under one or more
  ~ contributor license agreements.  See the NOTICE file distributed with
  ~ this work for additional information regarding copyright ownership.
  ~ The ASF licenses this file to You under the Apache License, Version 2.0
  ~ (the "License"); you may not use this file except in compliance with
  ~ the License.  You may obtain a copy of the License at
  ~
  ~     http://www.apache.org/licenses/LICENSE-2.0
  ~
  ~ Unless required by applicable law or agreed to in writing, software
  ~ distributed under the License is distributed on an "AS IS" BASIS,
  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  ~ See the License for the specific language governing permissions and
  ~ limitations under the License.
  -->

<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.nageoffer.shortlink.project.dao.mapper.ShortLinkMapper">

    <!-- 分页查询短链接 -->
    <select id="pageLink" parameterType="com.nageoffer.shortlink.project.dto.req.ShortLinkPageReqDTO"
            resultType="com.nageoffer.shortlink.project.dao.entity.ShortLinkDO">
        SELECT t.*,
        COALESCE(s.today_pv, 0) AS todayPv,
        COALESCE(s.today_uv, 0) AS todayUv,
        COALESCE(s.today_uip, 0) AS todayUip
        FROM t_link t
        LEFT JOIN t_link_stats_today s ON t.gid = s.gid
        AND t.full_short_url = s.full_short_url
        AND s.date = CURDATE()
        WHERE t.gid = #{gid}
        AND t.enable_status = 0
        AND t.del_flag = 0
        <choose>
            <when test="orderTag == 'todayPv'">
                ORDER BY todayPv DESC
            </when>
            <when test="orderTag == 'todayUv'">
                ORDER BY todayUv DESC
            </when>
            <when test="orderTag == 'todayUip'">
                ORDER BY todayUip DESC
            </when>
            <when test="orderTag == 'totalPv'">
                ORDER BY t.total_uv DESC
            </when>
            <when test="orderTag == 'totalUv'">
                ORDER BY t.total_pv DESC
            </when>
            <when test="orderTag == 'totalUip'">
                ORDER BY t.total_uip DESC
            </when>
            <otherwise>
                ORDER BY t.create_time DESC
            </otherwise>
        </choose>
    </select>
</mapper>