Commit 21cc67ca by zhiwj

代码结构调整

parent 16990677
......@@ -18,6 +18,8 @@ import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;
/**
*
* @ClassName: BillingEnterpriseSmsPackageServiceImpl
......@@ -77,19 +79,17 @@ public class BillingEnterpriseSmsPackageServiceImpl implements BillingEnterprise
@Override
public RAtomicLong getTotalSmsInPackageByEnterpriseId(Integer enterpriseId) {
RedissonClient redisClient = RedisUtil.getRedisClient();
RAtomicLong smsCountR = redisClient.getAtomicLong(Constants.ENTERPRISE_BILLING_SMS_PACKAGE_COUNT + enterpriseId);
long smsPackageCount = smsCountR.get();
if (smsPackageCount == 0) {
Long smsCountCache = RedisUtil.getCache(Constants.ENTERPRISE_BILLING_SMS_PACKAGE_COUNT + enterpriseId, Long.class);
if (smsCountCache == null) {
try {
RedisUtil.lock(Constants.ENTERPRISE_BILLING_SMS_PACKAGE_COUNT_LOCK + enterpriseId, 30L);
logger.info("redis中没有套餐包 查询数据库:{}", enterpriseId);
Integer totalSms = tabBillingEnterpriseSmsPackageMapper.getTotalSmsInPackageByEnterpriseId(enterpriseId);
if (totalSms != null) {
smsCountR.set(totalSms);
} else {
smsCountR.set(0);
}
RedisUtil.setCache(Constants.ENTERPRISE_BILLING_SMS_PACKAGE_COUNT + enterpriseId, totalSms != null ? totalSms : 0, 1L, TimeUnit.HOURS);
}finally {
RedisUtil.unlock(Constants.ENTERPRISE_BILLING_SMS_PACKAGE_COUNT_LOCK + enterpriseId);
}
return smsCountR;
}
return redisClient.getAtomicLong(Constants.ENTERPRISE_BILLING_SMS_PACKAGE_COUNT + enterpriseId);
}
}
......@@ -183,14 +183,23 @@ public class SmsRecordApiServiceImpl implements SmsRecordApiService {
套餐包计费
这里不能改成 decrementAndGet自减, 否则会出现减到0的情况
*/
boolean smsCountDeduct = rSmsCount.compareAndSet(smsCount, smsCount - 1);
long curr = rSmsCount.decrementAndGet();
if (curr < 0) {
// 因为并发超扣了 套餐包已经用完 那应该扣账户
billingAccountService.deductAccountByCache(fee.doubleValue() / 1000, enterpriseId);
return 4;
} else {
// 扣套餐包正常
return 0;
}
/* boolean smsCountDeduct = rSmsCount.compareAndSet(smsCount, smsCount - 1);
// rSmsCount.decrementAndGet();
if (!smsCountDeduct) {
// 设置失败 重新计算
return deduct(rSmsCount, enterpriseId, fee);
} else {
return 0;
}
}*/
} else {
// 账户计费
billingAccountService.deductAccountByCache(fee.doubleValue() / 1000, enterpriseId);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment