133 lines
5.1 KiB
PHP
133 lines
5.1 KiB
PHP
<?php
|
||
|
||
namespace app\controller\generalization\api\v1;
|
||
|
||
use app\dao\UserDao;
|
||
use app\dao\UserRewardDao;
|
||
use support\Request;
|
||
use App\Utils\ApiResponse;
|
||
use App\model\Admin;
|
||
use app\model\Recommend;
|
||
use app\model\User;
|
||
use app\model\UserPhone;
|
||
use app\model\UserPhoneLog;
|
||
use hg\apidoc\annotation as Apidoc;
|
||
use app\model\Withdraw;
|
||
use App\Utils\Random;
|
||
use support\Db;
|
||
|
||
/**
|
||
* @Apidoc\Title("admin推广链接控制器")
|
||
* @Apidoc\Group("admin")
|
||
*/
|
||
class PromotionController
|
||
{
|
||
/**
|
||
* @Apidoc\Title("管理员生成推广链接")
|
||
* @Apidoc\Url("admin/api/v1/Promotion/Generatelink")
|
||
* @Apidoc\Method("POST")
|
||
*/
|
||
public function Generatelink(Request $request)
|
||
{
|
||
$invite_code = UserDao::Createuser();
|
||
return ApiResponse::success(200, $invite_code);
|
||
}
|
||
/**
|
||
* @Apidoc\Title("获取推广列表所有")
|
||
* @Apidoc\Url("admin/api/v1/Promotion/list")
|
||
* @Apidoc\Method("POST")
|
||
*/
|
||
public function list(Request $request)
|
||
{
|
||
// 获取请求的参数
|
||
$data = $request->post();
|
||
|
||
// 构建查询构造器
|
||
$query = User::query();
|
||
|
||
// 根据 key 进行模糊查询
|
||
if (!empty($data['key'])) {
|
||
$key = $data['key'];
|
||
$query->where(function ($query) use ($key) {
|
||
$query->where('users.invite_code', 'like', '%' . $key . '%')
|
||
->orWhere('users.remark', 'like', '%' . $key . '%');
|
||
});
|
||
}
|
||
|
||
// 根据 status 过滤,假设 status 字段存在并且不是 -1
|
||
if (isset($data['status']) && $data['status'] != -1) {
|
||
$status = (int)$data['status']; // 强制转换为整数
|
||
// 使用 users 表的别名明确指定 status 字段
|
||
$query->where('users.status', $status);
|
||
}
|
||
|
||
// 使用 join 进行关联查询 f_id 对应的用户名称
|
||
$users = $query->leftJoin('users as u', 'users.f_id', '=', 'u.id') // 假设 users 表有 id 字段,f_id 关联到父用户
|
||
->select(
|
||
'users.id',
|
||
'users.username',
|
||
'users.updated_at',
|
||
'users.invite_code',
|
||
'users.login_time',
|
||
'users.money',
|
||
'users.remark',
|
||
'users.status',
|
||
'users.vip_id',
|
||
'users.created_at',
|
||
'u.username as parent_username' // 选择所需字段,并为父用户用户名起别名
|
||
)->where('users.isrobot', 1)
|
||
->orderBy('users.id', 'desc')->get(); // 确保按 users 表的 id 排序
|
||
return ApiResponse::success(200, $users);
|
||
}
|
||
|
||
/**
|
||
* @Apidoc\Title(" 获取推广链接详细注册名单")
|
||
* @Apidoc\Url("admin/api/v1/Promotion/detailed")
|
||
* @Apidoc\Param("invite_code", type="string", require=true, desc="机器人的邀请码")
|
||
* @Apidoc\Param("start_date", type="string", require=false, desc="开始时间,格式:YYYY-MM-DD")
|
||
* @Apidoc\Param("end_date", type="string", require=false, desc="结束时间,格式:YYYY-MM-DD")
|
||
* @Apidoc\Method("POST")
|
||
*/
|
||
public function detailed(Request $request)
|
||
{
|
||
$data = $request->post();
|
||
|
||
// 获取机器人邀请码对应的用户 ID
|
||
$users = User::where('robot_invite_code', $data['invite_code'])->pluck('id')->toArray();
|
||
|
||
// 获取时间段参数,默认使用今天
|
||
$startDate = isset($data['start_date']) ? $data['start_date'] : date('Y-m-d');
|
||
$endDate = isset($data['end_date']) ? $data['end_date'] : date('Y-m-d');
|
||
|
||
// 获取用户手机号(按 user_id 分组)并根据时间范围筛选
|
||
$UserPhone = UserPhone::whereIn('user_id', $users)
|
||
->whereBetween('created_at', [$startDate . ' 00:00:00', $endDate . ' 23:59:59']) // 时间范围筛选
|
||
->select('user_id', DB::raw('count(*) as phone_count')) // 聚合查询,按 user_id 分组,计算每个 user_id 的手机数量
|
||
->groupBy('user_id')
|
||
->get();
|
||
|
||
// 获取注册的用户数量,按时间筛选
|
||
$res['register'] = User::whereIn('id', $users)
|
||
->whereBetween('created_at', [$startDate . ' 00:00:00', $endDate . ' 23:59:59']) // 时间范围筛选
|
||
->count();
|
||
|
||
// 获取绑定手机号的用户数量
|
||
$res['binding'] = count($UserPhone);
|
||
|
||
// 获取注册的用户详细信息(筛选注册时间段)
|
||
$res['register_user'] = User::whereIn('id', $users)
|
||
->whereBetween('created_at', [$startDate . ' 00:00:00', $endDate . ' 23:59:59']) // 时间范围筛选
|
||
->orderBy('id', 'desc')
|
||
->get();
|
||
|
||
// 获取绑定手机号的用户详细信息
|
||
$userIdsWithPhones = $UserPhone->pluck('user_id')->toArray(); // 获取绑定手机号的用户 ID 列表
|
||
$res['binding_user'] = User::whereIn('id', $userIdsWithPhones)
|
||
->whereBetween('created_at', [$startDate . ' 00:00:00', $endDate . ' 23:59:59']) // 时间范围筛选
|
||
->orderBy('id', 'desc')
|
||
->get();
|
||
|
||
return ApiResponse::success(200, $res);
|
||
}
|
||
}
|