Pu Zhibing
2025-03-26 cbf2486983b77a27af9968bbb362cb8d43789115
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
package com.ruoyi.account.service.impl;
 
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.account.api.model.AppUser;
import com.ruoyi.account.api.model.BalanceChangeRecord;
import com.ruoyi.account.api.model.BalanceChangeRecordCopy;
import com.ruoyi.account.dto.BalanceQuery;
import com.ruoyi.account.mapper.BalanceChangeRecordMapper;
import com.ruoyi.account.service.AppUserService;
import com.ruoyi.account.service.BalanceChangeRecordService;
import com.ruoyi.account.vo.CommissionStatistics;
import com.ruoyi.account.vo.WalletStatistics;
import com.ruoyi.account.vo.WalletStatisticsDetail;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.bean.BeanUtils;
import com.ruoyi.order.feignClient.RemoteOrderGoodsClient;
import com.ruoyi.order.model.Order;
import com.ruoyi.other.api.domain.ShopBalanceStatement;
import com.ruoyi.other.api.feignClient.ShopClient;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
 
@Service
public class BalanceChangeRecordServiceImpl extends ServiceImpl<BalanceChangeRecordMapper, BalanceChangeRecord> implements BalanceChangeRecordService {
    @Resource
    private ShopClient shopClient;
    @Resource
    private RemoteOrderGoodsClient remoteOrderGoodsClient;
    @Resource
    private AppUserService appUserService;
 
    @Override
    public IPage<BalanceChangeRecord> pageList(BalanceQuery agentQuery) {
        Page<BalanceChangeRecord> page = new Page<>();
        page.setCurrent(agentQuery.getPageNum());
        page.setSize(agentQuery.getPageSize());
        return this.baseMapper.pageList(page, agentQuery);
    }
 
    @Override
    public CommissionStatistics commissionStatistics(Page<BalanceChangeRecord> page, BalanceChangeRecord balanceChangeRecord) {
        balanceChangeRecord.setChangeType(4);
        List<BalanceChangeRecord> balanceChangeRecordList = this.baseMapper.selectBalanceChangeRecordList(balanceChangeRecord);
 
        BigDecimal totalCommission = balanceChangeRecordList.stream()
                .filter(item -> !(item.getChangeType().equals(2) && item.getChangeType().equals(5)))
                .map(BalanceChangeRecord::getChangeAmount)
                .reduce(BigDecimal.ZERO, BigDecimal::add)
                .setScale(2, RoundingMode.HALF_UP);
 
        IPage<BalanceChangeRecord> balanceChangeRecordIPage = this.baseMapper.queryCommissionStatistics(page, balanceChangeRecord);
        // 过滤商城购物
        List<BalanceChangeRecord> collect = balanceChangeRecordIPage.getRecords().stream().filter(e -> e.getChangeType() != 5).collect(Collectors.toList());
        for (BalanceChangeRecord changeRecord : collect) {
            if (changeRecord.getChangeDirection() == -1){
                // 减少
                changeRecord.setChangeAmountString("-¥"+changeRecord.getChangeAmount());
            }else {
                changeRecord.setChangeAmountString("+¥"+changeRecord.getChangeAmount());
            }
        }
        balanceChangeRecordIPage.setRecords(collect);
        return new CommissionStatistics(totalCommission, balanceChangeRecordIPage);
    }
 
