浏览代码

事件附件补偿

Memory_LG 2 月之前
父节点
当前提交
a09394e7df

+ 5 - 1
src/main/java/com/sooka/sponest/event/eventAttach/mapper/EventAttachMapper.java

@@ -17,5 +17,9 @@ public interface EventAttachMapper {
 
     List<EventAttach> selectEventAttachListProcess(EventAttach eventAttach);
 
-    void removeAttachByIds(List<Integer> ids);
+    void removeAttachByIds(List<String> ids);
+
+    void insertCompensationByEventId(@Param("eventId") String eventId, @Param("createTime") String createTime);
+
+    void removeCompensationEvent(String eventId);
 }

+ 5 - 1
src/main/java/com/sooka/sponest/event/eventAttach/service/EventAttachService.java

@@ -1,6 +1,7 @@
 package com.sooka.sponest.event.eventAttach.service;
 
 import com.sooka.sponest.event.eventAttach.domain.EventAttach;
+import org.apache.ibatis.annotations.Param;
 
 import java.util.List;
 
@@ -32,6 +33,9 @@ public interface EventAttachService {
     List<EventAttach> selectEventAttachListProcess(EventAttach eventAttach);
 
 
-    void removeAttachByIds(List<Integer> ids);
+    void removeAttachByIds(List<String> ids);
 
+    void insertCompensationByEventId(String eventId, String createTime);
+
+    void removeCompensationEvent(String eventId);
 }

+ 11 - 1
src/main/java/com/sooka/sponest/event/eventAttach/service/impl/EventAttachServiceImpl.java

@@ -52,7 +52,17 @@ public class EventAttachServiceImpl implements EventAttachService {
     }
 
     @Override
-    public void removeAttachByIds(List<Integer> ids) {
+    public void removeAttachByIds(List<String> ids) {
         eventAttachMapper.removeAttachByIds(ids);
     }
+
+    @Override
+    public void insertCompensationByEventId(String eventId, String createTime) {
+        eventAttachMapper.insertCompensationByEventId(eventId, createTime);
+    }
+
+    @Override
+    public void removeCompensationEvent(String eventId) {
+        eventAttachMapper.removeCompensationEvent(eventId);
+    }
 }

+ 1 - 0
src/main/java/com/sooka/sponest/event/eventProcess/mapper/EventProcessMapper.java

@@ -35,4 +35,5 @@ public interface EventProcessMapper {
 
     void deleteEventProcess(EventInfo eventInfo);
 
+    List<EventInfoBO> selectProcessAttachFromUnconfirmed();
 }

+ 2 - 0
src/main/java/com/sooka/sponest/event/eventProcess/service/EventProcessService.java

@@ -65,4 +65,6 @@ public interface EventProcessService {
     void insertSensorEvent(SensorEventVo sensorEventVo);
 
     void insertHousingEvent(SensorEventVo sensorEventVo);
+
+    List<EventInfoBO> selectProcessAttachFromUnconfirmed();
 }

+ 21 - 30
src/main/java/com/sooka/sponest/event/eventProcess/service/impl/EventProcessServiceImpl.java

@@ -30,11 +30,12 @@ import com.sooka.sponest.event.eventProcess.mapper.EventProcessMapper;
 import com.sooka.sponest.event.eventProcess.service.EventProcessService;
 import com.sooka.sponest.event.eventType.domain.EventType;
 import com.sooka.sponest.event.eventType.service.EventTypeService;
-import com.sooka.sponest.event.utils.AddAttachPrefix;
+import com.sooka.sponest.event.utils.AttachUtils;
 import com.sooka.sponest.event.utils.eventEnum.EventEnum;
 import com.sooka.sponest.monitor.api.RemoteMonitorService;
 import com.sooka.sponest.monitor.api.domain.CenterdataTAidevicedept;
 import com.sooka.sponest.monitor.api.domain.CenterdataTCamera;
+import org.apache.commons.collections4.MapUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.stereotype.Service;
@@ -635,6 +636,11 @@ public class EventProcessServiceImpl extends BaseServiceImpl implements EventPro
         return AjaxResult.success();
     }
 
+    @Override
+    public List<EventInfoBO> selectProcessAttachFromUnconfirmed() {
+        return eventProcessMapper.selectProcessAttachFromUnconfirmed();
+    }
+
     /**
      * 列表设置附件路径
      *
@@ -642,7 +648,7 @@ public class EventProcessServiceImpl extends BaseServiceImpl implements EventPro
      */
     private void listSetPath(List<EventInfoBO> eventInfoBOList) {
         eventInfoBOList.forEach(eventInfo -> {
-            eventInfo.setAttachList(AddAttachPrefix.setPath(eventInfo.getAttachList(), eventInfo.getEventId(), eventAttachService));
+            eventInfo.setAttachList(AttachUtils.setPath(eventInfo.getAttachList(), eventInfo.getEventId(), eventAttachService));
         });
     }
 
@@ -654,44 +660,29 @@ public class EventProcessServiceImpl extends BaseServiceImpl implements EventPro
      */
     private void detailSetPath(List<EventLog> eventLogList) {
         eventLogList.forEach(log -> {
-            log.setAttachList(AddAttachPrefix.setPath(log.getAttachList(), log.getId(), eventAttachService));
+            log.setAttachList(AttachUtils.setPath(log.getAttachList(), log.getId(), eventAttachService));
         });
     }
 
 
     private void moveAttach(List<EventAttach> attachList, String eventId) {
 
-        List<Integer> ids = new ArrayList<>();
-        List<String> newPathList = new ArrayList<>();
+//        List<Integer> ids = new ArrayList<>();
+//        List<String> newPathList = new ArrayList<>();
         //附件拼接前缀
-        attachList = AddAttachPrefix.setPath(attachList, eventId, eventAttachService);
-        boolean flag = true;
-
-        for (EventAttach attach : attachList) {
-            if (FILE_URL.equals(attach.getSourceType())) {
-                newPathList.add(attach.getPath());
-                ids.add(attach.getId());
-            } else {
-                MultipartFile multipartFile = FilePrefixUtils.urlToMultipartFile(attach.getPath(), System.currentTimeMillis() + ".jpg");
-                if (null != multipartFile) {
-                    R<SysFile> upload = fileService.upload(multipartFile);
-                    if (HttpStatus.SUCCESS == upload.getCode()) {
-                        newPathList.add(upload.getData().getUrl());
-                        ids.add(attach.getId());
-                    } else {
-                        logger.error("获取图片失败,{}{}", attach.getId(), attach.getPath());
-                        flag = false;
-                        break;
-                    }
-                }
-            }
+        attachList = AttachUtils.setPath(attachList, eventId, eventAttachService);
+
+        Map<String, Object> fileResult = AttachUtils.uploadUrlToFileServer(attachList, fileService);
+
 
-        }
 
         //存储附件信息
-        if (flag) {
-            eventAttachService.batchInsertEventAttachProcess(attachList.get(0).getLogId(), eventId, newPathList, EVENT_LOG_FILE_SOURCE_99, FILE_URL);
-            eventAttachService.removeAttachByIds(ids);
+        if (MapUtils.getBoolean(fileResult, "flag")) {
+            eventAttachService.batchInsertEventAttachProcess(attachList.get(0).getLogId(), eventId, Arrays.asList(MapUtils.getString(fileResult,"paths").split(",")), EVENT_LOG_FILE_SOURCE_99, FILE_URL);
+            eventAttachService.removeAttachByIds(Arrays.asList(MapUtils.getString(fileResult,"ids").split(",")));
+        }else{
+            //将事件id写入到补偿表; 等待补偿机制处理附件
+            eventAttachService.insertCompensationByEventId(eventId, DateUtils.getTime());
         }
     }
 }

+ 75 - 0
src/main/java/com/sooka/sponest/event/task/TaskHandler.java

@@ -0,0 +1,75 @@
+package com.sooka.sponest.event.task;
+
+import com.ruoyi.common.core.constant.HttpStatus;
+import com.ruoyi.common.core.domain.R;
+import com.ruoyi.common.core.utils.DateUtils;
+import com.ruoyi.common.core.utils.file.FilePrefixUtils;
+import com.ruoyi.system.api.RemoteFileService;
+import com.ruoyi.system.api.domain.SysFile;
+import com.sooka.sponest.event.eventAttach.domain.EventAttach;
+import com.sooka.sponest.event.eventAttach.service.EventAttachService;
+import com.sooka.sponest.event.eventProcess.domain.BO.EventInfoBO;
+import com.sooka.sponest.event.eventProcess.service.EventProcessService;
+import com.sooka.sponest.event.utils.AttachUtils;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.collections4.MapUtils;
+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 javax.annotation.Resource;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
+
+import static com.sooka.sponest.event.utils.eventConstants.EventConstants.EVENT_LOG_FILE_SOURCE_99;
+import static com.sooka.sponest.event.utils.eventConstants.EventConstants.FILE_URL;
+
+@RestController
+@RequestMapping("/eventAttachCompensation")
+@Slf4j
+public class TaskHandler {
+
+    @Resource
+    private EventProcessService eventProcessService;
+    @Resource
+    private RemoteFileService fileService;
+    @Resource
+    private EventAttachService eventAttachService;
+
+
+    /**
+     * 附件补偿机制
+     */
+    @GetMapping("/attachUnconfirmedToProcess")
+    public void attachUnconfirmedToProcess(){
+
+        List<EventInfoBO> eventCompensationList = eventProcessService.selectProcessAttachFromUnconfirmed();
+        if(!eventCompensationList.isEmpty()){
+            for (EventInfoBO eventInfoBO : eventCompensationList) {
+                List<EventAttach> attachList = eventInfoBO.getAttachList();
+                if(!attachList.isEmpty()){
+                    Map<String, Object> fileResult = AttachUtils.uploadUrlToFileServer(attachList, fileService);
+
+                    //当同一个事件的全部附件都上传到本地服务器才会返回true
+                    if (MapUtils.getBoolean(fileResult, "flag")) {
+
+                        //将附件写入到确认表
+                        eventAttachService.batchInsertEventAttachProcess(attachList.get(0).getLogId(), eventInfoBO.getEventId(), Arrays.asList(MapUtils.getString(fileResult,"paths").split(",")), EVENT_LOG_FILE_SOURCE_99, FILE_URL);
+
+                        //移除待确认附件表
+                        eventAttachService.removeAttachByIds(Arrays.asList(MapUtils.getString(fileResult,"ids").split(",")));
+
+                        //移除补偿事件信息
+                        eventAttachService.removeCompensationEvent(eventInfoBO.getEventId());
+
+                    }
+                }
+
+            }
+        }
+
+    }
+
+}

