<?php


namespace app\api\controller\user;

use app\admin\model\user\User;
use app\Request;
use crmeb\services\UtilService;
use app\models\order\PaymentReceived as Received;

/**
 * 支付码类
 * Class UserRechargeController
 * @package app\api\controller\user
 */
class PaymentReceived
{
    /**
     * 获取商户收款码
     * @param Request $request
     * @return string
     */
    public function collection_code(Request $request)
    {
        $code = Received::collection($request->uid());
        if (intval(input('amount', 0)) > 0) {
            $arr = ['code' => $code, 'amount' => input('amount')];
            $c_code = uniqid("c_");
            cache($c_code, $arr, 600);
            return app('json')->successful($c_code);
        } else {
            return app('json')->successful($code);
        }


    }

    /**
     * 获取付款码
     * @param Request $request
     * @return mixed
     */
    public function payment_code(Request $request)
    {
        $code = uniqid("qr_", true);
        cache($code, $request->uid(), 60);
        return app('json')->successful(['code' => $code]);
    }

    /**
     * 检测是否有效
     * @param Request $request
     * @return mixed
     */
    public function code_state(Request $request)
    {
        $code = input('code', '');
        if (cache($code)) {
            return app('json')->successful('effective');
        } else {
            if (Received::be(['code' => $code])) {
                return app('json')->successful('use');
            } else {
                return app('json')->successful('overdue');
            }
        }

    }

    /**
     * 创建订单
     * @param Request $request
     * @return mixed
     */
    public function payment_create(Request $request)
    {
        list($code, $type, $amount) = UtilService::postMore(
            [
                ['code', ''],
                ['type', 0],
                ['amount', 0],
            ], $request, true
        );
        if (is_numeric($code)) {
            $collect_uid = User::where('merchant_no', $code)->value('uid');
            $pay_type = 0;
            $pay_uid = $request->uid();
        } elseif (!is_numeric($code)) {
            if (strpos($code, "c_") === false) {
                $pay_uid = cache($code);
                if (intval($pay_uid) == 0) {
                    return app('json')->fail('付款码已过期');
                }
                $collect_uid = $request->uid();
                $pay_type = 1;
            } else {
                //商家收款设置金额
                $collect = cache($code);
                $collect_uid = User::where('merchant_no', $collect['code'])->value('uid');
                $pay_type = 0;
                $pay_uid = $request->uid();
                $amount = $collect['amount'];
                cache($code, null);
            }
        } else {
            return app('json')->fail('参数错误');
        }
        if ($amount == 0) return app('json')->fail('金额不能为0');
        if (intval($collect_uid) == 0 || intval($pay_uid) == 0) {
            return app('json')->fail('二维码有误，重新获取');
        }
        if (intval($collect_uid) == intval($pay_uid)) {
            return app('json')->fail('自己不能付自己');
        }
        $rs = Received::Create_Payment($pay_type, $code, $pay_uid, $collect_uid, $amount, $type);
        if (!$rs) return app('json')->fail(Received::getErrorInfo(''));
        return app('json')->successful('处理成功');
    }

    /**
     * 我的订单
     * @param Request $request
     */
    public function lst(Request $request)
    {
        $where = UtilService::postMore(
            [
                ['way', 'pay'],
                ['pay_type', -1],
                ['uid', $request->uid()],
                ['page', 1],
                ['limit', 20],
                ['is_tip', -1],
            ], $request
        );
        return app('json')->successful(Received::lst($where));
    }

    /**
     * 获取收款信息
     * @param Request $request
     * @return mixed
     */
    public function collection_lst(Request $request)
    {
        $where = UtilService::postMore(
            [
                ['way', 'collect'],
                ['pay_type', -1],
                ['uid', $request->uid()],
                ['is_tip', 0],
                ['page', 1],
                ['limit', 20],
            ], $request
        );
        return app('json')->successful(Received::lst($where));
    }

    public function collection_set_tip(Request $request)
    {
        if (!Received::be(['collect_uid' => $request->uid(), 'id' => input('id', 0)])) {
            return app('json')->fail('参数错误');
        }
        Received::where('id', input('id', 0))->update(['is_tip' => 1]);
        return app('json')->successful('处理成功');
    }

    public function test()
    {
        $data = json_decode('{"pay_type":1,"code":"qr_601e27df155161.70945051","pay_uid":"3239","collect_uid":3239,"amount":"100"}', true);
        $rs = Received::Create_Payment($data['pay_type'], $data['code'], $data['pay_uid'], $data['collect_uid'], $data['amount']);
        dump($rs);
        echo Received::getErrorInfo();

    }
}