Reward.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. $query = RewardModel::where('agent_id', $agent->user_id)
  32. ->where('status', RewardModel::COMMISSION_REWARD_STATUS_ACCOUNTED) // 只查询已结算的佣金
  33. ->with(['commissionOrder']) // 关联佣金订单信息
  34. ->order('commission_time desc'); // 按佣金时间倒序
  35. // 年月筛选 (支持YYYY-MM格式)
  36. if (!empty($yearMonth)) {
  37. $timeWhere = $this->getYearMonthConditions($yearMonth);
  38. if ($timeWhere) {
  39. $query->where($timeWhere);
  40. }
  41. }
  42. // 分页查询
  43. $data = $query->paginate($pageSize);
  44. $paginateData = $data->toArray();
  45. // 处理佣金列表数据
  46. $this->processRewardList($paginateData['data']);
  47. // 计算summary统计信息
  48. $totalAmount = '0.00';
  49. $arrData = $paginateData['data'] ?? [];
  50. foreach ($arrData as $record) {
  51. $totalAmount = BcMath::add($totalAmount, $record['commission'], 2);
  52. }
  53. // 重新构造返回数据
  54. $result = [
  55. 'total' => $paginateData['total'],
  56. 'per_page' => $paginateData['per_page'],
  57. 'current_page' => $paginateData['current_page'],
  58. 'last_page' => $paginateData['last_page'],
  59. 'list' => $paginateData['data'],
  60. 'summary' => [
  61. 'total_amount' => BcMath::format($totalAmount),
  62. ]
  63. ];
  64. $this->success("", $result);
  65. }
  66. /**
  67. * 处理佣金列表数据
  68. * @param array $rewardList 佣金列表数据的引用
  69. * @return void
  70. */
  71. private function processRewardList(&$rewardList)
  72. {
  73. if (empty($rewardList)) {
  74. return;
  75. }
  76. foreach ($rewardList as &$reward) {
  77. // 格式化佣金金额
  78. $reward['commission'] = BcMath::format($reward['commission'] ?? '0.00');
  79. // 格式化佣金时间
  80. $reward['commission_time_text'] = !empty($reward['commission_time'])
  81. ? date('Y-m-d H:i:s', $reward['commission_time'])
  82. : '';
  83. // 添加佣金类型描述
  84. $reward['type_text'] = $this->getRewardTypeText($reward['type'] ?? '');
  85. // 添加订单信息(如果有关联订单)
  86. if (!empty($reward['commission_order'])) {
  87. $order = $reward['commission_order'];
  88. $reward['order_info'] = [
  89. 'order_id' => $order['order_id'] ?? '',
  90. 'amount' => BcMath::format($order['amount'] ?? '0.00'),
  91. 'buyer_nickname' => $order['buyer_nickname'] ?? '',
  92. ];
  93. }
  94. // 清理不需要的关联数据,避免冗余
  95. unset($reward['commission_order']);
  96. }
  97. }
  98. /**
  99. * 获取佣金类型描述
  100. * @param string $type 佣金类型
  101. * @return string
  102. */
  103. private function getRewardTypeText($type)
  104. {
  105. $typeMap = [
  106. 'money' => '现金佣金',
  107. 'score' => '积分佣金',
  108. 'commission' => '佣金收益',
  109. ];
  110. return $typeMap[$type] ?? '未知类型';
  111. }
  112. /**
  113. * 获取年月筛选条件 (与提现模块保持一致)
  114. * @param string $yearMonth 年月字符串 (YYYY-MM格式)
  115. * @return array|null 返回包含时间条件的数组
  116. */
  117. private function getYearMonthConditions($yearMonth)
  118. {
  119. if (empty($yearMonth)) {
  120. return null;
  121. }
  122. // 解析年月字符串 (YYYY-MM)
  123. if (preg_match('/^(\d{4})-(\d{2})$/', $yearMonth, $matches)) {
  124. $year = (int)$matches[1];
  125. $month = (int)$matches[2];
  126. // 按年月筛选
  127. $startTime = mktime(0, 0, 0, $month, 1, $year);
  128. $endTime = mktime(23, 59, 59, $month + 1, 0, $year);
  129. return [
  130. 'commission_time' => ['between', [$startTime, $endTime]]
  131. ];
  132. }
  133. return null;
  134. }
  135. }