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

142 lines
3.7 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?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 FactoryImageService extends BaseService
{
private MediaUrlService $media;
public function __construct()
{
parent::__construct();
$this->model = FactoryImageModel::class;
$this->selectField = ['id', 'factory', 'url', 'status', 'created_at', 'updated_at'];
$this->queryField = ['factory' => '=', 'status' => '='];
$this->orderBy = ['name' => 'id', 'sort' => 'desc'];
$this->media = MediaUrlService::getInstance();
}
/**
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function list(): array
{
$this->with = ['factoryInfo'];
$result = $this->getPageList();
foreach ($result['items'] as &$item) {
$item['factory_name'] = $item['factory_info']['name'] ?? '';
unset($item['factory_info']);
$item['url'] = $this->media->toPublic($item['url'] ?? '');
}
unset($item);
return $result;
}
public function option(): mixed
{
$this->optionField = ['id', 'url as name'];
return $this->getOption();
}
/**
* @throws Exception
*/
public function detail($id): mixed
{
$info = $this->getDetail($id);
$info->url = $this->media->toPublic($info->url);
return $info;
}
/**
* 某工厂的全部产品图(老接口 factory-image/image-list
*/
public function imageList(int $factoryId): array
{
if ($factoryId <= 0) {
return [];
}
$rows = FactoryImageModel::where('factory', $factoryId)
->where('deleted_at', 0)
->orderBy('id')
->get(['id', 'factory', 'url', 'status'])
->toArray();
$this->media->publicEach($rows, ['url']);
return $rows;
}
/**
* 新增:一次可上传多张
* @throws Exception
*/
public function create($params): mixed
{
$factoryId = (int) ($params['factory'] ?? 0);
if (!FactoryInfoModel::where('id', $factoryId)->where('deleted_at', 0)->exists()) {
$this->utils->errorThrow('工厂不存在');
}
$urls = $params['url'] ?? '';
$urls = is_array($urls) ? $urls : [$urls];
$now = time();
$rows = [];
foreach ($urls as $url) {
$url = $this->media->toStorage(is_string($url) ? $url : '');
if ($url === '') {
continue;
}
$rows[] = [
'factory' => $factoryId,
'url' => $url,
'created_at' => $now,
];
}
if (empty($rows)) {
$this->utils->errorThrow('请上传图片');
}
if (count($rows) === 1) {
return $this->insert($rows[0]);
}
return FactoryImageModel::insert($rows);
}
/**
* @throws Exception
*/
public function update($id, $params): mixed
{
if (array_key_exists('url', $params)) {
$params['url'] = $this->media->firstOf($params['url']);
}
return $this->save($id, $params);
}
/**
* @throws Exception
*/
public function delete($id): mixed
{
return $this->del(is_array($id) ? $id : [$id]);
}
/**
* @throws Exception
*/
public function status($id, $status): mixed
{
return $this->save($id, ['status' => (int) $status]);
}
}