<?php

namespace app\api\controller\auction;


use app\models\auction\Auction;
use app\models\auction\AuctionApply;
use app\models\auction\AuctionBooking;
use app\models\auction\AuctionGu;
use app\models\auction\AuctionOrder;
use app\models\auction\AuctionPay;
use app\models\auction\AuctionProduct;
use app\models\auction\AuctionTime;
use app\models\user\User;
use app\models\user\UserBill;
use app\Request;
use Monolog\Handler\Curl\Util;
use think\facade\Cache;
use crmeb\services\{
    CacheService,
    ExpressService,
    SystemConfigService
};
use crmeb\services\UtilService;
use crmeb\repositories\OrderRepository;
use think\facade\Db;
use think\facade\Validate;


class AuctionOrderController
{
    /**
     * 申诉
     * @param Request $request
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function plead(Request $request)
    {
        $data = UtilService::postMore([
            'order_id'
        ]);
        $order = AuctionOrder::where([['order_id', '=', $data['order_id']], ['collection_id', '=', $request->uid()]])->find();
        if ($order['appeal'] == 1) return app('json')->fail('该订单已在申诉中');// 失败
        if (!$order) return app('json')->fail('没有订单');// 失败
        $product = AuctionProduct::where('id', $order['product_id'])->find();

        $product['is_show'] = 0;//下架
        $product['frozen'] = 1; // 冻结
        $order['appeal'] = 1;// 申诉
        $order['is_gs'] = 0;
        $order->save();
        $product->save();
        AuctionTime::where([ ['product_id', '=', $product['id']], ['uid', '=', $request->uid()]])->delete();
        return app('json')->success('申诉成功');// 失败
    }

    /**
     * 撤销申诉
     * @param Request $request
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function revoke(Request $request)
    {
        $data = UtilService::postMore([
            'order_id'
        ]);
        $order = AuctionOrder::where([['order_id', '=', $data['order_id']], ['collection_id', '=', $request->uid()]])->find();
        if ($order['appeal'] == 0) return app('json')->fail('该订单未申诉');// 失败
        if (!$order) return app('json')->fail('没有订单');// 失败
        $product = AuctionProduct::where('id', $order['product_id'])->find();

        $product['frozen'] = 0;
        $order['appeal'] = 0;// 撤销申诉
        $order->save();
        $product->save();
        return app('json')->success('撤销申诉成功');// 失败


    }

    /**
     * 修改支付凭证
     * @param Request $request
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function edit_voucher(Request $request)
    {

        $data = UtilService::getMore([
            ['order_id'],
            ['image']
        ]);
        if (!$data['image'] || !$data['order_id'])  return app('json')->fail('请上传打款凭证');

        $order = AuctionOrder::where('order_id', $data['order_id'])->find();
        if (!$order) return app('json')->fail('订单不存在');
        $order['upload_image'] = $data['image'];
        $order['status'] = 2;
        $order['voucher_time'] = time();

        if ($order->save()){
            return app('json')->successful('上传成功');
        }else{
            return app('json')->fail('上传失败');
        }
    }








}