<?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.m2pool.manage.mapper.ManageWalletOutInMapper">

    <select id="getWalletIn" resultType="com.m2pool.manage.entity.ManageWalletOutIn">
        SELECT
            coin,
            `user`,
            create_date as dateIn,
            amount AS allocationAmount,
            COALESCE(should_out_date,create_date) AS `shouldOutDate`,
            max_height AS maxHeight
        FROM
            wallet_in
        <where>
                <if test="startDate != null">
                    `should_out_date` >= DATE(#{startDate})
                </if>
        </where>
    </select>
    <select id="getWalletOut" resultType="com.m2pool.manage.entity.ManageWalletOutIn">
        SELECT
            coin,
            `user`,
            address,
            DATE(`date`) as dateIn,
            DATE(`date`) as shouldOutDate,
            `date` as dateOut,
            max_height AS maxHeight,
            tx_id AS txId,
            amount AS transferAmount
        FROM
            wallet_outv2
        <where>
            <if test="startDate != null">
                `date` >= #{startDate}
            </if>
        </where>
    </select>
    <select id="summaryOfPendingPayments" resultType="com.m2pool.manage.dto.SummaryOfPendingPaymentsDto">
        select coin, max(max_height) as maxHeight, max(should_out_date) AS shouldOutDate, `user`, sum(amount) as needPayAmount from wallet_in where state = 2 group by coin,`user`
    </select>
    <select id="getMaxDateIn" resultType="java.time.LocalDateTime">
        select `date_out` from manage_wallet_out_in order by `date_out` desc limit 1
    </select>
</mapper>
