TicketChatController.php 2.8 KB
<?php

namespace App\Http\Controllers\Aside\WorkOrder;

use App\Enums\Common\Code;
use App\Http\Controllers\Aside\BaseController;
use App\Http\Requests\Api\WorkOrder\TicketChatStoreRequest;
use App\Models\WorkOrder\TicketChat;
use App\Models\WorkOrder\Tickets;
use Illuminate\Http\Request;

class TicketChatController extends BaseController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index($ticket_id)
    {
        $ticket = Tickets::find($ticket_id);;
        if (!$ticket) return response('工单未找到', 404);
        $chats = TicketChat::where('ticket_id', $ticket->id)->get();
        $this->response('success', Code::SUCCESS, $chats);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(TicketChatStoreRequest $request, $ticket_id)
    {
        $validated = $request->validated();
        $ticket = Tickets::find($ticket_id);
        if (!$ticket) return response('工单未找到', 404);
        if ($ticket->project->is_del) return response('项目状态异常', 400);

        $chat = new TicketChat();
        $chat->ticket_id = $ticket->id;
        $chat->content = $validated['content'];

        $files = $validated['files'] ?? [];
        if (empty($files) || (is_array($files) && count(array_filter($files, function($v){ return !is_null($v); })) === 0)) {
            $chat->files = null;
        } else {
            $chat->files = json_encode($files);
        }

        $chat->submit_username = $this->manage['name'];
        $chat->submit_side = 1;
        $chat->manage_id = $this->manage['id'];
        $chat->save();
        $this->response('success', Code::SUCCESS, $chat);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}