Files
bsh_sample/app/common/service/SkuAttributeService.php
T
2025-12-17 14:30:10 +08:00

110 lines
2.6 KiB
PHP

<?php
namespace app\common\service;
use app\common\logic\PostCategoryLogic;
use app\common\logic\SkuAttributeLogic;
use app\common\validate\SkuAttributeValidate;
use app\common\traits\crud\CrudServiceTrait;
use app\common\model\SkuAttribute;
use support\Request;
class SkuAttributeService
{
use CrudServiceTrait;
protected $logic;
/**
* 指定当前要操作的模型
*/
protected string $modelClass = SkuAttribute::class;
/**
* 验证器
*/
protected $validator = SkuAttributeValidate::class;
public function __construct()
{
$this->logic = (new SkuAttributeLogic());
}
/**
* 获取验证器
*/
public function getValidator()
{
return new $this->validator;
}
/**
* 构造 CrudUtil 查询配置
*/
protected function buildQueryConfig(Request $request, string $scene = 'default'): array
{
$p = $request->all();
$config = [];
$config['model'] = $this->modelClass;
switch ($scene) {
case 'default':
case 'index':
$config['order']['sort'] = 'desc';
$config['order']['id'] = 'desc';
break;
case 'show':
$config['where']['id'] = $p['id'];
break;
case 'store':
break;
case 'update':
case 'destroy':
$config['where']['id'] = $p['id'];
break;
}
return $config;
}
/**
* 添加前置处理
*/
public function beforeServiceStore($data, Request $request)
{
$options = [];
foreach ($data['options_json'] as $v){
$options[] = $v['value'];
}
$data['options_json'] = json_encode($options, JSON_UNESCAPED_UNICODE);
return $data;
}
/**
* 添加前置处理
*/
public function beforeServiceUpdate($data, Request $request)
{
$options = [];
if (!empty($data['options_json'])) {
foreach ($data['options_json'] as $v){
$options[] = $v['value'];
}
}
$data['options_json'] = json_encode($options, JSON_UNESCAPED_UNICODE);
return $data;
}
/**
* 获取系统list
*/
public function preset($params)
{
$where = [];
if(isset($params['category_id']) && $params['category_id']) {
$where['category_id'] = $params['category_id'];
}
$result = $this->logic->getList($where)->toArray();
return $result;
}
}