|
@@ -0,0 +1,73 @@
|
|
|
+package cn.kdan.pdf.tech.core.task;
|
|
|
+
|
|
|
+import cn.kdan.pdf.tech.core.enums.LicenseCodeStatusEnum;
|
|
|
+import cn.kdan.pdf.tech.core.enums.SubscriptionStatusEnum;
|
|
|
+import cn.kdan.pdf.tech.core.model.LicenseCodes;
|
|
|
+import cn.kdan.pdf.tech.core.model.Subscriptions;
|
|
|
+import cn.kdan.pdf.tech.core.pojo.dto.VppPrivateDeployment;
|
|
|
+import cn.kdan.pdf.tech.core.service.SubscriptionService;
|
|
|
+import cn.kdan.pdf.tech.core.service.VppDeviceService;
|
|
|
+import cn.kdan.pdf.tech.core.service.VppLicenseCodeService;
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import lombok.RequiredArgsConstructor;
|
|
|
+import org.apache.commons.codec.digest.DigestUtils;
|
|
|
+import org.springframework.beans.BeanUtils;
|
|
|
+import org.springframework.scheduling.annotation.Scheduled;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
+
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+@Component
|
|
|
+@RequiredArgsConstructor
|
|
|
+public class LicenseTask {
|
|
|
+ private final VppLicenseCodeService licenseCodeService;
|
|
|
+ private final VppDeviceService vppDeviceService;
|
|
|
+ private final SubscriptionService subscriptionService;
|
|
|
+ @Scheduled(fixedRate = 3 * 60 * 60 * 1000) // 每3小时执行一次
|
|
|
+ public void handleLicenseCode() {
|
|
|
+ List<LicenseCodes> licenseCodes = licenseCodeService.listActive();
|
|
|
+ //已处理过过期的订阅id
|
|
|
+ List<String> list = new ArrayList<>();
|
|
|
+ //验证序列码有效性
|
|
|
+ //如果过期时间小于当前时间,则置为过期,并且修改订阅状态,序列码状态,设备状态
|
|
|
+ licenseCodes.stream().filter(licenseCode -> licenseCode.getAssignedDate() != null).forEach(licenseCode -> {
|
|
|
+ verifySign(licenseCode);
|
|
|
+ if (licenseCode.getEndUpAt().getTime() < System.currentTimeMillis()) {
|
|
|
+ //过期序列码置为过期
|
|
|
+ LicenseCodes expiredLicenseCode = new LicenseCodes();
|
|
|
+ expiredLicenseCode.setId(licenseCode.getId());
|
|
|
+ expiredLicenseCode.setValidFlag(LicenseCodeStatusEnum.EXPIRED.code());
|
|
|
+ licenseCodeService.update(expiredLicenseCode);
|
|
|
+ if (!StringUtils.isEmpty(licenseCode.getSubscriptionId()) && !list.contains(licenseCode.getSubscriptionId())) {
|
|
|
+ Subscriptions subscriptions = new Subscriptions();
|
|
|
+ subscriptions.setId(licenseCode.getSubscriptionId());
|
|
|
+ subscriptions.setStatus(SubscriptionStatusEnum.EXPIRED.value());
|
|
|
+ //加到已处理过的订阅idList中,防止重复处理
|
|
|
+ subscriptionService.update(subscriptions);
|
|
|
+ //更新设备
|
|
|
+ vppDeviceService.expireDevice(licenseCode.getSubscriptionId());
|
|
|
+ list.add(licenseCode.getSubscriptionId());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ private void verifySign(LicenseCodes licenseCode) {
|
|
|
+ //验证序列码是否合法
|
|
|
+ String sign = licenseCode.getSign();
|
|
|
+ VppPrivateDeployment deployment = new VppPrivateDeployment();
|
|
|
+ BeanUtils.copyProperties(licenseCode, deployment);
|
|
|
+ //将对象转化为json
|
|
|
+ String deploymentJson = JSON.toJSONString(deployment) + "vpp_private_deployment";
|
|
|
+ //对上述json进行MD5加密并且和sign对比
|
|
|
+ String signOriginal = DigestUtils.md5Hex(deploymentJson);
|
|
|
+ if (!signOriginal.equals(sign)) {
|
|
|
+ LicenseCodes expiredLicenseCode = new LicenseCodes();
|
|
|
+ expiredLicenseCode.setId(licenseCode.getId());
|
|
|
+ expiredLicenseCode.setValidFlag(LicenseCodeStatusEnum.EXPIRED.code());
|
|
|
+ licenseCodeService.update(expiredLicenseCode);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|