<?php

namespace App\Services;

use App\Constants\HttpEnumCode;
use App\Model\DoctorAccount;
use App\Model\DoctorAccountDay;
use App\Model\DoctorBankCard;
use App\Model\DoctorWithdrawal;
use App\Model\OrderInquiry;
use App\Model\UserDoctor;

/**
 * 医生账户
 */
class DoctorAccountService extends BaseService
{
    /**
     * 获取我的账户数据
     * @return array
     */
    public function getDoctorAccount(): array
    {
        $user_info = $this->request->getAttribute("userInfo") ?? [];

        $date = $this->request->input('date');

        // 获取医生账户余额
        $balance_account = $this->getDoctorBalanceAccount($user_info['client_user_id']);
        if ($balance_account > 0) {
            $balance_account = $balance_account * 0.75;
        }

        // 获取医生月度余额
        $amount_total_month = $this->getDoctorMonthAmountTotal($user_info['client_user_id'], $date);
        if ($amount_total_month > 0) {
            $amount_total_month = $amount_total_month * 0.75;
        }

        // 获取医生月度已提现金额-审核通过时间为准
        $params = array();
        $params['doctor_id'] = $user_info['client_user_id'];
        $doctor_withdrawal = DoctorWithdrawal::getOne($params);
        if (empty($doctor_withdrawal)) {
            $withdrawal_amount_month = 0;
        } else {
            $withdrawal_amount_month = $doctor_withdrawal['actual_withdrawal_amount'];
        }

        // 获取医生每日账单数据
        $bill = [];

        $fields = [
            'total_amount',
            'month',
            'day',
        ];

        $params = array();
        $params['doctor_id'] = $user_info['client_user_id'];
        $params['year'] = date('Y', strtotime($date));
        $params['month'] = date('m', strtotime($date));
        $doctor_account_days = DoctorAccountDay::getList($params, $fields);
        if (!empty($doctor_account_days)) {
            foreach ($doctor_account_days as $doctor_account_day) {
                $data = array();
                $data['total_amount'] = ceil($doctor_account_day['total_amount'] * 0.75 * 100) / 100;
                $data['month'] = $doctor_account_day['month'];
                $data['day'] = $doctor_account_day['day'];
                $bill[] = $data;
            }
            unset($doctor_account_days);
        }

        $result = array();
        $result['balance_account'] = $balance_account; // 账户余额
        $result['amount_total_month'] = $amount_total_month; // 月余额
        $result['withdrawal_amount_month'] = $withdrawal_amount_month; // 月已提现金额
        $result['bill'] = $bill; // 账单

        return success($result);
    }

    /**
     * 获取我的账户日账单明细数据
     * @return array
     */
    public function getDoctorAccountInfo(): array
    {
        $user_info = $this->request->getAttribute("userInfo") ?? [];

        $date = $this->request->input('date');
        $page = $this->request->input('page', 1);
        $per_page = $this->request->input('per_page', 10);

        // 获取当天开始时间
        $start_date = date('Y-m-d 00:00:00', strtotime($date));

        // 获取当天结束时间
        $end_date = date('Y-m-d 23:59:59', strtotime($date));

        $reception_time = [$start_date, $end_date];

        // 获取医生当日接诊订单金额
        $fields = [
            'order_inquiry_id',
            'inquiry_type',
            'inquiry_mode',
            'inquiry_status',
            'inquiry_refund_status',
            'inquiry_no',
            'amount_total',
            'payment_amount_total',
            'reception_time',
            'finish_time',
            'patient_name',
            'patient_sex',
            'patient_age',
        ];

        $params = array();
        $params['doctor_id'] = $user_info['client_user_id'];
        $params['inquiry_status'] = 6; // inquiry_status：问诊订单状态（1:待支付 2:待分配 3:待接诊 4:已接诊 5:已完成 6:已结束 7:已取消）
        $params['inquiry_refund_status'] = 0; // inquiry_refund_status：问诊订单退款状态（0:无退款 1:申请退款 2:退款中 3:退款成功 4:拒绝退款 5:退款关闭）

        $order_inquiry = OrderInquiry:: getDoctorDateOrderInquiryPage($params, $reception_time, $fields,$page,$per_page);
        if (!empty($order_inquiry['data'])) {
            foreach ($order_inquiry['data'] as &$item) {
                $item['amount_total'] = $item['amount_total'] * 0.75;
            }
        }

        return success($order_inquiry);
    }

