seatonwan9
2025-08-14 e4f9152bcacf02be0cb376dcb225eaf444b8951b
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
package com.webmanage.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.webmanage.entity.UserPoints;
import com.webmanage.mapper.UserPointsMapper;
import com.webmanage.service.UserPointsService;
import com.webmanage.vo.PointsDetailVO;
import com.webmanage.vo.PointsStatsVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.ArrayList;
import java.util.List;
 
/**
 * 用户积分Service实现类
 * 
 * @author webmanage
 * @date 2024-08-07
 */
@Service
public class UserPointsServiceImpl extends ServiceImpl<UserPointsMapper, UserPoints> implements UserPointsService {
    
    @Override
    public PointsStatsVO getPersonalPointsStats(Long userId) {
        UserPoints userPoints = this.baseMapper.selectByUserId(userId);
        
        PointsStatsVO statsVO = new PointsStatsVO();
        if (userPoints != null) {
            statsVO.setBalance(userPoints.getBalance());
            statsVO.setTotalEarned(userPoints.getTotalEarned());
            statsVO.setTotalConsumed(userPoints.getTotalConsumed());
            statsVO.setTotalConverted(userPoints.getTotalConverted());
        } else {
            statsVO.setBalance(0);
            statsVO.setTotalEarned(0);
            statsVO.setTotalConsumed(0);
            statsVO.setTotalConverted(0);
        }
        
        // 设置详情(这里可以根据实际需求查询数据库)
        statsVO.setEarnedDetails(new ArrayList<>());
        statsVO.setConsumedDetails(new ArrayList<>());
        statsVO.setConvertedDetails(new ArrayList<>());
        
        return statsVO;
    }
    
    @Override
    public PointsStatsVO getUnitPointsStats(Long unitId) {
        UserPoints userPoints = this.baseMapper.selectByUnitId(unitId);
        
        PointsStatsVO statsVO = new PointsStatsVO();
        if (userPoints != null) {
            statsVO.setBalance(userPoints.getBalance());
            statsVO.setTotalEarned(userPoints.getTotalEarned());
            statsVO.setTotalConsumed(userPoints.getTotalConsumed());
            statsVO.setTotalConverted(userPoints.getTotalConverted());
        } else {
            statsVO.setBalance(0);
            statsVO.setTotalEarned(0);
            statsVO.setTotalConsumed(0);
            statsVO.setTotalConverted(0);
        }
        
        // 设置获取积分详情
        List<PointsDetailVO> earnedDetails = new ArrayList<>();
        earnedDetails.add(createDetailVO("resource_contribution", 10000, 50.0));
        earnedDetails.add(createDetailVO("resource_transaction", 8000, 40.0));
        earnedDetails.add(createDetailVO("resource_dissemination", 1900, 9.5));
        earnedDetails.add(createDetailVO("other", 100, 0.5));
        statsVO.setEarnedDetails(earnedDetails);
        
        // 设置消耗积分详情
        List<PointsDetailVO> consumedDetails = new ArrayList<>();
        consumedDetails.add(createDetailVO("resource_transaction", 17500, 97.2));
        consumedDetails.add(createDetailVO("resource_dissemination", 500, 2.8));
        statsVO.setConsumedDetails(consumedDetails);
        
        // 设置转换积分详情
        statsVO.setConvertedDetails(new ArrayList<>());
        
        return statsVO;
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateUserPoints(Long userId, Long unitId, Integer points) {
        // 查询用户积分记录
        LambdaQueryWrapper<UserPoints> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserPoints::getUserId, userId);
        UserPoints userPoints = this.getOne(wrapper);
        
        if (userPoints == null) {
            // 创建新的积分记录
            userPoints = new UserPoints();
            userPoints.setUserId(userId);
            userPoints.setUnitId(unitId);
            userPoints.setBalance(points > 0 ? points : 0);
            userPoints.setTotalEarned(points > 0 ? points : 0);
            userPoints.setTotalConsumed(points < 0 ? -points : 0);
            userPoints.setTotalConverted(0);
            this.save(userPoints);
        } else {
            // 更新积分记录
            userPoints.setBalance(userPoints.getBalance() + points);
            if (points > 0) {
                userPoints.setTotalEarned(userPoints.getTotalEarned() + points);
            } else {
                userPoints.setTotalConsumed(userPoints.getTotalConsumed() - points);
            }
            this.updateById(userPoints);
        }
    }
    
    /**
     * 创建积分详情VO
     */
    private PointsDetailVO createDetailVO(String category, Integer points, Double percentage) {
        PointsDetailVO detailVO = new PointsDetailVO();
        detailVO.setCategory(category);
        detailVO.setPoints(points);
        detailVO.setPercentage(percentage);
        return detailVO;
    }
}