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(String 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(String 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(String userId, String 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;
|
}
|
}
|