<?php

namespace app\controller\api\v1;

use support\Request;
use hg\apidoc\annotation as Apidoc;

use App\Utils\ApiResponse;

use App\model\Jobuser;
// use App\dao\UserDao;

/**
 * @Apidoc\Title("JOB用户控制器")
 */
class JobuserController
{
    /**
     * @Apidoc\Title("获取JOB用户列表")
     * @Apidoc\Url("/api/v1/jobuser/lists")
     * @Apidoc\Method("POST")
     * @Apidoc\Param(name="black", type="string", desc="是否黑户", required=false)
     * @Apidoc\Param(name="username", type="string", desc="账号", required=false)
     */
    public function lists(Request $request)
    {
        // 获取请求体中的参数
        $params = $request->all();

        // 初始化查询构造器
        $query = Jobuser::query();


        if ($params['black'] === null) {
            // 查询 black 字段为 null 的记录
            $query->whereNull('black');
        } else {
            // 查询 black 字段等于指定值的记录
            $query->where('black', $params['black']);
        }

        $query->when(!empty($params['username']), function ($query) use ($params) {
            $query->where('username', 'like', '%' . $params['username'] . '%');
        });
        $db = $query->get();
        return ApiResponse::success(200, $db);
    }

    /**
     * @Apidoc\Title("拉黑账户")
     * @Apidoc\Url("/api/v1/jobuser/getBlack")
     * @Apidoc\Method("POST")
     * @Apidoc\Query("id", type="string",require=true, desc="账户id",default="1")
     */
    public function getBlack(Request $request)
    {
        $data = $request->post();
        //根据id更改数据black字段的值
        $db = Jobuser::where('id', $data['id'])->update(['black' => 1]);
        //操作不成功
        if (!$db) {
            return ApiResponse::error(402, '操作失败');
        }
        return ApiResponse::success(200, $db);
    }

    /**
     * @Apidoc\Title("洗白账户")
     * @Apidoc\Url("/api/v1/jobuser/getOutBlack")
     * @Apidoc\Method("POST")
     * @Apidoc\Query("id", type="string",require=true, desc="账户id",default="1")
     */
    public function getOutBlack(Request $request)
    {
        $data = $request->post();
        //根据id更改数据black字段的值
        $db = Jobuser::where('id', $data['id'])->update(['black' => null]);
        //操作不成功
        if (!$db) {
            return ApiResponse::error(402, '操作失败');
        }
        return ApiResponse::success(200, $db);
    }

    /**
     * @Apidoc\Title("根据id找账户")
     * @Apidoc\Url("/api/v1/jobuser/getUserById")
     * @Apidoc\Method("POST")
     * @Apidoc\Query("id", type="string",require=true, desc="账户id",default="1")
     */
    public function getUserById(Request $request)
    {
        $data = $request->post();
        //根据id查找用户
        $db = Jobuser::where('user_id', $data['id'])->first();
        //操作不成功
        if (!$db) {
            return ApiResponse::error(402, '操作失败');
        }
        return ApiResponse::success(200, $db);
    }
}