+ 0 - 39
src/main/java/com/sooka/sponest/event/utils/AddAttachPrefix.java

@@ -1,39 +0,0 @@
-package com.sooka.sponest.event.utils;
-
-import com.ruoyi.common.core.utils.SpringUtils;
-import com.ruoyi.common.core.utils.file.FilePrefixUtils;
-import com.ruoyi.system.api.RemoteConfigService;
-import com.sooka.sponest.event.eventAttach.domain.EventAttach;
-import com.sooka.sponest.event.eventAttach.mapper.EventAttachMapper;
-import com.sooka.sponest.event.eventAttach.service.EventAttachService;
-import com.sooka.sponest.event.eventProcess.mapper.EventProcessMapper;
-import org.springframework.stereotype.Component;
-
-import javax.annotation.Resource;
-import java.util.Arrays;
-import java.util.List;
-import java.util.Map;
-
-import static com.sooka.sponest.event.utils.eventConstants.EventConstants.*;
-import static com.sooka.sponest.event.utils.eventConstants.EventConstants.DH_1;
-
-public class AddAttachPrefix {
-
-
-    public static List<EventAttach> setPath(List<EventAttach> attachList, String eventId, EventAttachService eventAttachService) {
-        if (attachList.isEmpty()) {
-            attachList = eventAttachService.selectEventAttachListUnconfirmed(eventId, null);
-        }
-        Map<String, String> keyMap = SpringUtils.getBean(RemoteConfigService.class).remotegetConfigMap(Arrays.asList(DH_1, DH_2, HIK_1, HIK_2, FILE_URL, APP_URL, DOWNLOAD_URL, INSECT_PESTS_URL)).getData();
-        String token = RemoteApiUtil.getInstance().getToken();
-        attachList.forEach(attach -> {
-            if (DH_1.equals(attach.getSourceType())) {
-                attach.setPath(FilePrefixUtils.getUrlPrefix(attach.getSourceType(), keyMap) + attach.getPath() + "?token=" + token);
-            } else {
-                attach.setPath(FilePrefixUtils.getUrlPrefix(attach.getSourceType(), keyMap) + attach.getPath());
-            }
-        });
-
-        return attachList;
-    }
-}

+ 87 - 0
src/main/java/com/sooka/sponest/event/utils/AttachUtils.java

