<?php

use App\Base\Base;

use App\Models\order\mysql\PayOrderItem;
use App\Services\pay\PayService;
use App\Services\refund\RefundService;
use App\Models\order\mysql\CallbackLog;
use Helpers\DingTalk;

class TccController extends Base
{
    /**
     * tcc test
     */
    public function tryAction()
    {
        if(!empty($this->params['sleep'])) {
            sleep($this->params['sleep']);
        }

        if(!empty($this->params['error'])) {
            throw new \App\Exception\BaseException(['msg'=>'try is error', 'code'=>2001]);
        }

        $this->success(['result'=>[]]);
    }

    public function mainAction() {
        if(!empty($this->params['sleep'])) {
            sleep($this->params['sleep']);
        }

        if(!empty($this->params['error'])) {
            $this->success(['data'=>2]);
        }

        $this->success(['data'=>1]);
    }

    public function sleepAction() {

        if(!empty($this->params['sleep'])) {
            sleep($this->params['sleep']);
        }

        $this->success(['result'=>$this->params]);

    }

    public function confirmAction() {

        if(!empty($this->params['sleep'])) {
            sleep($this->params['sleep']);
        }

        if(!empty($this->params['error'])) {
            throw new \App\Exception\BaseException(['msg'=>'confirm is error', 'code'=>2001]);
        }

        $this->success(['result'=>[]]);
    }

    public function cancelAction() {

        if(!empty($this->params['sleep'])) {
            sleep($this->params['sleep']);
        }

        if(!empty($this->params['error'])) {
            throw new \App\Exception\BaseException(['msg'=>'confirm is error', 'code'=>2001]);
        }

        $this->success(['result'=>[]]);
    }

    public function traceAction() {
        $params = $this->getRequest()->getPost();
        //$params['traceId'] = \Api\PhpUtils\Log\Tracer::getTraceId();

        file_put_contents('/tmp/aa.log', json_encode($_SERVER), FILE_APPEND);
        $params['traceId'] = \Helpers\Tracer::getTraceId();
        $this->success($params);
    }
}
