| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235 |
- <?php
- /**
- * 用途:分销流水 xhDistributionFlow 查询与展示格式化
- * 谁用:hdApp 客户详情-分红变动明细
- */
- namespace bizHd\distribution\classes;
- use bizHd\base\classes\BaseClass;
- use bizHd\custom\classes\CustomClass;
- use common\components\dateUtil;
- use Yii;
- use yii\db\Query;
- class DistributionFlowClass extends BaseClass
- {
- public static $baseFile = '\bizHd\distribution\models\DistributionFlow';
- /**
- * 客户分红流水列表(分页,支持按月份筛选)
- * @param int $shopId
- * @param int $customId xhCustom.id
- * @return array
- * @throws \Exception
- */
- public static function getFlowList($shopId, $customId)
- {
- $customId = (int)$customId;
- if ($customId < 0) {
- throw new \Exception('客户ID无效');
- }
- $where = self::buildFlowWhere($shopId, $customId);
- $data = self::getList('*', $where, 'flowTime DESC, id DESC');
- $customIds = [];
- foreach ($data['list'] as $row) {
- $cid = (int)($row['xhCustomId'] ?? 0);
- if ($cid > 0) {
- $customIds[] = $cid;
- }
- }
- $customMap = [];
- if (!empty($customIds)) {
- foreach (CustomClass::getCustomByIds(array_values(array_unique($customIds))) as $customItem) {
- $customMap[(int)$customItem['id']] = $customItem;
- }
- }
- $list = [];
- foreach ($data['list'] as $row) {
- $item = self::formatFlowRow($row);
- $cid = (int)($row['xhCustomId'] ?? 0);
- $custom = $customMap[$cid] ?? [];
- $item['distName'] = $custom['name'] ?? '';
- $item['distMobile'] = $custom['mobile'] ?? '';
- $item['distDisplay'] = self::formatDistDisplay($custom);
- $list[] = $item;
- }
- $data['list'] = $list;
- $data['monthTotal'] = (int)self::getCount($where);
- return $data;
- }
- /** 获佣人展示:优先姓名,无则手机尾号 */
- protected static function formatDistDisplay($custom)
- {
- if (!empty($custom['name'])) {
- return $custom['name'];
- }
- $mobile = isset($custom['mobile']) ? trim($custom['mobile']) : '';
- if (strlen($mobile) >= 4) {
- return '尾号' . substr($mobile, -4);
- }
- return '-';
- }
- /**
- * 组装查询条件(门店 + 客户 + 可选月份)
- */
- protected static function buildFlowWhere($shopId, $customId)
- {
- $where = [
- 'shopId' => (int)$shopId,
- ];
- if ($customId > 0) {
- $where['xhCustomId'] = (int)$customId;
- }
- $get = Yii::$app->request->get();
- $flowType = isset($get['flowType']) ? trim($get['flowType']) : '';
- if ($flowType !== '' && $flowType !== 'all') {
- $where['flowType'] = (int)$flowType;
- }
- $searchTime = isset($get['searchTime']) ? trim($get['searchTime']) : '';
- $startTime = isset($get['startTime']) ? trim($get['startTime']) : '';
- $endTime = isset($get['endTime']) ? trim($get['endTime']) : '';
- if ($searchTime === '') {
- // 默认当前月
- $searchTime = 'thisMonth';
- }
- if ($searchTime !== 'all') {
- $period = dateUtil::formatTime($searchTime, $startTime, $endTime);
- if (!empty($period['startTime']) && !empty($period['endTime'])) {
- $where['flowTime'] = ['between', [$period['startTime'], $period['endTime']]];
- }
- }
- // 查看某下线客户的贡献分红流水(仅指定分销员时)
- $buyerId = isset($get['buyerId']) ? (int)$get['buyerId'] : 0;
- if ($customId > 0 && $buyerId > 0) {
- $orderIds = (new Query())
- ->select('id')
- ->from('xhDistributionOrder')
- ->where([
- 'shopId' => (int)$shopId,
- 'distId' => (int)$customId,
- 'buyerId' => $buyerId,
- ])
- ->column();
- $where['flowType'] = 1;
- $where['refType'] = 1;
- // conditionQuery 的 IN 须写成 ['in', [...]];无订单时用不可能命中的 id
- $where['refId'] = !empty($orderIds) ? ['in', $orderIds] : -1;
- }
- return $where;
- }
- /**
- * 格式化单条流水供前端展示
- */
- protected static function formatFlowRow($row)
- {
- $flowType = (int)$row['flowType'];
- $flowStatus = (int)$row['flowStatus'];
- $amount = round((float)$row['amount'], 2);
- $goodsName = isset($row['remark']) ? trim($row['remark']) : '';
- $item = [
- 'id' => (int)$row['id'],
- 'flowType' => $flowType,
- 'flowTypeName' => self::flowTypeName($flowType),
- 'amount' => $amount,
- 'amountText' => self::formatAmountText($flowType, $amount),
- 'refSn' => $row['refSn'],
- 'buyerName' => $row['buyerName'],
- 'goodsName' => $goodsName,
- 'subTitle' => self::buildSubTitle($flowType, $row['buyerName'], $goodsName),
- 'flowTime' => $row['flowTime'],
- 'flowStatus' => $flowStatus,
- 'statusText' => self::statusText($flowType, $flowStatus),
- 'statusClass' => self::statusClass($flowType, $flowStatus),
- ];
- return $item;
- }
- protected static function flowTypeName($flowType)
- {
- $map = [
- 1 => '订单分红',
- 2 => '分红存入',
- 3 => '退款扣回',
- ];
- return isset($map[$flowType]) ? $map[$flowType] : '分销流水';
- }
- protected static function formatAmountText($flowType, $amount)
- {
- $abs = number_format(abs($amount), 2, '.', '');
- if ($flowType === 1) {
- return '+¥' . $abs;
- }
- return '-¥' . $abs;
- }
- protected static function buildSubTitle($flowType, $buyerName, $goodsName)
- {
- if ($flowType !== 1) {
- return '';
- }
- $buyerName = trim($buyerName);
- $goodsName = trim($goodsName);
- if ($buyerName !== '' && $goodsName !== '') {
- return $buyerName . ' · ' . $goodsName;
- }
- return $buyerName !== '' ? $buyerName : $goodsName;
- }
- /**
- * 状态文案:按 flowType 区分,避免分红存入误用订单分红的「待结算」
- * flowType=1 订单分红:1待结算 2已结算
- * flowType=2 分红存入:3存入成功(测试/默认写 1 时展示待存入)
- * flowType=3 退款扣回:4已扣回
- */
- protected static function statusText($flowType, $flowStatus)
- {
- if ($flowType === 1) {
- $map = [
- 1 => '待结算',
- 2 => '已结算',
- 4 => '已扣回',
- ];
- } elseif ($flowType === 2) {
- $map = [
- 1 => '待存入',
- 3 => '存入成功',
- ];
- } elseif ($flowType === 3) {
- $map = [
- 4 => '已扣回',
- ];
- } else {
- $map = [];
- }
- return isset($map[$flowStatus]) ? $map[$flowStatus] : '';
- }
- protected static function statusClass($flowType, $flowStatus)
- {
- if ($flowType === 1 && $flowStatus === 1) {
- return 'pending';
- }
- if ($flowType === 2 && $flowStatus === 1) {
- return 'pending';
- }
- if (($flowType === 1 && $flowStatus === 2)
- || ($flowType === 2 && $flowStatus === 3)) {
- return 'success';
- }
- return 'default';
- }
- }
|