| | |
| | | package com.yami.trading.api.controller; |
| | | |
| | | import com.yami.trading.bean.model.UserData; |
| | | import com.yami.trading.bean.model.User; |
| | | import com.yami.trading.bean.model.UserRecom; |
| | | import com.yami.trading.bean.user.dto.ChildrenLever; |
| | | import com.yami.trading.common.constants.Constants; |
| | | import com.yami.trading.common.domain.Result; |
| | | import com.yami.trading.common.exception.BusinessException; |
| | | import com.yami.trading.common.exception.YamiShopBindException; |
| | |
| | | |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import java.util.*; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | | * 我的推广 |
| | |
| | | @Autowired |
| | | protected SysparaService sysparaService; |
| | | |
| | | @RequestMapping("api/promote!getInviteSummary.action") |
| | | public Result<?> getInviteSummary() { |
| | | String userId = SecurityUtils.getUser().getUserId(); |
| | | Map<String, Object> data = buildInviteSummary(userId); |
| | | return Result.succeed(data); |
| | | } |
| | | |
| | | @RequestMapping("api/promote!getInviteChildren.action") |
| | | public Result<?> getInviteChildren(HttpServletRequest request) { |
| | | String levelRaw = request.getParameter("level"); |
| | | int level = StringUtils.isInteger(levelRaw) ? Integer.parseInt(levelRaw) : 1; |
| | | if (level < 1 || level > 2) { |
| | | throw new YamiShopBindException("代理层级错误"); |
| | | } |
| | | String pageNoRaw = request.getParameter("page_no"); |
| | | String pageSizeRaw = request.getParameter("page_size"); |
| | | int pageNo = StringUtils.isInteger(pageNoRaw) && Integer.parseInt(pageNoRaw) > 0 ? Integer.parseInt(pageNoRaw) : 1; |
| | | int pageSize = StringUtils.isInteger(pageSizeRaw) && Integer.parseInt(pageSizeRaw) > 0 ? Integer.parseInt(pageSizeRaw) : 10; |
| | | pageSize = Math.min(pageSize, 100); |
| | | |
| | | String userId = SecurityUtils.getUser().getUserId(); |
| | | List<String> level1Ids = getDirectChildIds(userId); |
| | | List<String> targetIds; |
| | | if (level == 1) { |
| | | targetIds = level1Ids; |
| | | } else { |
| | | targetIds = new ArrayList<>(); |
| | | for (String id : level1Ids) { |
| | | targetIds.addAll(getDirectChildIds(id)); |
| | | } |
| | | } |
| | | |
| | | List<Map<String, Object>> fullList = targetIds.stream().map(id -> { |
| | | User child = partyService.getById(id); |
| | | if (child == null) { |
| | | return null; |
| | | } |
| | | Map<String, Object> item = new HashMap<>(); |
| | | item.put("user_id", child.getUserId()); |
| | | item.put("username", maskUsername(child.getUserName())); |
| | | item.put("user_code", child.getUserCode()); |
| | | item.put("create_time", child.getCreateTime()); |
| | | item.put("direct_children_count", getDirectChildIds(child.getUserId()).size()); |
| | | return item; |
| | | }).filter(Objects::nonNull).collect(Collectors.toList()); |
| | | |
| | | int total = fullList.size(); |
| | | int from = Math.max((pageNo - 1) * pageSize, 0); |
| | | int to = Math.min(from + pageSize, total); |
| | | List<Map<String, Object>> pageList = from >= total ? Collections.emptyList() : fullList.subList(from, to); |
| | | |
| | | Map<String, Object> data = new HashMap<>(); |
| | | data.put("total", total); |
| | | data.put("level", level); |
| | | data.put("page_no", pageNo); |
| | | data.put("page_size", pageSize); |
| | | data.put("list", pageList); |
| | | return Result.succeed(data); |
| | | } |
| | | |
| | | @RequestMapping("api/promote!getPromote.action") |
| | | public Result getPromote(HttpServletRequest request) { |
| | | public Result<?> getPromote(HttpServletRequest request) { |
| | | // 层级 1为第一级 1,2,3,4总共4级代理 |
| | | String level_temp= request.getParameter("level"); |
| | | if (StringUtils.isNullOrEmpty(level_temp) |
| | |
| | | * 交易所-数据总览-PC端 |
| | | */ |
| | | @RequestMapping( "api/promote!getPromoteData.action") |
| | | public Result getPromoteData(HttpServletRequest request) { |
| | | public Result<?> getPromoteData(HttpServletRequest request) { |
| | | String partyId = SecurityUtils.getUser().getUserId(); |
| | | Map<String, String> dataMap = new HashMap<>(); |
| | | try { |
| | |
| | | } |
| | | } |
| | | } |
| | | |
| | | private Map<String, Object> buildInviteSummary(String userId) { |
| | | User currentUser = partyService.getById(userId); |
| | | List<String> level1Ids = getDirectChildIds(userId); |
| | | List<String> level2Ids = new ArrayList<>(); |
| | | for (String level1Id : level1Ids) { |
| | | level2Ids.addAll(getDirectChildIds(level1Id)); |
| | | } |
| | | Map<String, Object> data = new HashMap<>(); |
| | | String code = currentUser != null ? currentUser.getUserCode() : ""; |
| | | data.put("invite_code", code); |
| | | data.put("invite_link", Constants.buildRegisterInviteLink(code)); |
| | | data.put("total_count", level1Ids.size() + level2Ids.size()); |
| | | data.put("tree", buildInviteTree(userId)); |
| | | return data; |
| | | } |
| | | |
| | | /** |
| | | * 两级邀请关系树:直接下级及其各自的下级(嵌套在 children 中)。 |
| | | */ |
| | | private List<Map<String, Object>> buildInviteTree(String rootUserId) { |
| | | List<Map<String, Object>> tree = new ArrayList<>(); |
| | | for (String childId : getDirectChildIds(rootUserId)) { |
| | | User u = partyService.getById(childId); |
| | | if (u == null) { |
| | | continue; |
| | | } |
| | | Map<String, Object> node = inviteUserNode(u); |
| | | List<Map<String, Object>> children = new ArrayList<>(); |
| | | for (String gcId : getDirectChildIds(childId)) { |
| | | User g = partyService.getById(gcId); |
| | | if (g != null) { |
| | | children.add(inviteUserNode(g)); |
| | | } |
| | | } |
| | | node.put("children", children); |
| | | tree.add(node); |
| | | } |
| | | return tree; |
| | | } |
| | | |
| | | private Map<String, Object> inviteUserNode(User u) { |
| | | Map<String, Object> item = new HashMap<>(); |
| | | item.put("user_id", u.getUserId()); |
| | | item.put("username", maskUsername(u.getUserName())); |
| | | item.put("user_code", u.getUserCode()); |
| | | item.put("create_time", u.getCreateTime()); |
| | | return item; |
| | | } |
| | | |
| | | private List<String> getDirectChildIds(String userId) { |
| | | List<UserRecom> list = userRecomService.findRecoms(userId); |
| | | if (list == null || list.isEmpty()) { |
| | | return Collections.emptyList(); |
| | | } |
| | | return list.stream().map(UserRecom::getRecomUserId).filter(Objects::nonNull).collect(Collectors.toList()); |
| | | } |
| | | |
| | | private String maskUsername(String username) { |
| | | if (username == null) { |
| | | return ""; |
| | | } |
| | | int len = username.length(); |
| | | if (len <= 2) { |
| | | return username; |
| | | } |
| | | if (len <= 6) { |
| | | return username.substring(0, 1) + "***" + username.substring(len - 1); |
| | | } |
| | | return username.substring(0, 3) + "***" + username.substring(len - 2); |
| | | } |
| | | } |