<?php

namespace app\admin\controller\sys_manage;

use app\admin\validate\sys_manage\AdvValidate;
use app\controller\Curd;
use app\model\Adv;
use app\model\SysLog;
use app\model\SysUser;
use support\Request;
use support\Response;

class LogsController extends Curd
{
    public function __construct()
    {
        $this->model = new SysLog();
    }

    /** 列表
     * @Desc
     * @Author Gorden
     * @Date 2024/3/5 10:00
     *
     * @param Request $request
     * @return Response
     * @throws \support\exception\BusinessException
     */
    public function select(Request $request): Response
    {
        [$where, $format, $limit, $field, $order] = $this->selectInput($request);
        $order = $request->get('order', 'desc');
        $field = $field ?? 'log_create_time';

        if (!empty($where['log_admin_id'])) {
            $userIds = SysUser::where('user_name', 'like', '%' . $where['log_admin_id'] . '%')->pluck('user_id')->toArray();
            $where['log_admin_id'] = ['in', $userIds];
        }
        if (!empty($where['log_create_time'])) {
            $where['log_create_time'][0] = strtotime($where['log_create_time'][0]);
            $where['log_create_time'][1] = strtotime($where['log_create_time'][1]);
        }
        if (!empty($where['log_operation'])) {
            $where['log_operation'] = ['like', $where['log_operation']];
        }

        $query = $this->doSelect($where, $field, $order);
        return $this->doFormat($query, $format, $limit);
    }

    protected function doSelect(array $where, string $field = null, string $order = 'desc')
    {
        $model = $this->model->with('user');
        foreach ($where as $column => $value) {
            if (is_array($value)) {
                if ($value[0] === 'like' || $value[0] === 'not like') {
                    $model = $model->where($column, $value[0], "%$value[1]%");
                } elseif (in_array($value[0], ['>', '=', '<', '<>'])) {
                    $model = $model->where($column, $value[0], $value[1]);
                } elseif ($value[0] == 'in' && !empty($value[1])) {
                    $valArr = $value[1];
                    if (is_string($value[1])) {
                        $valArr = explode(",", trim($value[1]));
                    }
                    $model = $model->whereIn($column, $valArr);
                } elseif ($value[0] == 'not in' && !empty($value[1])) {
                    $valArr = $value[1];
                    if (is_string($value[1])) {
                        $valArr = explode(",", trim($value[1]));
                    }
                    $model = $model->whereNotIn($column, $valArr);
                } elseif ($value[0] == 'null') {
                    $model = $model->whereNull($column);
                } elseif ($value[0] == 'not null') {
                    $model = $model->whereNotNull($column);
                } elseif ($value[0] !== '' || $value[1] !== '') {
                    $model = $model->whereBetween($column, $value);
                }
            } else {
                $model = $model->where($column, $value);
            }
        }
        if ($field) {
            $model = $model->orderBy($field, $order);
        }
        return $model;
    }
}