浏览代码

四平大华的图片批量更新下载到本地定时任务

majunqi 1 年之前
父节点
当前提交
ced9f66f81

+ 3 - 0
src/main/java/com/sooka/sponest/data/system/attach/mapper/CenterdataTAttachMapper.java

@@ -4,6 +4,7 @@ import com.sooka.sponest.data.system.attach.domain.AppDataTAttach;
 import com.sooka.sponest.data.system.attach.domain.CenterdataTAttach;
 
 import java.util.List;
+import java.util.Map;
 
 /**
  * 附件Mapper接口
@@ -76,4 +77,6 @@ public interface CenterdataTAttachMapper {
     int insertEventFileLog(CenterdataTAttach res);
 
     int insertListCenterdataTAttach(List<CenterdataTAttach> list);
+
+    List<Map<String, Object>> selectFailUpattah();
 }

+ 5 - 0
src/main/java/com/sooka/sponest/data/system/attach/service/ICenterdataTAttachService.java

@@ -1,5 +1,6 @@
 package com.sooka.sponest.data.system.attach.service;
 
+import com.ruoyi.system.api.domain.SysFile;
 import com.sooka.sponest.data.system.attach.domain.CenterdataTAttach;
 
 import java.util.List;
@@ -61,4 +62,8 @@ public interface ICenterdataTAttachService {
 
     //新增附件List
     int insertListCenterdataTAttach(List<CenterdataTAttach> list);
+
+    List<Map<String, Object>> selectFailUpattah();
+
+    void updateEventAttachSchedule(Map<String, Object> paramMap,SysFile sysFile) throws Exception;
 }

+ 20 - 0
src/main/java/com/sooka/sponest/data/system/attach/service/impl/CenterdataTAttachServiceImpl.java

@@ -133,6 +133,21 @@ public class CenterdataTAttachServiceImpl extends BaseServiceImpl implements ICe
         });
     }
 
+    @Transactional
+    @Override
+    public void updateEventAttachSchedule(Map<String, Object> paramMap,SysFile sysFile) throws Exception{
+        CenterdataTAttach centerdataTAttach=new CenterdataTAttach();
+        centerdataTAttach.setFileName(MapUtils.getString(paramMap,"url"));
+        centerdataTAttach.setAttachPath(sysFile.getUrl());
+        centerdataTAttach.setBusSource("PC");
+        centerdataTAttach.setBusId(MapUtils.getString(paramMap,"busId"));
+       centerdataTAttachMapper.updateEventAttach(centerdataTAttach);
+        setSookaDataBase(centerdataTAttach);
+        centerdataTAttach.setFileName(JSON.toJSONString(centerdataTAttach));
+        centerdataTAttach.setCreateTime(DateUtils.getNowDate());
+       centerdataTAttachMapper.insertEventFileLog(centerdataTAttach);
+    }
+
     @Override
     public void updateEventAttach(CenterdataTAttach centerdataTAttach) {
         centerdataTAttachMapper.updateEventAttach(centerdataTAttach);
@@ -142,4 +157,9 @@ public class CenterdataTAttachServiceImpl extends BaseServiceImpl implements ICe
     public int insertListCenterdataTAttach(List<CenterdataTAttach> list) {
         return centerdataTAttachMapper.insertListCenterdataTAttach(list);
     }
+
+    @Override
+    public List<Map<String, Object>> selectFailUpattah() {
+        return centerdataTAttachMapper.selectFailUpattah();
+    }
 }

+ 71 - 0
src/main/java/com/sooka/sponest/data/utils/EventConstants.java

@@ -0,0 +1,71 @@
+package com.sooka.sponest.data.utils;
+
+/**
+ * 事件中心常量池
+ */
+public class EventConstants {
+
+    private EventConstants() {
+        throw new IllegalStateException("Utility class");
+    }
+
+    // 事件状态
+    public static final String FOREST_EVENT_STATUS_1 = "forest_event_status_1";// 上报
+    public static final String FOREST_EVENT_STATUS_2 = "forest_event_status_2";// 签收
+    public static final String FOREST_EVENT_STATUS_5 = "forest_event_status_5";// 办结
+    public static final String FOREST_EVENT_STATUS_7 = "forest_event_status_7";// 确认
+
+    // 事件来源
+    public static final String REPORTING_SOURCE_1 = "reporting_source_1";// AI
+    public static final String REPORTING_SOURCE_4 = "reporting_source_4";// 传感器
+
+    // 是否催办
+    public static final String SYS_ISURGE_2 = "sys_isurge_2";// 否
+
+    // 部门类型
+    public static final String DEPT_HANDLE_TYPE_2 = "dept_handleType_2";// 配合单位
+    public static final String DEPT_HANDLE_TYPE_3 = "dept_handleType_3";// 行管局
+
+    // 业务操作类型
+    public static final String BUS_OPER_TYPE_1 = "bus_oper_type_1";
+    public static final String BUS_OPER_TYPE_2 = "bus_oper_type_2";// 反馈
+
+    // 日志操作类型
+    public static final String LOG_OPER_TYPE_1 = "log_oper_type_1";
+    public static final String LOG_OPER_TYPE_3 = "log_oper_type_3";// AI反馈
+
+    // 附件索引
+    public static final String BUS_INDEX_1 = "bus_indx_forest";// 火情事件
+    public static final String BUS_INDEX_2 = "bus_indx_other";// 其他事件
+    public static final String BUS_INDEX_3 = "bus_indx_sensor";// 传感器事件
+
+    // 文件类型
+    public static final String FILE_TYPE_1 = "image";
+
+    // 文件前缀
+    public static final String FILE_PREFIX_LOCAL = "fileUrl";
+    public static final String FILE_PREFIX_APP = "appUrl";
+    public static final String FILE_PREFIX_HIK1 = "HIK_1";
+    public static final String FILE_PREFIX_HIK2 = "HIK_2";
+    public static final String FILE_PREFIX_DH1 = "DH_1";
+    public static final String FILE_PREFIX_DH2 = "DH_2";
+    public static final String FILE_DOWNLOAD = "downloadUrl";
+
+    // 部门id
+    public static final String SI_PING = "365";// 365_四平市
+    public static final String SI_PING_NAME = SI_PING + "_四平市";
+    public static final String SHUANG_LIAO = "369";// 369_双辽市
+    public static final String SHUANG_LIAO_NAME = SHUANG_LIAO + "_双辽市";
+    public static final String YI_TONG = "370";// 370_伊通县
+    public static final String YI_TONG_NAME = YI_TONG + "_伊通县";
+    public static final String LI_SHU = "371";// 371_梨树县
+    public static final String LI_SHU_NAME = LI_SHU + "_梨树县";
+    public static final String TIE_DONG = "372";// 372_铁东区
+    public static final String TIE_DONG_NAME = TIE_DONG + "_铁东区";
+    public static final String TIE_XI = "373";// 373_铁西区
+    public static final String TIE_XI_NAME = TIE_XI + "_铁西区";
+
+    // redis
+    public static final String SOCKET_PUSH_TOPIC = "socketpushTopic";// 可视化socket推送监听主题
+    public static final String EVENT_URGE_PREFIX = "event:urge:";// 事件催办的key前缀
+}

