LogsController.php 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. namespace app\admin\controller\sys_manage;
  3. use app\admin\validate\sys_manage\AdvValidate;
  4. use app\controller\Curd;
  5. use app\model\Adv;
  6. use app\model\SysLog;
  7. use app\model\SysUser;
  8. use support\Request;
  9. use support\Response;
  10. class LogsController extends Curd
  11. {
  12. public function __construct()
  13. {
  14. $this->model = new SysLog();
  15. }
  16. /** 列表
  17. * @Desc
  18. * @Author Gorden
  19. * @Date 2024/3/5 10:00
  20. *
  21. * @param Request $request
  22. * @return Response
  23. * @throws \support\exception\BusinessException
  24. */
  25. public function select(Request $request): Response
  26. {
  27. [$where, $format, $limit, $field, $order] = $this->selectInput($request);
  28. $order = $request->get('order', 'desc');
  29. $field = $field ?? 'log_create_time';
  30. if (!empty($where['log_admin_id'])) {
  31. $userIds = SysUser::where('user_name', 'like', '%' . $where['log_admin_id'] . '%')->pluck('user_id')->toArray();
  32. $where['log_admin_id'] = ['in', $userIds];
  33. }
  34. if (!empty($where['log_create_time'])) {
  35. $where['log_create_time'][0] = strtotime($where['log_create_time'][0]);
  36. $where['log_create_time'][1] = strtotime($where['log_create_time'][1]);
  37. }
  38. if (!empty($where['log_operation'])) {
  39. $where['log_operation'] = ['like', $where['log_operation']];
  40. }
  41. $query = $this->doSelect($where, $field, $order);
  42. return $this->doFormat($query, $format, $limit);
  43. }
  44. protected function doSelect(array $where, string $field = null, string $order = 'desc')
  45. {
  46. $model = $this->model->with('user');
  47. foreach ($where as $column => $value) {
  48. if (is_array($value)) {
  49. if ($value[0] === 'like' || $value[0] === 'not like') {
  50. $model = $model->where($column, $value[0], "%$value[1]%");
  51. } elseif (in_array($value[0], ['>', '=', '<', '<>'])) {
  52. $model = $model->where($column, $value[0], $value[1]);
  53. } elseif ($value[0] == 'in' && !empty($value[1])) {
  54. $valArr = $value[1];
  55. if (is_string($value[1])) {
  56. $valArr = explode(",", trim($value[1]));
  57. }
  58. $model = $model->whereIn($column, $valArr);
  59. } elseif ($value[0] == 'not in' && !empty($value[1])) {
  60. $valArr = $value[1];
  61. if (is_string($value[1])) {
  62. $valArr = explode(",", trim($value[1]));
  63. }
  64. $model = $model->whereNotIn($column, $valArr);
  65. } elseif ($value[0] == 'null') {
  66. $model = $model->whereNull($column);
  67. } elseif ($value[0] == 'not null') {
  68. $model = $model->whereNotNull($column);
  69. } elseif ($value[0] !== '' || $value[1] !== '') {
  70. $model = $model->whereBetween($column, $value);
  71. }
  72. } else {
  73. $model = $model->where($column, $value);
  74. }
  75. }
  76. if ($field) {
  77. $model = $model->orderBy($field, $order);
  78. }
  79. return $model;
  80. }
  81. }