|
@@ -8,18 +8,18 @@
|
|
|
|
|
|
namespace App\Http\Services\Backend\Server\Service;
|
|
namespace App\Http\Services\Backend\Server\Service;
|
|
|
|
|
|
|
|
+use App\Exceptions\ApiException;
|
|
use App\Http\Services\Service;
|
|
use App\Http\Services\Service;
|
|
-use App\Models\Service\Category;
|
|
|
|
use App\Models\Service\Project;
|
|
use App\Models\Service\Project;
|
|
-use App\Models\System\Dept;
|
|
|
|
-use App\Models\System\DictData;
|
|
|
|
-use App\Models\System\DictType;
|
|
|
|
-use Symfony\Component\HttpFoundation\Response;
|
|
|
|
|
|
+use Exception;
|
|
|
|
+use Illuminate\Support\Facades\DB;
|
|
|
|
|
|
class ProjectService extends Service
|
|
class ProjectService extends Service
|
|
{
|
|
{
|
|
protected array $simpleColumn = ['id', 'title', 'cover'];
|
|
protected array $simpleColumn = ['id', 'title', 'cover'];
|
|
- protected array $selectColumn = ['price', 'init_price as initPrice', 'material_price as materialPrice', 'time_long as timeLong', 'sort', 'status'];
|
|
|
|
|
|
+ protected array $selectColumn = ['sub_title as subTitle', 'time_long as timeLong', 'sort', 'status'];
|
|
|
|
+ protected array $selectDetailColumn = ['sale', 'material_price as materialPrice', 'com_balance as comBalance', 'is_add as isAdd', 'is_store as isStore', 'is_door as isDoor', 'introduce', 'explain', 'notice'];
|
|
|
|
+ protected string $selectRawColumn = "FORMAT(price/100,2) as price,FORMAT(init_price/100,2) as initPrice,FORMAT(material_price/100,2) as materialPrice";
|
|
protected array $appendColumn = ['created_at as createTime'];
|
|
protected array $appendColumn = ['created_at as createTime'];
|
|
|
|
|
|
public function getProjectList($params)
|
|
public function getProjectList($params)
|
|
@@ -28,32 +28,84 @@ class ProjectService extends Service
|
|
isset($params['title']) && filled($params['title']) && $project->whereLike('title', "%{$params['title']}%");
|
|
isset($params['title']) && filled($params['title']) && $project->whereLike('title', "%{$params['title']}%");
|
|
isset($params['status']) && filled($params['status']) && $project->where('status', $params['status']);
|
|
isset($params['status']) && filled($params['status']) && $project->where('status', $params['status']);
|
|
!empty($params['createTime']) && $project->whereIn('created_at', $params['createTime']);
|
|
!empty($params['createTime']) && $project->whereIn('created_at', $params['createTime']);
|
|
- $projectPage = $project->paginate($params['pageSize'], [...$this->simpleColumn, ...$this->selectColumn, ...$this->appendColumn], 'page', $params['pageNo']);
|
|
|
|
|
|
+ $projectPage = $project->select([...$this->simpleColumn, ...$this->selectColumn, ...$this->appendColumn])->selectRaw($this->selectRawColumn)->paginate($params['pageSize'], ['*'], 'page', $params['pageNo']);
|
|
return ['list' => $projectPage->items(), 'total' => $projectPage->total()];
|
|
return ['list' => $projectPage->items(), 'total' => $projectPage->total()];
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * @throws ApiException
|
|
|
|
+ */
|
|
public function createProject(array $data)
|
|
public function createProject(array $data)
|
|
{
|
|
{
|
|
$collection = collect($data);
|
|
$collection = collect($data);
|
|
- $category = $collection->forget('category');
|
|
|
|
- $project = self::toModel($collection, Project::class);
|
|
|
|
- return $project->create($project->getAttributes())->id;
|
|
|
|
|
|
+ $collection->forget('category');
|
|
|
|
+ DB::beginTransaction();
|
|
|
|
+
|
|
|
|
+ try {
|
|
|
|
+
|
|
|
|
+ // 保存服务项目
|
|
|
|
+ $project = self::toModel($collection, Project::class);
|
|
|
|
+ self::initProperty($project);
|
|
|
|
+ $project = $project->create($project->getAttributes());
|
|
|
|
+
|
|
|
|
+ // 关联服务项目所属分类
|
|
|
|
+ !empty($data['category']) && $project->saveCategory($data['category']);
|
|
|
|
+ DB::commit();
|
|
|
|
+ return $project->id;
|
|
|
|
+ } catch (Exception) {
|
|
|
|
+ DB::rollBack();
|
|
|
|
+ self::error('操作失败');
|
|
|
|
+ }
|
|
|
|
+
|
|
}
|
|
}
|
|
|
|
|
|
public function getProject(int $id)
|
|
public function getProject(int $id)
|
|
{
|
|
{
|
|
- return Project::query()->select([...$this->simpleColumn, ...$this->selectColumn])->find($id);
|
|
|
|
|
|
+ $project = Project::query()->select([...$this->simpleColumn, ...$this->selectColumn, ...$this->selectDetailColumn])->selectRaw($this->selectRawColumn)->find($id);
|
|
|
|
+ $project->category = $project->getCategory();
|
|
|
|
+ return $project;
|
|
}
|
|
}
|
|
|
|
|
|
public function updateProject(array $data, int $id): void
|
|
public function updateProject(array $data, int $id): void
|
|
{
|
|
{
|
|
- $project = self::toModel($data, Project::class);
|
|
|
|
- $project->where('id', $id)->update($project->getAttributes());
|
|
|
|
|
|
+ $collection = collect($data);
|
|
|
|
+ $collection->forget('category');
|
|
|
|
+ DB::beginTransaction();
|
|
|
|
+
|
|
|
|
+ try {
|
|
|
|
+ // 保存服务项目
|
|
|
|
+ $project = self::toModel([...$collection, 'id' => $id], Project::class);
|
|
|
|
+ self::initProperty($project);
|
|
|
|
+ $project->update($project->getAttributes());
|
|
|
|
+
|
|
|
|
+ // 清空服务项目所属分类
|
|
|
|
+ $project->deleteCategory();
|
|
|
|
+ // 关联服务项目所属分类
|
|
|
|
+ !empty($data['category']) && $project->saveCategory($data['category']);
|
|
|
|
+ DB::commit();
|
|
|
|
+ } catch (Exception) {
|
|
|
|
+ DB::rollBack();
|
|
|
|
+ self::error('操作失败');
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
public function deleteProject(int $id)
|
|
public function deleteProject(int $id)
|
|
{
|
|
{
|
|
|
|
+
|
|
$project = self::toModel(['id' => $id], Project::class);
|
|
$project = self::toModel(['id' => $id], Project::class);
|
|
|
|
+ // 清空服务项目所属分类
|
|
|
|
+ $project->deleteCategory();
|
|
return $project->delete();
|
|
return $project->delete();
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ protected static function initProperty(Project &$project): void
|
|
|
|
+ {
|
|
|
|
+ $project['price'] *= 100;
|
|
|
|
+ $project['init_price'] *= 100;
|
|
|
|
+ $project['material_price'] *= 100;
|
|
|
|
+ $project['total_sale'] = (int)($project['sale'] ?? 0 + $project['true_sale'] ?? 0);
|
|
|
|
+ $project['introduce'] = $project['introduce'] ?? '';
|
|
|
|
+ $project['explain'] = $project['explain'] ?? '';
|
|
|
|
+ $project['notice'] = $project['notice'] ?? '';
|
|
|
|
+ }
|
|
}
|
|
}
|