<?php

namespace app\admin\controller\sys_manage;

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

class BellsController
{
    public function orderNum()
    {
        $messageNum = SysMessage::where('message_status', 'PENDING')->count();

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

    public function orderList()
    {
        $messages = SysMessage::where('message_status', 'PENDING')
            ->select('message_id', 'message_title', 'join_message_object_json', 'message_addtimes','message_category')
            ->orderBy('message_addtimes', 'DESC')
            ->get()
            ->toArray();
        foreach ($messages as &$message) {
            if (!empty($message['join_message_object_json'])) {
                $messageObjectJson = json_decode($message['join_message_object_json'], true);
                $message['order_id'] = $messageObjectJson['order_id'] ?? '';
            }
        }

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

    /**
     * @Desc 关闭订单提醒
     * @Author Gorden
     * @Date 2024/10/28 15:28
     *
     * @param Request $request
     * @return \support\Response
     */
    public function closeOrderBell(Request $request)
    {
        $messageId = $request->post('message_id');
        if (!$messageId) {
            return json_fail("参数异常");
        }

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

        try {
            $message = SysMessage::where('message_id', $messageId)->first();
            $message->message_status = 'DONE';
            $message->save();

            return json_success('success');
        } catch (\Exception $e) {
            return json_fail('操作失败');
        }
    }
}