MainInviteFlowClass.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. /**
  3. * 邀请佣金变动流水 xhMainInviteFlow
  4. * 职责:佣金变动明细分页、格式化;配合 xhMainInvite.ableCommission 落 ableAfter 快照
  5. */
  6. namespace bizHd\shop\classes;
  7. use biz\shop\classes\ShopClass;
  8. use bizHd\base\classes\BaseClass;
  9. use common\components\dateUtil;
  10. use Yii;
  11. class MainInviteFlowClass extends BaseClass
  12. {
  13. public static $baseFile = '\bizHd\shop\models\MainInviteFlow';
  14. /** 邀请开通获佣 */
  15. const FLOW_TYPE_EARN = 1;
  16. /** 佣金存入中央钱包 */
  17. const FLOW_TYPE_DEPOSIT = 2;
  18. /** 退款扣回佣金 */
  19. const FLOW_TYPE_REFUND = 3;
  20. /** 关联 xhMainInviteCommission.id */
  21. const REF_TYPE_COMMISSION = 1;
  22. /** 关联 xhMainWalletChange.id */
  23. const REF_TYPE_WALLET_CHANGE = 2;
  24. /**
  25. * 佣金变动明细分页(默认近30天)
  26. */
  27. public static function getFlowList($mainId)
  28. {
  29. $where = self::buildFlowListWhere($mainId);
  30. $result = self::getList('*', $where, 'flowTime DESC,id DESC');
  31. $rows = $result['list'] ?? [];
  32. $list = [];
  33. foreach ($rows as $row) {
  34. $list[] = self::formatFlowListItem($row);
  35. }
  36. $result['list'] = $list;
  37. return $result;
  38. }
  39. /**
  40. * 列表查询条件:mainId + 时间筛选
  41. */
  42. protected static function buildFlowListWhere($mainId)
  43. {
  44. $where = ['mainId' => (int) $mainId];
  45. $get = Yii::$app->request->get();
  46. $searchTime = isset($get['searchTime']) ? trim((string) $get['searchTime']) : 'last30Days';
  47. $startTime = isset($get['startTime']) ? trim((string) $get['startTime']) : '';
  48. $endTime = isset($get['endTime']) ? trim((string) $get['endTime']) : '';
  49. if ($searchTime !== '' && $searchTime !== 'all') {
  50. $period = dateUtil::formatTime($searchTime, $startTime, $endTime);
  51. if (!empty($period['startTime']) && !empty($period['endTime'])) {
  52. $where['flowTime'] = ['between', [$period['startTime'], $period['endTime']]];
  53. }
  54. }
  55. return $where;
  56. }
  57. /**
  58. * 格式化单行,字段与 hdApp shopInviteCommissionList 一致
  59. */
  60. public static function formatFlowListItem($row)
  61. {
  62. $flowType = (int) ($row['flowType'] ?? 0);
  63. $lsShopId = (int) ($row['lsShopId'] ?? 0);
  64. $shop = [];
  65. if ($lsShopId > 0) {
  66. $shop = ShopClass::getById($lsShopId, false) ?: [];
  67. }
  68. $shopName = trim((string) ($row['inviteeShopName'] ?? ''));
  69. $mobile = trim((string) ($row['inviteeMobile'] ?? ''));
  70. $mobileMask = $mobile !== '' ? MainInviteCommissionClass::maskMobile($mobile) : '-';
  71. if ($flowType === self::FLOW_TYPE_DEPOSIT) {
  72. $orderSnLabel = '存入编号';
  73. $baseAmountLabel = '存余额金额';
  74. $recordType = 'deposit';
  75. $shopName = '存入钱包余额';
  76. $mobile = '';
  77. $mobileMask = '-';
  78. $shop = [];
  79. } else {
  80. $orderSnLabel = '会员订单号';
  81. $baseAmountLabel = '会员实付金额';
  82. $recordType = 'member';
  83. }
  84. $orderSn = trim((string) ($row['refSn'] ?? ''));
  85. $baseAmount = round((float) ($row['baseAmount'] ?? 0), 2);
  86. $rawAmount = round((float) ($row['amount'] ?? 0), 2);
  87. $commissionAmount = round(abs($rawAmount), 2);
  88. $commissionSign = $rawAmount < 0 ? -1 : 1;
  89. if ($flowType === self::FLOW_TYPE_REFUND) {
  90. $commissionSign = -1;
  91. }
  92. $createTime = trim((string) ($row['flowTime'] ?? ($row['addTime'] ?? '')));
  93. if ($createTime !== '' && strlen($createTime) === 10) {
  94. $createTime .= ' 00:00:00';
  95. }
  96. return [
  97. 'id' => $row['id'] ?? 0,
  98. 'shopName' => $shopName !== '' ? $shopName : '-',
  99. 'shopImg' => self::resolveShopImg($shop),
  100. 'mobile' => $mobile !== '' ? $mobile : '-',
  101. 'mobileMask' => $mobileMask,
  102. 'orderSnLabel' => $orderSnLabel,
  103. 'orderSn' => $orderSn !== '' ? $orderSn : '-',
  104. 'baseAmountLabel' => $baseAmountLabel,
  105. 'baseAmount' => $baseAmount,
  106. 'createTime' => $createTime !== '' ? $createTime : '-',
  107. 'commissionAmount' => $commissionAmount,
  108. 'commissionSign' => $commissionSign,
  109. 'recordType' => $recordType,
  110. 'flowType' => $flowType,
  111. 'ableAfter' => round((float) ($row['ableAfter'] ?? 0), 2),
  112. ];
  113. }
  114. protected static function resolveShopImg($shop)
  115. {
  116. $avatar = is_array($shop) ? ($shop['avatar'] ?? '') : '';
  117. if (empty($avatar)) {
  118. return '';
  119. }
  120. if (strpos($avatar, 'http') === 0) {
  121. return $avatar;
  122. }
  123. $host = Yii::$app->params['hdImgHost'] ?? '';
  124. return $host . $avatar;
  125. }
  126. }