DevLogic.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. namespace App\Module\Dev\Logics;
  3. use App\Module\Dev\Events\DevLogCreatedEvent;
  4. use App\Module\Dev\Models\Dev;
  5. use App\Module\Dev\Models\DevLog;
  6. use Illuminate\Support\Facades\DB;
  7. use Illuminate\Support\Facades\Log;
  8. use Illuminate\Support\Facades\Request;
  9. /**
  10. * 开发工具逻辑类
  11. *
  12. * 处理开发工具相关的业务逻辑
  13. */
  14. class DevLogic
  15. {
  16. /**
  17. * 获取开发工具列表
  18. *
  19. * @param array $params 查询参数
  20. * @return \Illuminate\Pagination\LengthAwarePaginator
  21. */
  22. public static function getDevTools(array $params = [])
  23. {
  24. $query = Dev::query();
  25. if (!empty($params['name'])) {
  26. $query->where('name', 'like', '%' . $params['name'] . '%');
  27. }
  28. if (isset($params['status'])) {
  29. $query->where('status', $params['status']);
  30. }
  31. return $query->paginate($params['per_page'] ?? 15);
  32. }
  33. /**
  34. * 创建开发工具
  35. *
  36. * @param array $data 开发工具数据
  37. * @return Dev
  38. */
  39. public static function createDevTool(array $data): Dev
  40. {
  41. try {
  42. return DB::transaction(function () use ($data) {
  43. return Dev::create($data);
  44. });
  45. } catch (\Exception $e) {
  46. Log::error('创建开发工具失败', [
  47. 'error' => $e->getMessage(),
  48. 'data' => $data
  49. ]);
  50. throw $e;
  51. }
  52. }
  53. /**
  54. * 更新开发工具
  55. *
  56. * @param int $id 开发工具ID
  57. * @param array $data 开发工具数据
  58. * @return bool
  59. */
  60. public static function updateDevTool(int $id, array $data): bool
  61. {
  62. try {
  63. return DB::transaction(function () use ($id, $data) {
  64. $devTool = Dev::find($id);
  65. if (!$devTool) {
  66. return false;
  67. }
  68. return $devTool->update($data);
  69. });
  70. } catch (\Exception $e) {
  71. Log::error('更新开发工具失败', [
  72. 'error' => $e->getMessage(),
  73. 'id' => $id,
  74. 'data' => $data
  75. ]);
  76. return false;
  77. }
  78. }
  79. /**
  80. * 删除开发工具
  81. *
  82. * @param int $id 开发工具ID
  83. * @return bool
  84. */
  85. public static function deleteDevTool(int $id): bool
  86. {
  87. try {
  88. return DB::transaction(function () use ($id) {
  89. $devTool = Dev::find($id);
  90. if (!$devTool) {
  91. return false;
  92. }
  93. return $devTool->delete();
  94. });
  95. } catch (\Exception $e) {
  96. Log::error('删除开发工具失败', [
  97. 'error' => $e->getMessage(),
  98. 'id' => $id
  99. ]);
  100. return false;
  101. }
  102. }
  103. /**
  104. * 记录开发日志
  105. *
  106. * @param string $type 日志类型
  107. * @param string $content 日志内容
  108. * @param array $extraData 额外数据
  109. * @return DevLog
  110. */
  111. public static function log(string $type, string $content, array $extraData = []): DevLog
  112. {
  113. try {
  114. $data = [
  115. 'type' => $type,
  116. 'content' => $content,
  117. 'ip' => Request::ip(),
  118. 'user_id' => auth()->id() ?? 0,
  119. 'user_agent' => Request::userAgent(),
  120. 'extra_data' => $extraData,
  121. ];
  122. $log = DevLog::create($data);
  123. // 触发日志创建事件
  124. event(new DevLogCreatedEvent($log));
  125. return $log;
  126. } catch (\Exception $e) {
  127. Log::error('记录开发日志失败', [
  128. 'error' => $e->getMessage(),
  129. 'type' => $type,
  130. 'content' => $content,
  131. 'extra_data' => $extraData
  132. ]);
  133. // 创建失败时,返回一个空的日志对象
  134. return new DevLog();
  135. }
  136. }
  137. }