erp/app/Http/Controllers/Goods/GoodsBrandsController.php

107 lines
3.1 KiB
PHP
Raw Normal View History

2022-07-27 19:06:16 +08:00
<?php
namespace App\Http\Controllers\Goods;
use App\Http\Controllers\Controller;
use App\Http\Resources\GoodsBrandResource;
use App\Models\GoodsBrand;
2022-08-01 17:06:43 +08:00
use App\Models\Log as LogModel;
2022-07-28 13:46:08 +08:00
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;
2022-07-27 19:06:16 +08:00
class GoodsBrandsController extends Controller
{
2022-08-01 17:06:43 +08:00
public function __construct(Request $request)
{
$this->log = new LogModel([
'module' => 'goods',
'action' => $request->getMethod(),
'target_type' => 'goods_brand',
]);
}
2022-07-27 19:06:16 +08:00
public function index()
{
$goodsBrands = GoodsBrand::query()->paginate();
return GoodsBrandResource::collection($goodsBrands);
}
public function store(Request $request)
{
2022-07-28 13:46:08 +08:00
$validator = Validator::make($request->all(), [
'names' => 'required|array',
'names.*' => 'required|string|max:255|unique:goods_brands,name',
2022-07-27 19:06:16 +08:00
]);
2022-07-28 13:46:08 +08:00
if ($validator->fails()) {
$this->setValidatorFailResponse($validator->getMessageBag()->getMessages());
return response($this->res, $this->res['httpCode']);
2022-07-28 13:46:08 +08:00
}
$goodsBrands = [];
foreach ($request->names as $name) {
$goodsBrands[] = ['name' => $name];
}
$goodsBrand = new GoodsBrand();
if (!$goodsBrand->batchInsert($goodsBrands)) {
$this->res = [
'httpCode' => 500,
'errorCode' => 500500,
'errorMessage' => '批量添加失败',
];
}
2022-08-01 17:06:43 +08:00
$this->setAfterUpdate($goodsBrands);
$this->addLog(0, 'add');
2022-07-27 19:06:16 +08:00
2022-07-28 13:46:08 +08:00
return response($this->res, $this->res['httpCode']);
2022-07-27 19:06:16 +08:00
}
public function show($id)
{
2022-07-28 13:46:08 +08:00
return new GoodsBrandResource(GoodsBrand::query()->find($id));
2022-07-27 19:06:16 +08:00
}
2022-07-28 13:46:08 +08:00
public function update($id, Request $request)
2022-07-27 19:06:16 +08:00
{
2022-07-28 13:46:08 +08:00
$validator = Validator::make($request->all(), [
'name' => [
'required',
'string',
'max:255',
Rule::unique('goods_brands')->ignore($id),
]
]);
if ($validator->fails()) {
$this->setValidatorFailResponse($validator->getMessageBag()->getMessages());
2022-07-28 13:46:08 +08:00
return response($this->res, $this->res['httpCode']);
}
$goodsBrand = GoodsBrand::query()->find($id);
2022-08-01 17:06:43 +08:00
$this->setBeforeUpdate($goodsBrand->name);
2022-07-28 13:46:08 +08:00
$goodsBrand->name = request('name');
$goodsBrand->save();
2022-08-01 17:06:43 +08:00
$this->setAfterUpdate($goodsBrand->name);
$this->addLog($id, 'name');
2022-07-27 19:06:16 +08:00
2022-07-28 13:46:08 +08:00
return new GoodsBrandResource($goodsBrand);
2022-07-27 19:06:16 +08:00
}
2022-07-28 13:46:08 +08:00
public function destroy($id)
2022-07-27 19:06:16 +08:00
{
2022-07-28 13:46:08 +08:00
$goodsBrand = GoodsBrand::query()->find($id);
try {
$goodsBrand->delete();
2022-08-01 17:06:43 +08:00
$this->addLog($id, 'status');
2022-07-28 13:46:08 +08:00
} catch (\Exception $e) {
$this->res = [
'httpCode' => 500,
'errorCode' => 500416,
'errorMessage' => $e->getMessage(),
];
}
2022-07-27 19:06:16 +08:00
2022-07-28 13:46:08 +08:00
return response($this->res, $this->res['httpCode']);
2022-07-27 19:06:16 +08:00
}
}