@@ -0,0 +1,87 @@
+package com.sooka.sponest.event.utils;
+
+import com.ruoyi.common.core.constant.HttpStatus;
+import com.ruoyi.common.core.domain.R;
+import com.ruoyi.common.core.utils.SpringUtils;
+import com.ruoyi.common.core.utils.file.FilePrefixUtils;
+import com.ruoyi.system.api.RemoteConfigService;
+import com.ruoyi.system.api.RemoteFileService;
+import com.ruoyi.system.api.domain.SysFile;
+import com.sooka.sponest.event.eventAttach.domain.EventAttach;
+import com.sooka.sponest.event.eventAttach.service.EventAttachService;
+import org.springframework.web.multipart.MultipartFile;
+
+import java.util.*;
+
+import static com.sooka.sponest.event.utils.eventConstants.EventConstants.*;
+
+public class AttachUtils {
+
+
+    public static List<EventAttach> setPath(List<EventAttach> attachList, String eventId, EventAttachService eventAttachService) {
+        if (attachList.isEmpty()) {
+            attachList = eventAttachService.selectEventAttachListUnconfirmed(eventId, null);
+        }
+        return setPrefis(attachList);
+    }
+
+    public static List<EventAttach> setPrefis(List<EventAttach> attachList) {
+        Map<String, String> keyMap = SpringUtils.getBean(RemoteConfigService.class).remotegetConfigMap(Arrays.asList(DH_1, DH_2, HIK_1, HIK_2, FILE_URL, APP_URL, DOWNLOAD_URL, INSECT_PESTS_URL)).getData();
+        String token = RemoteApiUtil.getInstance().getToken();
+        attachList.forEach(attach -> {
+            if (DH_1.equals(attach.getSourceType())) {
+                attach.setPath(FilePrefixUtils.getUrlPrefix(attach.getSourceType(), keyMap) + attach.getPath() + "?token=" + token);
+            } else {
+                attach.setPath(FilePrefixUtils.getUrlPrefix(attach.getSourceType(), keyMap) + attach.getPath());
+            }
+        });
+        return attachList;
+    }
+
+
+    public static Map<String, Object> uploadUrlToFileServer(List<EventAttach> attachList, RemoteFileService fileService) {
+        //设置附件上传标识, 一个事件的所有图片都上传完毕才会返回true, 否则为false
+        boolean flag = true;
+        //
+        StringBuilder ids = new StringBuilder();
+        StringBuilder paths = new StringBuilder();
+
+        for (EventAttach attach : attachList) {
+            if (FILE_URL.equals(attach.getSourceType())) {
+                paths.append(attach.getPath()).append(",");
+                ids.append(attach.getId()).append(",");
+            } else {
+                MultipartFile multipartFile = FilePrefixUtils.urlToMultipartFile(attach.getPath(), System.currentTimeMillis() + ".jpg");
+                if (null != multipartFile) {
+                    R<SysFile> upload = fileService.upload(multipartFile);
+                    if (HttpStatus.SUCCESS == upload.getCode()) {
+                        paths.append(attach.getPath()).append(",");
+                        ids.append(attach.getId()).append(",");
+                    } else {
+                        flag = false;
+                        break;
+                    }
+                }
+            }
+        }
+        // 检查 ids 是否不为空
+        if (ids.length() > 0) {
+            // 移除最后一个字符
+            ids.deleteCharAt(ids.length() - 1);
+        }
+        // 检查 ids 是否不为空
+        if (paths.length() > 0) {
+            // 移除最后一个字符
+            paths.deleteCharAt(paths.length() - 1);
+        }
+
+
+        HashMap<String, Object> result = new HashMap<>();
+        result.put("flag", flag);
+        if (flag) {
+            result.put("ids", ids);
+            result.put("paths", paths);
+        }
+        return result;
+    }
+}

+ 13 - 0
src/main/resources/mapper/event/eventAttach/EventAttachMapper.xml

@@ -15,6 +15,8 @@
     </resultMap>
 
 
+
+
     <insert id="insertEventAttachUnconfirmed" parameterType="EventAttach">
         insert into event_attach_unconfirmed
         <trim prefix="(" suffix=")" suffixOverrides=",">
@@ -68,4 +70,15 @@
         </foreach>
     </delete>
 
+
+
+    <insert id="insertCompensationByEventId" parameterType="string">
+        insert into event_compensation (event_id, create_time) value
+        (#{eventId}, #{createTime})
+    </insert>
+
+    <delete id="removeCompensationEvent" parameterType="string">
+        delete from event_compensation where event_id = #{eventId}
+    </delete>
+
 </mapper>

+ 6 - 2
src/main/resources/mapper/event/eventProcess/EventProcessMapper.xml

@@ -6,7 +6,7 @@
 
     <!--  *************************************** resultMap块 开始***********************************************  -->
 
-    <!--  查询待确认事件 / 无异常事件列表  -->
+    <!--  查询待确认事件  -->
     <resultMap type="EventInfoBO" id="EventInfoResult">
         <result property="eventId" column="event_id"/>
         <result property="eventType" column="event_type"/>
@@ -107,7 +107,7 @@
         order by sort
     </select>
 
-    <!--  确认事件列表  -->
+    <!--  确认事件列表  / 无异常事件列表  -->
     <resultMap type="EventInfoBO" id="EventInfoProcessResult">
         <result property="eventId" column="event_id"/>
         <result property="eventType" column="event_type"/>
@@ -517,6 +517,10 @@
         DELETE FROM event_process
         WHERE event_id = #{eventId}
     </delete>
+
+    <select id="selectProcessAttachFromUnconfirmed" resultMap="EventInfoResult">
+        select event_id from event_compensation
+    </select>
     <!-- **************************************** 业务块 结束 *********************************************** -->