Browse Source

定时任务更新剩余时间

zhoutg 5 years ago
parent
commit
2369079c6e

+ 2 - 1
common/src/main/java/com/diagbot/util/RSAEncrypt.java

@@ -30,7 +30,8 @@ public class RSAEncrypt {
         //生成公钥和私钥
         // genKeyPair();
         //加密字符串
-        String message = "1";
+//        String message = "2019-11-21 00:00:00";
+        String message = "2019-11-21 00:00:00";
         String messageEn = encrypt(message, pubKey);
         System.out.println(message + "\t加密后的字符串为:" + messageEn);
         String messageDe = decrypt(messageEn, privateKey);

+ 1 - 1
gateway-service/src/main/java/com/diagbot/client/TranServiceClient.java

@@ -39,5 +39,5 @@ public interface TranServiceClient {
      * @return
      */
     @PostMapping("/sysSet/refreshLicence")
-    RespDTO<List<SysSetInfoDTO>> refreshLicence();
+    RespDTO<Boolean> refreshLicence();
 }

+ 1 - 1
gateway-service/src/main/java/com/diagbot/client/hystrix/TranServiceHystrix.java

@@ -33,7 +33,7 @@ public class TranServiceHystrix implements TranServiceClient {
     }
 
     @Override
-    public RespDTO<List<SysSetInfoDTO>> refreshLicence() {
+    public RespDTO<Boolean> refreshLicence() {
         log.error("【hystrix】调用{}异常", "refreshLicence");
         return null;
     }

+ 6 - 2
gateway-service/src/main/java/com/diagbot/config/ScheduleTask.java

@@ -1,6 +1,7 @@
 package com.diagbot.config;
 
 import com.diagbot.client.TranServiceClient;
+import com.diagbot.filter.GlobalGatewayFilter;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.context.annotation.Configuration;
 import org.springframework.scheduling.annotation.EnableScheduling;
@@ -8,6 +9,7 @@ import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.stereotype.Component;
 
 import java.time.LocalDateTime;
+import java.util.Date;
 
 /**
  * @description:
@@ -19,10 +21,12 @@ import java.time.LocalDateTime;
 @EnableScheduling   // 2.开启定时任务
 public class ScheduleTask {
 
+    @Autowired
+    GlobalGatewayFilter globalGatewayFilter;
 
     // 添加定时任务,每天12点执行
     @Scheduled(cron = "0 0 12 * * ?")
-    private void configureTasks() {
-        System.err.println("执行静态定时任务时间: " + LocalDateTime.now());
+    public void configureTasks() {
+        globalGatewayFilter.refreshLicenceMap();
     }
 }

+ 33 - 15
gateway-service/src/main/java/com/diagbot/filter/GlobalGatewayFilter.java

@@ -16,6 +16,7 @@ import com.diagbot.util.DateUtil;
 import com.diagbot.util.EnDecodeUtil;
 import com.diagbot.util.GsonUtil;
 import com.diagbot.util.RSAEncrypt;
+import com.diagbot.util.RespDTOUtil;
 import com.diagbot.util.StringUtil;
 import com.diagbot.vo.HospitalListVO;
 import lombok.extern.slf4j.Slf4j;
@@ -217,12 +218,25 @@ public class GlobalGatewayFilter implements GlobalFilter {
     }
 
 
+    /**
+     * 更新licence剩余天数,减1
+     */
     public void refreshLicenceMap() {
-        tranServiceClient.refreshLicence();
-        for (String key : licenceMap.keySet()) {
-            if (key.indexOf(LICENCE_REMAIN) > -1) {
-
+        RespDTO<Boolean> res = tranServiceClient.refreshLicence();
+        RespDTOUtil.respIsNG(res);
+        if (res.data == false) {
+            throw new CommonException(CommonErrorCode.SERVER_IS_ERROR, "更新licence剩余天数失败");
+        }
+        try {
+            for (String key : licenceMap.keySet()) {
+                if (key.indexOf(LICENCE_REMAIN) > -1) {
+                    String val = licenceMap.get(key);
+                    System.out.println(key + " : " + String.valueOf(Integer.parseInt(val) - 1));
+                    licenceMap.put(key, String.valueOf(Integer.parseInt(val) - 1));
+                }
             }
+        } catch (Exception e) {
+            throw new CommonException(CommonErrorCode.SERVER_IS_ERROR, "更新licence剩余天数失败");
         }
     }
 
@@ -234,16 +248,13 @@ public class GlobalGatewayFilter implements GlobalFilter {
      * @param serviceName 服务名称
      */
     public void verifyLicenceExpire(String hospitalCode, String serviceName) {
+        if (StringUtil.isEmpty(hospitalCode) || StringUtil.isEmpty(serviceName)) {
+            return ;
+        }
         String licenceExpire = licenceMap.get(hospitalCode + LICENCE_EXPIRE + serviceName);
         if (StringUtil.isNotEmpty(licenceExpire)) {
-            String licence = "";
-            try {
-                licence = RSAEncrypt.decrypt(licenceExpire);
-            } catch (Exception e) { // licence 被篡改
-                throw new CommonException(CommonErrorCode.LICENCE_ERROR);
-            }
             String nowStr = DateUtil.format(new Date(), "YYYY-MM-dd HH:mm:ss");
-            if (licence.compareTo(nowStr) < 0) { // licence 已到期
+            if (licenceExpire.compareTo(nowStr) < 0) { // licence 已到期
                 throw new CommonException(CommonErrorCode.LICENCE_EXPIRE);
             }
         }
@@ -257,11 +268,14 @@ public class GlobalGatewayFilter implements GlobalFilter {
      * @param serviceName 服务名称
      */
     public void verifyLicenceRemain(String hospitalCode, String serviceName) {
+        if (StringUtil.isEmpty(hospitalCode) || StringUtil.isEmpty(serviceName)) {
+            return ;
+        }
         String licenceRemain = licenceMap.get(hospitalCode + LICENCE_REMAIN + serviceName);
         if (StringUtil.isNotEmpty(licenceRemain)) {
             int day = 0;
             try {
-                day = Integer.parseInt(RSAEncrypt.decrypt(licenceRemain));
+                day = Integer.parseInt(licenceRemain);
             } catch (Exception e) { // licence 被篡改
                 throw new CommonException(CommonErrorCode.LICENCE_ERROR);
             }
@@ -277,7 +291,6 @@ public class GlobalGatewayFilter implements GlobalFilter {
      */
     public void initLicenceMap() {
         // 初始化licence
-        licenceMap.clear();
         if (!initLicence) {
             HospitalListVO hospitalListVO = new HospitalListVO();
             List<String> codeList = new ArrayList<>();
@@ -286,16 +299,21 @@ public class GlobalGatewayFilter implements GlobalFilter {
             codeList.add(LICENCE_START);
             hospitalListVO.setCodeList(codeList);
             RespDTO<List<SysSetInfoDTO>> respDTO = tranServiceClient.getSysSetInfoDataByList(hospitalListVO);
-            if (respDTO != null) {
+            if (respDTO != null && respDTO.data != null) {
                 for (SysSetInfoDTO setInfoDTO : respDTO.data) {
                     String serName = SysTypeEnum.getName(setInfoDTO.getSysType());
                     if (LICENCE_START.equals(setInfoDTO.getCode())) {
                         licenceMap.put(setInfoDTO.getCode(), setInfoDTO.getValue());
                     } else {
-                        licenceMap.put(setInfoDTO.getHospitalCode() + setInfoDTO.getCode() + serName.substring(0, serName.indexOf("-")), setInfoDTO.getValue());
+                        try {
+                            licenceMap.put(setInfoDTO.getHospitalCode() + setInfoDTO.getCode() + serName.substring(0, serName.indexOf("-")), RSAEncrypt.decrypt(setInfoDTO.getValue()));
+                        } catch (Exception e) {
+                            throw new CommonException(CommonErrorCode.LICENCE_ERROR);
+                        }
                     }
                 }
             }
+            initLicence = true;
         }
     }
 }

+ 7 - 1
tran-service/src/main/java/com/diagbot/facade/SysSetFacade.java

@@ -6,6 +6,7 @@ import com.diagbot.dto.SysSetInfoDTO;
 import com.diagbot.entity.SysSet;
 import com.diagbot.exception.CommonErrorCode;
 import com.diagbot.exception.CommonException;
+import com.diagbot.service.SysSetService;
 import com.diagbot.service.impl.SysSetServiceImpl;
 import com.diagbot.util.BeanUtil;
 import com.diagbot.util.DateUtil;
@@ -15,6 +16,8 @@ import com.diagbot.util.RSAEncrypt;
 import com.diagbot.vo.HospitalListVO;
 import com.diagbot.vo.HospitalSetVO;
 import org.bouncycastle.jcajce.provider.asymmetric.RSA;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Qualifier;
 import org.springframework.stereotype.Component;
 
 import java.util.List;
@@ -27,6 +30,9 @@ import java.util.List;
 @Component
 public class SysSetFacade extends SysSetServiceImpl {
 
+    @Autowired
+    @Qualifier("sysSetServiceImpl")
+    SysSetService sysSetService;
 
     /**
      * @param hospitalSetVO
@@ -77,7 +83,7 @@ public class SysSetFacade extends SysSetServiceImpl {
                 sysSet.setValue(RSAEncrypt.encrypt(String.valueOf(Integer.parseInt(val) - 1)));
                 sysSet.setGmtModified(DateUtil.now());
             }
-            this.updateBatchById(sysSetList);
+            sysSetService.updateBatchById(sysSetList);
         } catch (Exception e) {
             e.printStackTrace();
             throw new CommonException(CommonErrorCode.SERVER_IS_ERROR, "更新licence失败");

+ 1 - 1
tran-service/src/main/java/com/diagbot/web/SysSetController.java

@@ -52,7 +52,7 @@ public class SysSetController {
     @SysLogger("getSysSetInfoDatasByList")
     public RespDTO<List<SysSetInfoDTO>> getSysSetInfoDataByList(@RequestBody HospitalListVO hospitalListVO) {
         List<SysSetInfoDTO> data = sysSetFacade.getSysSetInfoDataByList(hospitalListVO);
-        return RespDTO.onSuc(null);
+        return RespDTO.onSuc(data);
     }