|  | @@ -1,6 +1,9 @@
 | 
	
		
			
				|  |  |  package cn.newfeifan.mall.module.distri.service.integral;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  import cn.newfeifan.mall.framework.mybatis.core.query.LambdaQueryWrapperX;
 | 
	
		
			
				|  |  | +import cn.newfeifan.mall.module.distri.dal.mysql.ptprofit.PtProfitMapper;
 | 
	
		
			
				|  |  | +import cn.newfeifan.mall.module.distri.enums.CaclEnum;
 | 
	
		
			
				|  |  | +import cn.newfeifan.mall.module.distri.service.ptprofitlog.PtProfitLogService;
 | 
	
		
			
				|  |  |  import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 | 
	
		
			
				|  |  |  import org.springframework.stereotype.Service;
 | 
	
		
			
				|  |  |  
 | 
	
	
		
			
				|  | @@ -34,6 +37,12 @@ public class IntegralServiceImpl implements IntegralService {
 | 
	
		
			
				|  |  |      @Resource
 | 
	
		
			
				|  |  |      private IntegralMapper integralMapper;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +    @Resource
 | 
	
		
			
				|  |  | +    private PtProfitMapper ptProfitMapper;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    @Resource
 | 
	
		
			
				|  |  | +    private PtProfitLogService ptProfitLogService;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |      @Override
 | 
	
		
			
				|  |  |      public Long createIntegral(IntegralSaveReqVO createReqVO) {
 | 
	
		
			
				|  |  |          // 插入
 | 
	
	
		
			
				|  | @@ -81,4 +90,57 @@ public class IntegralServiceImpl implements IntegralService {
 | 
	
		
			
				|  |  |          return integralMapper.selectOne(new LambdaQueryWrapper<IntegralDO>().eq(IntegralDO::getUserId, descendant));
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +    /**
 | 
	
		
			
				|  |  | +     * add by Ben
 | 
	
		
			
				|  |  | +     * 修改用户积分
 | 
	
		
			
				|  |  | +     *
 | 
	
		
			
				|  |  | +     * @param userId 要修改积分的用户
 | 
	
		
			
				|  |  | +     * @param caclEnum 导致积分变化的事件类型枚举
 | 
	
		
			
				|  |  | +     * @param amount 用户可用积分变化金额,可为负数
 | 
	
		
			
				|  |  | +     * @param freezeAmount 用户冻结积分变化金额,可为负数
 | 
	
		
			
				|  |  | +     * @return 返回是否执行成功。执行失败,通常是就是用户积分不足,没积分抵扣
 | 
	
		
			
				|  |  | +     */
 | 
	
		
			
				|  |  | +    @Override
 | 
	
		
			
				|  |  | +    public Boolean updateUserIntegral(Long userId, CaclEnum caclEnum, Integer amount, Integer freezeAmount,
 | 
	
		
			
				|  |  | +                                      Long tradeOrderId,
 | 
	
		
			
				|  |  | +                                      String OrderNum
 | 
	
		
			
				|  |  | +                                      ) {
 | 
	
		
			
				|  |  | +        Boolean success = null;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        //修改用户积分钱包的积分
 | 
	
		
			
				|  |  | +        int effectRowNum = ptProfitMapper.updateUserIntegral(amount,amount,freezeAmount,freezeAmount,userId);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        if (effectRowNum==1) {//如果成功修改积分记录
 | 
	
		
			
				|  |  | +            success = true;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            //用户修改后的当前可用的钱包余额
 | 
	
		
			
				|  |  | +            Integer afterAmount = null;
 | 
	
		
			
				|  |  | +            //用户修改后的冻结积分钱包余额
 | 
	
		
			
				|  |  | +            Integer afterFreezeAmount = null;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            //查询用户修改后的积分
 | 
	
		
			
				|  |  | +            Map<String, Object> map = ptProfitMapper.selectUserIntegral(userId);
 | 
	
		
			
				|  |  | +            Object currentQuotaObj = map.get("current_quota");
 | 
	
		
			
				|  |  | +            Object freezeQuotaObj = map.get("freeze_quota");
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            afterAmount = (Integer)currentQuotaObj;
 | 
	
		
			
				|  |  | +            afterFreezeAmount = (Integer)freezeQuotaObj;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            if(afterAmount<0||afterFreezeAmount<0){
 | 
	
		
			
				|  |  | +                throw new RuntimeException("用户钱包为负数:用户IO为"+userId+"的用户可用钱包为"+afterAmount+",冻结钱包为"+afterFreezeAmount);
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            //记录用户的积分变动日志
 | 
	
		
			
				|  |  | +            ptProfitLogService.addMessage(userId, caclEnum, amount, afterAmount,
 | 
	
		
			
				|  |  | +                    freezeAmount, afterFreezeAmount,null,tradeOrderId,OrderNum);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        }else if(effectRowNum<1){//如果钱包积分不足,不够抵扣
 | 
	
		
			
				|  |  | +            success = false;
 | 
	
		
			
				|  |  | +        } else if (effectRowNum>1) {
 | 
	
		
			
				|  |  | +//            success = false;
 | 
	
		
			
				|  |  | +            throw new RuntimeException("用户IO为"+userId+"的用户钱包不只一个!");
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        return success;
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  |  }
 |