142 lines
3.7 KiB
PHP
142 lines
3.7 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 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]);
|
||
}
|
||
}
|