Browse Source

Merge branch 'hotfix-20180927-wangyc' into dev

wangyc 7 years ago
parent
commit
392ccc5a07

+ 10 - 2
src/main/java/com/uas/platform/b2c/prod/product/common/CommonTask.java

@@ -5,6 +5,7 @@ import com.uas.platform.b2c.prod.product.common.dao.CommonCountDao;
 import com.uas.platform.b2c.prod.product.common.model.CommonCount;
 import com.uas.platform.core.util.HttpUtil;
 import java.util.Date;
+import org.apache.log4j.Logger;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.jdbc.core.JdbcTemplate;
 import org.springframework.stereotype.Component;
@@ -24,6 +25,8 @@ public class CommonTask {
 
     private final JdbcTemplate jdbcTemplate;
 
+    private final Logger logger = Logger.getLogger(getClass());
+
     @Autowired
     public CommonTask(CommonCountDao commonCountDao, JdbcTemplate jdbcTemplate) {
         this.commonCountDao = commonCountDao;
@@ -37,6 +40,7 @@ public class CommonTask {
             if (CommonCount.SourceType.HTTP.equals(commonCount.getSourceType())) {
                 try {
                     String responseText = HttpUtil.sendGetRequest(commonCount.getSql(), null).getResponseText();
+                    logger.info(String.format("commonTask-%s: %s , result: %s", commonCount.getItem(), commonCount.getSql(), responseText));
                     if (!StringUtils.isEmpty(responseText)) {
                         if (commonCount.getSql().contains("113.105.74.140:8092")) {
                             commonCount.setCount(JSON.parseObject(responseText).getDouble("totalCount"));
@@ -45,14 +49,18 @@ public class CommonTask {
                         } else {
                             commonCount.setCount(Double.parseDouble(responseText));
                         }
+                        commonCount.setUpdateTime(new Date());
                     }
                 } catch (Exception e) {
+                    logger.error(String.format("请求:%s,执行错误:%s", commonCount.getSql(), e.getMessage()));
                 }
             } else {
                 // 默认,通过执行SQL语句获取数据结果
-                commonCount.setCount(jdbcTemplate.queryForObject(commonCount.getSql(), Double.class));
+                Double count = jdbcTemplate.queryForObject(commonCount.getSql(), Double.class);
+                logger.info(String.format("commonTask-%s: %s , result: %s", commonCount.getItem(), commonCount.getSql(), count));
+                commonCount.setCount(count);
+                commonCount.setUpdateTime(new Date());
             }
-            commonCount.setUpdateTime(new Date());
         }
         commonCountDao.save(commonCounts);
     }

+ 13 - 2
src/main/java/com/uas/platform/b2c/prod/product/common/api/CommonCountController.java

@@ -1,5 +1,6 @@
 package com.uas.platform.b2c.prod.product.common.api;
 
+import com.uas.platform.b2c.prod.product.common.CommonTask;
 import com.uas.platform.b2c.prod.product.common.model.CommonCount;
 import com.uas.platform.b2c.prod.product.common.service.CommonCountService;
 import com.uas.platform.b2c.prod.store.model.StoreStatus;
@@ -34,6 +35,9 @@ public class CommonCountController {
     @Autowired
     private StoreInService storeInService;
 
+    @Autowired
+    private CommonTask commonTask;
+
     /**
      * 获取已激活的通用计数器
      * @return
@@ -64,10 +68,10 @@ public class CommonCountController {
      * @date 2017年11月7日17:03:34
      * @return
      */
-    @RequestMapping(value ="/{id}/count", method = RequestMethod.GET)
+    @RequestMapping(value = "/{id}/count", method = RequestMethod.GET)
     public String findCountById(@PathVariable("id") Long id) {
         CommonCount commonCount = commonCountService.findById(id);
-        if(commonCount == null && commonCount.getCount() != null)
+        if (commonCount == null && commonCount.getCount() != null)
             throw new IllegalArgumentException("参数ID错误,数据不存在");
 
         String result = "";
@@ -123,4 +127,11 @@ public class CommonCountController {
         return commonCountService.findAllForCust();
     }
 
+    /**
+     * 执行更新任务
+     */
+    @RequestMapping(value = "/updateTask", method = RequestMethod.GET)
+    public void updateCount() {
+        commonTask.updateCount();
+    }
 }