+ 47 - 0
src/main/java/com/sooka/sponest/data/utils/RemoteApiUtil.java

@@ -0,0 +1,47 @@
+package com.sooka.sponest.data.utils;
+
+import com.ruoyi.common.core.utils.SpringUtils;
+
+import com.sooka.sponest.monitor.api.RemoteMonitorService;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Component;
+
+
+/**
+ * @Auther: mjq
+ * @Date: 2023/6/29 - 06 - 29 - 8:54
+ * @Description: 发送短信和推送工具类
+ * @version: 1.0
+ */
+@Component
+public class RemoteApiUtil {
+
+    private final Logger logger = LoggerFactory.getLogger(this.getClass());
+
+    private static RemoteApiUtil instance;
+
+    public static RemoteApiUtil getInstance() {
+        if (instance == null) {
+            instance = SpringUtils.getBean(RemoteApiUtil.class);
+        }
+        return instance;
+    }
+    @Value("${sooka.dahuafile:}")
+    private Boolean dahuaFile;
+
+    /**
+     * 获取大华token
+     *
+     * @return
+     */
+    public String getToken() {
+        if (dahuaFile) {
+            return SpringUtils.getBean(RemoteMonitorService.class).getToken();
+        }
+        return "";
+    }
+
+}

+ 183 - 0
src/main/java/com/sooka/sponest/data/utils/ScheduleAtachUtil.java

