<?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;

class CallbackController extends Base
{
    /**
     * 创建支付订单，返回前端ping++的支付信息
     */
    public function payAction()
    {
        $raw_data = file_get_contents('php://input');

        $paySrv = new PayService();

        $data = json_decode($raw_data, true);

        if(!empty($data['data']['object'])) {

            if($data['type'] == 'charge.succeeded') {
                $merchant_order_no = $data['data']['object']['order_no'];
            } else {
                $merchant_order_no = $data['data']['object']['merchant_order_no'];
            }

            CallbackLog::insert([
                'type'=>$data['type'] ?? '',
                'merchant_order_no'=>$merchant_order_no ?? '',
                'object_id'=>$data['data']['object']['id'] ?? '',
                'data'=>base64_encode(gzcompress($raw_data)), //压缩存储
            ]);
        }
        //$str = gzuncompress(base64_decode($info['data']));

        $ret = $paySrv->call_back($data);
        $this->success(['result'=>$ret]);
    }

    public function refundAction() {

        $raw_data = file_get_contents('php://input');
        $data = json_decode($raw_data, true);

        if(!empty($data['data']['object'])) {

            if($data['type'] == 'refund.succeeded') {
                $merchant_order_no = $data['data']['object']['order_no'];
            } else {
                $merchant_order_no = $data['data']['object']['merchant_order_no'];
            }
            CallbackLog::insert([
                'type'=>$data['type'] ?? '',
                'merchant_order_no'=>$merchant_order_no ?? '',
                'object_id'=>$data['data']['object']['id'] ?? '',
                'data'=>base64_encode(gzcompress($raw_data)), //压缩存储
            ]);
        }

        $refundSrv = new RefundService();
        $ret = $refundSrv->call_back($data);
        $this->success(['result'=>$ret]);
    }

    public function daemonAction() {

        $params = $this->params;
        $date    = $params['date'] ?? date('Y-m-d');
        $all    = $params['all'] ?? 0;


        $timestamp = strtotime($date);
        $start = date('Y-m-d', $timestamp);
        $end = date('Y-m-d', $timestamp + 86400);

        if($all != 1) {
            $where['notify_account_status'] = 0;
        }

        $where['can_notify_account'] = 1;
        $where['notify_account_times[<]'] = 10;
        $where['can_notify_account_time[<>]'] = [$start, $end];
        $where['ORDER'] = ['can_notify_account_time'=>'ASC'];
        $where['LIMIT'] = [0, 100];

        $list = PayOrderItem::selectMaster('*', $where);

        $payService = new PayService();
        foreach ($list as $row) {
            $payService->sync_account_wallet($row['order_item_id']);
        }

        $step = empty($list) ? 120 : 5;

        $this->success(['result'=>$list, 'step'=>$step]);

    }

    public function decryptAction() {
        $params = $this->params;
        $key   = $params['key'] ?? '';
        if(empty($key)) {
            throw new \App\Exception\BaseException(['msg'=>'请传入解密数据', 'code'=>'2031']);
        }

        $decrypt = gzuncompress(base64_decode($key));

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

    }
}
