“mitao”
2026-02-03 6b56672a7e02a0bf340021f00a4898bee9b13834
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
package com.ruoyi.system.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.basic.PageInfo;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUserApplet;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.system.mapper.TAppUserMapper;
import com.ruoyi.system.model.*;
import com.ruoyi.system.query.AppUserListQuery;
import com.ruoyi.system.query.PromotionPageListQuery;
import com.ruoyi.system.query.UserPromotionPageListQuery;
import com.ruoyi.system.service.*;
import com.ruoyi.system.vo.PromotionPageListVo;
import com.ruoyi.system.vo.UserPromotionPageListVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
 
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
 
/**
 * <p>
 * 用户管理 服务实现类
 * </p>
 *
 * @author xiaochen
 * @since 2025-11-06
 */
@Slf4j
@Service
@RequiredArgsConstructor(onConstructor_ = {@Lazy})
public class TAppUserServiceImpl extends ServiceImpl<TAppUserMapper, TAppUser> implements TAppUserService {
    private final ISysUserService sysUserService;
 
    @Autowired
    private TBigGiftPackageOrderService tBigGiftPackageOrderService;
 
    @Autowired
    private TPhysicalGoodsOrderService tPhysicalGoodsOrderService;
 
    @Autowired
    private TCommissionTransactionFlowService tCommissionTransactionFlowService;
 
    @Autowired
    private TIntegralSettlementService tIntegralSettlementService;
 
    @Autowired
    private TCompanyService tCompanyService;
 
    @Override
    public PageInfo<TAppUser> pageList(AppUserListQuery query) {
        PageInfo<TAppUser> pageInfo = new PageInfo<>(query.getPageNum(), query.getPageSize());
        List<TAppUser> list = this.baseMapper.pageList(query,pageInfo);
        pageInfo.setRecords( list);
        return pageInfo;
    }
    @Override
    public PageInfo<TAppUser> transferPageUserList(AppUserListQuery query) {
        PageInfo<TAppUser> pageInfo = new PageInfo<>(query.getPageNum(), query.getPageSize());
        List<TAppUser> list = this.baseMapper.transferPageUserList(query,pageInfo);
        pageInfo.setRecords( list);
        return pageInfo;
    }
 
    @Override
    public void confirmSuperior(Integer superiorType, Integer superiorId, LoginUserApplet loginUserApplet) {
        TAppUser appUser = this.lambdaQuery().eq(TAppUser::getId, loginUserApplet.getUserId()).ne(TAppUser::getStatus,3)
                .last("LIMIT 1").one();
        if (Objects.nonNull(appUser)) {
            if (superiorType.equals(1)) {
                //查询上级用户所属公司ID
                SysUser sysUser = sysUserService.selectUserById(superiorId.longValue());
                if (Objects.nonNull(sysUser)) {
                    appUser.setReferrerSysUserId(superiorId);
                    appUser.setReferrerSysUserTime(LocalDateTime.now());
                } else {
                    log.info("推广员不存在,绑定superiorId={}", superiorId);
                }
            } else {
                if (Objects.isNull(appUser.getReferrerStatus()) || appUser.getReferrerStatus().equals(1)) {
                    appUser.setReferrerUserId(superiorId);
                    appUser.setReferrerTime(LocalDateTime.now());
                    appUser.setReferrerStatus(1);
                }
            }
            this.updateById(appUser);
        }
    }
 
