Files
fdesk/app/common/service/VehVehicleSkuService.php
T
2025-10-11 16:06:42 +08:00

234 lines
7.4 KiB
PHP

<?php
namespace app\common\service;
use app\common\logic\Fingerprint;
use app\common\logic\ModelNaming;
use app\common\logic\SkuCodeBuilder;
use app\common\logic\VehVehicleSkuLogic;
use app\common\model\Brand;
use app\common\model\BrandSeries;
use app\common\validate\VehVehicleSkuValidate;
use app\common\traits\crud\CrudServiceTrait;
use app\common\model\VehVehicleSku;
use support\Request;
class VehVehicleSkuService
{
use CrudServiceTrait;
/**
* 指定当前要操作的模型
*/
protected string $modelClass = VehVehicleSku::class;
/**
* 验证器
*/
protected $validator = VehVehicleSkuValidate::class;
protected $logic;
public function __construct()
{
$this->logic = (new VehVehicleSkuLogic());
}
/**
* 获取验证器
*/
public function getValidator()
{
return new $this->validator;
}
// // 枚举
// private const MAPS = [
// 'BODY' => [
// 'SEDAN' => '三厢轿车', 'SUV'=>'SUV', 'HB'=>'两厢/掀背', 'MPV'=>'MPV', 'PICKUP'=>'皮卡'
// ],
// 'DRIVE' => [
// 'FWD'=>'前驱', 'RWD'=>'后驱', 'AWD'=>'四驱(AWD)', '4WD'=>'四驱(4WD)'
// ],
// 'MOTOR_LAYOUT' => [
// 'FWD'=>'前置单电机', 'RWD'=>'后置单电机', 'AWD'=>'双电机四驱', 'DM-I'=>'DM-i', 'DM-P'=>'DM-p'
// ],
// 'FUEL' => [
// 'GAS'=>'汽油', 'HEV'=>'油电混动', 'PHEV'=>'插电混动', 'EV'=>'纯电', 'EREV'=>'增程'
// ],
// 'TRANS' => [
// 'MT'=>'手动', 'AT'=>'自动', 'CVT'=>'无级', 'DCT'=>'双离合', '1SPEED'=>'单速'
// ],
// 'RANGE_STD' => [
// 'CLTC'=>'CLTC', 'WLTP'=>'WLTP', 'EPA'=>'EPA', 'NEDC'=>'NEDC'
// ],
// 'EMISSION' => [
// 'CHN6'=>'国六', 'EURO6'=>'欧6', 'EPA'=>'美国EPA'
// ],
// ];
// // *_code取中文
// public function label(string $dict, ?string $code): ?string
// {
// if ($code === null || $code === '') return null;
// $dict = strtoupper($dict);
// $code = strtoupper($code);
// return self::MAPS[$dict][$code] ?? $code;
// }
// // 静态映射把 *_code → 中文
// private function backfill(array $d): array
// {
// $d['body_type'] = $this->label('BODY', $d['body_type_code']);
// $d['drive_type'] = $this->label('DRIVE', $d['drive_type_code']);
// $d['motor_layout'] = $this->label('MOTOR_LAYOUT', $d['motor_layout_code']);
// $d['fuel_type'] = $this->label('FUEL', $d['fuel_type_code']);
// $d['transmission'] = $this->label('TRANS', $d['transmission_code']);
// $d['range_std'] = $this->label('RANGE_STD', $d['range_std_code']);
// $d['emission_std'] = $this->label('EMISSION', $d['emission_std_code']);
// return $d;
// }
protected function buildQueryConfig(Request $request, string $scene = 'default'): array
{
$p = $request->all();
$config = [];
$config['model'] = $this->modelClass;
switch ($scene) {
case 'default':
$config['where']['is_active'] = 1;
$config['order']['id'] = 'desc';
// 关键字筛选
if (isset($p['keyword']) && $p['keyword']) {
$k = '%' . $p['keyword'] . '%';
foreach (['model_name','series_name','brand_name',] as $col) {
$config['orWhere'][] = [$col, 'like', $k];
}
}
break;
case 'index':
$config['where']['is_active'] = 1;
$config['order']['id'] = 'desc';
// 关键字筛选
if (isset($p['keyword']) && $p['keyword']) {
$k = '%' . $p['keyword'] . '%';
foreach (['model_name','series_name','brand_name',] as $col) {
$config['orWhere'][] = [$col, 'like', $k];
}
}
if(isset($p['brand_id']) && $p['brand_id']){
$brand_code = Brand::where('id', $p['brand_id'])->value('brand_code');
$config['where']['brand_code'] = $brand_code;
}
if(isset($p['series_id']) && $p['series_id']){
$series_code = BrandSeries::where('id', $p['series_id'])->value('series_code');
$config['where']['series_code'] = $series_code;
}
break;
case 'show':
$config['where']['id'] = $p['id'];
$config['where']['is_active'] = 1;
break;
case 'store':
break;
case 'update':
$config['where']['id'] = $p['id'];
break;
case 'destroy':
$config['where']['id'] = $p['id'];
break;
}
return $config;
}
public function beforeServiceStore($data, Request $request)
{
// $data = $this->backfill($data);
if (empty($data['model_code'])) {
$data['model_code'] = ModelNaming::buildModelCode($data);
}
if (empty($data['model_name'])) {
$data['model_name'] = ModelNaming::buildModelName($data);
}
if (empty($data['engine_disp_l'])) {
$data['engine_disp_l'] = 0;
}
if (empty($data['engine_turbo'])) {
$data['engine_turbo'] = 0;
}
if (empty($data['engine_power_kw'])) {
$data['engine_power_kw'] = 0;
}
if (empty($data['battery_kwh'])) {
$data['battery_kwh'] = 0;
}
if (empty($data['gears'])) {
$data['gears'] = 1;
}
if (empty($data['range_km'])) { //续航
$data['range_km'] = 0;
}
if (empty($data['doors'])) { //车位数
$data['doors'] = 0;
}
if (empty($data['seats'])) { //座位数
$data['seats'] = 0;
}
if (empty($data['weight_kg'])) { //整车质量
$data['weight_kg'] = 0;
}
$data['sku_code'] = SkuCodeBuilder::build($data);
$data['corp_id'] = 0;
$data['created_by'] = $request->user->uid;
$data['updated_by'] = $request->user->uid;
// 指纹
$data['fingerprint'] = Fingerprint::makeFromArray($data);
return $data;
}
protected function afterServiceStore($res)
{
return $res;
}
public function beforeServiceUpdate($data, Request $request)
{
unset($data['created_at']);
unset($data['updated_at']);
// $data = $this->backfill($data);
if (empty($data['model_code'])) {
$data['model_code'] = ModelNaming::buildModelCode($data);
}
if (empty($data['model_name'])) {
$data['model_name'] = ModelNaming::buildModelName($data);
}
// 填充系统字段
$data['updated_by'] = $request->user->uid;
// 指纹
$data['fingerprint'] = Fingerprint::makeFromArray($data);
return $data;
}
protected function afterServiceUpdate($res)
{
return $res;
}
/**
* 获取车型list
*/
public function getList($data)
{
return $this->logic->getList($data);
}
}