<?php

namespace App\Dao\Bank;

use App\Cache\FinanceCache;
use App\Consts\FinanceConsts;
use App\Models\Bank\Banks;
use App\Models\Bank\ChannelUserCashAccount;

class BankDao
{
    /**
     * @return mixed
     */
    public function bankList()
    {
        return Banks::select('id', 'name', 'code', 'source')
                    ->where('is_show', 1)
                    ->where('source', 'LIANLIANPAY')
                    ->orderBy('id', 'asc')
                    ->get();
    }

    /**
     * @param $bankId
     * @return mixed
     */
    public function getBank($bankId)
    {
        return Banks::select('id', 'name', 'code', 'source')
                    ->where('id', $bankId)
                    ->where('is_show', 1)
                    ->first();
    }

    /**
     * @param     $channelUserId
     * @param int $isCompany
     * @return array
     */
    public function getValidCashAccountsByChannelUserId($channelUserId, int $isCompany = -1): array
    {
        if (empty($channelUserId)) {
            return [];
        }

        $query = ChannelUserCashAccount::where('channel_user_id', $channelUserId)->where('deleted_at', '=', null);

        // 对公对私
        if (in_array($isCompany, [0, 1])) {
            $query->where('is_company', $isCompany);
        } else {
            $query->orderBy('is_company', 'desc');
        }

        $result = $query->orderBy('updated_at', 'desc')->get();
        return $result ? $result->toArray() : [];
    }

    /**
     * @param $id
     * @return array
     */
    public function getValidCashAccountById($id): array
    {
        if (empty($id)) {
            return [];
        }

        $result = ChannelUserCashAccount::where('id', $id)->where('deleted_at', '=', null)->first();
        return $result ? $result->toArray() : [];
    }

    /**
     * 添加银行账号
     *
     * @param $param
     * @return bool|int
     */
    public function addCashAccount($param)
    {
        $channelUserId = (int)getProp($param, 'channel_user_id');
        $cardNumber    = trim(getProp($param, 'card_number'));
        if (empty($channelUserId) || empty($cardNumber) || empty($param)) {
            return false;
        }

        $data = [
            'channel_user_id' => $channelUserId,
            'account_name'    => trim(getProp($param, 'account_name')),
            'identity_card'   => trim(getProp($param, 'identity_card')),
            'card_number'     => $cardNumber,
            'account_bank'    => trim(getProp($param, 'account_bank')),
            'bank'            => trim(getProp($param, 'bank')),
            'province'        => trim(getProp($param, 'province')),
            'phone'           => trim(getProp($param, 'phone')),
            'bank_id'         => (int)getProp($param, 'bank_id'),
            'is_company'      => (int)getProp($param, 'is_company'),
            'status'          => FinanceConsts::STATUS_FORBID_UPDATE,
            'created_at'      => date('Y-m-d H:i:s'),
            'updated_at'      => date('Y-m-d H:i:s'),
        ];

        return ChannelUserCashAccount::insertGetId($data);
    }

    /**
     * 删除账号
     *
     * @param $cardId
     * @return bool
     */
    public function deleteCashAccountByCardId($cardId): bool
    {
        if (empty($cardId)) {
            return false;
        }

        return ChannelUserCashAccount::where('id', $cardId)
                                     ->update(['deleted_at' => date('Y-m-d H:i:s')]);
    }

    /**
     * @param $channelUserId
     * @return bool
     */
    public function deleteCashAccountByChannelUserId($channelUserId): bool
    {
        if (empty($channelUserId)) {
            return false;
        }

        return ChannelUserCashAccount::where('channel_user_id', $channelUserId)
                                     ->update(['deleted_at' => date('Y-m-d H:i:s')]);
    }

    /**
     * 校验手机验证码
     *
     * @param $channelId
     * @param $phone
     * @param $code
     * @return bool
     */
    public function checkSmsCode($channelId, $phone, $code): bool
    {
        if (empty($code)) {
            return false;
        }

        // 对比验证码
        $phoneCode = FinanceCache::getSmsCode($channelId, $phone);
        if (empty($phoneCode) || (string)$code !== (string)$phoneCode) {
            return false;
        }

        return true;
    }
}