123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- <?php
- namespace app\api\controller\commission;
- use app\common\model\commission\Reward as RewardModel;
- use app\common\model\commission\Agent as AgentModel;
- use app\common\library\BcMath;
- class Reward extends Commission
- {
- protected $noNeedLogin = [];
- protected $noNeedRight = ['*'];
- /**
- * 分销商佣金列表
- */
- public function list()
- {
- $params = $this->request->param();
-
- // 使用验证器验证分页参数:page, page_size, year_month
- $validate = new \app\api\validate\Reward();
- if (!$validate->scene('list')->check($params)) {
- $this->error($validate->getError());
- }
-
- $agentId = $this->service->user->id;
-
- // 验证当前用户是否为代理商
- $agent = $this->service->agent;
- if (!$agent) {
- $this->error('您还不是分销商');
- }
-
- // 获取验证后的参数
- $pageSize = isset($params['page_size']) ? (int)$params['page_size'] : 8;
- $yearMonth = isset($params['year_month']) ? trim($params['year_month']) : '';
- // 构建查询条件数组
- $whereConditions = [
- 'agent_id' => $agent->user_id,
- 'status' => RewardModel::COMMISSION_REWARD_STATUS_ACCOUNTED // 只查询已结算的佣金
- ];
- // 年月筛选 (支持YYYY-MM格式)
- if (!empty($yearMonth)) {
- $timeWhere = $this->getYearMonthConditions($yearMonth);
- if ($timeWhere) {
- $whereConditions = array_merge($whereConditions, $timeWhere);
- }
- }
- // 使用数组构造查询
- $query = RewardModel::where($whereConditions)
- ->with(['commissionOrder']) // 关联佣金订单信息
- ->order('commission_time desc'); // 按佣金时间倒序
- // 分页查询
- $data = $query->paginate($pageSize);
- $paginateData = $data->toArray();
-
- // 处理佣金列表数据
- $this->processRewardList($paginateData['data']);
-
- // 计算summary统计信息
- $totalAmount = '0.00';
- $arrData = $paginateData['data'] ?? [];
- foreach ($arrData as $record) {
- $totalAmount = BcMath::add($totalAmount, $record['commission'], 2);
- }
-
- // 重新构造返回数据
- $result = [
- 'total' => $paginateData['total'],
- 'per_page' => $paginateData['per_page'],
- 'current_page' => $paginateData['current_page'],
- 'last_page' => $paginateData['last_page'],
- 'list' => $paginateData['data'],
- 'summary' => [
- 'total_amount' => BcMath::format($totalAmount),
- ]
- ];
- $this->success("", $result);
- }
- /**
- * 处理佣金列表数据
- * @param array $rewardList 佣金列表数据的引用
- * @return void
- */
- private function processRewardList(&$rewardList)
- {
- if (empty($rewardList)) {
- return;
- }
- foreach ($rewardList as &$reward) {
- // 格式化佣金金额
- $reward['commission'] = BcMath::format($reward['commission'] ?? '0.00');
-
- // 格式化佣金时间
- $reward['commission_time_text'] = !empty($reward['commission_time'])
- ? date('Y-m-d H:i:s', $reward['commission_time'])
- : '';
-
- // 添加佣金类型描述
- $reward['type_text'] = $this->getRewardTypeText($reward['type'] ?? '');
-
- // 添加订单信息(如果有关联订单)
- if (!empty($reward['commission_order'])) {
- $order = $reward['commission_order'];
- $reward['order_info'] = [
- 'order_id' => $order['order_id'] ?? '',
- 'amount' => BcMath::format($order['amount'] ?? '0.00'),
- 'buyer_nickname' => $order['buyer_nickname'] ?? '',
- ];
- }
-
- // 清理不需要的关联数据,避免冗余
- unset($reward['commission_order']);
- }
- }
- /**
- * 获取佣金类型描述
- * @param string $type 佣金类型
- * @return string
- */
- private function getRewardTypeText($type)
- {
- $typeMap = [
- 'money' => '现金佣金',
- 'score' => '积分佣金',
- 'commission' => '佣金收益',
- ];
- return $typeMap[$type] ?? '未知类型';
- }
- /**
- * 获取年月筛选条件 (与提现模块保持一致)
- * @param string $yearMonth 年月字符串 (YYYY-MM格式)
- * @return array|null 返回包含时间条件的数组
- */
- private function getYearMonthConditions($yearMonth)
- {
- if (empty($yearMonth)) {
- return null;
- }
-
- // 解析年月字符串 (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 null;
- }
- }
|