<?php

namespace App\Services\Pay;

use App\Exceptions\AlertException;
use App\Exceptions\ApiException;
use App\Models\OrderModel;
use App\Services\Service;

class OrderService extends Service
{

    /**
     * 企业付款到零钱
     * @param  int  $uid
     * @param  int  $pay_fee  单位：分
     * @param  string  $desc  描述
     * @return bool
     * @throws \ApiException
     */
    public function payments(int $uid, int $pay_fee, string $desc = "口袋高校提现")
    {
        $pay_url = config('api.pay_url') . "/payments/minitransfer";
        $params = [
            'uid' => $uid,
            'amount' => $pay_fee,
            'app_order_id' => "share_payments_" . uniqid(),
            'desc' => $desc
        ];
        $result = \Curl::to($pay_url)->withData($params)->asJsonResponse()->post();
        if (isset($result->code) && $result->code == 200) {
            return true;
        } else {
            throw new \ApiException($result->message, $result->code);
        }
    }


    /**
     * 订单退款
     * @param  int  $order_id  订单ID
     * @param  int  $refund_fee  退款金额
     * @param  string  $refund_desc  退款描述
     * @param  bool  $all
     * @return bool
     * @throws AlertException
     * @throws ApiException
     */
    public function refund(int $order_id, int $refund_fee, $refund_desc = "", $all = false)
    {
        $order = OrderModel::find($order_id);
        if (collect($order)->isEmpty()) {
            throw new ApiException('支付订单错误或不存在', 404);
        }

        if ($all && $order->create_at < 1533114000) {
            throw new AlertException("过期订单,请联系小向导微信:fpdxkf", 103);
        } else {
            $pay_url = config('api.pay_url') . "/payments/order/{$order->out_trade_no}/refund";
            $params = [
                'refund_fee' => $refund_fee,
                'refund_desc' => $refund_desc
            ];
        }
        $result = \Curl::to($pay_url)->withData($params)->asJsonResponse()->post();
        if (isset($result->code) && $result->code == 200) {
            // 使用优惠券逻辑
            if ($order->coupon_id) {
                $couponService = new CouponService();
                $couponService->setUid($order->uid)
                    ->setCouponId($order->coupon_id)
                    ->unUse();
            }
            return true;
        } else {
            throw new AlertException(isset($result->message) ? $result->message : "订单退款异常", 500);
        }
    }


    /**
     * 订单取消支付
     * @param  int  $order_id
     * @return bool
     * @throws AlertException
     * @throws ApiException
     */
    public function closeOrder(int $order_id)
    {
        $order = OrderModel::find($order_id);
        if (!$order) {
            throw new ApiException("订单不存在", 404001);
        }

        $pay_url = config('api.pay_url') . "/payments/order/{$order->out_trade_no}/close";
        $result = \Curl::to($pay_url)->asJsonResponse()->put();
        if (isset($result->code) && $result->code == 200) {
            // 置为关闭状态
            $order->order_state = 3;
            $order->save();

            // 是否使用优惠券
            if ($order->coupon_id) {
                $couponService = new CouponService();
                $couponService->setUid($order->uid)
                    ->setCouponId($order->coupon_id)
                    ->unUse();
            }
            return true;
        } else {
            throw new ApiException(
                isset($result->message) ? $result->message : "取消订单异常[{$order_id}]",
                500
            );
        }
    }
}