@@ -0,0 +1,183 @@
+package com.sooka.sponest.data.utils;
+
+import com.alibaba.fastjson.JSON;
+import com.ruoyi.common.core.utils.DateUtils;
+import com.ruoyi.common.core.utils.SpringUtils;
+import com.ruoyi.common.core.utils.file.FilePrefixUtils;
+import com.ruoyi.common.redis.service.RedisService;
+import com.ruoyi.system.api.RemoteConfigService;
+import com.ruoyi.system.api.RemoteFileService;
+import com.ruoyi.system.api.domain.SysFile;
+import com.sooka.sponest.data.system.attach.domain.CenterdataTAttach;
+import com.sooka.sponest.data.system.attach.mapper.CenterdataTAttachMapper;
+import com.sooka.sponest.data.system.attach.service.ICenterdataTAttachService;
+import org.apache.commons.collections4.MapUtils;
+import org.apache.commons.fileupload.FileItem;
+import org.apache.commons.fileupload.disk.DiskFileItemFactory;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.scheduling.annotation.EnableScheduling;
+import org.springframework.scheduling.annotation.Scheduled;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.multipart.MultipartFile;
+import org.springframework.web.multipart.commons.CommonsMultipartFile;
+
+import javax.annotation.Resource;
+import java.io.*;
+import java.net.HttpURLConnection;
+import java.net.URL;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
+
+@Configuration
+@EnableScheduling
+//@RestController
+//@RequestMapping("/testattach")
+public class ScheduleAtachUtil {
+    private final Logger logger = LoggerFactory.getLogger(this.getClass());
+
+    @Autowired
+    private ICenterdataTAttachService centerdataTAttachService;
+    @Resource
+    private RedisService redisService;
+
+
+
+    @GetMapping("/attach")
+    public void runAtach() throws Exception {
+        logger.info("执行定时任务大华图片本地上传开始=>{}", DateUtils.dateTimeNow());
+        Map<String, String> paramMap = SpringUtils.getBean(RemoteConfigService.class).remotegetConfigMap(Arrays.asList(EventConstants.FILE_PREFIX_LOCAL, EventConstants.FILE_PREFIX_HIK1, EventConstants.FILE_PREFIX_HIK2, EventConstants.FILE_PREFIX_DH1, EventConstants.FILE_PREFIX_DH2)).getData();
+        //paramMap.put("token",  redisService.getCacheObject("DH_TOKEN"));// 大华token
+        List<Map<String, Object>> list = centerdataTAttachService.selectFailUpattah();
+        int i = 0;
+        for (Map map : list) {
+            paramMap.put("czlx", "AI");
+            paramMap.put("busId", MapUtils.getString(map, "busId"));
+            logger.info("event_name-------------->"+MapUtils.getString(map, "event_name"));
+            logger.info("create_time-------------->"+MapUtils.getString(map, "create_time"));
+            logger.info("==================>大华图片远程接口之前" + MapUtils.getString(map, "busId") + "当前序号" + i);
+            centerdataTAttachService.updateEventAttach(paramMap);
+            logger.info("延时1分钟");
+            i++;
+
+        }
+        logger.info("执行定时任务大华图片本地上传结束=====>{}", DateUtils.dateTimeNow());
+    }
+    //每周的周日晚上8点开始运行
+    @Scheduled(cron = "* * 20 ? * 1")
+    @GetMapping("/urlToMultipartFile")
+    public void urlToMultipartFile() throws Exception{
+        logger.info("执行定时任务大华图片本地上传开始=>{}", DateUtils.dateTimeNow());
+        Map<String, String> paramMap = SpringUtils.getBean(RemoteConfigService.class).remotegetConfigMap(Arrays.asList(EventConstants.FILE_PREFIX_LOCAL, EventConstants.FILE_PREFIX_HIK1, EventConstants.FILE_PREFIX_HIK2, EventConstants.FILE_PREFIX_DH1, EventConstants.FILE_PREFIX_DH2)).getData();
+        List<Map<String, Object>> attaches = centerdataTAttachService.selectFailUpattah();
+        logger.info("此次需要更新的图片的总数==================>" + attaches.size());
+        int i = 0;
+        for (Map<String, Object> item : attaches) {
+            logger.info("event_name-------------->"+MapUtils.getString(item, "event_name"));
+            logger.info("create_time-------------->"+MapUtils.getString(item, "create_time"));
+            logger.info("busId==================>" + MapUtils.getString(item, "busId") + "当前序号" + i);
+            paramMap.put("token", redisService.getCacheObject("DH_TOKEN"));// 大华token
+            File file = null;
+            MultipartFile multipartFile = null;
+            String url = "";
+            if ("DH_1".equals(item.get("BusSource"))) {
+                url = FilePrefixUtils.getUrlPrefix(item.get("BusSource").toString(), paramMap) + item.get("url") + "?token=" + MapUtils.getString(paramMap, "token");
+            } else {
+                url = FilePrefixUtils.getUrlPrefix(item.get("BusSource").toString(), paramMap) + item.get("url");
+            }
+            HttpURLConnection httpUrl = null;
+            try {
+                httpUrl = (HttpURLConnection) new URL(url).openConnection();
+                // 设置连接超时时间为5秒
+                httpUrl.setConnectTimeout(5000);
+                // 设置读取超时时间为10秒
+                httpUrl.setReadTimeout(10000);
+                httpUrl.connect();
+                logger.info("url===>" + url);
+                logger.info("Code===>" + httpUrl.getResponseCode());
+                logger.info("meaasge===>" + httpUrl.getResponseMessage());
+                httpUrl.connect();
+                file = inputStreamToFile(httpUrl.getInputStream(), System.currentTimeMillis() + ".jpg");
+                multipartFile = fileToMultipartFile(file);
+                httpUrl.disconnect();
+            } catch (Exception e) {
+                logger.info(e.getMessage());
+            } finally {
+                httpUrl.disconnect();
+            }
+            long l = System.currentTimeMillis();
+            logger.info("-------------<>" + l);
+            logger.info("multipartFile-------------" + multipartFile);
+            logger.info("getOriginalFilename---------------------------->" + multipartFile.getOriginalFilename());
+            if (null != multipartFile) {
+                SysFile sysFile = SpringUtils.getBean(RemoteFileService.class).upload(multipartFile).getData();
+                try {
+                    Thread.sleep(30000);
+                } catch (InterruptedException e) {
+                    e.printStackTrace();
+                }
+                logger.info("sysFile===========>" + sysFile);
+                logger.info("getWebUrl===========>" + sysFile.getWebUrl());
+                logger.info("====>" + (System.currentTimeMillis() - l));
+                centerdataTAttachService.updateEventAttachSchedule(item, sysFile);
+            }
+        }
+
+
+    }
+    public static File inputStreamToFile(InputStream ins, String name) {
+        File file = new File(System.getProperty("java.io.tmpdir") + File.separator + name);
+        int len = 8192;
+        byte[] buffer = new byte[len];
+        OutputStream os = null;
+        int bytesRead;
+        try {
+            os = new FileOutputStream(file);
+            while ((bytesRead = ins.read(buffer, 0, len)) != -1) {
+                os.write(buffer, 0, bytesRead);
+            }
+        } catch (IOException e) {
+            e.printStackTrace();
+        } finally {
+            try {
+                os.close();
+                ins.close();
+            } catch (IOException e) {
+                e.printStackTrace();
+            }
+        }
+        return file;
+    }
+    public static MultipartFile fileToMultipartFile(File file) {
+        DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory(16, null);
+        FileItem item = diskFileItemFactory.createItem(file.getName(), "text/plain", true, file.getName());
+        int len = 8192;
+        byte[] buffer = new byte[len];
+        FileInputStream fis = null;
+        OutputStream os = null;
+        int bytesRead;
+        try {
+            fis = new FileInputStream(file);
+            os = item.getOutputStream();
+            while ((bytesRead = fis.read(buffer, 0, len)) != -1) {
+                os.write(buffer, 0, bytesRead);
+            }
+        } catch (IOException e) {
+            e.printStackTrace();
+        } finally {
+            try {
+                os.close();
+                fis.close();
+            } catch (IOException e) {
+                e.printStackTrace();
+            }
+        }
+        return new CommonsMultipartFile(item);
+    }
+
+}

