<?php
/**
 *
 * @file:VerifyService.php
 * @Date: 2023/5/30
 * @Time: 10:12
 */


namespace Modules\Jiesuan\Services;

use Modules\Jiesuan\Models\BankCards;

class VerifyService extends BaseService
{

    /**
     *  审核列表
     * name: verifyList
     * @param array $param
     * date 2023/05/30 10:15
     */
    public static function verifyList(array $param = [], $isAll = false)
    {
        $list = self::QuerySql($param);
        $list->orderBy("bank_cards.id",'desc')->select('bank_cards.id', 'bank_cards.owner_name', 'bank_cards.name_of_payee', 'bank_cards.status', 'bank_cards.card_no', 'bank_cards.bank_name', 'bank_cards.phone_of_payee', 'bank_cards.bank_name', 'bank_cards.remark', 'bank_cards.created_at', 'bank_cards.checked_at', 'bank_cards.company_uid', 'users.username as business_name');
        if ($isAll) {
            $list = $list->get();
        } else {
            $list = $list->paginate(getProp($param, 'limit', 10));
        }
        if (!$list->isEmpty()) {
            foreach ($list as $val) {
                $val->status_text = ConstService::BANK_CARD_CHECK_STATUS[$val->status] ?? "-";
            }
        }
        return $list;
    }


    private static function QuerySql(array $param)
    {
        $sql = BankCards::query()->leftJoin('users', 'users.id', '=', 'bank_cards.business_id');
        if (getProp($param, 'status', 1) == 1) {
            $sql->where('bank_cards.status', '=', 1);
        } else {
            $sql->where('bank_cards.status', '>', 1);
        }

        // 商户名称
        if (getProp($param, 'owner_name', '')) {
            $sql->where('bank_cards.owner_name', "like", "%{$param['owner_name']}%");
        }
        // 商务
        if (getProp($param, 'bank_cards.business_id', 0)) {
            $sql->where('bank_cards.business_id', $param['business_id']);
        }
        // 创建时间
        if (getProp($param, 'created_at_start')) {
            $sql->where('bank_cards.created_at', '>=', $param['created_at_start']);
        }
        if (getProp($param, 'created_at_end')) {
            $end = date("Y-m-d",strtotime($param['created_at_end'])). " 23:59:59";
            $sql->where('bank_cards.created_at', "<=", $end);
        }
        // 审核时间
        if (getProp($param, 'pay_at_start')) {
            $sql->where('bank_cards.pay_at_at', '>=', $param['pay_at_start']);
        }
        if (getProp($param, 'pay_at_end')) {
            $end = date("Y-m-d",strtotime($param['pay_at_end'])). " 23:59:59";
            $sql->where('bank_cards.pay_at', "<=", $end);
        }
        return $sql;
    }

    /**
     *  更新银行卡审核信息
     * name: checkBankInfo
     * @param mixed $id
     * @param array $param
     * $param = [
     * 'status' => 2,
     * 'checked_user' => [ // 审核人信息
     *      "id"=>1，
     *      "user_name"=>, //
     *      "email"=>
     *      ];
     *     'checked_at' => '2023-02-02 09:28:56' // 审核时间
     *     'remark' =>  '' // 备注
     *
     * ];
     * date 2023/05/30 14:19
     */
    public static function checkBankInfo(mixed $id, array $param)
    {
        $info = BankCards::where('id', $id)->where('status', '>', 0)->first();
        if (empty($info)) {
            self::throwErrMsg("银行卡信息不存在或已删除，请刷新重试！");
        }
        if ($info['status'] != 1) {
            self::throwErrMsg("改收款信息已审核，无需再次审核");
        }
        $res = BankCards::where('id', $id)->update($param);
        if ($res) {
            return "操作成功";
        }
        self::throwErrMsg('操作失败');
    }
}
