ViewService.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. namespace App\Services;
  3. use Illuminate\Support\Facades\DB;
  4. use Illuminate\Support\Facades\Log;
  5. /**
  6. * 视图管理
  7. */
  8. class ViewService
  9. {
  10. /**
  11. * 获取视图列表
  12. *
  13. * @param string $viewName 视图名称
  14. * @param array $whereParams 查询参数
  15. * @param array $pageParams 分页参数
  16. * @param array $sortParams 排序参数
  17. */
  18. public function viewList($viewName, array $whereParams, array $pageParams, array $sortParams = []): array
  19. {
  20. try {
  21. $query = DB::table($viewName);
  22. // 条件筛选
  23. foreach ($whereParams as $key => $value) {
  24. if (is_string($value)) {
  25. $query->where($key, 'like', "%{$value}%");
  26. } else {
  27. $query->where($key, $value);
  28. }
  29. }
  30. // 排序
  31. if (! empty($sortParams)) {
  32. $query->orderBy($sortParams['orderBy'], $sortParams['orderDir']);
  33. }
  34. $result = $query->paginate($pageParams['perPage'], ['*'], 'page', $pageParams['page']);
  35. return [
  36. 'data' => [
  37. 'items' => $result->items(),
  38. 'total' => $result->total(),
  39. ],
  40. 'msg' => 'success',
  41. 'status' => 0,
  42. ];
  43. } catch (\Exception $e) {
  44. Log::error('获取视图数据失败', [
  45. 'view_name' => $viewName,
  46. 'error' => $e->getMessage(),
  47. 'trace' => $e->getTraceAsString(),
  48. ]);
  49. return [
  50. 'data' => [
  51. 'items' => [],
  52. ],
  53. 'msg' => '获取数据失败',
  54. 'status' => 1,
  55. ];
  56. }
  57. }
  58. }