|
@@ -97,6 +97,7 @@ class Agent extends Commission
|
|
|
|
|
|
/**
|
|
/**
|
|
* 构造代理商身份数据(包括标题和区域信息)
|
|
* 构造代理商身份数据(包括标题和区域信息)
|
|
|
|
+ * 优化版本:直接使用数据库中存储的名称字段,无需关联查询区域表
|
|
* @param array $agentData 代理商数据
|
|
* @param array $agentData 代理商数据
|
|
* @return array
|
|
* @return array
|
|
*/
|
|
*/
|
|
@@ -115,18 +116,14 @@ class Agent extends Commission
|
|
|
|
|
|
case AgentType::PROVINCE:
|
|
case AgentType::PROVINCE:
|
|
// 省级代理商:山东省代理商
|
|
// 省级代理商:山东省代理商
|
|
- if (!empty($agentData['manage_province_id'])) {
|
|
|
|
- $provinceInfo = \app\common\model\Area::where('id', $agentData['manage_province_id'])->find();
|
|
|
|
- if ($provinceInfo) {
|
|
|
|
- $result['title'] = $provinceInfo['name'] . '代理商';
|
|
|
|
- $result['area_info'] = [
|
|
|
|
- 'province_name' => $provinceInfo['name'],
|
|
|
|
- 'province_id' => $agentData['manage_province_id']
|
|
|
|
- ];
|
|
|
|
- $result['area_text'] = $provinceInfo['name'];
|
|
|
|
- } else {
|
|
|
|
- $result['title'] = '省级代理商';
|
|
|
|
- }
|
|
|
|
|
|
+ $provinceName = $agentData['manage_province_name'] ?? '';
|
|
|
|
+ if (!empty($provinceName)) {
|
|
|
|
+ $result['title'] = $provinceName . '代理商';
|
|
|
|
+ $result['area_info'] = [
|
|
|
|
+ 'province_name' => $provinceName,
|
|
|
|
+ 'province_id' => $agentData['manage_province_id'] ?? null
|
|
|
|
+ ];
|
|
|
|
+ $result['area_text'] = $provinceName;
|
|
} else {
|
|
} else {
|
|
$result['title'] = '省级代理商';
|
|
$result['title'] = '省级代理商';
|
|
}
|
|
}
|
|
@@ -138,26 +135,20 @@ class Agent extends Commission
|
|
$areaText = [];
|
|
$areaText = [];
|
|
|
|
|
|
// 获取省份信息
|
|
// 获取省份信息
|
|
- if (!empty($agentData['manage_province_id'])) {
|
|
|
|
- $provinceInfo = \app\common\model\Area::where('id', $agentData['manage_province_id'])->find();
|
|
|
|
- if ($provinceInfo) {
|
|
|
|
- $areaInfo['province_name'] = $provinceInfo['name'];
|
|
|
|
- $areaInfo['province_id'] = $agentData['manage_province_id'];
|
|
|
|
- $areaText[] = $provinceInfo['name'];
|
|
|
|
- }
|
|
|
|
|
|
+ $provinceName = $agentData['manage_province_name'] ?? '';
|
|
|
|
+ if (!empty($provinceName)) {
|
|
|
|
+ $areaInfo['province_name'] = $provinceName;
|
|
|
|
+ $areaInfo['province_id'] = $agentData['manage_province_id'] ?? null;
|
|
|
|
+ $areaText[] = $provinceName;
|
|
}
|
|
}
|
|
|
|
|
|
// 获取城市信息
|
|
// 获取城市信息
|
|
- if (!empty($agentData['manage_city_id'])) {
|
|
|
|
- $cityInfo = \app\common\model\Area::where('id', $agentData['manage_city_id'])->find();
|
|
|
|
- if ($cityInfo) {
|
|
|
|
- $result['title'] = $cityInfo['name'] . '代理商';
|
|
|
|
- $areaInfo['city_name'] = $cityInfo['name'];
|
|
|
|
- $areaInfo['city_id'] = $agentData['manage_city_id'];
|
|
|
|
- $areaText[] = $cityInfo['name'];
|
|
|
|
- } else {
|
|
|
|
- $result['title'] = '市级代理商';
|
|
|
|
- }
|
|
|
|
|
|
+ $cityName = $agentData['manage_city_name'] ?? '';
|
|
|
|
+ if (!empty($cityName)) {
|
|
|
|
+ $result['title'] = $cityName . '代理商';
|
|
|
|
+ $areaInfo['city_name'] = $cityName;
|
|
|
|
+ $areaInfo['city_id'] = $agentData['manage_city_id'] ?? null;
|
|
|
|
+ $areaText[] = $cityName;
|
|
} else {
|
|
} else {
|
|
$result['title'] = '市级代理商';
|
|
$result['title'] = '市级代理商';
|
|
}
|
|
}
|
|
@@ -174,36 +165,28 @@ class Agent extends Commission
|
|
$areaText = [];
|
|
$areaText = [];
|
|
|
|
|
|
// 获取省份信息
|
|
// 获取省份信息
|
|
- if (!empty($agentData['manage_province_id'])) {
|
|
|
|
- $provinceInfo = \app\common\model\Area::where('id', $agentData['manage_province_id'])->find();
|
|
|
|
- if ($provinceInfo) {
|
|
|
|
- $areaInfo['province_name'] = $provinceInfo['name'];
|
|
|
|
- $areaInfo['province_id'] = $agentData['manage_province_id'];
|
|
|
|
- $areaText[] = $provinceInfo['name'];
|
|
|
|
- }
|
|
|
|
|
|
+ $provinceName = $agentData['manage_province_name'] ?? '';
|
|
|
|
+ if (!empty($provinceName)) {
|
|
|
|
+ $areaInfo['province_name'] = $provinceName;
|
|
|
|
+ $areaInfo['province_id'] = $agentData['manage_province_id'] ?? null;
|
|
|
|
+ $areaText[] = $provinceName;
|
|
}
|
|
}
|
|
|
|
|
|
// 获取城市信息
|
|
// 获取城市信息
|
|
- if (!empty($agentData['manage_city_id'])) {
|
|
|
|
- $cityInfo = \app\common\model\Area::where('id', $agentData['manage_city_id'])->find();
|
|
|
|
- if ($cityInfo) {
|
|
|
|
- $areaInfo['city_name'] = $cityInfo['name'];
|
|
|
|
- $areaInfo['city_id'] = $agentData['manage_city_id'];
|
|
|
|
- $areaText[] = $cityInfo['name'];
|
|
|
|
- }
|
|
|
|
|
|
+ $cityName = $agentData['manage_city_name'] ?? '';
|
|
|
|
+ if (!empty($cityName)) {
|
|
|
|
+ $areaInfo['city_name'] = $cityName;
|
|
|
|
+ $areaInfo['city_id'] = $agentData['manage_city_id'] ?? null;
|
|
|
|
+ $areaText[] = $cityName;
|
|
}
|
|
}
|
|
|
|
|
|
// 获取区域信息
|
|
// 获取区域信息
|
|
- if (!empty($agentData['manage_district_id'])) {
|
|
|
|
- $districtInfo = \app\common\model\Area::where('id', $agentData['manage_district_id'])->find();
|
|
|
|
- if ($districtInfo) {
|
|
|
|
- $result['title'] = $districtInfo['name'] . '代理商';
|
|
|
|
- $areaInfo['district_name'] = $districtInfo['name'];
|
|
|
|
- $areaInfo['district_id'] = $agentData['manage_district_id'];
|
|
|
|
- $areaText[] = $districtInfo['name'];
|
|
|
|
- } else {
|
|
|
|
- $result['title'] = '区域代理商';
|
|
|
|
- }
|
|
|
|
|
|
+ $districtName = $agentData['manage_district_name'] ?? '';
|
|
|
|
+ if (!empty($districtName)) {
|
|
|
|
+ $result['title'] = $districtName . '代理商';
|
|
|
|
+ $areaInfo['district_name'] = $districtName;
|
|
|
|
+ $areaInfo['district_id'] = $agentData['manage_district_id'] ?? null;
|
|
|
|
+ $areaText[] = $districtName;
|
|
} else {
|
|
} else {
|
|
$result['title'] = '区域代理商';
|
|
$result['title'] = '区域代理商';
|
|
}
|
|
}
|
|
@@ -218,13 +201,15 @@ class Agent extends Commission
|
|
return $result;
|
|
return $result;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+
|
|
|
|
+
|
|
|
|
|
|
// 我的团队
|
|
// 我的团队
|
|
public function team()
|
|
public function team()
|
|
{
|
|
{
|
|
$params = $this->request->param();
|
|
$params = $this->request->param();
|
|
|
|
|
|
- // 使用验证器验证分页参数:page, page_size
|
|
|
|
|
|
+ // 使用验证器验证分页参数:page, page_size, time_filter, start_date, end_date
|
|
$validate = new \app\api\validate\Agent();
|
|
$validate = new \app\api\validate\Agent();
|
|
if (!$validate->scene('team')->check($params)) {
|
|
if (!$validate->scene('team')->check($params)) {
|
|
$this->error($validate->getError());
|
|
$this->error($validate->getError());
|
|
@@ -232,17 +217,155 @@ class Agent extends Commission
|
|
|
|
|
|
$agentId = $this->service->user->id;
|
|
$agentId = $this->service->user->id;
|
|
|
|
|
|
- // 获取验证后的分页参数
|
|
|
|
|
|
+ // 获取验证后的参数
|
|
$pageSize = isset($params['page_size']) ? (int)$params['page_size'] : 8;
|
|
$pageSize = isset($params['page_size']) ? (int)$params['page_size'] : 8;
|
|
|
|
+ $timeFilter = isset($params['time_filter']) ? $params['time_filter'] : 'all';
|
|
|
|
+ $startDate = isset($params['start_date']) ? $params['start_date'] : '';
|
|
|
|
+ $endDate = isset($params['end_date']) ? $params['end_date'] : '';
|
|
|
|
+
|
|
|
|
+ // 构建查询条件
|
|
|
|
+ $query = UserModel::where('parent_user_id', $agentId)
|
|
|
|
+ ->where('status', 1);
|
|
|
|
+
|
|
|
|
+ // 根据时间筛选参数添加时间条件
|
|
|
|
+ if ($timeFilter !== 'all') {
|
|
|
|
+ $timeConditions = $this->getTimeFilterConditions($timeFilter, $startDate, $endDate);
|
|
|
|
+ if ($timeConditions) {
|
|
|
|
+ $query->where('bind_time', '>=', $timeConditions['start_time']);
|
|
|
|
+ if (isset($timeConditions['end_time'])) {
|
|
|
|
+ $query->where('bind_time', '<=', $timeConditions['end_time']);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
|
|
- $data = UserModel::where('parent_user_id', $agentId)
|
|
|
|
- ->where('status', 'normal')
|
|
|
|
|
|
+ $data = $query->field('id,username,nickname,avatar,mobile,bind_time,commission,order_count,total_consume,status,parent_user_id')
|
|
->with(['agent' => function ($query) {
|
|
->with(['agent' => function ($query) {
|
|
- return $query->with('level_info');
|
|
|
|
|
|
+ return $query->with('level_info')->field('user_id,agent_type,child_user_count_all,child_order_money_all');
|
|
}])
|
|
}])
|
|
->paginate($pageSize);
|
|
->paginate($pageSize);
|
|
|
|
|
|
- $this->success("", $data);
|
|
|
|
|
|
+ // 将分页对象转换为数组进行处理
|
|
|
|
+ $paginateData = $data->toArray();
|
|
|
|
+
|
|
|
|
+ // 为代理商成员添加身份标题和统计数据
|
|
|
|
+ $this->addAgentIdentityTitle($paginateData['data']);
|
|
|
|
+
|
|
|
|
+ // 获取当前代理商信息
|
|
|
|
+ $currentAgent = $this->service->agent;
|
|
|
|
+
|
|
|
|
+ // 重新构造返回数据
|
|
|
|
+ $result = [
|
|
|
|
+ 'total' => $paginateData['total'],
|
|
|
|
+ 'per_page' => $paginateData['per_page'],
|
|
|
|
+ 'current_page' => $paginateData['current_page'],
|
|
|
|
+ 'last_page' => $paginateData['last_page'],
|
|
|
|
+ 'list' => $paginateData['data'],
|
|
|
|
+ 'agent_info' => [
|
|
|
|
+ 'child_user_count_all' => intval($currentAgent['child_user_count_all'] ?? 0),
|
|
|
|
+ 'child_order_money_all' => BcMath::format($currentAgent['child_order_money_all'] ?? '0.00')
|
|
|
|
+ ]
|
|
|
|
+ ];
|
|
|
|
+
|
|
|
|
+ $this->success("", $result);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 为团队成员添加身份标题和统计数据(统一处理所有用户)
|
|
|
|
+ * @param array $teamMembers 团队成员数据数组的引用
|
|
|
|
+ * @return void
|
|
|
|
+ */
|
|
|
|
+ private function addAgentIdentityTitle(&$teamMembers)
|
|
|
|
+ {
|
|
|
|
+ if (empty($teamMembers)) {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ foreach ($teamMembers as &$member) {
|
|
|
|
+ //处理头像
|
|
|
|
+ $member['avatar'] = !empty($member['avatar']) ? cdnurl($member['avatar']) : '';
|
|
|
|
+
|
|
|
|
+ //格式化绑定时间
|
|
|
|
+ $member['bind_time_text'] = !empty($member['bind_time']) ? date('Y-m-d', $member['bind_time']) : '';
|
|
|
|
+
|
|
|
|
+ if (!empty($member['agent'])) {
|
|
|
|
+ // 代理商用户:构造具体的代理商身份标题
|
|
|
|
+ $identityData = $this->buildAgentIdentityData($member['agent']);
|
|
|
|
+ $member['agent_identity_title'] = $identityData['title'];
|
|
|
|
+
|
|
|
|
+ // 添加代理商统计数据
|
|
|
|
+ $member['order_count'] = intval($member['agent']['child_user_count_all'] ?? 0);
|
|
|
|
+ $member['total_consume'] = BcMath::format($member['agent']['child_order_money_all'] ?? '0.00');
|
|
|
|
+ } else {
|
|
|
|
+ // 普通用户:统一设置为"普通用户"
|
|
|
|
+ $member['agent_identity_title'] = '普通用户';
|
|
|
|
+
|
|
|
|
+ // 添加普通用户统计数据
|
|
|
|
+ $member['order_count'] = intval($member['order_count'] ?? 0);
|
|
|
|
+ $member['total_consume'] = BcMath::format($member['total_consume'] ?? '0.00');
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 获取时间筛选条件
|
|
|
|
+ * @param string $timeFilter 时间筛选类型
|
|
|
|
+ * @param string $startDate 开始日期 (Y-m-d格式,用于自定义时间段)
|
|
|
|
+ * @param string $endDate 结束日期 (Y-m-d格式,用于自定义时间段)
|
|
|
|
+ * @return array|null 返回包含开始时间和结束时间的数组
|
|
|
|
+ */
|
|
|
|
+ private function getTimeFilterConditions($timeFilter, $startDate = '', $endDate = '')
|
|
|
|
+ {
|
|
|
|
+ $currentTime = time();
|
|
|
|
+
|
|
|
|
+ switch ($timeFilter) {
|
|
|
|
+ case 'recent_7_days':
|
|
|
|
+ return [
|
|
|
|
+ 'start_time' => $currentTime - 7 * 24 * 3600,
|
|
|
|
+ ];
|
|
|
|
+
|
|
|
|
+ case 'recent_30_days':
|
|
|
|
+ return [
|
|
|
|
+ 'start_time' => $currentTime - 30 * 24 * 3600,
|
|
|
|
+ ];
|
|
|
|
+
|
|
|
|
+ case 'recent_90_days':
|
|
|
|
+ return [
|
|
|
|
+ 'start_time' => $currentTime - 90 * 24 * 3600,
|
|
|
|
+ ];
|
|
|
|
+
|
|
|
|
+ case 'current_year':
|
|
|
|
+ // 当前年份的开始时间
|
|
|
|
+ $yearStart = strtotime(date('Y-01-01 00:00:00'));
|
|
|
|
+ // 当前年份的结束时间
|
|
|
|
+ $yearEnd = strtotime(date('Y-12-31 23:59:59'));
|
|
|
|
+ return [
|
|
|
|
+ 'start_time' => $yearStart,
|
|
|
|
+ 'end_time' => $yearEnd,
|
|
|
|
+ ];
|
|
|
|
+
|
|
|
|
+ case 'custom':
|
|
|
|
+ // 自定义时间段
|
|
|
|
+ if (empty($startDate) || empty($endDate)) {
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ $startTime = strtotime($startDate . ' 00:00:00');
|
|
|
|
+ $endTime = strtotime($endDate . ' 23:59:59');
|
|
|
|
+
|
|
|
|
+ if ($startTime === false || $endTime === false || $startTime > $endTime) {
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return [
|
|
|
|
+ 'start_time' => $startTime,
|
|
|
|
+ 'end_time' => $endTime,
|
|
|
|
+ ];
|
|
|
|
+
|
|
|
|
+ default:
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
// 佣金转余额/提现
|
|
// 佣金转余额/提现
|