Files
lgp-admin-plus-api/app/Service/business/FactoryInfoService.php
LQ bcf54c2727 初始化
缺陷:主题配色需要优化整体的同风格
2026-08-14 23:21:21 +08:00

104 lines
2.8 KiB
PHP

<?php
namespace App\Service\business;
use App\BaseApp\BaseService;
use App\Models\business\FactoryImageModel;
use App\Models\business\FactoryInfoModel;
use App\Service\common\MediaUrlService;
use Exception;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
/**
* 工厂管理
*/
class FactoryInfoService extends BaseService
{
private MediaUrlService $media;
public function __construct()
{
parent::__construct();
$this->model = FactoryInfoModel::class;
$this->selectField = ['id', 'name', 'phone', 'classification', 'cover', 'address', 'status', 'created_at', 'updated_at'];
$this->queryField = ['name' => 'like', 'phone' => 'like', 'classification' => '=', 'status' => '='];
$this->media = MediaUrlService::getInstance();
}
/**
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function list(): array
{
$this->with = ['classificationInfo'];
$result = $this->getPageList();
foreach ($result['items'] as &$item) {
$item['classification_name'] = $item['classification_info']['name'] ?? '';
unset($item['classification_info']);
$item['cover'] = $this->media->toPublic($item['cover'] ?? '');
}
unset($item);
return $result;
}
public function option(): mixed
{
return $this->getOption();
}
/**
* @throws Exception
*/
public function detail($id): mixed
{
$info = $this->getDetail($id);
$info->cover = $this->media->toPublic($info->cover);
return $info;
}
public function create($params): mixed
{
if (array_key_exists('cover', $params)) {
$params['cover'] = $this->media->firstOf($params['cover']);
}
return $this->insert($params);
}
/**
* @throws Exception
*/
public function update($id, $params): mixed
{
if (array_key_exists('cover', $params)) {
$params['cover'] = $this->media->firstOf($params['cover']);
}
return $this->save($id, $params);
}
/**
* 删除工厂时连带软删它的产品图,避免留下查不到工厂的图片
* @throws Exception
*/
public function delete($id): mixed
{
$ids = array_values(array_filter(array_map('intval', is_array($id) ? $id : [$id])));
if (empty($ids)) {
$this->utils->errorThrow('参数错误');
}
$now = time();
FactoryImageModel::whereIn('factory', $ids)->where('deleted_at', 0)
->update(['deleted_at' => $now, 'updated_at' => $now]);
return $this->del($ids);
}
/**
* @throws Exception
*/
public function status($id, $status): mixed
{
return $this->save($id, ['status' => (int) $status]);
}
}