You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

80 lines
2.3 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
namespace App\Http\Controllers\Web;
use App\Http\Controllers\Controller;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
class CompanyController extends Controller
{
/**
* 显示用户列表页面(有公司名称并且报名审核通过的用户)
*/
public function search()
{
// 查询有公司名称并且报名审核通过的用户排除已匹配到公司的用户company_id > 0
$users = User::whereNotNull('company_name')
->where('company_name', '!=', '')
->where(function ($query) {
$query->whereNull('company_id')
->orWhere('company_id', '<=', 0);
})
->whereHas('courseSigns', function ($query) {
$query->where('status', 1); // 审核通过
})
->select('id', 'username', 'company_name', 'company_id')
->orderBy('id', 'desc')
->get();
$totalCount = $users->count();
return view('company_search', compact('users', 'totalCount'));
}
/**
* 更新用户公司信息
*/
public function updateUserCompany(Request $request)
{
$validator = Validator::make($request->all(), [
'user_id' => 'required|integer|exists:users,id',
'company_name' => 'required|string',
]);
if ($validator->fails()) {
return response()->json([
'code' => 400,
'msg' => $validator->errors()->first()
], 400);
}
try {
$user = User::find($request->user_id);
if (!$user) {
return response()->json([
'code' => 404,
'msg' => '用户不存在'
], 404);
}
$user->company_name = $request->company_name;
$user->company_id = null; // 设置 company_id 为 null
$user->save();
return response()->json([
'code' => 200,
'msg' => '更新成功',
'data' => $user
]);
} catch (\Exception $e) {
return response()->json([
'code' => 500,
'msg' => '更新失败: ' . $e->getMessage()
], 500);
}
}
}