DistributionFlowClass.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <?php
  2. /**
  3. * 用途:分销流水 xhDistributionFlow 查询与展示格式化
  4. * 谁用:hdApp 客户详情-分红变动明细
  5. */
  6. namespace bizHd\distribution\classes;
  7. use bizHd\base\classes\BaseClass;
  8. use bizHd\custom\classes\CustomClass;
  9. use common\components\dateUtil;
  10. use Yii;
  11. use yii\db\Query;
  12. class DistributionFlowClass extends BaseClass
  13. {
  14. public static $baseFile = '\bizHd\distribution\models\DistributionFlow';
  15. /**
  16. * 客户分红流水列表(分页,支持按月份筛选)
  17. * @param int $shopId
  18. * @param int $customId xhCustom.id
  19. * @return array
  20. * @throws \Exception
  21. */
  22. public static function getFlowList($shopId, $customId)
  23. {
  24. $customId = (int)$customId;
  25. if ($customId < 0) {
  26. throw new \Exception('客户ID无效');
  27. }
  28. $where = self::buildFlowWhere($shopId, $customId);
  29. $data = self::getList('*', $where, 'flowTime DESC, id DESC');
  30. $customIds = [];
  31. foreach ($data['list'] as $row) {
  32. $cid = (int)($row['xhCustomId'] ?? 0);
  33. if ($cid > 0) {
  34. $customIds[] = $cid;
  35. }
  36. }
  37. $customMap = [];
  38. if (!empty($customIds)) {
  39. foreach (CustomClass::getCustomByIds(array_values(array_unique($customIds))) as $customItem) {
  40. $customMap[(int)$customItem['id']] = $customItem;
  41. }
  42. }
  43. $list = [];
  44. foreach ($data['list'] as $row) {
  45. $item = self::formatFlowRow($row);
  46. $cid = (int)($row['xhCustomId'] ?? 0);
  47. $custom = $customMap[$cid] ?? [];
  48. $item['distName'] = $custom['name'] ?? '';
  49. $item['distMobile'] = $custom['mobile'] ?? '';
  50. $item['distDisplay'] = self::formatDistDisplay($custom);
  51. $list[] = $item;
  52. }
  53. $data['list'] = $list;
  54. $data['monthTotal'] = (int)self::getCount($where);
  55. return $data;
  56. }
  57. /** 获佣人展示:优先姓名,无则手机尾号 */
  58. protected static function formatDistDisplay($custom)
  59. {
  60. if (!empty($custom['name'])) {
  61. return $custom['name'];
  62. }
  63. $mobile = isset($custom['mobile']) ? trim($custom['mobile']) : '';
  64. if (strlen($mobile) >= 4) {
  65. return '尾号' . substr($mobile, -4);
  66. }
  67. return '-';
  68. }
  69. /**
  70. * 组装查询条件(门店 + 客户 + 可选月份)
  71. */
  72. protected static function buildFlowWhere($shopId, $customId)
  73. {
  74. $where = [
  75. 'shopId' => (int)$shopId,
  76. ];
  77. if ($customId > 0) {
  78. $where['xhCustomId'] = (int)$customId;
  79. }
  80. $get = Yii::$app->request->get();
  81. $flowType = isset($get['flowType']) ? trim($get['flowType']) : '';
  82. if ($flowType !== '' && $flowType !== 'all') {
  83. $where['flowType'] = (int)$flowType;
  84. }
  85. $searchTime = isset($get['searchTime']) ? trim($get['searchTime']) : '';
  86. $startTime = isset($get['startTime']) ? trim($get['startTime']) : '';
  87. $endTime = isset($get['endTime']) ? trim($get['endTime']) : '';
  88. if ($searchTime === '') {
  89. // 默认当前月
  90. $searchTime = 'thisMonth';
  91. }
  92. if ($searchTime !== 'all') {
  93. $period = dateUtil::formatTime($searchTime, $startTime, $endTime);
  94. if (!empty($period['startTime']) && !empty($period['endTime'])) {
  95. $where['flowTime'] = ['between', [$period['startTime'], $period['endTime']]];
  96. }
  97. }
  98. // 查看某下线客户的贡献分红流水(仅指定分销员时)
  99. $buyerId = isset($get['buyerId']) ? (int)$get['buyerId'] : 0;
  100. if ($customId > 0 && $buyerId > 0) {
  101. $orderIds = (new Query())
  102. ->select('id')
  103. ->from('xhDistributionOrder')
  104. ->where([
  105. 'shopId' => (int)$shopId,
  106. 'distId' => (int)$customId,
  107. 'buyerId' => $buyerId,
  108. ])
  109. ->column();
  110. $where['flowType'] = 1;
  111. $where['refType'] = 1;
  112. // conditionQuery 的 IN 须写成 ['in', [...]];无订单时用不可能命中的 id
  113. $where['refId'] = !empty($orderIds) ? ['in', $orderIds] : -1;
  114. }
  115. return $where;
  116. }
  117. /**
  118. * 格式化单条流水供前端展示
  119. */
  120. protected static function formatFlowRow($row)
  121. {
  122. $flowType = (int)$row['flowType'];
  123. $flowStatus = (int)$row['flowStatus'];
  124. $amount = round((float)$row['amount'], 2);
  125. $goodsName = isset($row['remark']) ? trim($row['remark']) : '';
  126. $item = [
  127. 'id' => (int)$row['id'],
  128. 'flowType' => $flowType,
  129. 'flowTypeName' => self::flowTypeName($flowType),
  130. 'amount' => $amount,
  131. 'amountText' => self::formatAmountText($flowType, $amount),
  132. 'refSn' => $row['refSn'],
  133. 'buyerName' => $row['buyerName'],
  134. 'goodsName' => $goodsName,
  135. 'subTitle' => self::buildSubTitle($flowType, $row['buyerName'], $goodsName),
  136. 'flowTime' => $row['flowTime'],
  137. 'flowStatus' => $flowStatus,
  138. 'statusText' => self::statusText($flowType, $flowStatus),
  139. 'statusClass' => self::statusClass($flowType, $flowStatus),
  140. ];
  141. return $item;
  142. }
  143. protected static function flowTypeName($flowType)
  144. {
  145. $map = [
  146. 1 => '订单分红',
  147. 2 => '分红存入',
  148. 3 => '退款扣回',
  149. ];
  150. return isset($map[$flowType]) ? $map[$flowType] : '分销流水';
  151. }
  152. protected static function formatAmountText($flowType, $amount)
  153. {
  154. $abs = number_format(abs($amount), 2, '.', '');
  155. if ($flowType === 1) {
  156. return '+¥' . $abs;
  157. }
  158. return '-¥' . $abs;
  159. }
  160. protected static function buildSubTitle($flowType, $buyerName, $goodsName)
  161. {
  162. if ($flowType !== 1) {
  163. return '';
  164. }
  165. $buyerName = trim($buyerName);
  166. $goodsName = trim($goodsName);
  167. if ($buyerName !== '' && $goodsName !== '') {
  168. return $buyerName . ' · ' . $goodsName;
  169. }
  170. return $buyerName !== '' ? $buyerName : $goodsName;
  171. }
  172. /**
  173. * 状态文案:按 flowType 区分,避免分红存入误用订单分红的「待结算」
  174. * flowType=1 订单分红:1待结算 2已结算
  175. * flowType=2 分红存入:3存入成功(测试/默认写 1 时展示待存入)
  176. * flowType=3 退款扣回:4已扣回
  177. */
  178. protected static function statusText($flowType, $flowStatus)
  179. {
  180. if ($flowType === 1) {
  181. $map = [
  182. 1 => '待结算',
  183. 2 => '已结算',
  184. 4 => '已扣回',
  185. ];
  186. } elseif ($flowType === 2) {
  187. $map = [
  188. 1 => '待存入',
  189. 3 => '存入成功',
  190. ];
  191. } elseif ($flowType === 3) {
  192. $map = [
  193. 4 => '已扣回',
  194. ];
  195. } else {
  196. $map = [];
  197. }
  198. return isset($map[$flowStatus]) ? $map[$flowStatus] : '';
  199. }
  200. protected static function statusClass($flowType, $flowStatus)
  201. {
  202. if ($flowType === 1 && $flowStatus === 1) {
  203. return 'pending';
  204. }
  205. if ($flowType === 2 && $flowStatus === 1) {
  206. return 'pending';
  207. }
  208. if (($flowType === 1 && $flowStatus === 2)
  209. || ($flowType === 2 && $flowStatus === 3)) {
  210. return 'success';
  211. }
  212. return 'default';
  213. }
  214. }