<?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.springblade.modules.distribution.mapper.CommissionMapper">


    <select id="statistics" resultType="org.springblade.modules.distribution.vo.CommisStatisVO">

        SELECT
            a.payoutsAccounts,
            b.noPayoutsAccounts,
            a.payoutsAccounts + b.noPayoutsAccounts AS allAccounts
        FROM
	        (SELECT SUM( amount ) AS payoutsAccounts FROM t_commission WHERE benefit_user_id = #{userId} AND is_payouts = 1 AND is_delete = 0 ) a
	    LEFT JOIN
	        (SELECT SUM( amount ) AS noPayoutsAccounts FROM t_commission WHERE benefit_user_id = #{userId} AND is_payouts = 0 AND is_delete = 0 ) b ON 1 =1

    </select>
    <select id="selectNotPayouts" resultType="org.springblade.modules.distribution.vo.CommissionVO">
        SELECT
            o.order_no,
            o.third_order_no,
            u.open_id,
            c.*
        FROM
            t_commission c
            JOIN t_order o ON o.id = c.order_id
            JOIN blade_user_wechat u ON u.user_id = c.benefit_user_id
            JOIN t_order_groupon og ON c.order_id = og.order_id
            JOIN t_groupon g ON og.groupon_id = g.id
        WHERE
            c.is_payouts = 0
            AND o.order_state = 4
            AND o.is_aftersales = 0
            AND c.is_use = 1
            AND TIMESTAMPDIFF(DAY, g.take_start_time, NOW()) >= #{days}
    </select>


</mapper>
