<?php
/**
 * @package merchant
 * @Author: Qinii
 * @Date: 2020/5/27
 */
namespace app\controller\api\merchant\order;

use app\common\repositories\merchant\order\OrderGzcRepository;
use app\traits\GzcApiRequest;
use think\App;
use crmeb\basic\BaseController;
use app\common\repositories\merchant\order\OrderMerchantRepository;
use think\facade\Db;
use think\facade\Log;

class GzcNotify extends BaseController
{
    use GzcApiRequest;

    /**
     * @var OrderMerchantRepository
     */
    protected $repository;

    /**
     * StoreBrand constructor.
     * @param App $app
     * @param OrderMerchantRepository $repository
     */
    public function __construct(App $app, OrderMerchantRepository $repository)
    {
        parent::__construct($app);
        $this->repository = $repository;
    }
    /**
     * 入账回调处理
     * @return mixed
     * @author php迷途小书童
     * @created 2021/3/4 15:02
     */
    public function notify(){
        try {
            $post = $this->request->post();
            Log::info(json_encode($post));
            $OrderGzcRepository = app()->make(OrderGzcRepository::class);
            $order = $OrderGzcRepository->getWhere(['gzc_deposit_no' => $post['depositDetailNo']]);
            if (!$order)
                return app('json')->fail('error', '订单不存在');
            if ($order->success == 1)
                return app('json')->fail('error', '入账已确认');
            //修改订单状态
            Db::transaction(function () use ($order, $post) {
                if ($post['isSuccess'] == '2000') {
                    $order->success = 1;
                }
                $order->gzc_confirm_result = json_encode($post);
                return $order->save();
            });
            return app('json')->success(['result' => 'SUCCESS']);
        } catch (\Throwable $e) {
            Log::info($e->getMessage());
            return app('json')->fail('error', $e->getMessage());
        }
    }

}
