Files
usedcar/app/console/controller/VehicleNewAttributesController.php
T
2025-06-23 09:23:12 +08:00

151 lines
4.6 KiB
PHP

<?php
namespace app\console\controller;
use app\console\model\VehicleNewAttributeCategoriesTranslationModel;
use support\Request;
use support\Response;
use app\console\model\VehicleNewAttributesModel;
use app\console\service\VehicleNewAttributesService;
use app\console\validate\VehicleNewAttributesValidator;
use app\mzl\common\controller\BaseController;
class VehicleNewAttributesController extends BaseController
{
/**
* 不需要验证token的接口
* @var array
*/
protected $noNeedJwt = [];
/**
* 验证器
*/
protected mixed $validator = VehicleNewAttributesValidator::class;
/**
* 返回业务 Service 实例
*/
protected function getService()
{
return new VehicleNewAttributesService(new VehicleNewAttributesModel());
}
/**
* 额外字段处理 (根据场景处理)
* @param array $item
* @param string $scene
* @return array
*/
protected function postProcessItem(array $item, string $scene): array
{
if ($scene === 'default' || $scene === 'show') {
$result = [
'id' => $item['id'],
'status' => $item['status'],
'category_id' => $item['category_id'],
'input_type' => $item['input_type'],
'is_img' => $item['is_img'],
'attribute_name' => isset($item['translation']) ? $item['translation']['attribute_name'] : '',
];
if(!empty($item['category_id'])){
$category = VehicleNewAttributeCategoriesTranslationModel::where(['categories_id'=>$item['category_id']])->first();
$result['category_name'] = $category['category_name'];
}
foreach ($item['value'] as $value) {
$valueRes['id'] = $value['id'];
$valueRes['attribute_id'] = $value['attribute_id'];
$valueRes['color_code'] = $value['color_code'];
$valueRes['value_name'] = isset($value['translation']) ? $value['translation']['value_name']:'';
$result['value'][] = $valueRes;
}
return $result;
}
if ($scene === 'preset') {
return [
'id' => $item['id'],
'is_img' => $item['is_img'],
'attribute_name' => isset($item['translation']) ? $item['translation']['attribute_name'] : '',
];
}
return $item;
}
/**
* 参数预处理 (根据场景处理)
* @param Request $request
* @param string $scene
* @return array
*/
protected function prepareParams(Request $request, string $scene): array
{
$params = parent::prepareParams($request, $scene);
if ($scene === 'default') {
}
return $params;
}
/**
* 分页
*
* @param Request $request 请求对象
* @return Response 响应对象
*/
public function index(Request $request, string $scene = 'default'): Response
{
try {
return parent::index($request, $scene);
} catch (\Throwable $th) {
return $this->errorResponse($th->getMessage().$th->getFile().$th->getLine());
}
}
public function add(Request $request): Response
{
try {
return $this->getService()->add($request->all())
? $this->successResponse()
: $this->errorResponse();
} catch (\Throwable $th) {
return $this->errorResponse($th->getMessage().$th->getLine().$th->getFile());
}
}
public function presets(Request $request, string $scene = 'preset'): Response
{
try {
return parent::all($request, $scene);
} catch (\Throwable $th) {
return $this->errorResponse($th->getMessage().$th->getLine().$th->getFile());
}
}
public function edit(Request $request, int $id): Response
{
try {
return $this->getService()->edit($request->all(), $id)
? $this->successResponse()
: $this->errorResponse();
} catch (\Throwable $th) {
return $this->errorResponse($th->getMessage().$th->getLine().$th->getFile());
}
}
public function detail(Request $request, int $id, string $scene = 'show'): Response
{
try {
$request->setGet(['id'=>$id]);
return parent::one($request, $scene);
} catch (\Throwable $th) {
return $this->errorResponse($th->getMessage().$th->getLine().$th->getFile());
}
}
}