|
|
@@ -1,12 +1,17 @@
|
|
|
package com.fs.company.service.impl;
|
|
|
|
|
|
import java.math.BigDecimal;
|
|
|
+import java.math.BigInteger;
|
|
|
import java.util.Date;
|
|
|
import java.util.List;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
|
+import com.fs.common.constant.FsConstants;
|
|
|
import com.fs.common.core.domain.R;
|
|
|
+import com.fs.common.core.redis.RedisCache;
|
|
|
import com.fs.common.utils.DateUtils;
|
|
|
+import com.fs.common.utils.StringUtils;
|
|
|
import com.fs.company.constant.PaymentStatus;
|
|
|
import com.fs.company.domain.Company;
|
|
|
import com.fs.company.domain.CompanyMoneyLogs;
|
|
|
@@ -19,7 +24,10 @@ import com.fs.company.service.CompanyRechargeOrderService;
|
|
|
import com.fs.company.vo.CompanyRechargeExportVO;
|
|
|
import com.fs.company.vo.CompanyRechargeVO;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.redisson.api.RLock;
|
|
|
+import org.redisson.api.RedissonClient;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Qualifier;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import com.fs.company.mapper.CompanyRechargeMapper;
|
|
|
import com.fs.company.domain.CompanyRecharge;
|
|
|
@@ -45,6 +53,12 @@ public class CompanyRechargeServiceImpl implements ICompanyRechargeService
|
|
|
private CompanyMapper companyMapper;
|
|
|
@Autowired
|
|
|
private CompanyRechargeOrderService companyRechargeOrderService;
|
|
|
+ @Qualifier("redissonClient")
|
|
|
+ @Autowired
|
|
|
+ private RedissonClient redissonClient;
|
|
|
+ @Autowired
|
|
|
+ private RedisCache redisCache;
|
|
|
+
|
|
|
/**
|
|
|
* 查询充值
|
|
|
*
|
|
|
@@ -93,6 +107,54 @@ public class CompanyRechargeServiceImpl implements ICompanyRechargeService
|
|
|
{
|
|
|
return companyRechargeMapper.updateCompanyRecharge(companyRecharge);
|
|
|
}
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public R syncUpdateRedisCompanyRecharge(Company company, BigDecimal rechargeMoney) {
|
|
|
+ if(company.getCompanyId() == null){
|
|
|
+ log.error("公司充值-审核-同步更新到缓存,参数错误,公司id:{}, 充值余额:{}", company.getCompanyId(), rechargeMoney);
|
|
|
+ return R.error("公司id为空");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 公司红包真实余额key
|
|
|
+ String companyMoneyKey = FsConstants.COMPANY_MONEY_KEY + company.getCompanyId();
|
|
|
+
|
|
|
+ // 加锁,与看课发放红包的加锁保持一致
|
|
|
+ RLock lock = redissonClient.getLock(FsConstants.COMPANY_MONEY_LOCK + company.getCompanyId());
|
|
|
+ boolean lockAcquired = false;
|
|
|
+ try {
|
|
|
+ BigDecimal newMoney = company.getMoney();
|
|
|
+
|
|
|
+ // 尝试加锁
|
|
|
+ lockAcquired = lock.tryLock(3, 10, TimeUnit.SECONDS);
|
|
|
+ if (lockAcquired) {
|
|
|
+ BigDecimal redisMoney;
|
|
|
+ // 获取当前余额
|
|
|
+ String moneyStr = redisCache.getCacheObject(companyMoneyKey);
|
|
|
+ if (StringUtils.isNotEmpty(moneyStr)) {
|
|
|
+ redisMoney = new BigDecimal(moneyStr);
|
|
|
+ } else {
|
|
|
+ redisMoney = company.getMoney();
|
|
|
+ }
|
|
|
+
|
|
|
+ newMoney = redisMoney.add(rechargeMoney);
|
|
|
+
|
|
|
+ redisCache.setCacheObject(companyMoneyKey, newMoney.toString());
|
|
|
+ }
|
|
|
+ return R.ok().put("newMoney", newMoney);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("公司充值-审核-同步更新到缓存,参数错误,请求异常,异常信息:{}", e.getMessage(), e);
|
|
|
+ return R.error("审核失败,请重试");
|
|
|
+ } finally {
|
|
|
+ if (lockAcquired && lock.isHeldByCurrentThread()) {
|
|
|
+ try {
|
|
|
+ lock.unlock();
|
|
|
+ } catch (IllegalMonitorStateException e) {
|
|
|
+ log.warn("尝试释放非当前线程持有的锁: companyId:{}", company.getCompanyId());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
public List<CompanyRechargeVO> selectCompanyRechargeListVO(CompanyRechargeVO companyRecharge) {
|
|
|
return companyRechargeMapper.selectCompanyRechargeListVO(companyRecharge);
|