<?php

namespace App\Http\Controllers;

use App\Components\AsyncResponse;
use App\DemandProcess;
use App\Http\Requests\Demand\Process\DemandProcessRequest;
use Illuminate\Support\Facades\Auth;

class DemandProcessController extends Controller
{
    use AsyncResponse;

    /**
     * 添加处理记录
     *
     * @param DemandProcessRequest $request
     */
    public function storeApi(DemandProcessRequest $request)
    {
        $params = $request->all();
        $params['processor'] = Auth::user()['id'];

        $demandProcess = DemandProcess::query()->create($params);

        $demandProcess->loadMissing('user');

        $this->success($demandProcess);
    }

    /**
     * @param DemandProcessRequest $request
     * @throws \Exception
     */
    public function destroyApi(DemandProcessRequest  $request)
    {

        $demandProcess = DemandProcess::query()->where('id',$request['id'])->first();

        if($demandProcess['processor']!==Auth::user()['id'])$this->error('非当前过程创建人不可删除');

        try {
            $bool = $demandProcess->delete();
            if($bool)$this->success();
            $this->error('删除失败');
        } catch (\Exception $e) {
            $this->error('删除异常'.$e->getMessage());
        }


    }
}
