Reward.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. namespace app\api\controller\commission;
  3. use app\common\model\commission\Reward as RewardModel;
  4. use app\common\model\commission\Agent as AgentModel;
  5. use app\common\library\BcMath;
  6. class Reward extends Commission
  7. {
  8. protected $noNeedLogin = [];
  9. protected $noNeedRight = ['*'];
  10. /**
  11. * 分销商佣金列表
  12. */
  13. public function list()
  14. {
  15. $params = $this->request->param();
  16. // 使用验证器验证分页参数:page, page_size, year_month
  17. $validate = new \app\api\validate\Reward();
  18. if (!$validate->scene('list')->check($params)) {
  19. $this->error($validate->getError());
  20. }
  21. $agentId = $this->service->user->id;
  22. // 验证当前用户是否为代理商
  23. $agent = $this->service->agent;
  24. if (!$agent) {
  25. $this->error('您还不是分销商');
  26. }
  27. // 获取验证后的参数
  28. $pageSize = isset($params['page_size']) ? (int)$params['page_size'] : 8;
  29. $yearMonth = isset($params['year_month']) ? trim($params['year_month']) : '';
  30. // 构建查询条件数组
  31. $whereConditions = [
  32. 'agent_id' => $agent->user_id,
  33. 'status' => RewardModel::COMMISSION_REWARD_STATUS_ACCOUNTED // 只查询已结算的佣金
  34. ];
  35. // 年月筛选 (支持YYYY-MM格式)
  36. if (!empty($yearMonth)) {
  37. $timeWhere = $this->getYearMonthConditions($yearMonth);
  38. if ($timeWhere) {
  39. $whereConditions = array_merge($whereConditions, $timeWhere);
  40. }
  41. }
  42. // 使用数组构造查询
  43. $query = RewardModel::where($whereConditions)
  44. ->with(['commissionOrder']) // 关联佣金订单信息
  45. ->order('commission_time desc'); // 按佣金时间倒序
  46. // 分页查询
  47. $data = $query->paginate($pageSize);
  48. $paginateData = $data->toArray();
  49. // 处理佣金列表数据
  50. $this->processRewardList($paginateData['data']);
  51. // 计算summary统计信息
  52. $totalAmount = '0.00';
  53. $arrData = $paginateData['data'] ?? [];
  54. foreach ($arrData as $record) {
  55. $totalAmount = BcMath::add($totalAmount, $record['commission'], 2);
  56. }
  57. // 重新构造返回数据
  58. $result = [
  59. 'total' => $paginateData['total'],
  60. 'per_page' => $paginateData['per_page'],
  61. 'current_page' => $paginateData['current_page'],
  62. 'last_page' => $paginateData['last_page'],
  63. 'list' => $paginateData['data'],
  64. 'summary' => [
  65. 'total_amount' => BcMath::format($totalAmount),
  66. ]
  67. ];
  68. $this->success("", $result);
  69. }
  70. /**
  71. * 处理佣金列表数据
  72. * @param array $rewardList 佣金列表数据的引用
  73. * @return void
  74. */
  75. private function processRewardList(&$rewardList)
  76. {
  77. if (empty($rewardList)) {
  78. return;
  79. }
  80. foreach ($rewardList as &$reward) {
  81. // 格式化佣金金额
  82. $reward['commission'] = BcMath::format($reward['commission'] ?? '0.00');
  83. // 格式化佣金时间
  84. $reward['commission_time_text'] = !empty($reward['commission_time'])
  85. ? date('Y-m-d H:i:s', $reward['commission_time'])
  86. : '';
  87. // 添加佣金类型描述
  88. $reward['type_text'] = $this->getRewardTypeText($reward['type'] ?? '');
  89. // 添加订单信息(如果有关联订单)
  90. if (!empty($reward['commission_order'])) {
  91. $order = $reward['commission_order'];
  92. $reward['order_info'] = [
  93. 'order_id' => $order['order_id'] ?? '',
  94. 'amount' => BcMath::format($order['amount'] ?? '0.00'),
  95. 'buyer_nickname' => $order['buyer_nickname'] ?? '',
  96. ];
  97. }
  98. // 清理不需要的关联数据,避免冗余
  99. unset($reward['commission_order']);
  100. }
  101. }
  102. /**
  103. * 获取佣金类型描述
  104. * @param string $type 佣金类型
  105. * @return string
  106. */
  107. private function getRewardTypeText($type)
  108. {
  109. $typeMap = [
  110. 'money' => '现金佣金',
  111. 'score' => '积分佣金',
  112. 'commission' => '佣金收益',
  113. ];
  114. return $typeMap[$type] ?? '未知类型';
  115. }
  116. /**
  117. * 获取年月筛选条件 (与提现模块保持一致)
  118. * @param string $yearMonth 年月字符串 (YYYY-MM格式)
  119. * @return array|null 返回包含时间条件的数组
  120. */
  121. private function getYearMonthConditions($yearMonth)
  122. {
  123. if (empty($yearMonth)) {
  124. return null;
  125. }
  126. // 解析年月字符串 (YYYY-MM)
  127. if (preg_match('/^(\d{4})-(\d{2})$/', $yearMonth, $matches)) {
  128. $year = (int)$matches[1];
  129. $month = (int)$matches[2];
  130. // 按年月筛选
  131. $startTime = mktime(0, 0, 0, $month, 1, $year);
  132. $endTime = mktime(23, 59, 59, $month + 1, 0, $year);
  133. return [
  134. 'commission_time' => ['between', [$startTime, $endTime]]
  135. ];
  136. }
  137. return null;
  138. }
  139. }