|
@@ -16,7 +16,6 @@ import com.ym.mec.common.page.WrapperUtil;
|
|
|
import com.ym.mec.common.tenant.TenantContextHolder;
|
|
|
import com.ym.mec.thirdparty.yqpay.DateUtils;
|
|
|
import org.apache.commons.collections.CollectionUtils;
|
|
|
-import org.redisson.api.RBucket;
|
|
|
import org.redisson.api.RedissonClient;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
@@ -29,7 +28,6 @@ import java.math.MathContext;
|
|
|
import java.math.RoundingMode;
|
|
|
import java.text.ParseException;
|
|
|
import java.util.*;
|
|
|
-import java.util.concurrent.TimeUnit;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
@@ -62,13 +60,6 @@ public class TenantAssetsInfoServiceImpl extends ServiceImpl<TenantAssetsInfoDao
|
|
|
public void courseDeductionRules(List<CourseSchedule> dto) {
|
|
|
Integer tenantId = TenantContextHolder.getTenantId();
|
|
|
|
|
|
- //防止重复点击 加锁
|
|
|
- String key = "Tenant_Course_Deduct:" + tenantId;
|
|
|
- RBucket<Object> bucket = redissonClient.getBucket(key);
|
|
|
- //原子操作 抢锁成功为true
|
|
|
- if (!bucket.trySet(tenantId, 1L, TimeUnit.MINUTES)) {
|
|
|
- throw new BizException("有人正在排课请勿同时操作!");
|
|
|
- }
|
|
|
//校验课程 筛选出线上课
|
|
|
dto = getOnlineCourse(dto);
|
|
|
if (CollectionUtils.isEmpty(dto)) {
|
|
@@ -78,7 +69,7 @@ public class TenantAssetsInfoServiceImpl extends ServiceImpl<TenantAssetsInfoDao
|
|
|
TenantAssetsInfo assetsInfo = this.getOne(new WrapperUtil<TenantAssetsInfo>()
|
|
|
.hasEq("tenant_id_", tenantId).queryWrapper());
|
|
|
if (Objects.isNull(assetsInfo)) {
|
|
|
- throw new BizException("未查询到该机构的账户资产信息!");
|
|
|
+ throw new BizException("未查询到该账户资产信息!");
|
|
|
}
|
|
|
|
|
|
//获取云教室规则 String人数-BigDecimal每分钟扣费标准
|
|
@@ -90,17 +81,14 @@ public class TenantAssetsInfoServiceImpl extends ServiceImpl<TenantAssetsInfoDao
|
|
|
//获取每分钟扣费标准
|
|
|
BigDecimal minutePrice = rule.get(String.valueOf(totalPeople));
|
|
|
if (Objects.isNull(minutePrice)) {
|
|
|
- throw new BizException("该机构未设置 " + totalPeople + " 人(含老师)的课程收费标准,请前往机构产品设置中确认该人数的设置是否存在!");
|
|
|
+ //实际上是没有这个扣费标准
|
|
|
+ throw new BizException("课程人数已达上限,请联系教务老师!");
|
|
|
}
|
|
|
//计算总上课时间
|
|
|
BigDecimal courseDate = getCourseDate(course);
|
|
|
//课程单价 = 每分钟扣费标准 * 总上课时间
|
|
|
BigDecimal coursePrice = minutePrice.multiply(courseDate, new MathContext(2, RoundingMode.HALF_UP));
|
|
|
frozenAmount = frozenAmount.add(coursePrice);
|
|
|
- //判断余额是否足够
|
|
|
- if (assetsInfo.getBalance().compareTo(frozenAmount) < 0) {
|
|
|
- throw new BizException("余额不足!");
|
|
|
- }
|
|
|
|
|
|
//写入流水
|
|
|
TenantCloudCourseRecord record = new TenantCloudCourseRecord();
|
|
@@ -113,10 +101,12 @@ public class TenantAssetsInfoServiceImpl extends ServiceImpl<TenantAssetsInfoDao
|
|
|
tenantCloudCourseRecordService.save(record);
|
|
|
}
|
|
|
|
|
|
- assetsInfo.setBalance(assetsInfo.getBalance().subtract(frozenAmount));
|
|
|
- assetsInfo.setFrozenAmount(frozenAmount.add(assetsInfo.getFrozenAmount()));
|
|
|
- this.updateById(assetsInfo);
|
|
|
- bucket.delete();
|
|
|
+ //直接修改余额,利用数据库的规则来防ABA的出现
|
|
|
+ if (baseMapper.updateAmount(frozenAmount) != 1) {
|
|
|
+ //可能是余额不足
|
|
|
+ throw new BizException("线上课预约火爆,请联系教务老师!");
|
|
|
+ }
|
|
|
+
|
|
|
}
|
|
|
|
|
|
private List<CourseSchedule> getOnlineCourse(List<CourseSchedule> dto) {
|
|
@@ -133,13 +123,13 @@ public class TenantAssetsInfoServiceImpl extends ServiceImpl<TenantAssetsInfoDao
|
|
|
TenantConfig tenantConfig = tenantConfigService.getOne(new WrapperUtil<TenantConfig>()
|
|
|
.hasEq("tenant_id_", tenantId).queryWrapper());
|
|
|
if (Objects.isNull(tenantConfig)) {
|
|
|
- throw new BizException("未查询到机构云教室规则信息!");
|
|
|
+ throw new BizException("未查询到云教室规则信息!");
|
|
|
}
|
|
|
return Optional.ofNullable(tenantConfig.getConfig())
|
|
|
.map(c -> JSON.parseObject(c, TenantConfigDetail.class))
|
|
|
.map(TenantConfigDetail::getCloud_room_rule)
|
|
|
.map(CloudRoomRule::getCloud_room_config)
|
|
|
- .orElseThrow(() -> new BizException("未查询到机构云教室规则!"));
|
|
|
+ .orElseThrow(() -> new BizException("未查询到云教室规则!"));
|
|
|
}
|
|
|
|
|
|
private BigDecimal getCourseDate(CourseSchedule course) {
|