| | |
| | | |
| | | import cn.hutool.core.collection.CollectionUtil; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | 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.feignClient.AppUserClient; |
| | | import com.ruoyi.account.api.model.AppUser; |
| | | import com.ruoyi.account.api.model.BalanceChangeRecord; |
| | | import com.ruoyi.account.api.model.UserPoint; |
| | | import com.ruoyi.account.enums.PointChangeType; |
| | | import com.ruoyi.account.api.model.*; |
| | | import com.ruoyi.account.mapper.UserPointCopyMapper; |
| | | import com.ruoyi.account.mapper.UserPointMapper; |
| | | import com.ruoyi.account.service.UserPointService; |
| | | import com.ruoyi.account.service.*; |
| | | import com.ruoyi.account.vo.UserPointDetailVO; |
| | | import com.ruoyi.account.vo.UserPointStatistics; |
| | | import com.ruoyi.account.vo.UserPointVO; |
| | | import com.ruoyi.account.vo.UserStatisticsDetail; |
| | | import com.ruoyi.common.core.domain.R; |
| | | import com.ruoyi.common.core.utils.PhoneNumberValidator; |
| | | import com.ruoyi.common.core.web.page.PageInfo; |
| | | import com.ruoyi.common.security.service.TokenService; |
| | | import com.ruoyi.order.feignClient.OrderClient; |
| | | import com.ruoyi.order.feignClient.RemoteOrderGoodsClient; |
| | | import com.ruoyi.order.model.Order; |
| | | import com.ruoyi.order.model.OrderGood; |
| | | import com.ruoyi.other.api.domain.VipSetting; |
| | | import com.ruoyi.other.api.enums.PointChangeType; |
| | | import com.ruoyi.system.api.domain.SysUser; |
| | | import com.ruoyi.system.api.model.LoginUser; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.util.CollectionUtils; |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.math.BigDecimal; |
| | | import java.time.LocalDateTime; |
| | | import java.util.Collections; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.time.format.DateTimeFormatter; |
| | | import java.util.*; |
| | | import java.util.concurrent.atomic.AtomicReference; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | |
| | | * @since 2024-11-21 |
| | | */ |
| | | @Service |
| | | @Slf4j |
| | | public class UserPointServiceImpl extends ServiceImpl<UserPointMapper, UserPoint> implements UserPointService { |
| | | @Resource |
| | | private AppUserClient appUserClient; |
| | | private AppUserService appUserService; |
| | | @Resource |
| | | private TokenService tokenService; |
| | | @Resource |
| | | private VipSettingService vipSettingService; |
| | | @Resource |
| | | private UserPointCopyMapper userPointCopyMapper; |
| | | @Resource |
| | | private UserPointService userPointService; |
| | | @Resource |
| | | private IAppUserGiveawayTemporaryService appUserGiveawayTemporaryService; |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | @Override |
| | | public UserPointVO getUserPoint(Long userId) { |
| | | AppUser appUser = appUserClient.getAppUserById(userId); |
| | | List<UserPoint> userPointList = list(new LambdaQueryWrapper<UserPoint>() |
| | | .eq(UserPoint::getAppUserId, userId)); |
| | | |
| | | Map<Integer, Integer> userBalanceMap = userPointList.stream() |
| | | .collect(Collectors.toMap(UserPoint::getType, UserPoint::getBalance)); |
| | | |
| | | Integer lavePoint = appUser.getLavePoint(); |
| | | AppUser appUser = appUserService.getById(userId); |
| | | VipSetting vipSetting = vipSettingService.getVipSettingByUserId(userId); |
| | | List<AppUserGiveawayTemporary> list = appUserGiveawayTemporaryService.list(new LambdaQueryWrapper<AppUserGiveawayTemporary>() |
| | | .eq(AppUserGiveawayTemporary::getAppUserId, userId)); |
| | | int totalPoint = 0; |
| | | int sharePoint = 0; |
| | | int shopAchievementPoint = 0; |
| | | int shopSharePoint = 0; |
| | | if(list.size() > 0){ |
| | | totalPoint = list.stream().mapToInt(AppUserGiveawayTemporary::getTotalPoint).sum(); |
| | | sharePoint = list.stream().mapToInt(AppUserGiveawayTemporary::getSharePoint).sum(); |
| | | shopAchievementPoint = list.stream().mapToInt(AppUserGiveawayTemporary::getShopAchievementPoint).sum(); |
| | | shopSharePoint = list.stream().mapToInt(AppUserGiveawayTemporary::getShopSharePoint).sum(); |
| | | } |
| | | UserPointVO userPointVO = new UserPointVO(); |
| | | userPointVO.setTotalPoint(lavePoint); |
| | | // userPointVO.setConsumePoint(lavePoint); |
| | | userPointVO.setShopPoint(userBalanceMap.get(PointChangeType.CONSUME.getCode())); |
| | | userPointVO.setSharePoint(userBalanceMap.get(PointChangeType.COMMISSION_RETURN.getCode())); |
| | | userPointVO.setPullNewPoint(userBalanceMap.get(PointChangeType.NEW_USER_REFERRAL.getCode())); |
| | | userPointVO.setShopAchievementPoint(userBalanceMap.get(PointChangeType.STORE_PERFORMANCE.getCode())); |
| | | userPointVO.setShopSharePoint(userBalanceMap.get(PointChangeType.STORE_COMMISSION_RETURN.getCode())); |
| | | userPointVO.setTotalPoint(appUser.getTotalPoint() + totalPoint); |
| | | userPointVO.setConsumePoint(appUser.getAvailablePoint()); |
| | | userPointVO.setShopPoint(appUser.getShopPoint()); |
| | | userPointVO.setSharePoint(appUser.getSharePoint() + sharePoint); |
| | | userPointVO.setPullNewPoint(appUser.getTotalInvitePoint()); |
| | | userPointVO.setShopAchievementPoint(appUser.getShopAchievementPoint() + shopAchievementPoint); |
| | | userPointVO.setShopSharePoint(appUser.getShopSharePoint() + shopSharePoint); |
| | | userPointVO.setGiftPoint(vipSetting.getVipGiftRole() == 1 ? 1 : 0); |
| | | return userPointVO; |
| | | } |
| | | |
| | | |
| | | |
| | | @Override |
| | | public List<UserPointDetailVO> getUserPointDetail(Long userId, LocalDateTime startTime, LocalDateTime endTime, Integer type) { |
| | | List<UserPoint> userPointList = list(new LambdaQueryWrapper<UserPoint>() |
| | | .between(startTime != null, UserPoint::getCreateTime, startTime, endTime) |
| | | .eq(type != null, UserPoint::getType, type) |
| | | .eq(UserPoint::getAppUserId, userId)); |
| | | if (CollectionUtil.isNotEmpty(userPointList)) { |
| | | return userPointList.stream().map(p -> { |
| | | UserPointDetailVO userPointDetailVO = new UserPointDetailVO(); |
| | | userPointDetailVO.setType(p.getType()); |
| | | userPointDetailVO.setVariablePoint(p.getVariablePoint()); |
| | | userPointDetailVO.setCreateTime(p.getCreateTime()); |
| | | return userPointDetailVO; |
| | | }).collect(Collectors.toList()); |
| | | public PageInfo<UserPointDetailVO> getUserPointDetail(Long userId, LocalDateTime startTime, LocalDateTime endTime, Integer type, Integer pageCurr, Integer pageSize) { |
| | | PageInfo<UserPointDetailVO> pageInfo = new PageInfo<>(pageCurr, pageSize); |
| | | List<UserPointDetailVO> userPointDetail = this.baseMapper.getUserPointDetail(pageInfo, userId, startTime, endTime, type); |
| | | for (UserPointDetailVO userPointDetailVO : userPointDetail) { |
| | | Integer type1 = userPointDetailVO.getType(); |
| | | if (type1 != null && type1.equals(12)){ |
| | | AppUser appUser = appUserService.getById(userPointDetailVO.getReceiveUserId()); |
| | | userPointDetailVO.setName(appUser.getName()); |
| | | } |
| | | if (type1 != null && type1.equals(13)){ |
| | | AppUser appUser = appUserService.getById(userPointDetailVO.getTransferUserId()); |
| | | userPointDetailVO.setName(appUser.getName()); |
| | | } |
| | | userPointDetailVO.setFlag(userPointDetailVO.getChangeDirection() == -1 ? 2 : 1); |
| | | } |
| | | return Collections.emptyList(); |
| | | |
| | | pageInfo.setRecords(userPointDetail); |
| | | return pageInfo; |
| | | } |
| | | |
| | | |
| | | @Override |
| | | @Transactional |
| | | public R transferPoint(Integer point, String phone) { |
| | | if (!PhoneNumberValidator.isValidChinaPhoneNumber(phone)) { |
| | | return R.fail("无效的电话号码"); |
| | | } |
| | | |
| | | LoginUser loginUserApplet = tokenService.getLoginUserApplet(); |
| | | Long userid = loginUserApplet.getUserid(); |
| | | |
| | | VipSetting vipSetting = vipSettingService.getVipSettingByUserId(userid); |
| | | if (vipSetting == null) { |
| | | return R.fail("VIP 设置未找到"); |
| | | } |
| | | if (vipSetting.getVipGiftRole() == 0) { |
| | | return R.fail("转赠积分权限未开启"); |
| | | } |
| | | |
| | | AppUser appUser = appUserService.getById(userid); |
| | | if (appUser == null) { |
| | | return R.fail("用户未找到"); |
| | | } |
| | | // 可转赠积分总数 |
| | | Integer transferablePoint = appUser.getTransferablePoint(); |
| | | if (point > transferablePoint) { |
| | | return R.fail("可转赠积分不足"); |
| | | } |
| | | |
| | | AppUser appUserForPhoe = appUserService.getOne(new LambdaQueryWrapper<AppUser>() |
| | | .eq(AppUser::getPhone, phone).eq(AppUser::getDelFlag, 0).ne(AppUser::getStatus, 3)); |
| | | if (appUserForPhoe == null) { |
| | | return R.fail("目标用户不存在"); |
| | | } |
| | | if(appUserForPhoe.getStatus() == 2){ |
| | | return R.fail("目标用户已被冻结"); |
| | | } |
| | | if(appUserForPhoe.getId().equals(userid)){ |
| | | return R.fail("不能转增给自己"); |
| | | } |
| | | Integer lavePoint1 = appUserForPhoe.getLavePoint(); |
| | | appUserForPhoe.setLavePoint(appUserForPhoe.getLavePoint() + point); |
| | | appUserForPhoe.setTotalPoint(appUserForPhoe.getTotalPoint() + point); |
| | | appUserForPhoe.setTransferablePoint(transferablePoint + point); |
| | | appUserForPhoe.setAvailablePoint(appUserForPhoe.getAvailablePoint() + point); |
| | | appUserForPhoe.setTotalAvailablePoint(appUserForPhoe.getTotalAvailablePoint() + point); |
| | | appUserService.updateById(appUserForPhoe); |
| | | //构建积分流水记录 |
| | | if(point > 0){ |
| | | UserPoint userPoint = new UserPoint(); |
| | | userPoint.setType(12); |
| | | userPoint.setVariablePoint(point); |
| | | userPoint.setCreateTime(LocalDateTime.now()); |
| | | userPoint.setAppUserId(appUserForPhoe.getId()); |
| | | userPoint.setObjectId(userid); |
| | | userPoint.setChangeDirection(1); |
| | | userPointService.save(userPoint); |
| | | } |
| | | |
| | | Integer lavePoint = appUser.getLavePoint(); |
| | | appUser.setLavePoint(appUser.getLavePoint() - point); |
| | | appUser.setTransferablePoint(appUser.getTransferablePoint() - point); |
| | | appUser.setAvailablePoint(appUser.getAvailablePoint() - point); |
| | | appUserService.updateById(appUser); |
| | | //构建积分流水记录 |
| | | if(point > 0){ |
| | | UserPoint userPoint = new UserPoint(); |
| | | userPoint.setType(13); |
| | | userPoint.setVariablePoint(point); |
| | | userPoint.setCreateTime(LocalDateTime.now()); |
| | | userPoint.setAppUserId(appUser.getId()); |
| | | userPoint.setObjectId(appUserForPhoe.getId()); |
| | | userPoint.setChangeDirection(-1); |
| | | userPointService.save(userPoint); |
| | | } |
| | | return R.ok(); |
| | | } |
| | | |
| | | |
| | | @Override |
| | | public UserPointStatistics getStatistics(UserPoint userPoint) { |
| | | List<UserPoint> userPointList = this.baseMapper.selectUserPoint(userPoint); |
| | | |
| | | userPointList.forEach(item -> { |
| | | item.setVariablePoint(item.getVariablePoint() * item.getChangeDirection()); |
| | | }); |
| | | |
| | | Map<Integer, Integer> userBalanceMap = userPointList.stream() |
| | | .collect(Collectors.groupingBy( |
| | | UserPoint::getType, |
| | | Collectors.summingInt(UserPoint::getVariablePoint) |
| | | )); |
| | | |
| | | int consumePoint = userBalanceMap.getOrDefault(PointChangeType.CONSUME.getCode(), 0); |
| | | int sharePoint = userBalanceMap.getOrDefault(PointChangeType.REBATE.getCode(), 0); |
| | | int pullNewPoint = userBalanceMap.getOrDefault(PointChangeType.PULL_NEW.getCode(), 0); |
| | | int registerPoint = userBalanceMap.getOrDefault(PointChangeType.REGISTER.getCode(), 0); |
| | | |
| | | // 做工积分:每日分享积分 + 每日签到积分 + 每日使用时长积分 |
| | | int share = userBalanceMap.getOrDefault(PointChangeType.SHARE.getCode(), 0); |
| | | int signIn = userBalanceMap.getOrDefault(PointChangeType.SIGN_IN.getCode(), 0); |
| | | int useTime = userBalanceMap.getOrDefault(PointChangeType.USE_TIME.getCode(), 0); |
| | | int workPoint = share + signIn + useTime; |
| | | |
| | | int shopAchievementPoint = userBalanceMap.getOrDefault(PointChangeType.TECHNICIAN_ACHIEVEMENT.getCode(), 0); |
| | | //总积分 |
| | | int totalPoint = consumePoint+sharePoint+pullNewPoint+registerPoint+workPoint+shopAchievementPoint; |
| | | UserPointStatistics userPointStatistics = new UserPointStatistics(); |
| | | userPointStatistics.setTotalPoint(totalPoint); |
| | | userPointStatistics.setConsumePoint(consumePoint); |
| | | userPointStatistics.setSharePoint(sharePoint); |
| | | userPointStatistics.setPullNewPoint(pullNewPoint); |
| | | userPointStatistics.setRegisterPoint(registerPoint); |
| | | userPointStatistics.setWorkPoint(workPoint); |
| | | userPointStatistics.setShopAchievementPoint(shopAchievementPoint); |
| | | return userPointStatistics; |
| | | } |
| | | |
| | | |
| | | @Override |
| | | public IPage<UserPoint> getUserPointPage(Page<UserPoint> page, UserPoint userPoint) { |
| | | IPage<UserPoint> userPointIPage = this.baseMapper.queryUserPointPage(page, userPoint); |
| | | userPointIPage.getRecords().forEach(item -> { |
| | | item.setVariablePoint(item.getVariablePoint() * item.getChangeDirection()); |
| | | }); |
| | | return userPointIPage; |
| | | } |
| | | } |