LogsController.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. $query = $this->doSelect($where, $field, $order);
  39. return $this->doFormat($query, $format, $limit);
  40. }
  41. protected function doSelect(array $where, string $field = null, string $order = 'desc')
  42. {
  43. $model = $this->model->with('user');
  44. foreach ($where as $column => $value) {
  45. if (is_array($value)) {
  46. if ($value[0] === 'like' || $value[0] === 'not like') {
  47. $model = $model->where($column, $value[0], "%$value[1]%");
  48. } elseif (in_array($value[0], ['>', '=', '<', '<>'])) {
  49. $model = $model->where($column, $value[0], $value[1]);
  50. } elseif ($value[0] == 'in' && !empty($value[1])) {
  51. $valArr = $value[1];
  52. if (is_string($value[1])) {
  53. $valArr = explode(",", trim($value[1]));
  54. }
  55. $model = $model->whereIn($column, $valArr);
  56. } elseif ($value[0] == 'not in' && !empty($value[1])) {
  57. $valArr = $value[1];
  58. if (is_string($value[1])) {
  59. $valArr = explode(",", trim($value[1]));
  60. }
  61. $model = $model->whereNotIn($column, $valArr);
  62. } elseif ($value[0] == 'null') {
  63. $model = $model->whereNull($column);
  64. } elseif ($value[0] == 'not null') {
  65. $model = $model->whereNotNull($column);
  66. } elseif ($value[0] !== '' || $value[1] !== '') {
  67. $model = $model->whereBetween($column, $value);
  68. }
  69. } else {
  70. $model = $model->where($column, $value);
  71. }
  72. }
  73. if ($field) {
  74. $model = $model->orderBy($field, $order);
  75. }
  76. return $model;
  77. }
  78. }