<?php

namespace app\admin\controller\sys_manage;

use app\model\Order;
use support\Log;
use support\Request;

class BellsController
{
    public function orderNum()
    {
        $orderNum = Order::whereJsonContains('order_config_json->bell', 'Y')->count();

        return json_success('success', ['num' => $orderNum]);
    }

    public function orderList()
    {
        $orders = Order::whereJsonContains('order_config_json->bell', 'Y')
            ->select('order_id', 'order_addtimes', 'order_status_system')
            ->get()
            ->toArray();

        return json_success('success', ['orders' => $orders]);
    }

    public function closeOrderBell(Request $request)
    {
        $orderId = $request->post('order_id');
        if (!$orderId) {
            return json_fail("参数异常");
        }

        Log::info("订单提醒操作",$request->post());

        try {
            $order = Order::where('order_id', $orderId)->first();
            if (!empty($order->order_config_json)) {
                $orderConfigJson = json_decode($order->order_config_json, true);
                unset($orderConfigJson['bell']);

                $order->order_config_json = json_encode($orderConfigJson);
                $order->save();
            }
            return json_success('success');
        }catch (\Exception $e){
            return json_fail('操作失败');
        }
    }
}