    @Override
    public WalletStatistics walletStatistics(Page<BalanceChangeRecord> page, BalanceChangeRecord balanceChangeRecord) {
        ShopBalanceStatement shopBalanceStatement = new ShopBalanceStatement();
        shopBalanceStatement.setType(1);
 
        List<BalanceChangeRecord> balanceChangeRecordList = this.baseMapper.selectBalanceChangeRecordList1(balanceChangeRecord);
        List<WalletStatisticsDetail> walletStatisticsDetailList = new ArrayList<>();
        BigDecimal totalWithdraw = BigDecimal.ZERO;
        for (BalanceChangeRecord changeRecord : balanceChangeRecordList) {
 
            WalletStatisticsDetail walletStatisticsDetail = new WalletStatisticsDetail();
            BeanUtils.copyBeanProp(walletStatisticsDetail, changeRecord);
 
    
            if(changeRecord.getChangeType() == 2){
                BigDecimal changeAmount = changeRecord.getChangeAmount();
                totalWithdraw = totalWithdraw.add(changeAmount);
            }
            if (changeRecord.getChangeDirection() == -1){
                walletStatisticsDetail.setFlag(2);
            }else {
                walletStatisticsDetail.setFlag(1);
            }
            walletStatisticsDetailList.add(walletStatisticsDetail);
        }
        // 按时间排序(倒序)
        walletStatisticsDetailList.sort(Comparator.comparing(WalletStatisticsDetail::getCreateTime).reversed());
        long current = page.getCurrent();
        long size = page.getSize();
        if (current < 1) {
            current = 1;
        }
        int fromIndex = (int) ((current - 1) * size);
        int toIndex = (int) Math.min(fromIndex + size, walletStatisticsDetailList.size());
 
 
        Page<WalletStatisticsDetail> walletStatisticsDetailPage = new Page<>();
        WalletStatistics walletStatistics = new WalletStatistics();
        if (fromIndex >= walletStatisticsDetailList.size()) {
            walletStatistics.setPage(walletStatisticsDetailPage);
            return walletStatistics;
        }
 
 
        Map<Integer, BigDecimal> shopCommissionMap = walletStatisticsDetailList.stream()
                .collect(Collectors.groupingBy(
                        WalletStatisticsDetail::getChangeType,
                        Collectors.reducing(
                                BigDecimal.ZERO,
                                WalletStatisticsDetail::getChangeAmount,
                                BigDecimal::add
                        )
                ));
 
        BigDecimal totalRecharge = shopCommissionMap.get(1);
        BigDecimal totalShopWithdraw = shopCommissionMap.get(7);
 
        List<WalletStatisticsDetail> walletStatisticsDetailList2 = walletStatisticsDetailList.subList(fromIndex, toIndex);
        for (WalletStatisticsDetail changeRecord : walletStatisticsDetailList2) {
            Integer flag = changeRecord.getFlag();
            if (flag.equals(1)){
                changeRecord.setChangeAmountString("+¥"+changeRecord.getChangeAmount());
            }else {
                changeRecord.setChangeAmountString("-¥"+changeRecord.getChangeAmount());
            }
            walletStatisticsDetailPage.setCurrent(current);
            walletStatisticsDetailPage.setSize(size);
            walletStatisticsDetailPage.setTotal(walletStatisticsDetailList.size());
            walletStatisticsDetailPage.setRecords(walletStatisticsDetailList2);
            walletStatistics.setPage(walletStatisticsDetailPage);
            walletStatistics.setTotalRecharge(totalRecharge);
            walletStatistics.setTotalWithdraw(totalWithdraw);
            walletStatistics.setTotalShopWithdraw(totalShopWithdraw);
        }
        return walletStatistics;
    }
    
    @Override
    public IPage<BalanceChangeRecordCopy> change(Page<BalanceChangeRecordCopy> page, Integer changeType, LocalDateTime startTime, LocalDateTime endTime, Long userId) {
        List<BalanceChangeRecordCopy> change = this.baseMapper.change(page, changeType, startTime, endTime, userId);
        List<Long> orderIds = change.stream().map(BalanceChangeRecordCopy::getOrderId).collect(Collectors.toList());
        if (CollectionUtil.isNotEmpty(orderIds)){
            R<List<Order>> r = remoteOrderGoodsClient.getOrderListByIds(orderIds);
            if (!R.isSuccess(r)){
                return null;
            }
            List<Order> orderList = r.getData();
            change.forEach(bc -> {
                orderList.stream().filter(o -> o.getId().equals(bc.getOrderId())).findFirst().ifPresent(o -> {
                    Long appUserId = o.getAppUserId();
                    AppUser appUser = appUserService.getById(appUserId);
                    if(null != appUser){
                        bc.setUserName(appUser.getName());
                        bc.setAmount(o.getPaymentAmount());
                    }
                });
            
                if (bc.getChangeDirection() == -1){
                    bc.setFlag(2);
                }else {
                    bc.setFlag(1);
                }
            });
        }
        page.setRecords(change);
        return page;
    }
}