    /**
     * 获取提现数据
     * @return array
     */
    public function getDoctorWithdrawal(): array
    {
        $user_info = $this->request->getAttribute("userInfo") ?? [];

        // 获取医生信息
        $params = array();
        $params['doctor_id'] = $user_info['client_user_id'];

        $fields = [
            'doctor_id',
            'user_name',
            'iden_auth_status',
            'idcard_status',
            'multi_point_status',
            'avatar',
            'is_bind_bank',
        ];
        $user_doctor = UserDoctor::getOne($params, $fields);
        if (empty($user_doctor)) {
            return fail(HttpEnumCode::HTTP_ERROR, "非法医生");
        }

        // 检测医生身份认证
        $UserDoctorService = new UserDoctorService();
        $res = $UserDoctorService->checkDoctorAuth($user_doctor);
        if ($res !== true) {
            return fail(HttpEnumCode::HTTP_ERROR, $res);
        }

        if ($user_doctor['is_bind_bank'] != 1) {
            return fail(HttpEnumCode::HTTP_ERROR, "未绑定结算银行卡");
        }

        // 获取医生结算银行卡
        $params = array();
        $params['doctor_id'] = $user_info['client_user_id'];
        $doctor_bank_card = DoctorBankCard::getWithBankOne($params);
        if (empty($doctor_bank_card)) {
            return success();
        }

        $result = array();
        $result['bank_card_id'] = $doctor_bank_card['bank_card_id'];
        $result['bank_icon_path'] = $doctor_bank_card['BasicBank']['bank_icon_path'];
        $result['bank_name'] = $doctor_bank_card['BasicBank']['bank_name'];
        $result['bank_card_code_mask'] = $doctor_bank_card['bank_card_code_mask'];


        // 获取医生账户余额
        $balance_account = $this->getDoctorBalanceAccount($user_info['client_user_id']);
        if ($balance_account > 0) {
            $balance_account = $balance_account * 0.75;
        }

        // 计算医生个人所得税
        $income_tax = $this->computeIndividualIncomeTax($balance_account);

        $result['withdrawal_amount'] = floor(($balance_account - $income_tax) * 100) / 100;

        $income_tax = floor($income_tax * 100) / 100;
        $result['income_tax'] = $income_tax;

        return success($result);
    }

    /**
     * 获取可提现问诊订单列表
     * @return array
     */
    public function getDoctorWithdrawalOrderList(): array
    {
        $user_info = $this->request->getAttribute("userInfo") ?? [];

        $page = $this->request->input('page', 1);
        $per_page = $this->request->input('per_page', 10);

        // 获取医生当日接诊订单金额
        $fields = [
            'order_inquiry_id',
            'inquiry_type',
            'inquiry_mode',
            'inquiry_status',
            'inquiry_refund_status',
            'inquiry_no',
            'amount_total',
            'payment_amount_total',
            'reception_time',
            'finish_time',
            'patient_name',
            'patient_sex',
            'patient_age',
        ];

        $params = array();
        $params['doctor_id'] = $user_info['client_user_id'];
        $params['inquiry_status'] = 6; // inquiry_status：问诊订单状态（1:待支付 2:待分配 3:待接诊 4:已接诊 5:已完成 6:已结束 7:已取消）
        $params['inquiry_refund_status'] = 0; // inquiry_refund_status：问诊订单退款状态（0:无退款 1:申请退款 2:退款中 3:退款成功 4:拒绝退款 5:退款关闭）

        $order_inquiry = OrderInquiry:: getDoctorOrderInquiryPage($params, $fields,$page,$per_page);
        if (!empty($order_inquiry['data'])) {
            foreach ($order_inquiry['data'] as &$item) {
                $item['amount_total'] = $item['amount_total'] * 0.75;
            }
        }

        return success($order_inquiry);
    }

    /**
     * 获取医生提现记录列表
     * @return array
     */
    public function getDoctorWithdrawalRecordList(): array
    {
        $user_info = $this->request->getAttribute("userInfo") ?? [];

        $year = $this->request->input('year');
        $page = $this->request->input('page',1);
        $per_page = $this->request->input('per_page',10);

        // 获取当年开始时间
        $start_date = $year.'-1-1 00:00:00';

        // 获取当年结束时间
        $end_date = $year.'-12-31 23:59:59';

        $created_at_params = [$start_date, $end_date];

        $params = array();
        $params['doctor_id'] = $user_info['client_user_id'];
        $doctor_withdrawal = DoctorWithdrawal::getDatePage($params,$created_at_params,['*'],$page,$per_page);
        if (empty($doctor_withdrawal['data'])){
            return success();
        }

        return success($doctor_withdrawal);

    }

    /**
     * 获取医生账户余额
     * @param string $doctor_id
     * @return int
     */
    public function getDoctorBalanceAccount(string $doctor_id): int
    {
        $params = array();
        $params['doctor_id'] = $doctor_id;
        $doctor_account = DoctorAccount::getOne($params);
        if (empty($doctor_account)) {
            $balance_account = 0;
        } else {
            $balance_account = $doctor_account['balance_account'];
        }

        return $balance_account;
    }

    /**
     * 获取某月医生账户已结束订单金额
     * @param string $doctor_id 医生id
     * @param string $date 日期 Ymd形式
     * @return float|int|mixed|string
     */
    public function getDoctorMonthAmountTotal(string $doctor_id, string $date): mixed
    {
        $params = array();
        $params['doctor_id'] = $doctor_id;
        $params['year'] = date('Y', strtotime($date));
        $params['month'] = date('m', strtotime($date));

        $total_amount = DoctorAccountDay::getDoctorSumTotalAmount($params);
        if (empty($total_amount)) {
            $total_amount = 0;
        }

        return $total_amount;
    }

    /**
     * 计算个人所得税
     * @param string|int $income
     * @return float|int
     */
    protected function computeIndividualIncomeTax(string|int $income): float|int
    {
        if ($income <= 800) {
            return 0;
        }

        if ($income <= 4000) {
            $income = $income - 800;
        }

        // 实际纳税金额
        $income = $income * 0.8;

        if ($income <= 20000) {
            $tax_rate = 0.2; // 税率
            $quick_deduction = 0; // 速算扣除数
        } elseif ($income <= 50000) {
            $tax_rate = 0.3;// 税率
            $quick_deduction = 2000; // 速算扣除数
        } else {
            $tax_rate = 0.4;
            $quick_deduction = 7000; // 速算扣除数
        }

        $income_tax = $income * $tax_rate - $quick_deduction;

        return $income_tax;
    }
}