feat:积分管理

This commit is contained in:
dzc committed 2026-04-09 17:22:41 +08:00
1 parent 1ae623ce66
commit d5ae9d4d9f
21 files changed
+436 -46

No files matched your search

@@ -65,7 +65,7 @@ public class AdRecordController {
public R<Page<AdRecordVo>> integralPage(@ParameterObject Page page, @ParameterObject AdRecordDto dto) {
PjcloudUser user = SecurityUtils.getUser();
dto.setUserId(user.getId());
dto.setIntegralNumNullStatus(YesNoEnum.YES.getCode());
dto.setIntegralNumNullStatus(YesNoEnum.NO.getCode());
return R.ok(adRecordService.pageList(page, dto));
}
/* ************************************* start app start ************************************* */
@@ -99,12 +99,6 @@ public class AdRecordEntity extends Model<AdRecordEntity> {
@Schema(description = "广告预计收入")
private Integer adPredictPrice;
/**
* 获取的积分
*/
@Schema(description = "获取的积分")
private Integer integralNum;
/**
* 创建人
*/
@@ -15,7 +15,6 @@
a.visit_duration,
a.status,
a.extra,
a.integral_num,
a.create_time,
a.update_time,
a.create_by,
@@ -38,13 +37,8 @@
<if test="dto.adType != null and dto.adType != '' ">
and a.ad_type = #{dto.adType}
</if>
<if test="dto.integralNumNullStatus != null and dto.adType != '' ">
<if test="dto.integralNumNullStatus = '1' ">
and a.integral_num is null
</if>
<if test="dto.integralNumNullStatus = '0' ">
and a.integral_num is not null
</if>
<if test="dto.status != null and dto.status != '' ">
and a.status = #{dto.status}
</if>
<if test="dto.createTimeList != null and dto.createTimeList.length == 2">
and a.create_time between #{dto.createTimeList[0]} and #{dto.createTimeList[1]}
@@ -1,5 +1,6 @@
package com.bsd.pjcloud.thirdparty.dto.ad;
import cn.hutool.core.util.ObjectUtil;
import lombok.Data;
import java.util.Map;
@@ -51,7 +52,9 @@ public class UniappAdCallbackDto {
dto.setTransId(map.get("trans_id"));
dto.setUserId(Long.parseLong(map.get("user_id")));
dto.setExtra(map.get("extra"));
dto.setCpm(Integer.parseInt(map.get("cpm")));
if (ObjectUtil.isNotEmpty(map.get("cpm"))) {
dto.setCpm(Integer.parseInt(map.get("cpm")));
}
} catch (Exception e) {
e.printStackTrace();
}
@@ -7,6 +7,7 @@ import com.bsd.pjcloud.common.security.annotation.Inner;
import com.bsd.pjcloud.integrated.api.feign.RemoterIntegratedAdService;
import com.bsd.pjcloud.thirdparty.config.AdConfig;
import com.bsd.pjcloud.thirdparty.dto.ad.UniappAdCallbackDto;
import com.bsd.pjcloud.wallet.api.feign.RemoteIntegralTransactionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
@@ -35,9 +36,12 @@ import java.util.Map;
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class ThirdUniAdController {
final private String TAG = "uniapp广告";
final private AdConfig adConfig;
final private RemoterIntegratedAdService remoterIntegratedAdService;
final private RemoteIntegralTransactionService remoteIntegralTransactionService;
@Inner(value = false)
@GetMapping("/callback")
@@ -73,6 +77,11 @@ public class ThirdUniAdController {
if (ObjectUtil.isNotEmpty(res) && res) {
result.put("isValid", true);
}
// 增加用户积分
Boolean isAddIntegral = remoteIntegralTransactionService.remoteAddRewardIedntegral(dto.getUserId(), dto.getTransId()).getData();
if (ObjectUtil.isEmpty(isAddIntegral) || !isAddIntegral) {
log.error("|{}|增加用户积分失败,userId:{}", TAG, dto.getUserId());
}
return result;
}
@@ -0,0 +1,37 @@
package com.bsd.pjcloud.wallet.api.constant.Integral;
import cn.hutool.core.util.ObjectUtil;
import com.bsd.pjcloud.wallet.api.constant.WalletBizEnum;
import lombok.AllArgsConstructor;
import lombok.Getter;
import java.util.Arrays;
import java.util.List;
/**
* 积分业务枚举
*/
@Getter
@AllArgsConstructor
public enum WalletIntegralBizEnum {
REWARDED_AD("1", "激励广告"),
UPDATE("5", "更新积分"),
;
private final String code;
private final String desc;
public static WalletIntegralBizEnum valueOfByType(String code) {
return Arrays.stream(WalletIntegralBizEnum.values())
.filter(v -> ObjectUtil.equal(v.getCode(), code))
.findFirst().orElse(null);
}
public static WalletIntegralBizEnum descOf(String description) {
return Arrays.stream(WalletIntegralBizEnum.values())
.filter(v -> ObjectUtil.equal(v.getDesc(), description))
.findFirst().orElse(null);
}
}
@@ -5,4 +5,5 @@ import lombok.Data;
@Data
public class ActCouponDto extends ActCouponEntity {
}
@@ -1,9 +1,33 @@
package com.bsd.pjcloud.wallet.api.dto.integral;
import com.bsd.pjcloud.wallet.api.entity.integral.IntegralTransactionEntity;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
@Data
public class IntegralTransactionDto extends IntegralTransactionEntity {
/**
* 类型 - 收入
*/
public static final Integer TYPE_INCOME = 1;
/**
* 类型 - 支出
*/
public static final Integer TYPE_EXPENSE = 2;
private Integer type;
/**
* 用户id
*/
@Schema(description = "用户id")
private Long userId;
/**
* 添加余额用户id
*/
@Schema(description = "添加余额用户id")
private Long addUserId;
}
@@ -27,9 +27,9 @@ public class IntegralTransactionEntity extends Model<IntegralTransactionEntity>
private Long id;
/**
* 积分总数
* 积分钱包id
*/
@Schema(description = "积分总数")
@Schema(description = "积分钱包id")
private Long integralId;
/**
@@ -0,0 +1,17 @@
package com.bsd.pjcloud.wallet.api.feign;
import com.bsd.pjcloud.common.core.constant.ServiceNameConstants;
import com.bsd.pjcloud.common.core.util.R;
import com.bsd.pjcloud.common.feign.annotation.NoToken;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
@FeignClient(contextId = "remoteIntegralTransactionService", value = ServiceNameConstants.WALLET_SERVICE)
public interface RemoteIntegralTransactionService {
@NoToken
@GetMapping("/integral-transaction/remote/add-rewarded-integral")
R<Boolean> remoteAddRewardIedntegral(@RequestParam Long userId, @RequestParam String bizId);
}
@@ -1,9 +1,19 @@
package com.bsd.pjcloud.wallet.api.vo.integral;
import com.bsd.pjcloud.admin.api.vo.base.BaseUserInterface;
import com.bsd.pjcloud.wallet.api.entity.integral.IntegralManageEntity;
import lombok.Data;
@Data
public class IntegralManageVo extends IntegralManageEntity {
public class IntegralManageVo extends IntegralManageEntity implements BaseUserInterface {
/**
* 用户相关
*/
private String username;
private String userMobile;
private String userAvatar;
}
@@ -1,5 +1,6 @@
com.bsd.pjcloud.wallet.api.feign.RemoteActCancelService
com.bsd.pjcloud.wallet.api.feign.RemoteWalletService
com.bsd.pjcloud.wallet.api.feign.RemoteIntegralTransactionService
com.bsd.pjcloud.wallet.api.feign.RemoteMchPickUpService
com.bsd.pjcloud.wallet.api.feign.RemoteMchRuleJoinService
com.bsd.pjcloud.wallet.api.feign.RemoteCodeUseService
@@ -2,7 +2,6 @@ package com.bsd.pjcloud.wallet.controller.Integral;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bsd.pjcloud.common.core.util.R;
@@ -43,6 +42,7 @@ public class IntegralManageController {
@Operation(summary = "创建或获取用户积分", description = "创建或获取用户积分")
@GetMapping("/app/get-or-create")
public R<IntegralManageEntity> getOrCreate() {
PjcloudUser user = SecurityUtils.getUser();
return R.ok(integralManageService.getOrCreateIntegral(user.getId()));
@@ -7,8 +7,14 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bsd.pjcloud.common.core.util.R;
import com.bsd.pjcloud.common.excel.annotation.ResponseExcel;
import com.bsd.pjcloud.common.log.annotation.SysLog;
import com.bsd.pjcloud.common.security.annotation.Inner;
import com.bsd.pjcloud.common.security.service.PjcloudUser;
import com.bsd.pjcloud.common.security.util.SecurityUtils;
import com.bsd.pjcloud.wallet.api.constant.Integral.WalletIntegralBizEnum;
import com.bsd.pjcloud.wallet.api.dto.WalletTransactionDto;
import com.bsd.pjcloud.wallet.api.dto.integral.IntegralTransactionDto;
import com.bsd.pjcloud.wallet.api.entity.integral.IntegralTransactionEntity;
import com.bsd.pjcloud.wallet.api.vo.integral.IntegralTransactionVo;
import com.bsd.pjcloud.wallet.service.IntegralTransactionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
@@ -26,19 +32,54 @@ import java.util.List;
*/
@RestController
@RequiredArgsConstructor
@RequestMapping("/integralTransaction")
@Tag(description = "integralTransaction", name = "积分记录管理")
@RequestMapping("/integral-transaction")
@Tag(description = "integral-transaction", name = "积分记录管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class IntegralTransactionController {
private final IntegralTransactionService integralTransactionService;
/* ************************************* start 远程调用 start ************************************* */
@Inner
@Operation(summary = "增加信息流广告积分", description = "增加信息流广告积分")
@GetMapping("/remote/add-rewarded-integral")
public R<Boolean> remoteAddRewardIedntegral(@RequestParam Long userId, @RequestParam String bizId) {
return R.ok(integralTransactionService.remoteAddRewardIedntegral(userId, bizId));
}
/* ************************************* end 远程调用 end ************************************* */
/* ************************************* start app start ************************************* */
@Operation(summary = "增加信息流广告积分", description = "增加信息流广告积分")
@GetMapping("/app/rewarded-integral-page")
public R<Page<IntegralTransactionVo>> appRewardedIntegralPage(@ParameterObject Page page) {
PjcloudUser user = SecurityUtils.getUser();
IntegralTransactionDto dto = new IntegralTransactionDto();
dto.setUserId(user.getId());
dto.setBizType(WalletIntegralBizEnum.REWARDED_AD.getCode());
return R.ok(integralTransactionService.scopePageList(page, dto, null));
}
/* ************************************* end app end ************************************* */
@PostMapping("/update-integral")
@Operation(summary = "更新积分")
@PreAuthorize("@pms.hasPermission('integral_transaction_update_integral')")
public R<Boolean> updateIntegral(@RequestBody IntegralTransactionDto dto) {
PjcloudUser user = SecurityUtils.getUser();
dto.setAddUserId(user.getId());
return R.ok(integralTransactionService.updateIntegral(dto));
}
/**
* 分页查询
*/
@Operation(summary = "分页查询", description = "分页查询")
@GetMapping("/page")
@PreAuthorize("@pms.hasPermission('wallet_integralTransaction_view')")
public R getIntegralTransactionPage(@ParameterObject Page page, @ParameterObject IntegralTransactionDto dto) {
return R.ok(integralTransactionService.scopePageList(page, dto, null));
}
@@ -53,28 +94,6 @@ public class IntegralTransactionController {
return R.ok(integralTransactionService.getById(id));
}
/**
* 新增积分记录
*/
@Operation(summary = "新增积分记录", description = "新增积分记录")
@SysLog("新增积分记录")
@PostMapping
@PreAuthorize("@pms.hasPermission('wallet_integralTransaction_add')")
public R save(@RequestBody IntegralTransactionEntity integralTransaction) {
return R.ok(integralTransactionService.save(integralTransaction));
}
/**
* 修改积分记录
*/
@Operation(summary = "修改积分记录", description = "修改积分记录")
@SysLog("修改积分记录")
@PutMapping
@PreAuthorize("@pms.hasPermission('wallet_integralTransaction_edit')")
public R updateById(@RequestBody IntegralTransactionEntity integralTransaction) {
return R.ok(integralTransactionService.updateById(integralTransaction));
}
/**
* 通过id删除积分记录
*/
@@ -6,9 +6,14 @@ import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bsd.pjcloud.common.core.util.R;
import com.bsd.pjcloud.common.log.annotation.SysLog;
import com.bsd.pjcloud.common.security.service.PjcloudUser;
import com.bsd.pjcloud.common.security.util.SecurityUtils;
import com.bsd.pjcloud.wallet.api.dto.act.ActAdDto;
import com.bsd.pjcloud.wallet.api.dto.act.ActCouponDto;
import com.bsd.pjcloud.wallet.api.dto.act.ActCouponRecordDto;
import com.bsd.pjcloud.wallet.api.entity.act.ActAdEntity;
import com.bsd.pjcloud.wallet.api.vo.act.ActAdVo;
import com.bsd.pjcloud.wallet.api.vo.act.ActCouponRecordVo;
import com.bsd.pjcloud.wallet.api.vo.act.ActCouponVo;
import com.bsd.pjcloud.wallet.service.ActAdService;
import org.springframework.security.access.prepost.PreAuthorize;
@@ -43,6 +48,20 @@ public class ActAdController {
return R.ok(actAdService.rewardedAdCouponPage(page));
}
@Operation(summary = "激励广告兑换券领取分页", description = "激励广告兑换券领取分页")
@GetMapping("/app/rewarded-ad0-apply-coupon-page")
public R<Page<ActCouponRecordVo>> rewardedAdApplyCouponPage(@ParameterObject Page page) {
PjcloudUser user = SecurityUtils.getUser();
ActCouponRecordDto dto = new ActCouponRecordDto();
dto.setUserId(user.getId());
return R.ok(actAdService.rewardedAdApplyCouponPage(page, dto));
}
/* ************************************* end app end ************************************* */
/**
@@ -1,9 +1,12 @@
package com.bsd.pjcloud.wallet.mapper.integral;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bsd.pjcloud.common.data.datascope.DataScope;
import com.bsd.pjcloud.common.data.datascope.PjcloudBaseMapper;
import com.bsd.pjcloud.wallet.api.dto.integral.IntegralManageDto;
import com.bsd.pjcloud.wallet.api.entity.WalletManageEntity;
import com.bsd.pjcloud.wallet.api.entity.integral.IntegralManageEntity;
import com.bsd.pjcloud.wallet.api.vo.integral.IntegralManageVo;
import org.apache.ibatis.annotations.Mapper;
@@ -14,4 +17,37 @@ public interface IntegralManageMapper extends PjcloudBaseMapper<IntegralManageEn
Page<IntegralManageVo> scopePageList(Page page, @Param("dto") IntegralManageDto dto, DataScope dataScope);
/**
* 当充值的时候,更新积分
*/
default int updateWhenIntegral(Long id, Integer integral) {
LambdaUpdateWrapper<IntegralManageEntity> lambdaUpdateWrapper = new LambdaUpdateWrapper<IntegralManageEntity>()
.setSql(" integral_num = integral_num + " + integral + ", total_recharge = total_recharge + " + integral)
.eq(IntegralManageEntity::getId, id);
return update(null, lambdaUpdateWrapper);
}
/**
* 当消费退款时候,更新积分
*/
default int updateWhenConsumptionRefundIntegral(Long id, Integer integral) {
LambdaUpdateWrapper<IntegralManageEntity> lambdaUpdateWrapper = new LambdaUpdateWrapper<IntegralManageEntity>()
.setSql(" integral_num = integral_num + " + integral + ", total_expense = total_expense - " + integral)
.eq(IntegralManageEntity::getId, id);
return update(null, lambdaUpdateWrapper);
}
/**
* 当消费时候,更新钱包
* @param force 是否强制消费(会导致扣成负数)
*/
default int updateWhenConsumption(Long id, Integer integral, Boolean force) {
LambdaUpdateWrapper<IntegralManageEntity> lambdaUpdateWrapper = new LambdaUpdateWrapper<IntegralManageEntity>()
.setSql(" integral_num = integral_num - " + integral + ", total_expense = total_expense + " + integral)
.eq(IntegralManageEntity::getId, id);
if (ObjectUtil.isEmpty(force) || !force) {
lambdaUpdateWrapper.ge(IntegralManageEntity::getIntegralFreeze, integral);
}
return update(null, lambdaUpdateWrapper);
}
}
@@ -5,8 +5,11 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.bsd.pjcloud.common.data.datascope.DataScope;
import com.bsd.pjcloud.wallet.api.dto.act.ActAdDto;
import com.bsd.pjcloud.wallet.api.dto.act.ActCouponDto;
import com.bsd.pjcloud.wallet.api.dto.act.ActCouponRecordDto;
import com.bsd.pjcloud.wallet.api.entity.act.ActAdEntity;
import com.bsd.pjcloud.wallet.api.vo.act.ActAdVo;
import com.bsd.pjcloud.wallet.api.vo.act.ActCouponRecordVo;
import com.bsd.pjcloud.wallet.api.vo.act.ActCouponVo;
public interface ActAdService extends IService<ActAdEntity> {
@@ -21,4 +24,9 @@ public interface ActAdService extends IService<ActAdEntity> {
* 激励广告兑换券分页
*/
Page<ActCouponVo> rewardedAdCouponPage(Page page);
/**
* 激励广告兑换券领取分页
*/
Page<ActCouponRecordVo> rewardedAdApplyCouponPage(Page page, ActCouponRecordDto dto);
}
@@ -3,12 +3,34 @@ package com.bsd.pjcloud.wallet.service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.bsd.pjcloud.common.data.datascope.DataScope;
import com.bsd.pjcloud.wallet.api.constant.Integral.WalletIntegralBizEnum;
import com.bsd.pjcloud.wallet.api.constant.WalletBizEnum;
import com.bsd.pjcloud.wallet.api.dto.integral.IntegralTransactionDto;
import com.bsd.pjcloud.wallet.api.entity.WalletTransactionEntity;
import com.bsd.pjcloud.wallet.api.entity.integral.IntegralTransactionEntity;
import com.bsd.pjcloud.wallet.api.vo.integral.IntegralTransactionVo;
public interface IntegralTransactionService extends IService<IntegralTransactionEntity> {
/**
* 增加信息流广告积分
*/
Boolean remoteAddRewardIedntegral(Long userId, String bizId);
/**
* 增加积分
*/
IntegralTransactionEntity addIntegral(Long userId, Integer integral, WalletIntegralBizEnum bizType, String bizId);
/**
* 减少积分
*/
IntegralTransactionEntity reduceIntegral(Long userId, Integer integral, WalletIntegralBizEnum bizType, String bizId, Boolean force);
Page<IntegralTransactionVo> scopePageList(Page page, IntegralTransactionDto dto, DataScope dataScope);
/**
* 更新积分
*/
Boolean updateIntegral(IntegralTransactionDto dto);
}
@@ -10,13 +10,16 @@ import com.bsd.pjcloud.common.data.datascope.DataScope;
import com.bsd.pjcloud.wallet.api.constant.act.*;
import com.bsd.pjcloud.wallet.api.dto.act.ActAdDto;
import com.bsd.pjcloud.wallet.api.dto.act.ActCouponDto;
import com.bsd.pjcloud.wallet.api.dto.act.ActCouponRecordDto;
import com.bsd.pjcloud.wallet.api.dto.act.ActCreateAdCouponDto;
import com.bsd.pjcloud.wallet.api.entity.act.ActAdEntity;
import com.bsd.pjcloud.wallet.api.entity.act.ActCouponEntity;
import com.bsd.pjcloud.wallet.api.vo.act.ActAdVo;
import com.bsd.pjcloud.wallet.api.vo.act.ActCouponRecordVo;
import com.bsd.pjcloud.wallet.api.vo.act.ActCouponVo;
import com.bsd.pjcloud.wallet.mapper.act.ActAdMapper;
import com.bsd.pjcloud.wallet.service.ActAdService;
import com.bsd.pjcloud.wallet.service.ActCouponRecordService;
import com.bsd.pjcloud.wallet.service.ActCouponService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
@@ -33,7 +36,10 @@ import java.util.List;
@RequiredArgsConstructor
public class ActAdServiceImpl extends ServiceImpl<ActAdMapper, ActAdEntity> implements ActAdService {
private final static String REWARDED_AD_UNIQUE_CODE = "wbcHjM";
private final ActCouponService actCouponService;
private final ActCouponRecordService actCouponRecordService;
@Override
public Page<ActAdVo> scopePageList(Page page, ActAdDto dto, DataScope dataScope) {
@@ -68,6 +74,8 @@ public class ActAdServiceImpl extends ServiceImpl<ActAdMapper, ActAdEntity> impl
return baseMapper.selectById(entity.getId());
}
/* ************************************* start app start ************************************* */
/**
* 激励广告兑换券分页
*/
@@ -82,6 +90,22 @@ public class ActAdServiceImpl extends ServiceImpl<ActAdMapper, ActAdEntity> impl
return actCouponService.pageJoin(page, queryDto);
}
/**
* 激励广告兑换券领取分页
*/
@Override
public Page<ActCouponRecordVo> rewardedAdApplyCouponPage(Page page, ActCouponRecordDto dto) {
ActAdEntity actAd = baseMapper.selectOne(new LambdaQueryWrapper<ActAdEntity>().eq(ActAdEntity::getUniqueCode, "wbcHjM"));
if (ObjectUtil.isEmpty(actAd)) {
return new Page<>();
}
dto.setParentActId(actAd.getId());
return actCouponRecordService.pageList(page, dto);
}
/* ************************************* end app end ************************************* */
private void generateDefaultAct(ActAdEntity actAd) {
if (ObjectUtil.isEmpty(actAd.getTypeList())) {
return;
@@ -5,6 +5,11 @@ import cn.hutool.extra.spring.SpringUtil;
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.bsd.pjcloud.admin.api.entity.SysUser;
import com.bsd.pjcloud.admin.api.feign.RemoteUserService;
import com.bsd.pjcloud.admin.api.utils.DataUtil;
import com.bsd.pjcloud.common.core.util.CollectionUtils;
import com.bsd.pjcloud.common.core.util.R;
import com.bsd.pjcloud.common.data.datascope.DataScope;
import com.bsd.pjcloud.wallet.api.dto.integral.IntegralManageDto;
import com.bsd.pjcloud.wallet.api.entity.integral.IntegralManageEntity;
@@ -19,6 +24,9 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import java.util.ArrayList;
import java.util.List;
/**
* 用户积分
*/
@@ -30,6 +38,8 @@ public class IntegralManageServiceImpl extends ServiceImpl<IntegralManageMapper,
private final WalletSettingService walletSettingService;
private final RemoteUserService remoteUserService;
/**
* 创建或获取用户积分
*/
@@ -73,7 +83,23 @@ public class IntegralManageServiceImpl extends ServiceImpl<IntegralManageMapper,
*/
@Override
public Page<IntegralManageVo> scopePageList(Page page, IntegralManageDto dto, DataScope dataScope) {
return baseMapper.scopePageList(page, dto, dataScope);
Page<IntegralManageVo> pageList = baseMapper.scopePageList(page, dto, dataScope);
disposePageList(pageList.getRecords());
return pageList;
}
private void disposePageList(List<IntegralManageVo> list) {
if (ObjectUtil.isEmpty(list)) {
return;
}
// 用户
List<Long> userIds = list.stream().map(IntegralManageVo::getUserId).distinct().toList();
if (ObjectUtil.isNotEmpty(userIds)) {
R<List<SysUser>> userList = remoteUserService.getUserById(new ArrayList<>(userIds));
if (ObjectUtil.isNotEmpty(userList.getData())) {
DataUtil.convertUserList(list, CollectionUtils.convertMap(userList.getData(), SysUser::getUserId));
}
}
}
private IntegralManageServiceImpl getSelf() {
@@ -1,16 +1,29 @@
package com.bsd.pjcloud.wallet.service.impl.integral;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bsd.pjcloud.admin.api.dto.UserInfo;
import com.bsd.pjcloud.admin.api.feign.RemoteUserService;
import com.bsd.pjcloud.common.data.datascope.DataScope;
import com.bsd.pjcloud.wallet.api.constant.Integral.WalletIntegralBizEnum;
import com.bsd.pjcloud.wallet.api.constant.WalletBizEnum;
import com.bsd.pjcloud.wallet.api.dto.integral.IntegralTransactionDto;
import com.bsd.pjcloud.wallet.api.entity.WalletTransactionEntity;
import com.bsd.pjcloud.wallet.api.entity.integral.IntegralManageEntity;
import com.bsd.pjcloud.wallet.api.entity.integral.IntegralTransactionEntity;
import com.bsd.pjcloud.wallet.api.vo.WalletSettingVo;
import com.bsd.pjcloud.wallet.api.vo.integral.IntegralTransactionVo;
import com.bsd.pjcloud.wallet.mapper.integral.IntegralManageMapper;
import com.bsd.pjcloud.wallet.mapper.integral.IntegralTransactionMapper;
import com.bsd.pjcloud.wallet.service.IntegralManageService;
import com.bsd.pjcloud.wallet.service.IntegralTransactionService;
import com.bsd.pjcloud.wallet.service.WalletSettingService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
/**
@@ -22,9 +35,142 @@ import org.springframework.validation.annotation.Validated;
@Service
public class IntegralTransactionServiceImpl extends ServiceImpl<IntegralTransactionMapper, IntegralTransactionEntity> implements IntegralTransactionService {
static private final String TAG = "积分流水";
private final WalletSettingService walletSettingService;
private final IntegralManageService integralManageService;
private final IntegralManageMapper integralManageMapper;
private final RemoteUserService remoteUserService;
/* ************************************* start 远程调用 start ************************************* */
/**
* 增加信息流广告积分
*/
@Override
public Boolean remoteAddRewardIedntegral(Long userId, String bizId) {
// 获取积分配置
WalletSettingVo setting = walletSettingService.getSetting();
if (ObjectUtil.isEmpty(setting) || ObjectUtil.isEmpty(setting.getRewardedAdIntegral()) || setting.getRewardedAdIntegral() <= 0) {
return false;
}
IntegralManageEntity integral = integralManageService.getOrCreateIntegral(userId);
if (ObjectUtil.isEmpty(integral)) {
log.error("|{}|增加信息流广告积分,用户id({})不存在.", TAG, userId);
return false;
}
getSelf().addIntegral(userId, setting.getRewardedAdIntegral(), WalletIntegralBizEnum.REWARDED_AD, bizId);
return null;
}
/* ************************************* end 远程调用 end ************************************* */
/**
* 增加积分
*/
@Override
@Transactional(rollbackFor = Exception.class)
public IntegralTransactionEntity addIntegral(Long userId, Integer integral, WalletIntegralBizEnum bizType, String bizId) {
// 查询用户
UserInfo userInfo = remoteUserService.getInnerByUserId(userId).getData();
if (ObjectUtil.isEmpty(userInfo)) {
log.error("|{}|增加积分,用户id({})不存在.", TAG, userId);
throw new RuntimeException("扣款失败,用户钱包不存在");
}
IntegralManageEntity integralManage = integralManageService.getOrCreateIntegral(userId);
if (ObjectUtil.isEmpty(integralManage)) {
log.error("|{}|增加积分,用户id({})不存在.", TAG, userId);
throw new RuntimeException("扣款失败,用户钱包不存在");
}
switch (bizType) {
case UPDATE, REWARDED_AD -> {
integralManageMapper.updateWhenIntegral(integralManage.getId(), integral);
}
default -> {
log.error("|{}|用户id({})使用未实现的增加钱包余额的类型({}).", TAG, userId, bizType.getDesc());
throw new UnsupportedOperationException("待实现");
}
}
IntegralTransactionEntity insetObj = new IntegralTransactionEntity();
insetObj.setIntegralId(integralManage.getId());
insetObj.setDeptId(userInfo.getSysUser().getDeptId());
insetObj.setBizType(bizType.getCode());
insetObj.setBizId(bizId);
insetObj.setTitle(bizType.getDesc());
insetObj.setIntegralNum(integral);
insetObj.setIntegralBalance(integralManage.getIntegralNum() + integral);
save(insetObj);
return insetObj;
}
/**
* 减少积分
*/
@Override
public IntegralTransactionEntity reduceIntegral(Long userId, Integer integral, WalletIntegralBizEnum bizType, String bizId, Boolean force) {
if (ObjectUtil.isEmpty(force)) force = false;
// 查询用户
UserInfo userInfo = remoteUserService.getInnerByUserId(userId).getData();
if (ObjectUtil.isEmpty(userInfo)) {
log.error("|{}|减少积分,用户id({})不存在.", TAG, userId);
throw new RuntimeException("扣款失败,用户钱包不存在");
}
IntegralManageEntity integralManage = integralManageService.getOrCreateIntegral(userId);
if (ObjectUtil.isEmpty(integralManage)) {
log.error("|{}|减少积分,用户id({})不存在.", TAG, userId);
throw new RuntimeException("扣款失败,用户钱包不存在");
}
int updateCounts = integralManageMapper.updateWhenConsumption(integralManage.getId(), integral, force);
if (updateCounts == 0) {
log.error("|{}|减少积分,用户id({})积分不足.", TAG, integralManage.getUserId());
throw new RuntimeException("积分不足");
}
Integer afterBalance = integralManage.getIntegralNum() - integral;
IntegralTransactionEntity insetObj = new IntegralTransactionEntity();
insetObj.setIntegralId(integralManage.getId());
insetObj.setDeptId(userInfo.getSysUser().getDeptId());
insetObj.setBizType(bizType.getCode());
insetObj.setBizId(bizId);
insetObj.setTitle(bizType.getDesc());
insetObj.setIntegralNum(-integral);
insetObj.setIntegralBalance(afterBalance);
save(insetObj);
return insetObj;
}
@Override
public Page<IntegralTransactionVo> scopePageList(Page page, IntegralTransactionDto dto, DataScope dataScope) {
if (ObjectUtil.isNotEmpty(dto.getUserId())) {
IntegralManageEntity integralManage = integralManageService.getOrCreateIntegral(dto.getUserId());
if (ObjectUtil.isNotEmpty(integralManage)) {
dto.setIntegralId(integralManage.getId());
}
}
return baseMapper.scopePageList(page, dto, dataScope);
}
/**
* 更新积分
*/
@Override
public Boolean updateIntegral(IntegralTransactionDto dto) {
IntegralManageEntity integralManage = integralManageService.getById(dto.getId());
if (ObjectUtil.isEmpty(integralManage)) {
log.error("|{}|更新积分,用户id({})不存在.", TAG, integralManage.getUserId());
throw new RuntimeException("扣款失败,用户钱包不存在");
}
// 更新积分
IntegralTransactionEntity result = getSelf().addIntegral(integralManage.getUserId(), dto.getIntegralNum(), WalletIntegralBizEnum.UPDATE, String.valueOf(dto.getAddUserId()));
return ObjectUtil.isNotEmpty(result);
}
private IntegralTransactionServiceImpl getSelf() {
return SpringUtil.getBean(getClass());
}
}