Browse Source

私有化部署:定时任务,过期序列码设备和订阅,对于sign错误的也进行过期处理

tangxiangan 1 năm trước cách đây
mục cha
commit
22fe3fec1b

+ 2 - 0
pdf-tech-core/src/main/java/cn/kdan/pdf/tech/core/service/SubscriptionService.java

@@ -29,4 +29,6 @@ public interface SubscriptionService {
     Subscriptions getById(String id);
 
     void insert(Subscriptions subscription);
+
+    void update(Subscriptions subscription);
 }

+ 2 - 0
pdf-tech-core/src/main/java/cn/kdan/pdf/tech/core/service/VppDeviceService.java

@@ -65,4 +65,6 @@ public interface VppDeviceService {
      * @return ActivationVO
      */
     ActivationVO activation(VerifyParam verifyParam);
+
+    void expireDevice(String subscriptionId);
 }

+ 6 - 0
pdf-tech-core/src/main/java/cn/kdan/pdf/tech/core/service/VppLicenseCodeService.java

@@ -85,6 +85,12 @@ public interface VppLicenseCodeService {
      */
     List<LicenseCodes> getByCompanyId(String companyId);
 
+    /**
+     * 用于定时任务,获取所有活跃的序列码
+     * @return
+     */
+    List<LicenseCodes> listActive();
+
     /**
      * 获取公司下的未过期序列码
      * @param companyId

+ 5 - 0
pdf-tech-core/src/main/java/cn/kdan/pdf/tech/core/service/impl/SubscriptionServiceImpl.java

@@ -111,5 +111,10 @@ public class SubscriptionServiceImpl implements SubscriptionService {
         vppSubscriptionMapper.insert(subscription);
     }
 
+    @Override
+    public void update(Subscriptions subscription) {
+        vppSubscriptionMapper.updateByPrimaryKeySelective(subscription);
+    }
+
 
 }

+ 10 - 0
pdf-tech-core/src/main/java/cn/kdan/pdf/tech/core/service/impl/VppDeviceServiceImpl.java

@@ -294,4 +294,14 @@ public class VppDeviceServiceImpl implements VppDeviceService {
         return activationVO;
     }
 
+    @Override
+    public void expireDevice(String subscriptionId) {
+        Devices devices = new Devices();
+        devices.setStatus(DeviceStatusEnum.EXPIRED.code());
+        //更新一批订阅底下的所有设备为过期
+        DevicesExample devicesExample = new DevicesExample();
+        devicesExample.createCriteria().andSubscriptionIdEqualTo(subscriptionId);
+        extVppDeviceMapper.updateByExampleSelective(devices,devicesExample);
+    }
+
 }

+ 11 - 0
pdf-tech-core/src/main/java/cn/kdan/pdf/tech/core/service/impl/VppLicenseCodeServiceImpl.java

@@ -502,6 +502,17 @@ public class VppLicenseCodeServiceImpl implements VppLicenseCodeService {
         return licenseCodeMapper.selectByExample(example);
     }
 
+    @Override
+    public List<LicenseCodes> listActive() {
+        LicenseCodesExample example = new LicenseCodesExample();
+        //排除删除的退款的已过期的
+        example.createCriteria()
+               .andValidFlagNotEqualTo(LicenseCodeStatusEnum.DELETED.code())
+                .andValidFlagNotEqualTo(LicenseCodeStatusEnum.EXPIRED.code())
+                .andValidFlagNotEqualTo(LicenseCodeStatusEnum.Refunded.code());
+        return licenseCodeMapper.selectByExample(example);
+    }
+
     @Override
     public List<LicenseCodes> getByCompanyIdRemoveExpired(String companyId) {
         LicenseCodesExample example = new LicenseCodesExample();

+ 73 - 0
pdf-tech-core/src/main/java/cn/kdan/pdf/tech/core/task/LicenseTask.java

@@ -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);
+        }
+    }
+}