<?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.ai.da.mapper.primary.AffiliateMapper">

    <select id="getMonthlyApprovedAffiliate" resultType="java.util.Map">
        SELECT
            count(*) AS count
        FROM
            t_affiliate
        WHERE
            YEAR ( create_time ) = #{year}
          AND MONTH ( create_time ) = #{month}
          AND status = 'Active'
    </select>

    <select id="getAffiliateList" resultType="com.ai.da.model.vo.AffiliateVO">
        SELECT
        f.*,
        c.user_name
        FROM
        t_affiliate f
        LEFT JOIN
        t_account c
        ON
        f.account_id = c.id
        <where>
            1 = 1
            <if test="status != null and status != ''">
                AND f.status = #{status}
            </if>
            <if test="startTime != null and startTime != ''">
                AND f.create_time &gt; #{startTime}
            </if>
            <if test="endTime != null and endTime != ''">
                AND f.create_time &lt; #{endTime}
            </if>
            <if test="affiliateId != null">
                AND f.id = #{affiliateId}
            </if>
            AND f.is_deleted = 0
        </where>
        <choose>
            <when test="sortField != null and sortField != ''">
                ORDER BY
                <choose>
                    <when test="sortField == 'id'">f.id</when>
                    <when test="sortField == 'totalEarnings'">f.total_earnings</when>
                    <when test="sortField == 'createTime'">f.create_time</when>
                    <otherwise>f.create_time</otherwise>
                </choose>
                <choose>
                    <when test="order != null and order.toUpperCase() == 'DESC'">DESC</when>
                    <otherwise>ASC</otherwise>
                </choose>
            </when>
            <otherwise>
                ORDER BY f.create_time ASC
            </otherwise>
        </choose>
        LIMIT ${size} OFFSET ${offset}
    </select>

    <select id="queryAffiliateTotalCount" resultType="int">
        SELECT
        count(f.id)
        FROM
        t_affiliate f
        LEFT JOIN
        t_account c
        ON
        f.account_id = c.id
        <where>
            1 = 1
            <if test="status != null and status != ''">
                AND f.status = #{status}
            </if>
            <if test="startTime != null and startTime != ''">
                AND f.create_time &gt; #{startTime}
            </if>
            <if test="endTime != null and endTime != ''">
                AND f.create_time &lt; #{endTime}
            </if>
            <if test="affiliateId != null">
                AND f.id = #{affiliateId}
            </if>
        </where>
        AND f.is_deleted = 0
    </select>

    <select id="selectAllAffiliateUsername" resultType="java.util.Map">
        SELECT
        af.id,
        ac.user_name as username
        FROM t_affiliate af
        LEFT JOIN t_account ac ON af.account_id = ac.id
    </select>

</mapper>
