<?php

namespace App\Http\Controllers;

use App\Http\Helpers\ApiResponse;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests,ApiResponse;


    protected $res = [
        'httpCode' => 200,
        'message' => '操作成功',
        'errorCode' => 0,
        'errorMessage' => '',
    ];

    protected $log;

    protected function setValidatorFailResponse($errorMessage)
    {
        return $this->res = [
            'httpCode' => 400,
            'errorCode' => 400416,
            'errorMessage' => $errorMessage,
        ];
    }

    protected function setBeforeUpdateForLog($data)
    {
        $this->log->before_update = is_array($data) ? json_encode($data, 256) : $data;
    }

    protected function setAfterUpdateForLog($data)
    {
        $this->log->after_update = is_array($data) ? json_encode($data, 256) : $data;
    }

    protected function addLog($targetId = 0, $targetField = '', $targetType = '')
    {
        if ($targetType) {
            $this->log->target_type = $targetType;
        }

        return $this->log->add($targetId, $targetField);
    }
}