    @Override
    public List<PromotionPageListVo> promotionPageList(Page<PromotionPageListVo> page, PromotionPageListQuery query) {
        if (Objects.isNull(query.getCompanyId())) {
            throw new ServiceException("公司ID不能为空");
        }
        List<PromotionPageListVo> list = this.baseMapper.promotionPageList(page,query);
 
        if (!list.isEmpty()) {
            List<Integer> userIds = list.stream().map(PromotionPageListVo::getUserId).collect(Collectors.toList());
//            List<TPhysicalGoodsOrder> physicalGoodsOrders = tPhysicalGoodsOrderService.list(new LambdaQueryWrapper<TPhysicalGoodsOrder>().in(TPhysicalGoodsOrder::getAppUserId, userIds).notIn(TPhysicalGoodsOrder::getStatus,Arrays.asList(1,4)));
            List<TBigGiftPackageOrder> giftPackageOrders = tBigGiftPackageOrderService.list(new LambdaQueryWrapper<TBigGiftPackageOrder>().in(TBigGiftPackageOrder::getAppUserId, userIds).notIn(TBigGiftPackageOrder::getStatus,Arrays.asList(1,5)));
 
 
            List<TCommissionTransactionFlow> flows = tCommissionTransactionFlowService.list(new LambdaQueryWrapper<TCommissionTransactionFlow>().in(TCommissionTransactionFlow::getAppUserId, userIds));
 
            List<TIntegralSettlement> settlements = tIntegralSettlementService.list(new LambdaQueryWrapper<TIntegralSettlement>().in(TIntegralSettlement::getAppUserId, userIds));
            for (PromotionPageListVo promotionPageListVo : list) {
//                BigDecimal total = physicalGoodsOrders.stream().filter(e -> e.getAppUserId().equals(promotionPageListVo.getUserId())).map(TPhysicalGoodsOrder::getPaymentPrice).reduce(BigDecimal.ZERO, BigDecimal::add);
                BigDecimal totalOne = giftPackageOrders.stream().filter(e -> e.getAppUserId().equals(promotionPageListVo.getUserId())).map(TBigGiftPackageOrder::getPaymentPrice).reduce(BigDecimal.ZERO, BigDecimal::add);
                promotionPageListVo.setTotalConsume(totalOne);
 
                BigDecimal totalIncome = flows.stream().filter(e -> e.getAppUserId().equals(promotionPageListVo.getUserId())).map(TCommissionTransactionFlow::getCommissionAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
                promotionPageListVo.setTotalIncome(totalIncome);
 
                Integer totalSettledIncome = settlements.stream().filter(e -> e.getAppUserId().equals(promotionPageListVo.getUserId())).mapToInt(TIntegralSettlement::getTotalSettlementIntegral).sum();
                promotionPageListVo.setTotalSettledIncome(totalSettledIncome);
 
            }
        }
 
 
        return list;
    }
    @Override
    public List<UserPromotionPageListVo> userPromotionPageList(Page<UserPromotionPageListVo> page, UserPromotionPageListQuery query) {
        List<UserPromotionPageListVo> list = this.baseMapper.userPromotionPageList(page,query);
 
        if (!list.isEmpty()) {
            List<Integer> userIds = list.stream().map(UserPromotionPageListVo::getUserId).collect(Collectors.toList());
//            List<TPhysicalGoodsOrder> physicalGoodsOrders = tPhysicalGoodsOrderService.list(new LambdaQueryWrapper<TPhysicalGoodsOrder>().in(TPhysicalGoodsOrder::getAppUserId, userIds).notIn(TPhysicalGoodsOrder::getStatus,Arrays.asList(1,4)));
            List<TBigGiftPackageOrder> giftPackageOrders = tBigGiftPackageOrderService.list(new LambdaQueryWrapper<TBigGiftPackageOrder>().in(TBigGiftPackageOrder::getAppUserId, userIds).notIn(TBigGiftPackageOrder::getStatus,Arrays.asList(1,5)));
 
 
            List<TCommissionTransactionFlow> flows = tCommissionTransactionFlowService.list(new LambdaQueryWrapper<TCommissionTransactionFlow>().in(TCommissionTransactionFlow::getAppUserId, userIds));
 
            List<TIntegralSettlement> settlements = tIntegralSettlementService.list(new LambdaQueryWrapper<TIntegralSettlement>().in(TIntegralSettlement::getAppUserId, userIds));
            for (UserPromotionPageListVo promotionPageListVo : list) {
//                BigDecimal total = physicalGoodsOrders.stream().filter(e -> e.getAppUserId().equals(promotionPageListVo.getUserId())).map(TPhysicalGoodsOrder::getPaymentPrice).reduce(BigDecimal.ZERO, BigDecimal::add);
                BigDecimal totalOne = giftPackageOrders.stream().filter(e -> e.getAppUserId().equals(promotionPageListVo.getUserId())).map(TBigGiftPackageOrder::getPaymentPrice).reduce(BigDecimal.ZERO, BigDecimal::add);
                promotionPageListVo.setTotalConsume(totalOne);
 
                BigDecimal totalIncome = flows.stream().filter(e -> e.getAppUserId().equals(promotionPageListVo.getUserId())).map(TCommissionTransactionFlow::getCommissionAmount).reduce(BigDecimal.ZERO,BigDecimal::add);
                promotionPageListVo.setTotalIncome(totalIncome);
 
                Integer totalSettledIncome = settlements.stream().filter(e -> e.getAppUserId().equals(promotionPageListVo.getUserId())).mapToInt(TIntegralSettlement::getTotalSettlementIntegral).sum();
                promotionPageListVo.setTotalSettledIncome(totalSettledIncome);
 
            }
        }
 
 
        return list;
    }
 
    @Override
    public List<UserPromotionPageListVo> userPromotionPageListExport(UserPromotionPageListQuery query) {
        List<UserPromotionPageListVo> list = this.baseMapper.userPromotionPageListExport(query);
 
        if (!list.isEmpty()) {
            List<Integer> userIds = list.stream().map(UserPromotionPageListVo::getUserId).collect(Collectors.toList());
//            List<TPhysicalGoodsOrder> physicalGoodsOrders = tPhysicalGoodsOrderService.list(new LambdaQueryWrapper<TPhysicalGoodsOrder>().in(TPhysicalGoodsOrder::getAppUserId, userIds).notIn(TPhysicalGoodsOrder::getStatus,Arrays.asList(1,4)));
            List<TBigGiftPackageOrder> giftPackageOrders = tBigGiftPackageOrderService.list(new LambdaQueryWrapper<TBigGiftPackageOrder>().in(TBigGiftPackageOrder::getAppUserId, userIds).notIn(TBigGiftPackageOrder::getStatus,Arrays.asList(1,5)));
 
 
            List<TCommissionTransactionFlow> flows = tCommissionTransactionFlowService.list(new LambdaQueryWrapper<TCommissionTransactionFlow>().in(TCommissionTransactionFlow::getAppUserId, userIds));
 
            List<TIntegralSettlement> settlements = tIntegralSettlementService.list(new LambdaQueryWrapper<TIntegralSettlement>().in(TIntegralSettlement::getAppUserId, userIds));
            for (UserPromotionPageListVo promotionPageListVo : list) {
//                BigDecimal total = physicalGoodsOrders.stream().filter(e -> e.getAppUserId().equals(promotionPageListVo.getUserId())).map(TPhysicalGoodsOrder::getPaymentPrice).reduce(BigDecimal.ZERO, BigDecimal::add);
                BigDecimal totalOne = giftPackageOrders.stream().filter(e -> e.getAppUserId().equals(promotionPageListVo.getUserId())).map(TBigGiftPackageOrder::getPaymentPrice).reduce(BigDecimal.ZERO, BigDecimal::add);
                promotionPageListVo.setTotalConsume(totalOne);
 
                BigDecimal totalIncome = flows.stream().filter(e -> e.getAppUserId().equals(promotionPageListVo.getUserId())).map(TCommissionTransactionFlow::getCommissionAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
                promotionPageListVo.setTotalIncome(totalIncome);
 
                Integer totalSettledIncome = settlements.stream().filter(e -> e.getAppUserId().equals(promotionPageListVo.getUserId())).mapToInt(TIntegralSettlement::getTotalSettlementIntegral).sum();
                promotionPageListVo.setTotalSettledIncome(totalSettledIncome);
 
            }
        }
 
 
        return list;
    }
 
    @Override
    public List<PromotionPageListVo> promotionPageListExport(PromotionPageListQuery query) {
        List<PromotionPageListVo> list = this.baseMapper.promotionPageListExport(query);
 
        if (!list.isEmpty()) {
            List<Integer> userIds = list.stream().map(PromotionPageListVo::getUserId).collect(Collectors.toList());
//            List<TPhysicalGoodsOrder> physicalGoodsOrders = tPhysicalGoodsOrderService.list(new LambdaQueryWrapper<TPhysicalGoodsOrder>().in(TPhysicalGoodsOrder::getAppUserId, userIds).notIn(TPhysicalGoodsOrder::getStatus,Arrays.asList(1,4)));
            List<TBigGiftPackageOrder> giftPackageOrders = tBigGiftPackageOrderService.list(new LambdaQueryWrapper<TBigGiftPackageOrder>().in(TBigGiftPackageOrder::getAppUserId, userIds).notIn(TBigGiftPackageOrder::getStatus,Arrays.asList(1,5)));
 
 
            List<TCommissionTransactionFlow> flows = tCommissionTransactionFlowService.list(new LambdaQueryWrapper<TCommissionTransactionFlow>().in(TCommissionTransactionFlow::getAppUserId, userIds));
 
            List<TIntegralSettlement> settlements = tIntegralSettlementService.list(new LambdaQueryWrapper<TIntegralSettlement>().in(TIntegralSettlement::getAppUserId, userIds));
            for (PromotionPageListVo promotionPageListVo : list) {
//                BigDecimal total = physicalGoodsOrders.stream().filter(e -> e.getAppUserId().equals(promotionPageListVo.getUserId())).map(TPhysicalGoodsOrder::getPaymentPrice).reduce(BigDecimal.ZERO, BigDecimal::add);
                BigDecimal totalOne = giftPackageOrders.stream().filter(e -> e.getAppUserId().equals(promotionPageListVo.getUserId())).map(TBigGiftPackageOrder::getPaymentPrice).reduce(BigDecimal.ZERO, BigDecimal::add);
                promotionPageListVo.setTotalConsume(totalOne);
 
                BigDecimal totalIncome = flows.stream().filter(e -> e.getAppUserId().equals(promotionPageListVo.getUserId())).map(TCommissionTransactionFlow::getCommissionAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
                promotionPageListVo.setTotalIncome(totalIncome);
 
                Integer totalSettledIncome = settlements.stream().filter(e -> e.getAppUserId().equals(promotionPageListVo.getUserId())).mapToInt(TIntegralSettlement::getTotalSettlementIntegral).sum();
                promotionPageListVo.setTotalSettledIncome(totalSettledIncome);
 
            }
        }
 
 
        return list;
    }
 
    @Override
    public List<TAppUser> exportAppUser(AppUserListQuery query) {
        List<TAppUser> list = this.baseMapper.exportAppUser(query);
        return list;
    }
 
    @Override
    public TCompany getUserCompany(Integer appUserId) {
        //默认返回空对象
        TCompany company = new TCompany();
        TAppUser appUser = this.getById(appUserId);
        if (Objects.nonNull(appUser)) {
            if (Objects.nonNull(appUser.getCompanyId())) {
                company = tCompanyService.getById(appUser.getCompanyId());
            } else {
                // 判断是否同时存在两种推荐来源且推荐状态为临时
                boolean hasBothReferrer = appUser.getReferrerUserId() != null
                        && Integer.valueOf(1).equals(appUser.getReferrerStatus())
                        && appUser.getReferrerSysUserId() != null;
 
                if (hasBothReferrer) {
                    // 两种推荐来源都存在,比较推荐时间,取后推荐的用户的 companyId
                    LocalDateTime referrerTime = appUser.getReferrerTime();
                    LocalDateTime referrerSysUserTime = appUser.getReferrerSysUserTime();
 
                    // 判断哪个推荐时间更晚
                    boolean useSysUserCompany = false;
                    if (referrerSysUserTime != null && referrerTime != null) {
                        useSysUserCompany = referrerSysUserTime.isAfter(referrerTime);
                    } else if (referrerSysUserTime != null) {
                        useSysUserCompany = true;
                    }
                    // 如果 referrerTime != null 且 referrerSysUserTime == null,则 useSysUserCompany 保持 false
 
                    if (useSysUserCompany) {
                        // 使用股东(SysUser)的 companyId
                        SysUser sysUser = sysUserService.selectUserById(Long.valueOf(appUser.getReferrerSysUserId()));
                        if (sysUser != null && sysUser.getCompanyId() != null) {
                            company = tCompanyService.getById(sysUser.getCompanyId());
                            log.info("用户{}两种推荐来源都存在,股东推荐时间({})晚于普通推荐时间({}),通过股东(SysUser:{})关联公司{}进行加下单",
                                    appUserId, referrerSysUserTime, referrerTime, appUser.getReferrerSysUserId(), company.getId());
                        }
                    } else {
                        // 使用推荐人(TAppUser)的 companyId
                        TAppUser referrerAppUser = this.getById(appUser.getReferrerUserId());
                        if (referrerAppUser != null && referrerAppUser.getCompanyId() != null) {
                            company = tCompanyService.getById(referrerAppUser.getCompanyId());
                            log.info("用户{}两种推荐来源都存在,普通推荐时间({})晚于或等于股东推荐时间({}),通过推荐人(TAppUser:{})关联公司{}进行加下单",
                                    appUserId, referrerTime, referrerSysUserTime, appUser.getReferrerUserId(), company.getId());
                        }
                    }
                } else {
                    // 只有一种推荐来源,按原逻辑处理
                    // 优先使用 referrerSysUserId 对应的 SysUser 的 companyId
                    if (appUser.getReferrerSysUserId() != null) {
                        SysUser sysUser = sysUserService.selectUserById(Long.valueOf(appUser.getReferrerSysUserId()));
                        if (sysUser != null && sysUser.getCompanyId() != null) {
                            company = tCompanyService.getById(sysUser.getCompanyId());
                            log.info("用户{}通过股东(SysUser:{})关联公司{}进行加下单", appUserId, appUser.getReferrerSysUserId(), company.getId());
                        }
                    }
 
                    // 如果 referrerSysUserId 为空或对应的 SysUser 没有 companyId,则使用 referrerUserId 对应的 TAppUser 的 companyId
                    if (company.getId() == null && appUser.getReferrerUserId() != null) {
                        TAppUser referrerAppUser = this.getById(appUser.getReferrerUserId());
                        if (referrerAppUser != null && referrerAppUser.getCompanyId() != null) {
                            company = tCompanyService.getById(referrerAppUser.getCompanyId());
                            log.info("用户{}通过推荐人(TAppUser:{})关联公司{}进行加下单", appUserId, appUser.getReferrerUserId(), company.getId());
                        }
                    }
                }
            }
 
        }
        return company;
    }
}