|
|
@@ -82,95 +82,188 @@ class StoreProductGiftGroupService extends BaseServices
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 获取礼包推荐关系列表(树形结构)
|
|
|
+ * 获取礼包商品列表(最外层礼包名列表)
|
|
|
+ * @param array $where
|
|
|
+ * @return array
|
|
|
+ */
|
|
|
+ public function getGiftProductList($where = [])
|
|
|
+ {
|
|
|
+ [$page, $limit] = $this->getPageValue();
|
|
|
+
|
|
|
+ // 获取所有礼包商品
|
|
|
+ $giftGroups = $this->dao->getList([], 'DISTINCT product_id', 0, 0);
|
|
|
+
|
|
|
+ if (empty($giftGroups)) {
|
|
|
+ return ['list' => [], 'count' => 0];
|
|
|
+ }
|
|
|
+
|
|
|
+ $productIds = array_column($giftGroups, 'product_id');
|
|
|
+
|
|
|
+ // 获取商品信息
|
|
|
+ $productServices = app()->make(\app\services\product\product\StoreProductServices::class);
|
|
|
+ $products = $productServices->getColumn(['id' => $productIds, 'is_del' => 0], 'id,store_name,image,price,stock', 'id');
|
|
|
+
|
|
|
+ // 获取每个商品的用户数量
|
|
|
+ $list = [];
|
|
|
+ foreach ($productIds as $productId) {
|
|
|
+ if (!isset($products[$productId])) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ $product = $products[$productId];
|
|
|
+ $userCount = $this->dao->count(['product_id' => $productId]);
|
|
|
+
|
|
|
+ $list[] = [
|
|
|
+ 'product_id' => $productId,
|
|
|
+ 'store_name' => $product['store_name'],
|
|
|
+ 'image' => $product['image'],
|
|
|
+ 'price' => $product['price'],
|
|
|
+ 'stock' => $product['stock'],
|
|
|
+ 'user_count' => $userCount
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
+ // 分页处理
|
|
|
+ $count = count($list);
|
|
|
+ if ($page != 0 && $limit != 0) {
|
|
|
+ $list = array_slice($list, ($page - 1) * $limit, $limit);
|
|
|
+ }
|
|
|
+
|
|
|
+ return ['list' => $list, 'count' => $count];
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取推荐关系列表
|
|
|
* @param array $where
|
|
|
* @return array
|
|
|
*/
|
|
|
public function getRecommendationRelationList($where = [])
|
|
|
{
|
|
|
$product_id = $where['product_id'] ?? 0;
|
|
|
- [$page, $limit] = $this->getPageValue();
|
|
|
+ $uid = $where['uid'] ?? 0;
|
|
|
+
|
|
|
if (!$product_id) {
|
|
|
return ['list' => [], 'count' => 0];
|
|
|
}
|
|
|
|
|
|
+ // 如果传了uid,显示该用户的小组成员(fake_pid为该uid的用户)
|
|
|
+ if ($uid) {
|
|
|
+ return $this->getUserSubordinates($product_id, $uid);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 如果只传了product_id,显示该商品中最上层的推荐人
|
|
|
+ return $this->getTopLevelRelations($product_id);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取最上层推荐人(只存在于pid中,而没有在uid里的用户)
|
|
|
+ * @param int $product_id
|
|
|
+ * @return array
|
|
|
+ */
|
|
|
+ private function getTopLevelRelations($product_id)
|
|
|
+ {
|
|
|
// 获取所有该商品的推荐关系
|
|
|
$allRelations = $this->dao->getList([
|
|
|
'product_id' => $product_id
|
|
|
- ], 'id,product_id,uid,group_pid,pid,fake_pid,create_time', $page, $limit, 'id asc');
|
|
|
+ ], 'id,product_id,uid,group_pid,pid,fake_pid,create_time', 0, 0, 'id asc');
|
|
|
|
|
|
if (empty($allRelations)) {
|
|
|
return ['list' => [], 'count' => 0];
|
|
|
}
|
|
|
|
|
|
- // 获取用户信息
|
|
|
- $uids = array_column($allRelations, 'uid');
|
|
|
- $userServices = app()->make(\app\services\user\UserServices::class);
|
|
|
- $userList = $userServices->getColumn(['uid' => $uids], 'uid,nickname,avatar', 'uid');
|
|
|
-
|
|
|
- // 构建树形结构
|
|
|
- // 1. 找到所有根节点(group_pid = 0 或没有上级的节点)
|
|
|
- $tree = [];
|
|
|
- $groupMap = []; // group_pid => members
|
|
|
+ // 收集所有的pid和uid
|
|
|
+ $allPids = [];
|
|
|
+ $allUids = [];
|
|
|
|
|
|
- // 按小组分组
|
|
|
foreach ($allRelations as $relation) {
|
|
|
- $group_pid = $relation['group_pid'] ?: 'root';
|
|
|
- if (!isset($groupMap[$group_pid])) {
|
|
|
- $groupMap[$group_pid] = [];
|
|
|
+ if (!empty($relation['pid'])) {
|
|
|
+ $allPids[] = $relation['pid'];
|
|
|
}
|
|
|
+ $allUids[] = $relation['uid'];
|
|
|
+ }
|
|
|
+
|
|
|
+ // 去重
|
|
|
+ $allPids = array_unique($allPids);
|
|
|
+ $allUids = array_unique($allUids);
|
|
|
|
|
|
- // 添加用户信息
|
|
|
- $relation['nickname'] = $userList[$relation['uid']]['nickname'] ?? '';
|
|
|
- $relation['avatar'] = $userList[$relation['uid']]['avatar'] ?? '';
|
|
|
- $relation['create_time'] = date('Y-m-d H:i:s', $relation['create_time']);
|
|
|
+ // 找出只存在于pid中,而没有在uid里的用户(最上层推荐人)
|
|
|
+ $topLevelUids = array_diff($allPids, $allUids);
|
|
|
|
|
|
- $groupMap[$group_pid][] = $relation;
|
|
|
+ if (empty($topLevelUids)) {
|
|
|
+ return ['list' => [], 'count' => 0];
|
|
|
}
|
|
|
|
|
|
- // 构建树形结构
|
|
|
- foreach ($groupMap as $group_pid => $members) {
|
|
|
- if ($group_pid == 'root') {
|
|
|
- // 根节点小组
|
|
|
- foreach ($members as $member) {
|
|
|
- $tree[] = $this->buildRelationTree($member, $groupMap, $userList);
|
|
|
- }
|
|
|
+ // 获取这些用户的详细信息
|
|
|
+ $userServices = app()->make(\app\services\user\UserServices::class);
|
|
|
+ $users = $userServices->getColumn(['uid' => $topLevelUids], 'uid,nickname,avatar,phone', 'uid');
|
|
|
+
|
|
|
+ $list = [];
|
|
|
+ foreach ($topLevelUids as $topUid) {
|
|
|
+ if (!isset($users[$topUid])) {
|
|
|
+ continue;
|
|
|
}
|
|
|
+
|
|
|
+ // 统计该用户的下级数量(fake_pid为该用户的数量)
|
|
|
+ $subordinateCount = $this->dao->count([
|
|
|
+ 'product_id' => $product_id,
|
|
|
+ 'fake_pid' => $topUid
|
|
|
+ ]);
|
|
|
+
|
|
|
+ $list[] = [
|
|
|
+ 'uid' => $topUid,
|
|
|
+ 'nickname' => $users[$topUid]['nickname'] ?? '',
|
|
|
+ 'avatar' => $users[$topUid]['avatar'] ?? '',
|
|
|
+ 'phone' => $users[$topUid]['phone'] ?? '',
|
|
|
+ 'subordinate_count' => $subordinateCount
|
|
|
+ ];
|
|
|
}
|
|
|
|
|
|
- return ['list' => $tree, 'count' => count($allRelations)];
|
|
|
+ return ['list' => $list, 'count' => count($list)];
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 递归构建推荐关系树
|
|
|
- * @param array $relation
|
|
|
- * @param array $groupMap
|
|
|
- * @param array $userList
|
|
|
+ * 获取用户的下级成员(fake_pid为指定uid的用户)
|
|
|
+ * @param int $product_id
|
|
|
+ * @param int $uid
|
|
|
* @return array
|
|
|
*/
|
|
|
- private function buildRelationTree($relation, &$groupMap, $userList)
|
|
|
+ private function getUserSubordinates($product_id, $uid)
|
|
|
{
|
|
|
- $node = [
|
|
|
- 'id' => $relation['id'],
|
|
|
- 'uid' => $relation['uid'],
|
|
|
- 'nickname' => $relation['nickname'],
|
|
|
- 'avatar' => $relation['avatar'],
|
|
|
- 'group_pid' => $relation['group_pid'],
|
|
|
- 'pid' => $relation['pid'],
|
|
|
- 'fake_pid' => $relation['fake_pid'],
|
|
|
- 'create_time' => $relation['create_time'],
|
|
|
- 'children' => []
|
|
|
- ];
|
|
|
-
|
|
|
- // 查找该用户的下级小组
|
|
|
- $subGroupPid = $relation['uid'];
|
|
|
- if (isset($groupMap[$subGroupPid])) {
|
|
|
- foreach ($groupMap[$subGroupPid] as $subRelation) {
|
|
|
- $node['children'][] = $this->buildRelationTree($subRelation, $groupMap, $userList);
|
|
|
+ // 获取fake_pid为该uid的所有用户
|
|
|
+ $relations = $this->dao->getList([
|
|
|
+ 'product_id' => $product_id,
|
|
|
+ 'fake_pid' => $uid
|
|
|
+ ], 'id,product_id,uid,group_pid,pid,fake_pid,create_time', 0, 0, 'id asc');
|
|
|
+
|
|
|
+ if (empty($relations)) {
|
|
|
+ return ['list' => [], 'count' => 0];
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取用户信息
|
|
|
+ $uids = array_column($relations, 'uid');
|
|
|
+ $userServices = app()->make(\app\services\user\UserServices::class);
|
|
|
+ $userList = $userServices->getColumn(['uid' => $uids], 'uid,nickname,avatar,phone', 'uid');
|
|
|
+
|
|
|
+ $list = [];
|
|
|
+ foreach ($relations as $relation) {
|
|
|
+ if (!isset($userList[$relation['uid']])) {
|
|
|
+ continue;
|
|
|
}
|
|
|
+ $user = $userList[$relation['uid']];
|
|
|
+
|
|
|
+ $list[] = [
|
|
|
+ 'id' => $relation['id'],
|
|
|
+ 'uid' => $relation['uid'],
|
|
|
+ 'nickname' => $user['nickname'] ?? '',
|
|
|
+ 'avatar' => $user['avatar'] ?? '',
|
|
|
+ 'phone' => $user['phone'] ?? '',
|
|
|
+ 'group_pid' => $relation['group_pid'],
|
|
|
+ 'pid' => $relation['pid'],
|
|
|
+ 'fake_pid' => $relation['fake_pid'],
|
|
|
+ 'create_time' => date('Y-m-d H:i:s', $relation['create_time'])
|
|
|
+ ];
|
|
|
}
|
|
|
|
|
|
- return $node;
|
|
|
+ return ['list' => $list, 'count' => count($list)];
|
|
|
}
|
|
|
|
|
|
/**
|