+ 28 - 0
src/main/resources/mapper/system/attach/CenterdataTAttachMapper.xml

@@ -148,4 +148,32 @@
             )
         </foreach>
     </insert>
+    <select id="selectFailUpattah"  resultType="map">
+        SELECT
+        ch.bus_id busId,
+        ch.attach_path url,
+        ch.bus_source BusSource,
+        a.create_time,
+        a.event_name
+        FROM
+        onest_event.centerevent_t_eventcatalogue a
+        LEFT JOIN (
+        SELECT
+        SUBSTRING_INDEX( GROUP_CONCAT( id ORDER BY create_time ), ',', 1 ) id,
+        event_code
+        FROM
+        onest_event.centerevent_t_fire_log
+        WHERE
+        DATEDIFF( NOW(), create_time ) &lt;= 30
+        GROUP BY
+        event_code
+        ) d ON a.event_code = d.event_code
+        LEFT JOIN onest_data.centerdata_t_attach ch ON ch.bus_id = d.id
+        WHERE
+        ch.attach_path NOT LIKE 'group%'
+        AND a.report_source = 'reporting_source_1'
+        AND DATEDIFF( NOW(), a.create_time ) &lt;= 30
+        AND a.event_status_value IN ('forest_event_status_2','forest_event_status_5','forest_event_status_6','forest_event_status_7')
+        order by a.create_time
+    </select>
 </mapper>