1
zj
5 days ago b42c0777927e79bc77996b508a534ee4e56fd4c2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
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 com.yami.trading.common.util.DateUtil;
import com.yami.trading.common.util.DateUtils;
import com.yami.trading.common.util.StringUtils;
import com.yami.trading.security.common.util.SecurityUtils;
import com.yami.trading.service.syspara.SysparaService;
import com.yami.trading.service.user.UserDataService;
import com.yami.trading.service.user.UserRecomService;
import com.yami.trading.service.user.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * 我的推广
 */
@RestController
@CrossOrigin
@Slf4j
public class PromoteController {
    @Autowired
    protected UserRecomService userRecomService;
    @Autowired
    protected UserDataService userDataService;
    @Autowired
    protected UserService partyService;
    @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) {
        // 层级 1为第一级 1,2,3,4总共4级代理
        String  level_temp= request.getParameter("level");
        if (StringUtils.isNullOrEmpty(level_temp)
                || !StringUtils.isInteger(level_temp) || Integer.valueOf(level_temp) <= 0) {
            throw new YamiShopBindException("代理层级错误");
        }
        int level = Integer.valueOf(level_temp);
        String page_no = request.getParameter("page_no");
        if (StringUtils.isNullOrEmpty(page_no)
                || !StringUtils.isInteger(page_no) || Integer.valueOf(page_no) <= 0) {
            page_no = "1";
        }
        int pageNo = Integer.valueOf(page_no);
        String partyId = SecurityUtils.getUser().getUserId();
        Map<String, Object> data = new HashMap<String, Object>();
        Map<String, Object> data_total = new HashMap<String, Object>();
        List<Map<String, Object>> dataChilds = new ArrayList<Map<String, Object>>();
        ChildrenLever childrenLever = userDataService.cacheChildrenLever4(partyId);
        data.put("children", childrenLever.getLever1().size()
                + childrenLever.getLever2().size()
                + childrenLever.getLever3().size());
        data.put("level_1", childrenLever.getLever1().size());
        data.put("level_2", childrenLever.getLever2().size());
        data.put("level_3", childrenLever.getLever3().size());
//            data.put("level_4", childrenLever.getLever4().size());
        data_total.put("total", data);
        // 资金盘 定制化需求,后面盘口下架可以删
        dataChilds = this.userDataService.getChildrenLevelPagedForGalaxy(pageNo, 10, partyId, level);
 
        Map<String, UserData> map = userDataService.cacheByPartyId(partyId);
        double sum = 0;
        if (null != map && map.size() > 0) {
            for (UserData userData : map.values()) {
                sum += userData.getRecharge();
            }
        }
        // 总充值
        data_total.put("recharge_sum", sum);
 
        // 加密用户名
        handleChilds(dataChilds);
        data_total.put("list", dataChilds);
        return Result.succeed(data_total);
    }
 
    /**
     * 交易所-数据总览-PC端
     */
    @RequestMapping( "api/promote!getPromoteData.action")
    public Result<?> getPromoteData(HttpServletRequest request) {
        String partyId = SecurityUtils.getUser().getUserId();
        Map<String, String> dataMap = new HashMap<>();
        try {
            Date date = new Date();
            Date startTime = null;
            Date endTime = null;
            String type = request.getParameter("type");
            if (type == null) {
                return Result.failed("类型不能为空");
            }
            if (type.equals("day")) {
                startTime = DateUtils.getDayStart(DateUtils.addDate(date, 1));
                endTime = DateUtils.getDayEnd(DateUtils.addDate(date, 1));
            } else if (type.equals("week")) {
                startTime = DateUtil.getFirstDateOfWeek(date);
                endTime = DateUtil.getLastDateOfWeek(date);
            } else if (type.equals("month")) {
                startTime = DateUtil.getFirstDateOfMonth(date);
                endTime = DateUtil.getLastDateOfMonth(date);
            }
            System.out.println("推广数据总览 开始时间" + startTime);
            System.out.println("推广数据总览 结束时间" + endTime);
            dataMap = userDataService.getPromoteData(partyId, dataMap, startTime, endTime);
 
            Map<String, UserData> map = userDataService.cacheByPartyId(partyId);
            double sum = 0;
            if (null != map && map.size() > 0) {
                for (UserData userData : map.values()) {
                    sum += userData.getRechargeRecom();
                }
            }
 
            dataMap.put("rechargeRecom", String.valueOf(sum));
 
            return Result.succeed(dataMap);
        } catch (BusinessException e) {
            return Result.failed(e.getMessage());
        } catch (Throwable e) {
            log.error("error:", e);
            return Result.failed("程序错误");
        }
    }
 
    /**
     * 加密用户名
     */
    protected void handleChilds(List<Map<String, Object>> dataChilds) {
        for (Map<String, Object> data : dataChilds) {
            String username = data.get("username").toString();
            int length = username.length();
            if (username.length() > 2) {
                data.put("username", username.substring(0, 3) + "***" + username.substring(length - 3));
            }
        }
    }
 
    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);
    }
}