DistributionOrderClass.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. /**
  3. * 用途:订单分销记录业务(xhDistributionOrder)
  4. * 谁用:拉新客户-查看贡献明细(分红明细页)
  5. */
  6. namespace bizHd\distribution\classes;
  7. use bizHd\base\classes\BaseClass;
  8. use common\components\dateUtil;
  9. use Yii;
  10. use yii\db\Query;
  11. class DistributionOrderClass extends BaseClass
  12. {
  13. public static $baseFile = '\bizHd\distribution\models\DistributionOrder';
  14. /**
  15. * 分销员分红订单列表(xhDistributionOrder)
  16. * @param int $shopId
  17. * @param int $distId 获佣分销员 xhCustom.id
  18. * @param int $buyerId 下单客户 xhCustom.id,0 表示不限下线
  19. * @return array
  20. * @throws \Exception
  21. */
  22. public static function getContribOrderList($shopId, $distId, $buyerId = 0)
  23. {
  24. $shopId = (int)$shopId;
  25. $distId = (int)$distId;
  26. $buyerId = (int)$buyerId;
  27. if ($distId < 0) {
  28. throw new \Exception('参数无效');
  29. }
  30. $get = Yii::$app->request->get();
  31. $settleStatus = isset($get['settleStatus']) ? trim($get['settleStatus']) : '';
  32. $keyword = isset($get['keyword']) ? trim($get['keyword']) : '';
  33. $searchTime = isset($get['searchTime']) ? trim($get['searchTime']) : 'thisMonth';
  34. $startTime = isset($get['startTime']) ? trim($get['startTime']) : '';
  35. $endTime = isset($get['endTime']) ? trim($get['endTime']) : '';
  36. $period = dateUtil::formatTime($searchTime, $startTime, $endTime);
  37. // 本月分红合计(同筛选月份,不受 settleStatus Tab 影响)
  38. $monthQuery = (new Query())
  39. ->from('xhDistributionOrder')
  40. ->where(['shopId' => $shopId]);
  41. if ($distId > 0) {
  42. $monthQuery->andWhere(['distId' => $distId]);
  43. }
  44. if ($buyerId > 0) {
  45. $monthQuery->andWhere(['buyerId' => $buyerId]);
  46. }
  47. if (!empty($period['startTime']) && !empty($period['endTime'])) {
  48. $monthQuery->andWhere(['between', 'orderTime', $period['startTime'], $period['endTime']]);
  49. }
  50. $monthCommission = (float)$monthQuery->sum('commissionAmount');
  51. // Base::conditionQuery 不支持 or 组合,关键词搜索改用 Query 分页
  52. $page = isset($get['page']) ? max(1, (int)$get['page']) : 1;
  53. $pageSize = isset($get['pageSize']) && (int)$get['pageSize'] > 0
  54. ? (int)$get['pageSize']
  55. : (int)Yii::$app->params['pageSize'];
  56. $query = (new Query())
  57. ->from('xhDistributionOrder')
  58. ->where(['shopId' => $shopId]);
  59. if ($distId > 0) {
  60. $query->andWhere(['distId' => $distId]);
  61. }
  62. if ($buyerId > 0) {
  63. $query->andWhere(['buyerId' => $buyerId]);
  64. }
  65. if ($settleStatus !== '' && $settleStatus !== 'all') {
  66. $query->andWhere(['settleStatus' => (int)$settleStatus]);
  67. }
  68. if (!empty($period['startTime']) && !empty($period['endTime'])) {
  69. $query->andWhere(['between', 'orderTime', $period['startTime'], $period['endTime']]);
  70. }
  71. if ($keyword !== '') {
  72. $query->andWhere([
  73. 'or',
  74. ['like', 'orderSn', $keyword],
  75. ['like', 'buyerName', $keyword],
  76. ['like', 'distName', $keyword],
  77. ]);
  78. }
  79. $total = (int)(clone $query)->count('*', Yii::$app->db);
  80. $totalPage = $pageSize > 0 ? (int)ceil($total / $pageSize) : 0;
  81. $rows = $query
  82. ->orderBy(['orderTime' => SORT_DESC, 'id' => SORT_DESC])
  83. ->offset(($page - 1) * $pageSize)
  84. ->limit($pageSize)
  85. ->all(Yii::$app->db);
  86. $list = [];
  87. foreach ($rows as $row) {
  88. $list[] = self::formatContribOrderRow($row);
  89. }
  90. return [
  91. 'list' => $list,
  92. 'totalNum' => $total,
  93. 'totalPage' => $totalPage,
  94. 'moreData' => $page < $totalPage ? 1 : 0,
  95. 'monthCommission' => round($monthCommission, 2),
  96. ];
  97. }
  98. /**
  99. * 格式化贡献订单行(金额字段与库表一致,另附展示用文案)
  100. */
  101. protected static function formatContribOrderRow($row)
  102. {
  103. $settleStatus = (int)$row['settleStatus'];
  104. $commissionAmount = round((float)$row['commissionAmount'], 2);
  105. $isSettled = $settleStatus === 1;
  106. return [
  107. 'id' => (int)$row['id'],
  108. 'orderId' => (int)$row['orderId'],
  109. 'orderSn' => $row['orderSn'],
  110. 'buyerId' => (int)$row['buyerId'],
  111. 'buyerName' => $row['buyerName'],
  112. 'distId' => (int)$row['distId'],
  113. 'distName' => $row['distName'],
  114. 'orderAmount' => round((float)$row['orderAmount'], 2),
  115. 'payAmount' => round((float)$row['payAmount'], 2),
  116. 'sendCost' => round((float)$row['sendCost'], 2),
  117. 'commissionBase' => round((float)$row['commissionBase'], 2),
  118. 'commissionRate' => round((float)$row['commissionRate'], 2),
  119. 'commissionAmount' => $commissionAmount,
  120. 'bonusType' => (int)$row['bonusType'],
  121. 'bindDays' => (int)$row['bindDays'],
  122. 'settleStatus' => $settleStatus,
  123. 'orderTime' => $row['orderTime'],
  124. 'payTime' => $row['payTime'],
  125. 'finishTime' => $row['finishTime'],
  126. 'settleTime' => $row['settleTime'],
  127. 'invalidTime' => $row['invalidTime'],
  128. 'invalidReason' => $row['invalidReason'],
  129. 'amountText' => '+¥' . number_format($commissionAmount, 2, '.', ''),
  130. 'statusText' => self::settleStatusText($settleStatus),
  131. 'statusClass' => self::settleStatusClass($settleStatus),
  132. 'timeLabel' => $isSettled ? '结算时间' : '下单时间',
  133. 'timeValue' => $isSettled ? ($row['settleTime'] ?: $row['orderTime']) : $row['orderTime'],
  134. ];
  135. }
  136. protected static function settleStatusText($settleStatus)
  137. {
  138. $map = [
  139. 0 => '待结算',
  140. 1 => '已结算',
  141. 2 => '已失效',
  142. ];
  143. return isset($map[$settleStatus]) ? $map[$settleStatus] : '';
  144. }
  145. protected static function settleStatusClass($settleStatus)
  146. {
  147. if ($settleStatus === 0) {
  148. return 'pending';
  149. }
  150. if ($settleStatus === 1) {
  151. return 'success';
  152. }
  153. return 'invalid';
  154. }
  155. }