<?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.gree.mall.miniapp.commonmapper.MyProfitMapper">


    <select id="queryProfitSum" resultType="com.gree.mall.miniapp.bean.user.MyProfitBean">
          select
            sum(waitingAmount) as 'waitingAmount',
            sum(paidAmount) as 'paidAmount'
        from (
                SELECT
                        sum( IF ( STATUS = 'ING', amount, 0.00 ) ) AS waitingAmount,
                        sum( IF ( STATUS = 'OVER', amount, 0.00 ) ) AS paidAmount
                from order_share
                where (work_user_id = #{userId})
                    and status not in ('CANCEL','EXCEPTION')
                    and work_user_id !=''
            union all
                select
                    sum( IF ( settle_status = 'ING', worker_amount, 0.00 ) ) AS waitingAmount,
                    sum( IF ( settle_status = 'OVER', worker_amount, 0.00 ) ) AS paidAmount
                from pg_incre_order where worker_id= #{userId}
                    and settle_status not in ('CANCEL','EXCEPTION')
                    and pay_status='PAID'
        ) t
    </select>


    <select id="queryProfitSumByTime" resultType="java.math.BigDecimal">
          select
            sum(amount) as 'amount'
        from (
                SELECT
                        sum(amount) AS 'amount'
                from order_share
                where (work_user_id = #{userId})
                    and status not in ('CANCEL','EXCEPTION')
                    and work_user_id !=''
                    and create_time between #{startTime} and #{endTime}
            union all
                select
                    sum(worker_amount) AS 'amount'
                from pg_incre_order where worker_id= #{userId}
                    and settle_status not in ('CANCEL','EXCEPTION')
                      and pay_status='PAID'
                    and create_time between #{startTime} and #{endTime}
        ) t
    </select>

    <select id="queryOrderShareList" resultType="com.gree.mall.miniapp.bean.settle.OrderShareBean">
        select * from (
                (SELECT
                    1 as 'flag',
                    amount,
                    handle_fees,
                    order_id,
                    payment,
                    pay_time,
                    create_time,
                    status
                from order_share
                where (work_user_id = #{userId})
                        and work_user_id !=''
                        <if test="status != null and status !=''">
                            and status = #{status}
                        </if>
                )

            union all

                (select
                        2 as 'flag',
                        worker_amount AS 'amount',
                        0 as 'handleFees',
                        id as 'order_id',
                        amount as 'payment',
                        pay_time,
                        create_time,
                        settle_status as 'status'
                from pg_incre_order where worker_id= #{userId}
                        and pay_status='PAID'
                        <if test="status != null and status !=''">
                            and settle_status = #{status}
                        </if>
                )
        ) t order by t.create_time desc
    </select>

</mapper>