289 lines
9.0 KiB
PHP
289 lines
9.0 KiB
PHP
<?php
|
|
|
|
namespace App\Service;
|
|
|
|
use App\BaseApp\BaseService;
|
|
use App\Enum\ProjectFieldTypeEnum;
|
|
use App\Enum\ProjectStatusEnum;
|
|
use App\Enum\StatusEnum;
|
|
use App\Models\CollectionModel;
|
|
use App\Models\FeatureModel;
|
|
use App\Models\ProjectLabelRelationModel;
|
|
use App\Models\ProjectModel;
|
|
use App\Models\ScreenshotModel;
|
|
use App\Models\UserModel;
|
|
use Exception;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Psr\Container\ContainerExceptionInterface;
|
|
use Psr\Container\NotFoundExceptionInterface;
|
|
|
|
class ProjectService extends BaseService
|
|
{
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
$this->selectField = [ 'id', 'user_id', 'title', 'is_selected', 'is_carousel', 'module_image', 'is_sold', 'flowchart', 'content', 'cover', 'category_id', 'description', 'video_url', 'author_id', 'price', 'preferential_price', 'front', 'service', 'deploy', 'status', 'created_at', 'updated_at', 'deleted_at', ];
|
|
$this->model = ProjectModel::class;
|
|
}
|
|
|
|
/**
|
|
* 获取项目列表
|
|
* @return array
|
|
* @throws ContainerExceptionInterface
|
|
* @throws NotFoundExceptionInterface
|
|
*/
|
|
public function list(): array
|
|
{
|
|
$this->with = [
|
|
'frameworks:id,name',
|
|
'features:id,project_id,name',
|
|
'labels:id,name',
|
|
'category:id,name',
|
|
'screenshots:id,url,project_id',
|
|
// 'features:id,project_id,name',
|
|
'author:id,nick_name,avatar'
|
|
];
|
|
$result = $this->getPageList();
|
|
|
|
foreach ($result['items'] as &$item) {
|
|
$item['status_text'] = ProjectStatusEnum::from($item['status'])->description();
|
|
$item['features_names'] = array_column($item['features'], 'name');
|
|
$item['label_ids'] = array_column($item['labels'], 'id');
|
|
$item['screenshots'] = array_column($item['screenshots'], 'url');
|
|
}
|
|
return $result;
|
|
}
|
|
|
|
public function detail($id)
|
|
{
|
|
$this->with = [
|
|
'frameworks:id,name',
|
|
'labels:id,name',
|
|
'category:id,name',
|
|
'screenshots:id,url,project_id',
|
|
'features:id,project_id,name',
|
|
'author:id,nick_name,avatar'
|
|
];
|
|
|
|
return $this->getDetail($id);
|
|
}
|
|
|
|
/**
|
|
* 项目下拉列表
|
|
* @return mixed
|
|
*/
|
|
public function option()
|
|
{
|
|
$this->optionField = ['id', 'title'];
|
|
return $this->getOption();
|
|
}
|
|
|
|
/**
|
|
* 修改项目详细介绍
|
|
* @param $id
|
|
* @param $content
|
|
* @return mixed
|
|
* @throws Exception
|
|
*/
|
|
public function changeContent($id, $content)
|
|
{
|
|
return $this->save($id, ['content' => $content]);
|
|
}
|
|
|
|
/**
|
|
* 创建项目
|
|
* @param $params
|
|
* @return mixed
|
|
* @throws Exception
|
|
*/
|
|
public function create($params): mixed
|
|
{
|
|
$params['user_id'] = $this->userId;
|
|
$params['author_id'] = $params['author_id']?? $this->userId;
|
|
$screenshots = $params['screenshots'];
|
|
$labels = $params['labels'];
|
|
$features = $params['features'];
|
|
if (empty($params['preferential_price'])) {
|
|
$params['preferential_price'] = $params['price'];
|
|
}
|
|
if (empty($params['content'])) {
|
|
$params['content'] = '';
|
|
}
|
|
unset($params['screenshots'], $params['labels'], $params['features']);
|
|
DB::beginTransaction();
|
|
try {
|
|
$insertProjectModel = $this->insert($params);
|
|
|
|
$labelInsertData = [];
|
|
foreach ($labels as $label) {
|
|
if (!empty($label)) {
|
|
$labelInsertData[] = [
|
|
'project_id' => $insertProjectModel,
|
|
'label_id' => $label,
|
|
'created_at' => get_time()
|
|
];
|
|
}
|
|
}
|
|
$insertLabelModel = ProjectLabelRelationModel::insert($labelInsertData);
|
|
|
|
if (!$insertLabelModel) {
|
|
$this->utils->errorThrow('标签添加失败');
|
|
}
|
|
|
|
$featureInsertData = [];
|
|
foreach ($features as $feature) {
|
|
if (!empty($feature)) {
|
|
$featureInsertData[] = [
|
|
'project_id' => $insertProjectModel,
|
|
'name' => $feature,
|
|
'created_at' => get_time()
|
|
];
|
|
}
|
|
}
|
|
$insertFeatureModel = FeatureModel::insert($featureInsertData);
|
|
if (!$insertFeatureModel) {
|
|
$this->utils->errorThrow('项目功能添加失败');
|
|
}
|
|
|
|
$screenInsertData = [];
|
|
foreach ($screenshots as $screenshot) {
|
|
if (!empty($screenshot)) {
|
|
$screenInsertData[] = [
|
|
'project_id' => $insertProjectModel,
|
|
'url' => $screenshot,
|
|
'created_at' => get_time()
|
|
];
|
|
}
|
|
}
|
|
$insertScreenModel = ScreenshotModel::insert($screenInsertData);
|
|
if (!$insertScreenModel) {
|
|
$this->utils->errorThrow('项目截图添加失败');
|
|
}
|
|
|
|
DB::commit();
|
|
} catch (Exception $e) {
|
|
DB::rollBack();
|
|
$this->utils->errorThrow($e->getMessage());
|
|
}
|
|
|
|
return $insertProjectModel;
|
|
}
|
|
|
|
/**
|
|
* 编辑项目
|
|
* @param $id
|
|
* @param $params
|
|
* @return mixed
|
|
* @throws Exception
|
|
*/
|
|
public function update($id, $params): mixed
|
|
{
|
|
$params['user_id'] = $this->userId;
|
|
$params['author_id'] = $params['author_id']?? $this->userId;
|
|
$screenshots = $params['screenshots']?? [];
|
|
$labels = $params['labels']?? [];
|
|
$features = $params['features']?? [];
|
|
unset($params['screenshots'], $params['labels'], $params['features']);
|
|
|
|
if (empty($params['content'])) {
|
|
$params['content'] = '';
|
|
}
|
|
DB::beginTransaction();
|
|
try {
|
|
// 更新项目基本信息
|
|
$updateProjectModel = $this->save($id, $params);
|
|
|
|
// 先删除旧的标签关联数据
|
|
ProjectLabelRelationModel::where('project_id', $id)->delete();
|
|
$labelInsertData = [];
|
|
foreach ($labels as $label) {
|
|
$labelInsertData[] = [
|
|
'project_id' => $id,
|
|
'label_id' => $label,
|
|
'created_at' => get_time()
|
|
];
|
|
}
|
|
if (!empty($labelInsertData)) {
|
|
$insertLabelModel = ProjectLabelRelationModel::insert($labelInsertData);
|
|
if (!$insertLabelModel) {
|
|
$this->utils->errorThrow('标签更新失败');
|
|
}
|
|
}
|
|
|
|
// 先删除旧的功能数据
|
|
FeatureModel::where('project_id', $id)->delete();
|
|
$featureInsertData = [];
|
|
foreach ($features as $feature) {
|
|
$featureInsertData[] = [
|
|
'project_id' => $id,
|
|
'name' => $feature,
|
|
'created_at' => get_time()
|
|
];
|
|
}
|
|
if (!empty($featureInsertData)) {
|
|
$insertFeatureModel = FeatureModel::insert($featureInsertData);
|
|
if (!$insertFeatureModel) {
|
|
$this->utils->errorThrow('项目功能更新失败');
|
|
}
|
|
}
|
|
|
|
// 先删除旧的截图数据
|
|
ScreenshotModel::where('project_id', $id)->delete();
|
|
$screenInsertData = [];
|
|
foreach ($screenshots as $screenshot) {
|
|
$screenInsertData[] = [
|
|
'project_id' => $id,
|
|
'url' => $screenshot,
|
|
'created_at' => get_time()
|
|
];
|
|
}
|
|
if (!empty($screenInsertData)) {
|
|
$insertScreenModel = ScreenshotModel::insert($screenInsertData);
|
|
if (!$insertScreenModel) {
|
|
$this->utils->errorThrow('项目截图更新失败');
|
|
}
|
|
}
|
|
|
|
DB::commit();
|
|
} catch (Exception $e) {
|
|
DB::rollBack();
|
|
$this->utils->errorThrow($e->getMessage());
|
|
}
|
|
|
|
return $updateProjectModel;
|
|
}
|
|
|
|
/**
|
|
* 删除项目
|
|
* @param $ids
|
|
* @return mixed|true
|
|
* @throws Exception
|
|
*/
|
|
public function delete($ids): mixed
|
|
{
|
|
return $this->del($ids);
|
|
}
|
|
|
|
/**
|
|
* 修改项目(是否精选、是否出售、是否为轮播图)
|
|
* @param $id
|
|
* @param $type
|
|
* @return mixed
|
|
* @throws Exception
|
|
*/
|
|
public function changeType($id, $type)
|
|
{
|
|
$field = ProjectFieldTypeEnum::from($type)->description();
|
|
$this->selectField = [$field];
|
|
$detail = $this->detail($id);
|
|
if (empty($detail)) {
|
|
$this->utils->errorThrow('项目不存在');
|
|
}
|
|
$update = $this->save($id, [$field => $detail[$field] === 0? 1 : 0]);
|
|
if (!$update) {
|
|
$this->utils->errorThrow('项目状改失败');
|
|
}
|
|
return $update;
|
|
}
|
|
}
|