2025-07-04 13:45:50 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace app\ticket\logic;
|
|
|
|
|
|
2025-07-04 15:04:29 +08:00
|
|
|
use app\ticket\model\Categorie;
|
2025-07-04 13:45:50 +08:00
|
|
|
use support\Log;
|
|
|
|
|
use support\exception\BusinessException;
|
|
|
|
|
|
|
|
|
|
class CategorieLogic
|
|
|
|
|
{
|
|
|
|
|
public function list($params)
|
|
|
|
|
{
|
|
|
|
|
$where = [];
|
|
|
|
|
$size = $params['size'] ?? 10;
|
|
|
|
|
$current = $params['current'] ?? 1;
|
2025-07-04 15:04:29 +08:00
|
|
|
return Categorie::with('translation')->where($where)->paginate($size, ['*'], 'page', $current);
|
2025-07-04 13:45:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getAll($params)
|
|
|
|
|
{
|
|
|
|
|
$where = [];
|
|
|
|
|
$where[] = ['status', '=', 1];
|
2025-07-04 15:04:29 +08:00
|
|
|
return Categorie::with('translation')->where($where)->get();
|
2025-07-04 13:45:50 +08:00
|
|
|
}
|
|
|
|
|
|
2025-08-13 17:00:16 +08:00
|
|
|
public function find($id)
|
|
|
|
|
{
|
2025-08-18 11:20:35 +08:00
|
|
|
return Categorie::with(['translation' => function ($query) {
|
|
|
|
|
$query->where('locale', locale());
|
|
|
|
|
}])->where('status', 1)
|
2025-08-13 17:00:16 +08:00
|
|
|
->find($id);
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-04 13:45:50 +08:00
|
|
|
public function add(array $data)
|
|
|
|
|
{
|
|
|
|
|
try {
|
2025-07-04 15:04:29 +08:00
|
|
|
return Categorie::create($data);
|
2025-07-04 13:45:50 +08:00
|
|
|
} catch (\Throwable $e) {
|
|
|
|
|
Log::error('分类创建失败', [
|
|
|
|
|
'data' => $data,
|
|
|
|
|
'error' => $e->getMessage()
|
|
|
|
|
]);
|
2025-07-04 14:38:00 +08:00
|
|
|
throw new BusinessException('分类创建失败,请稍后重试', 500);
|
2025-07-04 13:45:50 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function edit(array $data)
|
|
|
|
|
{
|
|
|
|
|
try {
|
2025-07-04 15:04:29 +08:00
|
|
|
$categorie = Categorie::updateOrCreate(['id' => $data['id']], $data);
|
2025-07-04 13:45:50 +08:00
|
|
|
return $categorie;
|
|
|
|
|
} catch (\Throwable $e) {
|
|
|
|
|
Log::error('分类编辑失败', [
|
|
|
|
|
'data' => $data,
|
|
|
|
|
'error' => $e->getMessage()
|
|
|
|
|
]);
|
2025-07-04 14:38:00 +08:00
|
|
|
throw new BusinessException('分类编辑失败,请稍后重试', 500);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function delete(int $id)
|
|
|
|
|
{
|
|
|
|
|
try {
|
2025-07-04 15:04:29 +08:00
|
|
|
$categorie = Categorie::where(['id'=>$id])->delete();
|
2025-07-04 14:38:00 +08:00
|
|
|
return $categorie;
|
|
|
|
|
} catch (\Throwable $e) {
|
|
|
|
|
Log::error('分类删除失败', [
|
|
|
|
|
'id' => $id,
|
|
|
|
|
'error' => $e->getMessage()
|
|
|
|
|
]);
|
|
|
|
|
throw new BusinessException('分类删除失败,请稍后重试', 500);
|
2025-07-04 13:45:50 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|