|
@@ -19,7 +19,7 @@ class Reward extends Commission
|
|
|
{
|
|
|
$params = $this->request->param();
|
|
|
|
|
|
- // 使用验证器验证分页参数:page, page_size, time_filter, start_date, end_date
|
|
|
+ // 使用验证器验证分页参数:page, page_size, year_month
|
|
|
$validate = new \app\api\validate\Reward();
|
|
|
if (!$validate->scene('list')->check($params)) {
|
|
|
$this->error($validate->getError());
|
|
@@ -35,24 +35,19 @@ class Reward extends Commission
|
|
|
|
|
|
// 获取验证后的参数
|
|
|
$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'] : '';
|
|
|
+ $yearMonth = isset($params['year_month']) ? trim($params['year_month']) : '';
|
|
|
|
|
|
// 构建查询条件
|
|
|
- $query = RewardModel::where('agent_id', $agent->id)
|
|
|
+ $query = RewardModel::where('agent_id', $agent->user_id)
|
|
|
->where('status', RewardModel::COMMISSION_REWARD_STATUS_ACCOUNTED) // 只查询已结算的佣金
|
|
|
->with(['commissionOrder']) // 关联佣金订单信息
|
|
|
->order('commission_time desc'); // 按佣金时间倒序
|
|
|
|
|
|
- // 根据时间筛选参数添加时间条件
|
|
|
- if ($timeFilter !== 'all') {
|
|
|
- $timeConditions = $this->getTimeFilterConditions($timeFilter, $startDate, $endDate);
|
|
|
- if ($timeConditions) {
|
|
|
- $query->where('commission_time', '>=', $timeConditions['start_time']);
|
|
|
- if (isset($timeConditions['end_time'])) {
|
|
|
- $query->where('commission_time', '<=', $timeConditions['end_time']);
|
|
|
- }
|
|
|
+ // 年月筛选 (支持YYYY-MM格式)
|
|
|
+ if (!empty($yearMonth)) {
|
|
|
+ $timeWhere = $this->getYearMonthConditions($yearMonth);
|
|
|
+ if ($timeWhere) {
|
|
|
+ $query->where($timeWhere);
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -64,7 +59,11 @@ class Reward extends Commission
|
|
|
$this->processRewardList($paginateData['data']);
|
|
|
|
|
|
// 计算summary统计信息
|
|
|
- $summary = $this->calculateSummary($agent, $timeFilter, $startDate, $endDate);
|
|
|
+ $totalAmount = '0.00';
|
|
|
+ $arrData = $paginateData['data'] ?? [];
|
|
|
+ foreach ($arrData as $record) {
|
|
|
+ $totalAmount = BcMath::add($totalAmount, $record['commission'], 2);
|
|
|
+ }
|
|
|
|
|
|
// 重新构造返回数据
|
|
|
$result = [
|
|
@@ -73,7 +72,9 @@ class Reward extends Commission
|
|
|
'current_page' => $paginateData['current_page'],
|
|
|
'last_page' => $paginateData['last_page'],
|
|
|
'list' => $paginateData['data'],
|
|
|
- 'summary' => $summary
|
|
|
+ 'summary' => [
|
|
|
+ 'total_amount' => BcMath::format($totalAmount),
|
|
|
+ ]
|
|
|
];
|
|
|
|
|
|
$this->success("", $result);
|
|
@@ -134,117 +135,30 @@ class Reward extends Commission
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 计算summary统计信息
|
|
|
- * @param object $agent 当前代理商对象
|
|
|
- * @param string $timeFilter 时间筛选类型
|
|
|
- * @param string $startDate 开始日期
|
|
|
- * @param string $endDate 结束日期
|
|
|
- * @return array
|
|
|
+ * 获取年月筛选条件 (与提现模块保持一致)
|
|
|
+ * @param string $yearMonth 年月字符串 (YYYY-MM格式)
|
|
|
+ * @return array|null 返回包含时间条件的数组
|
|
|
*/
|
|
|
- private function calculateSummary($agent, $timeFilter, $startDate, $endDate)
|
|
|
+ private function getYearMonthConditions($yearMonth)
|
|
|
{
|
|
|
- // 根据筛选条件构建统计查询
|
|
|
- $query = RewardModel::where('agent_id', $agent->id)
|
|
|
- ->where('status', RewardModel::COMMISSION_REWARD_STATUS_ACCOUNTED);
|
|
|
-
|
|
|
- // 应用时间筛选
|
|
|
- if ($timeFilter !== 'all') {
|
|
|
- $timeConditions = $this->getTimeFilterConditions($timeFilter, $startDate, $endDate);
|
|
|
- if ($timeConditions) {
|
|
|
- $query->where('commission_time', '>=', $timeConditions['start_time']);
|
|
|
- if (isset($timeConditions['end_time'])) {
|
|
|
- $query->where('commission_time', '<=', $timeConditions['end_time']);
|
|
|
- }
|
|
|
- }
|
|
|
+ if (empty($yearMonth)) {
|
|
|
+ return null;
|
|
|
}
|
|
|
-
|
|
|
- // 计算筛选条件下的统计数据
|
|
|
- $totalCommission = $query->sum('commission') ?: '0.00';
|
|
|
- $totalCount = $query->count();
|
|
|
|
|
|
- // 按佣金类型统计
|
|
|
- $typeStats = $query->field('type, SUM(commission) as total_amount, COUNT(*) as count')
|
|
|
- ->group('type')
|
|
|
- ->select()
|
|
|
- ->toArray();
|
|
|
-
|
|
|
- $typeStatistics = [];
|
|
|
- foreach ($typeStats as $stat) {
|
|
|
- $typeStatistics[] = [
|
|
|
- 'type' => $stat['type'],
|
|
|
- 'type_text' => $this->getRewardTypeText($stat['type']),
|
|
|
- 'total_amount' => BcMath::format($stat['total_amount'] ?? '0.00'),
|
|
|
- 'count' => intval($stat['count'] ?? 0)
|
|
|
+ // 解析年月字符串 (YYYY-MM)
|
|
|
+ if (preg_match('/^(\d{4})-(\d{2})$/', $yearMonth, $matches)) {
|
|
|
+ $year = (int)$matches[1];
|
|
|
+ $month = (int)$matches[2];
|
|
|
+
|
|
|
+ // 按年月筛选
|
|
|
+ $startTime = mktime(0, 0, 0, $month, 1, $year);
|
|
|
+ $endTime = mktime(23, 59, 59, $month + 1, 0, $year);
|
|
|
+
|
|
|
+ return [
|
|
|
+ 'commission_time' => ['between', [$startTime, $endTime]]
|
|
|
];
|
|
|
}
|
|
|
-
|
|
|
- return [
|
|
|
- 'total_commission' => BcMath::format($totalCommission),
|
|
|
- 'total_count' => $totalCount,
|
|
|
- 'agent_total_income' => BcMath::format($agent->total_income ?? '0.00'),
|
|
|
- 'agent_withdrawn_amount' => BcMath::format($agent->withdrawn_amount ?? '0.00'),
|
|
|
- 'available_balance' => BcMath::format(BcMath::sub($agent->total_income ?? '0.00', $agent->withdrawn_amount ?? '0.00')),
|
|
|
- 'type_statistics' => $typeStatistics
|
|
|
- ];
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取时间筛选条件
|
|
|
- * @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;
|
|
|
- }
|
|
|
+ return null;
|
|
|
}
|
|
|
}
|