<?php
declare (strict_types = 1);

namespace app\admin\controller\v1;

use app\admin\model\v1\SysOperateLog;
use app\admin\model\v1\SysUserModel;

/**
 * 操作日志控制器
 */
class OperateLog
{
    // 分页
    public function index()
    {
        $param = request()->param([
            'title',
            'operator',
            'created_at',
            'page/d',
            'size/d'
        ]);

        $logs = SysOperateLog::field([
                'SysOperateLog.*',
                'SysUserModel.username'
            ])
            ->hasWhere('user', function($query) use($param) {
                if (!empty($param['operator'])) {
                    $query->where('username', 'like', "%{$param['operator']}%");
                }
                $query->withTrashed()->field(['id', 'username']);
            })
            ->withSearch(['title', 'created_at'], [
                'title'      => $param['title'] ?? null,
                'created_at' => $param['created_at'] ?? null
            ])
            ->order(['id' => 'desc'])
            ->paginate([
                'list_rows' => $param['size'] ?? 10,
                'page'      => $param['page'] ?? 1,
            ])
            ->hidden(['user_id', 'user']);

        return success('获取成功', $logs);
    }
}
