基于mall4j产品的二开项目后端
lee
2024-12-18 efdb99f8cecc4afb592afad79c761081d5d5cf22
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
package com.yami.shop.api.controller;
 
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yami.shop.bean.vo.DeviceIncomeVO;
import com.yami.shop.bean.model.*;
import com.yami.shop.common.constants.DictConstant;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.common.util.DateUtils;
import com.yami.shop.service.*;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
 
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.util.*;
 
@RestController
@RequestMapping("/tests")
public class TestsController {
 
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
 
 
    @Autowired
    private CdnTeamRelationService cdnTeamRelationService;
 
    @Autowired
    private AgentLevelService agentLevelService;
 
    @Autowired
    private CdnFlowService cdnFlowService;
 
    @Autowired
    private CdnUserWalletService cdnUserWalletService;
 
    @Autowired
    private CdnConfigService cdnConfigService;
 
    @Autowired
    private DeviceService deviceService;
 
    @Autowired
    private UserService userService;
 
    @Autowired
    private MarsAdverIncomeService marsAdverIncomeService;
 
    @Autowired
    private DeviceProfitLogService deviceProfitLogService;
 
    /**
     * 用户等级 判定算法(手机卡)
     */
    @GetMapping("/cancelUserLevel")
    public ServerResponseEntity<?> cancelUserLevel() {
        Date now = new Date();
        logger.info("用户等级调整开始。。。"+ DateUtil.format(now, "yyyy-MM-dd HH:mm:ss"));
        List<CdnTeamRelation> cdnTeamRelationList = cdnTeamRelationService.list(new LambdaQueryWrapper<CdnTeamRelation>());
        List<AgentLevel> agentLevelList = agentLevelService.getCdnAgentLevelList();
        List<CdnTeamRelation>  lists = new ArrayList<>();
        for (CdnTeamRelation relation : cdnTeamRelationList) {
            for (AgentLevel agentLevel : agentLevelList) {
                if(relation.getLevel() >= agentLevel.getId()){//等级只能升 不能降
                    continue;
                }else{
                    //直推人
                    Integer num = 0;//直推会员人数
                    Integer total = 0;//激活账号数
                    List<CdnTeamRelation> cdnTeamRelations = cdnTeamRelationService.getTeamRelationListSort(relation.getUserId());
                    if(cdnTeamRelations.size() >= agentLevel.getInvite()){//判断直推人条件
                        for (CdnTeamRelation c:cdnTeamRelations){//直推会员人数
                            if(c.getLevel() == (agentLevel.getId()-1)){
                                num++;
                            }
                        }
                        String pids = null;
                        if (StringUtils.isNotBlank(relation.getPids())) {
                            pids = relation.getPids() + relation.getUserId() + ",";
                        } else {
                            pids = "," + relation.getUserId() + ",";
                        }
                        CdnTeamRelation cdns = new CdnTeamRelation();
                        cdns.setPids(pids);
                        List<CdnTeamRelation> cdnTeamList = cdnTeamRelationService.getDividendList(cdns);
                        for (CdnTeamRelation c:cdnTeamList){
                            User d = new User();
                            d.setUserId(c.getUserId());
                            d.setActiveStatus("1");
                            total += userService.getctiveStatusCount(d);
                        }
                        if(num >= agentLevel.getMember() && total  >= agentLevel.getDeviceActive()){
                            relation.setLevel(agentLevel.getId());
                            if("V7".equals(agentLevel.getName())){
                                User user = userService.getUserByUserId(relation.getUserId());
                                if(user != null && user.getActiveTime() != null){
                                    if(DateUtils.flagIn30DayDate(new SimpleDateFormat("yyyy-MM-dd").format(user.getActiveTime()))){
                                        user.setActiveStatus("1");
                                        userService.updateById(user);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            lists.add(relation);
        }
        cdnTeamRelationService.updateBatchById(lists);
        return ServerResponseEntity.success();
    }
 
 
    /**
     * 帮扶、分享 、平级(手机卡)
     */
    @GetMapping("/cancelAssist")
    public ServerResponseEntity<?> cancelAssist() {
        Date now = new Date();
        logger.info("团队奖励计算开始。。。"+DateUtil.format(now, "yyyy-MM-dd HH:mm:ss"));
        //先计算合伙人分红 查询
        BigDecimal companyPoint = BigDecimal.valueOf(Double.valueOf( cdnConfigService.selectValueByName("company_point")));//公司沉淀
        BigDecimal teamPoint = BigDecimal.valueOf(Double.valueOf( cdnConfigService.selectValueByName("team_point")));//团队奖比例
        BigDecimal  partnerPoint = BigDecimal.valueOf(Double.valueOf( cdnConfigService.selectValueByName("partner_point")));//合伙人
        BigDecimal  directPoint = BigDecimal.valueOf(Double.valueOf( cdnConfigService.selectValueByName("share")));//直推奖
        BigDecimal  jcPoint = BigDecimal.valueOf(Double.valueOf( cdnConfigService.selectValueByName("team_range_point")));//团队中的级差比例
        BigDecimal incomeCount = marsAdverIncomeService.selectAllIncome(null);
        if(incomeCount != null){
            //合伙人总数
            List<User> userList = userService.list(new LambdaQueryWrapper<User>().eq(User::getIsPartner,1));
            for (User user:userList){
                //全天的总收益 *(1-公司沉淀0.3) * 50%(团队奖) * 8%  / 合伙人(查询)的总数
                BigDecimal money = incomeCount.multiply(BigDecimal.valueOf(1).subtract(companyPoint)).multiply(teamPoint).multiply(partnerPoint).divide(BigDecimal.valueOf(userList.size()),2, RoundingMode.HALF_UP);
                CdnFlow cdnFlows = new CdnFlow();
                CdnUserWallet cdnUserWallets = cdnUserWalletService.getOne(Wrappers.lambdaQuery(CdnUserWallet.class)
                        .eq(CdnUserWallet::getUserId, user.getUserId())
                        .eq(CdnUserWallet::getWalletId, DictConstant.WALLET_ID_10));
                if(cdnUserWallets.getMoney().compareTo(new BigDecimal(0)) == 0){
                    continue;
                }
                cdnFlows.setUserId(user.getUserId());
                cdnFlows.setUserIds(user.getUserId());
                if(cdnUserWallets.getMoney().compareTo(money) < 0){
                    cdnFlows.setMemo("合伙人("+user.getUserId()+")释放"+cdnUserWallets.getMoney());
                    cdnFlows.setMoney(cdnUserWallets.getMoney());
                }else{
                    cdnFlows.setMemo("合伙人("+user.getUserId()+")释放"+money);
                    cdnFlows.setMoney(money);
                }
                cdnFlows.setOldmoney(cdnUserWallets != null ? cdnUserWallets.getMoney() : BigDecimal.ZERO);
                cdnFlows.setFlownameId(DictConstant.FLOWNAME_ID_30);
                cdnFlows.setCreateTime(new Date());
                cdnFlows.setWalletId(DictConstant.WALLET_ID_10);
                cdnFlows.setDate(new Date());
                cdnFlowService.save(cdnFlows);
                cdnUserWallets.setMoney(cdnFlows.getOldmoney().subtract(cdnFlows.getMoney()));
                cdnUserWallets.setCreateTime(new Date());
                cdnUserWalletService.updateById(cdnUserWallets);
            }
        }
        List<CdnTeamRelation> cdnTeamRelationLists = cdnTeamRelationService.getTeamRelationListSort(null);
        Map<Integer,BigDecimal> map  =new HashMap<>();
        for (CdnTeamRelation cdnTeamRelation :cdnTeamRelationLists){
            //查询个人的幸运池 没有直接无收益
            CdnUserWallet cdnUserWallet = cdnUserWalletService.getOne(Wrappers.lambdaQuery(CdnUserWallet.class)
                    .eq(CdnUserWallet::getUserId,cdnTeamRelation.getUserId())
                    .eq(CdnUserWallet::getWalletId,10));
            if(cdnUserWallet.getMoney().compareTo(BigDecimal.ZERO) == 0){
                continue;
            }
            BigDecimal income = marsAdverIncomeService.selectAllIncome(cdnTeamRelation.getUserId());
            if(income  == null || income.compareTo(BigDecimal.ZERO) == 0){//没有业绩的
                continue;
            }
            CdnTeamRelation pCdnTeamRelation = cdnTeamRelationService.geteamRelationByUserId(cdnTeamRelation.getPid());
            if(pCdnTeamRelation != null){
                //直推奖 个人业绩 * (1-公司沉淀0.3) * 50%(团队奖) * 10%
                BigDecimal ztmoney = income.multiply(BigDecimal.valueOf(1).subtract(companyPoint)).multiply(teamPoint).multiply(directPoint);
                if(ztmoney.compareTo(BigDecimal.valueOf(0.01)) >= 0) {
                    setFlowLogs(pCdnTeamRelation.getUserId(), cdnTeamRelation.getUserId(), ztmoney, DictConstant.WALLET_ID_10, "直推奖励", DictConstant.FLOWNAME_ID_30);
                }
                //循环所有上级用户 计算平级奖 直到1毛为止
                if(cdnTeamRelation.getPids() != null) {
                    String[] pidsArrays = Arrays.copyOfRange(cdnTeamRelation.getPids().split(StrUtil.COMMA), 1, cdnTeamRelation.getPids().split(StrUtil.COMMA).length);
                    AgentLevel agentLevel = agentLevelService.getById(cdnTeamRelation.getLevel());
                    //加速释放比例
                    Integer id = 0;
                    BigDecimal releases= BigDecimal.ZERO;
                    if(agentLevel != null){
                        releases= agentLevel.getReleases();
                    }
                    Integer ids = 0;
//                    if(agentLevel != null){
//                        ids= agentLevel.getId();
//                    }
                    Integer parallels = 0;
                    // 将各个会员等级的收益全部汇总  个人今日收益 * (1-公司沉淀0.3) * 50%(团队奖) * 10%
                    BigDecimal countPoint = income.multiply(BigDecimal.valueOf(1).subtract(companyPoint)).multiply(teamPoint);
                    BigDecimal djPoint = countPoint.multiply(BigDecimal.valueOf(1).subtract(jcPoint));
                    BigDecimal pjCount = countPoint;
                    for (int i = pidsArrays.length - 1; i >= 0; i--) {
                        BigDecimal childMoeny = BigDecimal.ZERO;
                        CdnTeamRelation cdnTeamRelationPids = cdnTeamRelationService.getOne(new LambdaQueryWrapper<CdnTeamRelation>().eq(CdnTeamRelation::getUserId, pidsArrays[i]));
                        AgentLevel agentLevels = agentLevelService.getById(cdnTeamRelationPids.getLevel());
                        if(agentLevels != null){
                            if(map.containsKey(agentLevels.getId())){
                                BigDecimal value = map.get(agentLevels.getId()).add(djPoint);
                                map.put(agentLevels.getId(),value);
                            }else{
                                BigDecimal value = djPoint;
                                map.put(agentLevels.getId(),value);
                            }
                            //获取收益
                            if(id < agentLevels.getId()) {
                                childMoeny = countPoint.multiply(jcPoint).multiply((agentLevels.getReleases().subtract(releases)));
                                if(childMoeny.compareTo(BigDecimal.valueOf(0.01)) >= 0){
                                    setFlowLogs(cdnTeamRelationPids.getUserId(),cdnTeamRelation.getUserId(),childMoeny,DictConstant.WALLET_ID_10,"团队奖(级差)",DictConstant.FLOWNAME_ID_30);
                                }
                                releases = agentLevels.getReleases();
                                id = agentLevels.getId();
                            }
 
                            if (cdnTeamRelationPids.getLevel() == ids && cdnTeamRelationPids.getLevel() > parallels && ids != 0) {
                                //平级奖 本用户收益的10% 贡献给上级
                                BigDecimal pjmoney = pjCount.multiply(agentLevels.getParallels());
                                if (pjmoney.compareTo(BigDecimal.valueOf(0.01)) >= 0) {
                                    setFlowLogs(cdnTeamRelationPids.getUserId(), cdnTeamRelation.getUserId(), pjmoney, DictConstant.WALLET_ID_10, "管理津贴", DictConstant.FLOWNAME_ID_30);
                                }
                                ids = cdnTeamRelationPids.getLevel();
                                parallels = ids;
                                pjCount = pjmoney;
                            } else {
                                    ids = cdnTeamRelationPids.getLevel();
                            }
                        }else{
                            ids = 0;
                        }
                    }
                }
            }
        }
        //计算团队奖的业绩部分 例如:V1总业绩(是否带团队的收益)
        List<AgentLevel>  list = agentLevelService.list();
        for (AgentLevel agentLevel : list) {
            //根据会员等级计算今日总业绩
            BigDecimal count  = cdnTeamRelationService.selectTMell(agentLevel.getId());
            if(count  == null || count.compareTo(BigDecimal.ZERO) == 0){//没有业绩的
                continue;
            }
            //循环所有会员(V1会员)
            List<CdnTeamRelation> cdnTeamRelations = cdnTeamRelationService.getTeamRelationListByAgentLevel(agentLevel.getId());
            for (CdnTeamRelation c :cdnTeamRelations){
                //查询个人的幸运池 没有直接无收益
                CdnUserWallet cdnUserWallet = cdnUserWalletService.getOne(Wrappers.lambdaQuery(CdnUserWallet.class)
                        .eq(CdnUserWallet::getUserId,c.getUserId())
                        .eq(CdnUserWallet::getWalletId,10));
                if(cdnUserWallet.getMoney().compareTo(BigDecimal.ZERO) == 0){
                    continue;
                }
                if(c.getTeamTodayMobile() > 0){
                    BigDecimal pjmoney = map.get(c.getLevel()).multiply(BigDecimal.valueOf(c.getTeamTodayMobile())).divide(count,BigDecimal.ROUND_CEILING);
                    if (pjmoney.compareTo(BigDecimal.valueOf(0.01)) >= 0) {
                        setFlowLogs(c.getUserId(), null, pjmoney, DictConstant.WALLET_ID_10, "团队奖(业绩部分)", DictConstant.FLOWNAME_ID_30);
                    }
                }
            }
        }
        return ServerResponseEntity.success();
    }
 
    /**
     *
     * @param userId 受益人
     * @param userIds 贡献人
     * @param money 收益金额
     * @param walletId 钱包类型
     * @param desc 收益描述
     * @param flownameId 流水类型
     */
    private void  setFlowLogs(String userId,String userIds,BigDecimal money,Integer walletId,String desc,Integer flownameId){
        CdnFlow cdnFlow = new CdnFlow();
        cdnFlow.setUserIds(userIds);
        cdnFlow.setUserId(userId);
        cdnFlow.setFlownameId(flownameId);
        cdnFlow.setWalletId(walletId);
        //钱包更改
        CdnUserWallet cdnUserWallet = cdnUserWalletService.getOne(Wrappers.lambdaQuery(CdnUserWallet.class)
                .eq(CdnUserWallet::getUserId,userId)
                .eq(CdnUserWallet::getWalletId,walletId));
        if(cdnUserWallet.getMoney().compareTo(BigDecimal.ZERO) == 0){
            return;
        }
        if(money.compareTo(BigDecimal.ZERO) <= 0){//绿积分为0 已经释放完毕
            return;
        }else{
            if(money.compareTo(cdnUserWallet.getMoney()) >= 0){//分润超过剩余绿积分 释放最后的绿积分 并将SN改为结束
                cdnFlow.setMoney(cdnUserWallet.getMoney());
            }else{
                cdnFlow.setMoney(money);
            }
        }
        cdnFlow.setMemo(desc+":释放用户"+userIds);
        cdnFlow.setCreateTime(new Date());
        cdnFlow.setDate(new Date());
        cdnFlow.setOldmoney(cdnUserWallet.getMoney());
        cdnFlowService.save(cdnFlow);
        cdnUserWallet.setUserId(userId);
        cdnUserWallet.setWalletId(walletId);
        cdnUserWallet.setMoney(cdnUserWallet.getMoney().subtract(cdnFlow.getMoney()));
        cdnUserWallet.setUpdateTime(new Date());
        cdnUserWalletService.updateById(cdnUserWallet);
    }
 
    /**
     * 个人抽成
     * @return
     */
    @GetMapping("/cancelGeneral")
    public ServerResponseEntity<?> cancelGeneral() {
        Date now = new Date();
        logger.info("个人分成计算开始。。。"+DateUtil.format(now, "yyyy-MM-dd HH:mm:ss"));
        BigDecimal companyPoint = BigDecimal.valueOf(Double.valueOf( cdnConfigService.selectValueByName("company_point")));//公司沉淀
        BigDecimal mellPoint = BigDecimal.valueOf(Double.valueOf( cdnConfigService.selectValueByName("mell_point")));//个人比例
        List<CdnTeamRelation> cdnTeamRelationLists = cdnTeamRelationService.getTeamRelationListSort(null);
        for (CdnTeamRelation cdnTeamRelation : cdnTeamRelationLists) {
            //查询个人的幸运池 没有直接无收益
            CdnUserWallet cdnUserWallet = cdnUserWalletService.getOne(Wrappers.lambdaQuery(CdnUserWallet.class)
                    .eq(CdnUserWallet::getUserId,cdnTeamRelation.getUserId())
                    .eq(CdnUserWallet::getWalletId,DictConstant.WALLET_ID_10));
            if(cdnUserWallet.getMoney().compareTo(BigDecimal.ZERO) == 0){
                continue;
            }
            BigDecimal income = marsAdverIncomeService.selectAllIncome(cdnTeamRelation.getUserId());
            if(income != null  && income.compareTo(BigDecimal.ZERO) > 0){
                setFlowLogs(cdnTeamRelation.getUserId(), cdnTeamRelation.getUserId(), income.multiply(BigDecimal.valueOf(1).subtract(companyPoint)).multiply(mellPoint), DictConstant.WALLET_ID_10, "个人分润", DictConstant.FLOWNAME_ID_30);
            }
        }
        return ServerResponseEntity.success();
    }
 
 
    @PostMapping("/insertIncome")
    public ServerResponseEntity<?> cancelUserLevel(@RequestBody DeviceIncomeVO deviceVO) {
        Date now = new Date();
        logger.info("插入设备收益。。。"+DateUtil.format(now, "yyyy-MM-dd HH:mm:ss"));
        boolean b = deviceProfitLogService.saveBatch(deviceVO.getDeviceProfitLogs());
        if (b){
            logger.info("插入设备收益结束。。。"+DateUtil.format(now, "yyyy-MM-dd HH:mm:ss"));
            return ServerResponseEntity.success();
        }else{
            return ServerResponseEntity.showFailMsg("插入失败");
        }
    }
 
}