|
@@ -1,5 +1,6 @@
|
|
|
package com.citu.module.menduner.system.aop;
|
|
|
|
|
|
+import com.baomidou.dynamic.datasource.annotation.DSTransactional;
|
|
|
import com.citu.module.menduner.common.util.LoginUserContext;
|
|
|
import com.citu.module.menduner.system.controller.base.enterprise.vip.EnterpriseEntitlementRespVO;
|
|
|
import com.citu.module.menduner.system.dal.dataobject.enterprise.EnterpriseEntitlementDO;
|
|
@@ -14,6 +15,9 @@ import org.springframework.util.StringUtils;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
|
|
|
+import java.util.function.Consumer;
|
|
|
+import java.util.function.Supplier;
|
|
|
+
|
|
|
import static com.citu.framework.common.exception.enums.GlobalErrorCodeConstants.FORBIDDEN;
|
|
|
import static com.citu.framework.common.exception.util.ServiceExceptionUtil.exception;
|
|
|
import static com.citu.module.menduner.system.aop.VipEntitlementCheck.*;
|
|
@@ -34,18 +38,16 @@ public class VipEntitlementCheckAspect {
|
|
|
@Lazy
|
|
|
private EnterpriseEntitlementService enterpriseEntitlementService;
|
|
|
|
|
|
-
|
|
|
+ @DSTransactional
|
|
|
@Around("@annotation(vipEntitlementCheck)")
|
|
|
public Object around(ProceedingJoinPoint joinPoint, VipEntitlementCheck vipEntitlementCheck) {
|
|
|
|
|
|
boolean isEnterprise = VipEntitlementCheck.ENTERPRISE.equals(vipEntitlementCheck.userType());
|
|
|
Long userId = LoginUserContext.getUserId();
|
|
|
Long enterpriseId = LoginUserContext.getEnterpriseId3();
|
|
|
- validate(isEnterprise, enterpriseId, userId, vipEntitlementCheck.type(), vipEntitlementCheck.operate());
|
|
|
try {
|
|
|
- Object result = joinPoint.proceed();
|
|
|
- process(isEnterprise, enterpriseId, userId, vipEntitlementCheck.type(), vipEntitlementCheck.operate());
|
|
|
- return result;
|
|
|
+ deductQuota(isEnterprise, enterpriseId, userId, vipEntitlementCheck.type(), vipEntitlementCheck.operate());
|
|
|
+ return joinPoint.proceed();
|
|
|
} catch (Throwable e) {
|
|
|
e.getStackTrace();
|
|
|
throw exception(FORBIDDEN);
|
|
@@ -55,46 +57,23 @@ public class VipEntitlementCheckAspect {
|
|
|
/**
|
|
|
* 验证权限
|
|
|
*/
|
|
|
- public void validate(String type) {
|
|
|
+ public void deductQuota(String type) {
|
|
|
Long enterpriseId = LoginUserContext.getEnterpriseId3();
|
|
|
- validate(null != enterpriseId, enterpriseId, LoginUserContext.getUserId(), type, null);
|
|
|
+ deductQuota(null != enterpriseId, enterpriseId, LoginUserContext.getUserId(), type, OperationType.DEDUCT);
|
|
|
}
|
|
|
|
|
|
- public void validate(String type, VipEntitlementCheck.OperationType operator) {
|
|
|
- Long enterpriseId = LoginUserContext.getEnterpriseId3();
|
|
|
- validate(null != enterpriseId, enterpriseId, LoginUserContext.getUserId(), type, operator);
|
|
|
- }
|
|
|
|
|
|
- public void validate(String type, boolean isTrigger) {
|
|
|
+ public void deductQuota(String type, boolean isTrigger) {
|
|
|
if (!isTrigger) {
|
|
|
// 如果为false则不执行
|
|
|
return;
|
|
|
}
|
|
|
- validate(type);
|
|
|
+ deductQuota(type);
|
|
|
}
|
|
|
|
|
|
|
|
|
- /**
|
|
|
- * 权益处理
|
|
|
- *
|
|
|
- * @param type 权益类型
|
|
|
- * @param operator 操作
|
|
|
- */
|
|
|
- public void process(String type, VipEntitlementCheck.OperationType operator) {
|
|
|
- Long enterpriseId = LoginUserContext.getEnterpriseId3();
|
|
|
- process(null != enterpriseId, enterpriseId, LoginUserContext.getUserId(), type, operator);
|
|
|
- }
|
|
|
-
|
|
|
- public void process(String type, VipEntitlementCheck.OperationType operator, boolean isTrigger) {
|
|
|
- if (!isTrigger) {
|
|
|
- // 如果为false则不执行
|
|
|
- return;
|
|
|
- }
|
|
|
- process(type, operator);
|
|
|
- }
|
|
|
-
|
|
|
|
|
|
- private void validate(boolean isEnterprise, Long enterpriseId,
|
|
|
+ private void deductQuota(boolean isEnterprise, Long enterpriseId,
|
|
|
Long userId, String type,
|
|
|
VipEntitlementCheck.OperationType operator) {
|
|
|
|
|
@@ -111,75 +90,36 @@ public class VipEntitlementCheckAspect {
|
|
|
// 没有权限
|
|
|
throw exception(FORBIDDEN);
|
|
|
}
|
|
|
- if (null == operator || VipEntitlementCheck.OperationType.ADD.equals(operator)) {
|
|
|
- // 新增不做效验
|
|
|
- return;
|
|
|
- }
|
|
|
- switch (type) {
|
|
|
- case OPERATE_PUBLISH_JOB:
|
|
|
- if (entitlementRespVO.getPublishJobCount() <= 0) {
|
|
|
- throw exception(ENTERPRISE_ENTITLEMENT_QUOTA_OVERFLOW);
|
|
|
- }
|
|
|
- break;
|
|
|
- case OPERATE_SEARCH:
|
|
|
- if (entitlementRespVO.getSearchCount() <= 0) {
|
|
|
- throw exception(ENTERPRISE_ENTITLEMENT_QUOTA_OVERFLOW);
|
|
|
- }
|
|
|
- case OPERATE_LOOK_CV:
|
|
|
- if (entitlementRespVO.getLookCvCount() <= 0) {
|
|
|
- throw exception(ENTERPRISE_ENTITLEMENT_QUOTA_OVERFLOW);
|
|
|
- }
|
|
|
- break;
|
|
|
- default:
|
|
|
- throw exception(FORBIDDEN);
|
|
|
- }
|
|
|
+// if (null == operator || VipEntitlementCheck.OperationType.ADD.equals(operator)) {
|
|
|
+// // 新增不做效验
|
|
|
+// return;
|
|
|
+// }
|
|
|
+ deductQuota(type, entitlementRespVO,operator);
|
|
|
}
|
|
|
|
|
|
- private void process(boolean isEnterprise,
|
|
|
- Long enterpriseId, Long userId,
|
|
|
- String type, VipEntitlementCheck.OperationType operator) {
|
|
|
- if (!isEnterprise || LoginUserContext.checkIsSystemUser()) {
|
|
|
- // 用户
|
|
|
- return;
|
|
|
- }
|
|
|
- // TODO 设计业务的应该通过业务查询计数,但是如果按照这种办法,旧数据那些发布了很多职位的怎么办?
|
|
|
- // TODO 目前只单纯写个加减1,还未沟通具体逻辑
|
|
|
+ /**
|
|
|
+ * 扣减额度
|
|
|
+ */
|
|
|
+ private void deductQuota(String type, EnterpriseEntitlementRespVO entitlementRespVO, VipEntitlementCheck.OperationType operator) {
|
|
|
+ int amount = -1;
|
|
|
|
|
|
- // 企业
|
|
|
- EnterpriseEntitlementRespVO entitlementRespVO =
|
|
|
- enterpriseEntitlementService.getByEnterpriseIdAndUserId(enterpriseId, userId);
|
|
|
- int amount = VipEntitlementCheck.OperationType.ADD.equals(operator) ? 1 : -1;
|
|
|
- int num = 0;
|
|
|
switch (type) {
|
|
|
case OPERATE_PUBLISH_JOB:
|
|
|
- num = entitlementRespVO.getPublishJobCount() + amount;
|
|
|
- if (num > entitlementRespVO.getPackageInfo().getPublishJobCount()) {
|
|
|
- // 用户持有的额度大于套餐的额度
|
|
|
- // TODO 1、管理员给用户加的额度 ,要不要扣额度?
|
|
|
- // TODO 2、如果旧职位是开启状态的,现在操作关闭会计算累加
|
|
|
- // return;
|
|
|
- }
|
|
|
- entitlementRespVO.setPublishJobCount(num);
|
|
|
+ validateAndUpdateQuota(entitlementRespVO::getPublishJobCount, entitlementRespVO::setPublishJobCount,
|
|
|
+ entitlementRespVO.getPackageInfo().getPublishJobCount(), amount, operator);
|
|
|
break;
|
|
|
case OPERATE_SEARCH:
|
|
|
- num = entitlementRespVO.getSearchCount() + amount;
|
|
|
- if (num > entitlementRespVO.getPackageInfo().getPublishJobCount()) {
|
|
|
- // 用户持有的额度大于套餐的额度
|
|
|
- // return;
|
|
|
- }
|
|
|
- entitlementRespVO.setSearchCount(num);
|
|
|
+ validateAndUpdateQuota(entitlementRespVO::getSearchCount, entitlementRespVO::setSearchCount,
|
|
|
+ entitlementRespVO.getPackageInfo().getSearchCount(), amount, operator);
|
|
|
break;
|
|
|
case OPERATE_LOOK_CV:
|
|
|
- num = entitlementRespVO.getLookCvCount() + amount;
|
|
|
- if (num > entitlementRespVO.getPackageInfo().getLookCvCount()) {
|
|
|
- // 用户持有的额度大于套餐的额度
|
|
|
- // return;
|
|
|
- }
|
|
|
- entitlementRespVO.setLookCvCount(num);
|
|
|
+ validateAndUpdateQuota(entitlementRespVO::getLookCvCount, entitlementRespVO::setLookCvCount,
|
|
|
+ entitlementRespVO.getPackageInfo().getLookCvCount(), amount, operator);
|
|
|
break;
|
|
|
default:
|
|
|
- return;
|
|
|
+ throw exception(FORBIDDEN);
|
|
|
}
|
|
|
+
|
|
|
enterpriseEntitlementService.update(EnterpriseEntitlementDO.builder()
|
|
|
.id(entitlementRespVO.getId())
|
|
|
.publishJobCount(entitlementRespVO.getPublishJobCount())
|
|
@@ -188,4 +128,25 @@ public class VipEntitlementCheckAspect {
|
|
|
.build());
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 校验并更新额度
|
|
|
+ */
|
|
|
+ private void validateAndUpdateQuota(Supplier<Integer> getCurrentCount, Consumer<Integer> setNewCount,
|
|
|
+ int maxCount, int amount, VipEntitlementCheck.OperationType operator) {
|
|
|
+ int currentCount = getCurrentCount.get();
|
|
|
+ int newCount = currentCount + amount;
|
|
|
+ if (VipEntitlementCheck.OperationType.DEDUCT.equals(operator) && currentCount <= 0) {
|
|
|
+ throw exception(ENTERPRISE_ENTITLEMENT_QUOTA_OVERFLOW);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (newCount > maxCount) {
|
|
|
+ // 用户持有的额度大于套餐的额度
|
|
|
+ // TODO 1、管理员给用户加的额度 ,要不要扣额度?
|
|
|
+ // TODO 2、如果旧职位是开启状态的,现在操作关闭会计算累加
|
|
|
+ // return;
|
|
|
+ }
|
|
|
+
|
|
|
+ setNewCount.accept(newCount);
|
|
|
+ }
|
|
|
+
|
|
|
}
|