| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- <?php
- namespace App\Module\Dev\Logics;
- use App\Module\Dev\Events\DevLogCreatedEvent;
- use App\Module\Dev\Models\Dev;
- use App\Module\Dev\Models\DevLog;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- use Illuminate\Support\Facades\Request;
- /**
- * 开发工具逻辑类
- *
- * 处理开发工具相关的业务逻辑
- */
- class DevLogic
- {
- /**
- * 获取开发工具列表
- *
- * @param array $params 查询参数
- * @return \Illuminate\Pagination\LengthAwarePaginator
- */
- public static function getDevTools(array $params = [])
- {
- $query = Dev::query();
- if (!empty($params['name'])) {
- $query->where('name', 'like', '%' . $params['name'] . '%');
- }
- if (isset($params['status'])) {
- $query->where('status', $params['status']);
- }
- return $query->paginate($params['per_page'] ?? 15);
- }
- /**
- * 创建开发工具
- *
- * @param array $data 开发工具数据
- * @return Dev
- */
- public static function createDevTool(array $data): Dev
- {
- try {
- return DB::transaction(function () use ($data) {
- return Dev::create($data);
- });
- } catch (\Exception $e) {
- Log::error('创建开发工具失败', [
- 'error' => $e->getMessage(),
- 'data' => $data
- ]);
- throw $e;
- }
- }
- /**
- * 更新开发工具
- *
- * @param int $id 开发工具ID
- * @param array $data 开发工具数据
- * @return bool
- */
- public static function updateDevTool(int $id, array $data): bool
- {
- try {
- return DB::transaction(function () use ($id, $data) {
- $devTool = Dev::find($id);
- if (!$devTool) {
- return false;
- }
- return $devTool->update($data);
- });
- } catch (\Exception $e) {
- Log::error('更新开发工具失败', [
- 'error' => $e->getMessage(),
- 'id' => $id,
- 'data' => $data
- ]);
- return false;
- }
- }
- /**
- * 删除开发工具
- *
- * @param int $id 开发工具ID
- * @return bool
- */
- public static function deleteDevTool(int $id): bool
- {
- try {
- return DB::transaction(function () use ($id) {
- $devTool = Dev::find($id);
- if (!$devTool) {
- return false;
- }
- return $devTool->delete();
- });
- } catch (\Exception $e) {
- Log::error('删除开发工具失败', [
- 'error' => $e->getMessage(),
- 'id' => $id
- ]);
- return false;
- }
- }
- /**
- * 记录开发日志
- *
- * @param string $type 日志类型
- * @param string $content 日志内容
- * @param array $extraData 额外数据
- * @return DevLog
- */
- public static function log(string $type, string $content, array $extraData = []): DevLog
- {
- try {
- $data = [
- 'type' => $type,
- 'content' => $content,
- 'ip' => Request::ip(),
- 'user_id' => auth()->id() ?? 0,
- 'user_agent' => Request::userAgent(),
- 'extra_data' => $extraData,
- ];
- $log = DevLog::create($data);
- // 触发日志创建事件
- event(new DevLogCreatedEvent($log));
- return $log;
- } catch (\Exception $e) {
- Log::error('记录开发日志失败', [
- 'error' => $e->getMessage(),
- 'type' => $type,
- 'content' => $content,
- 'extra_data' => $extraData
- ]);
- // 创建失败时,返回一个空的日志对象
- return new DevLog();
- }
- }
- }
|