<?php

namespace App\Dao\Bank;

use App\Consts\FinanceConsts;
use App\Models\Withdraw\ChannelWithDrawCash;

class WithdrawDao
{
    /**
     * 获取多张卡今日提现记录
     *
     * @param $cardNumbers
     * @param $startTime
     * @param $endTime
     * @return mixed
     */
    public function getWithDrawLogsByBankAccountIds($cardNumbers, $startTime, $endTime)
    {
        return ChannelWithDrawCash::whereIn('bank_account', $cardNumbers)
                                  ->where('created_at', '>=', $startTime)
                                  ->where('created_at', '<=', $endTime)
                                  ->get();
    }

    /**
     * 提现记录
     *
     * @param $param
     * @return mixed
     */
    public function getWithdrawCashes($param)
    {
        $query = ChannelWithDrawCash::select('channel_withdraw_cashes.amount', 'channel_withdraw_cashes.status',
            'bank_account', 'account_name', 'channel_withdraw_cashes.created_at', 'payments.pay_time')
                                    ->leftJoin('payments', 'payments.withdraw_cash_id', 'channel_withdraw_cashes.id');

        // 站点
        $channelId = (int)getProp($param, 'channel_id');
        if ($channelId) {
            $query->where('distribution_channel_id', $channelId);
        }

        // 状态
        $status = getProp($param, 'status');
        if ($status && in_array($status, [FinanceConsts::CHECK_PENDING, FinanceConsts::AUDIT_FAILED, '待打款', '已打款'])) {
            if ($status == '待打款') {
                $query->whereIn('channel_withdraw_cashes.status', [
                    FinanceConsts::ARTIFICAL_WAITTING_PAID,
                    FinanceConsts::AUTO_WAITTING_PAID
                ]);
            } elseif ($status == '已打款') {
                $query->whereIn('channel_withdraw_cashes.status', [
                    FinanceConsts::ARTIFICAL_PAID_SUCCESS,
                    FinanceConsts::AUTO_PAID_SUCCESS
                ]);
            } else {
                $query->where('channel_withdraw_cashes.status', $status);
            }
        }

        // 时间
        $dateRange = getProp($param, 'date_range');
        if ($dateRange) {
            [$startDate, $endDate] = explode(',', $dateRange);
            if ($startDate && $endDate && $startDate <= $endDate) {
                $query->where('channel_withdraw_cashes.created_at', '>=', date('Y-m-d 00:00:00', strtotime($startDate)));
                $query->where('channel_withdraw_cashes.created_at', '<=', date('Y-m-d 23:59:50', strtotime($endDate)));
            }
        }

        return $query->orderBy('channel_withdraw_cashes.id', 'desc')->paginate();
    }

    /**
     * 判断渠道在日期内是否有提现
     *
     * @param $channelId
     * @param $startTime
     * @param $endTime
     * @return mixed
     */
    public function checkChannelIdIsInWithDraw($channelId, $startTime, $endTime)
    {
        return ChannelWithDrawCash::where('distribution_channel_id', $channelId)
                                  ->where('created_at', '>=', $startTime)
                                  ->where('created_at', '<=', $endTime)
                                  ->exists();
    }

    /**
     * 增加提现单
     *
     * @param $data
     */
    public function addWithDrawCashes($data)
    {
        return ChannelWithDrawCash::create($data);
    }

    /**
     * 最近一笔提现中
     *
     * @param $channelId
     * @return mixed
     */
    public function getLatestWithdrawingCash($channelId)
    {
        return ChannelWithDrawCash::where('distribution_channel_id', $channelId)
                                  ->whereIn('status', [
                                      FinanceConsts::CHECK_PENDING, FinanceConsts::AUDITING,
                                      FinanceConsts::AUDIT_PASS, FinanceConsts::WAITTING_PAID
                                  ])
                                  ->orderBy('id', 'desc')
                                  ->first();
    }
}