|
|
@@ -4,8 +4,10 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import com.etotem.cfc.common.Result;
|
|
|
import com.etotem.cfc.entity.DistributionRelation;
|
|
|
import com.etotem.cfc.entity.DistributionSystem;
|
|
|
+import com.etotem.cfc.entity.User;
|
|
|
import com.etotem.cfc.mapper.DistributionRelationMapper;
|
|
|
import com.etotem.cfc.mapper.DistributionSystemMapper;
|
|
|
+import com.etotem.cfc.mapper.UserMapper;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
@@ -20,6 +22,9 @@ public class DistributionService {
|
|
|
@Resource
|
|
|
private DistributionRelationMapper distributionRelationMapper;
|
|
|
|
|
|
+ @Resource
|
|
|
+ private UserMapper userMapper;
|
|
|
+
|
|
|
public Result<String> createSystem(DistributionSystem system) {
|
|
|
system.setEnabled(true);
|
|
|
system.setCreatedAt(new Date());
|
|
|
@@ -172,4 +177,152 @@ public class DistributionService {
|
|
|
- (chain.size() >= 2 ? level2Amount : 0));
|
|
|
return Result.success(result);
|
|
|
}
|
|
|
+
|
|
|
+ public Result<List<DistributionSystem>> getUserTeams(Long userId) {
|
|
|
+ List<DistributionRelation> relations = distributionRelationMapper.selectList(
|
|
|
+ new LambdaQueryWrapper<DistributionRelation>()
|
|
|
+ .eq(DistributionRelation::getUserId, userId)
|
|
|
+ );
|
|
|
+ if (relations.isEmpty()) return Result.success(new ArrayList<>());
|
|
|
+
|
|
|
+ List<Long> systemIds = new ArrayList<>();
|
|
|
+ for (DistributionRelation r : relations) {
|
|
|
+ systemIds.add(r.getSystemId());
|
|
|
+ }
|
|
|
+ List<DistributionSystem> systems = distributionSystemMapper.selectList(
|
|
|
+ new LambdaQueryWrapper<DistributionSystem>()
|
|
|
+ .in(DistributionSystem::getId, systemIds)
|
|
|
+ .orderByDesc(DistributionSystem::getCreatedAt)
|
|
|
+ );
|
|
|
+ return Result.success(systems);
|
|
|
+ }
|
|
|
+
|
|
|
+ public Result<List<Map<String, Object>>> getTeamTree(Long systemId) {
|
|
|
+ DistributionSystem system = distributionSystemMapper.selectById(systemId);
|
|
|
+ if (system == null) return Result.error("分销体系不存在");
|
|
|
+
|
|
|
+ List<DistributionRelation> allRelations = distributionRelationMapper.selectList(
|
|
|
+ new LambdaQueryWrapper<DistributionRelation>()
|
|
|
+ .eq(DistributionRelation::getSystemId, systemId)
|
|
|
+ .orderByAsc(DistributionRelation::getDepth)
|
|
|
+ .orderByDesc(DistributionRelation::getCreatedAt)
|
|
|
+ );
|
|
|
+
|
|
|
+ Map<Long, List<Map<String, Object>>> childrenMap = new LinkedHashMap<>();
|
|
|
+ List<Map<String, Object>> roots = new ArrayList<>();
|
|
|
+
|
|
|
+ for (DistributionRelation r : allRelations) {
|
|
|
+ Map<String, Object> node = new LinkedHashMap<>();
|
|
|
+ node.put("id", r.getId());
|
|
|
+ node.put("userId", r.getUserId());
|
|
|
+ node.put("parentId", r.getParentId());
|
|
|
+ node.put("depth", r.getDepth());
|
|
|
+ node.put("createdAt", r.getCreatedAt());
|
|
|
+ node.put("children", new ArrayList<Map<String, Object>>());
|
|
|
+
|
|
|
+ if (r.getParentId() == null) {
|
|
|
+ roots.add(node);
|
|
|
+ } else {
|
|
|
+ childrenMap.computeIfAbsent(r.getParentId(), k -> new ArrayList<>()).add(node);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ for (Map<String, Object> node : roots) {
|
|
|
+ buildTree(node, childrenMap);
|
|
|
+ }
|
|
|
+
|
|
|
+ return Result.success(roots);
|
|
|
+ }
|
|
|
+
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ private void buildTree(Map<String, Object> node, Map<Long, List<Map<String, Object>>> childrenMap) {
|
|
|
+ Long userId = (Long) node.get("userId");
|
|
|
+ List<Map<String, Object>> children = childrenMap.get(userId);
|
|
|
+ if (children != null) {
|
|
|
+ List<Map<String, Object>> nodeChildren = (List<Map<String, Object>>) node.get("children");
|
|
|
+ nodeChildren.addAll(children);
|
|
|
+ for (Map<String, Object> child : children) {
|
|
|
+ buildTree(child, childrenMap);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public Result<Map<String, Object>> getUserPosition(Long systemId, Long userId) {
|
|
|
+ DistributionRelation relation = distributionRelationMapper.selectOne(
|
|
|
+ new LambdaQueryWrapper<DistributionRelation>()
|
|
|
+ .eq(DistributionRelation::getSystemId, systemId)
|
|
|
+ .eq(DistributionRelation::getUserId, userId)
|
|
|
+ );
|
|
|
+ if (relation == null) return Result.error("用户不在该分销体系中");
|
|
|
+
|
|
|
+ List<DistributionRelation> children = distributionRelationMapper.selectList(
|
|
|
+ new LambdaQueryWrapper<DistributionRelation>()
|
|
|
+ .eq(DistributionRelation::getSystemId, systemId)
|
|
|
+ .eq(DistributionRelation::getParentId, userId)
|
|
|
+ );
|
|
|
+
|
|
|
+ Map<String, Object> result = new LinkedHashMap<>();
|
|
|
+ result.put("systemId", systemId);
|
|
|
+ result.put("userId", userId);
|
|
|
+ result.put("parentId", relation.getParentId());
|
|
|
+ result.put("depth", relation.getDepth());
|
|
|
+ result.put("childCount", children.size());
|
|
|
+ result.put("joinedAt", relation.getCreatedAt());
|
|
|
+ return Result.success(result);
|
|
|
+ }
|
|
|
+
|
|
|
+ public Result<List<Map<String, Object>>> getTeamMembers(Long systemId) {
|
|
|
+ DistributionSystem system = distributionSystemMapper.selectById(systemId);
|
|
|
+ if (system == null) return Result.error("分销体系不存在");
|
|
|
+
|
|
|
+ List<DistributionRelation> relations = distributionRelationMapper.selectList(
|
|
|
+ new LambdaQueryWrapper<DistributionRelation>()
|
|
|
+ .eq(DistributionRelation::getSystemId, systemId)
|
|
|
+ .orderByAsc(DistributionRelation::getDepth)
|
|
|
+ .orderByDesc(DistributionRelation::getCreatedAt)
|
|
|
+ );
|
|
|
+
|
|
|
+ List<Map<String, Object>> result = new ArrayList<>();
|
|
|
+ for (DistributionRelation r : relations) {
|
|
|
+ Map<String, Object> item = new LinkedHashMap<>();
|
|
|
+ item.put("id", r.getId());
|
|
|
+ item.put("systemId", r.getSystemId());
|
|
|
+ item.put("userId", r.getUserId());
|
|
|
+ item.put("parentId", r.getParentId());
|
|
|
+ item.put("depth", r.getDepth());
|
|
|
+ item.put("createdAt", r.getCreatedAt());
|
|
|
+
|
|
|
+ User user = userMapper.selectById(r.getUserId());
|
|
|
+ if (user != null) {
|
|
|
+ item.put("nickname", user.getNickname());
|
|
|
+ item.put("avatar", user.getAvatar());
|
|
|
+ item.put("phone", user.getPhone());
|
|
|
+ }
|
|
|
+ result.add(item);
|
|
|
+ }
|
|
|
+ return Result.success(result);
|
|
|
+ }
|
|
|
+
|
|
|
+ public Result<String> updateMemberTeam(Long id, Long parentId) {
|
|
|
+ DistributionRelation relation = distributionRelationMapper.selectById(id);
|
|
|
+ if (relation == null) return Result.error("关系不存在");
|
|
|
+
|
|
|
+ if (parentId == null) {
|
|
|
+ relation.setParentId(null);
|
|
|
+ relation.setDepth(0);
|
|
|
+ } else {
|
|
|
+ DistributionRelation parent = distributionRelationMapper.selectOne(
|
|
|
+ new LambdaQueryWrapper<DistributionRelation>()
|
|
|
+ .eq(DistributionRelation::getSystemId, relation.getSystemId())
|
|
|
+ .eq(DistributionRelation::getUserId, parentId)
|
|
|
+ );
|
|
|
+ if (parent == null) return Result.error("上级节点不存在");
|
|
|
+ if (parentId.equals(relation.getUserId())) return Result.error("不能将自己设为上级");
|
|
|
+ relation.setParentId(parentId);
|
|
|
+ relation.setDepth(parent.getDepth() + 1);
|
|
|
+ }
|
|
|
+
|
|
|
+ distributionRelationMapper.updateById(relation);
|
|
|
+ return Result.success("更新成功");
|
|
|
+ }
|
|
|
}
|