dengsixing před 6 dny
rodič
revize
7d2b6311a0

+ 28 - 1
src/main/java/com/fdkankan/deploy/Application.java

@@ -1,16 +1,43 @@
 package com.fdkankan.deploy;
 
+import cn.hutool.core.collection.CollUtil;
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.dtflys.forest.springboot.annotation.ForestScan;
+import com.fdkankan.deploy.entity.SceneReceiveLog;
+import com.fdkankan.deploy.service.SceneReceiveLogService;
 import org.mybatis.spring.annotation.MapperScan;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.CommandLineRunner;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.scheduling.annotation.EnableScheduling;
+
+import java.util.List;
 
 @SpringBootApplication
 @MapperScan("com.fdkankan.deploy.mapper")
 @ForestScan(basePackages = "com.fdkankan.deploy.httpclient")
-public class Application {
+@EnableScheduling
+public class Application implements CommandLineRunner {
+
     public static void main(String[] args) {
         SpringApplication.run(Application.class, args);
     }
 
+    @Autowired
+    private SceneReceiveLogService sceneReceiveLogService;
+
+    @Override
+    public void run(String... args) throws Exception {
+
+        //项目启动,把部署中的重新变为待部署,开始走部署逻辑
+        List<SceneReceiveLog> list = sceneReceiveLogService.list(new LambdaQueryWrapper<SceneReceiveLog>().eq(SceneReceiveLog::getStatus, 3));
+        if(CollUtil.isNotEmpty(list)){
+            list.stream().forEach(v->{
+                v.setStatus(2);
+            });
+            sceneReceiveLogService.saveOrUpdateBatch(list);
+        }
+
+    }
 }

+ 3 - 0
src/main/java/com/fdkankan/deploy/entity/SceneReceiveLog.java

@@ -33,9 +33,12 @@ public class SceneReceiveLog implements Serializable {
 
     private String type;
 
+    //2-传输完毕,等待部署中, 3-部署中
     private Integer status;
 
     private String taskId;
 
+    private String ext;
+
 
 }

+ 25 - 0
src/main/java/com/fdkankan/deploy/schedule/ScheduleJob.java

@@ -0,0 +1,25 @@
+package com.fdkankan.deploy.schedule;
+
+import com.fdkankan.deploy.service.SceneService;
+import lombok.extern.log4j.Log4j2;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.scheduling.annotation.Scheduled;
+import org.springframework.stereotype.Component;
+
+@Log4j2
+@Component
+public class ScheduleJob {
+
+    @Autowired
+    private SceneService sceneService;
+
+
+    @Scheduled(fixedDelay = 30*1000)
+    public void deploy() {
+        log.info("部署场景定时任务开始");
+        sceneService.deploy();
+        log.info("部署场景定时任务结束");
+    }
+
+
+}

+ 4 - 5
src/main/java/com/fdkankan/deploy/service/SceneService.java

@@ -18,11 +18,6 @@ import java.util.Map;
  * @since 2025-11-05
  */
 public interface SceneService extends IService<Scene> {
-    ResultData receive(Map<String, Object> params);
-
-    void donloadAndDeploy();
-
-    ResultData receive2(HttpServletRequest request) throws Exception;
 
     ResultData receive3(HttpServletRequest request) throws Exception;
 
@@ -33,4 +28,8 @@ public interface SceneService extends IService<Scene> {
     ResultData getDeployStatus(String taskId);
 
     void delete(Long id);
+
+    void deploy();
+
 }
+

+ 64 - 250
src/main/java/com/fdkankan/deploy/service/impl/SceneServiceImpl.java

@@ -3,18 +3,15 @@ package com.fdkankan.deploy.service.impl;
 import cn.hutool.core.collection.CollUtil;
 import cn.hutool.core.date.DateUtil;
 import cn.hutool.core.io.FileUtil;
-import cn.hutool.core.util.ZipUtil;
-import cn.hutool.http.HttpUtil;
 import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
-import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.fdkankan.common.constant.CommonSuccessStatus;
 import com.fdkankan.common.constant.ErrorCode;
 import com.fdkankan.common.exception.BusinessException;
 import com.fdkankan.common.util.DateExtUtil;
-import com.fdkankan.deploy.bean.SceneDeployBean;
 import com.fdkankan.deploy.entity.Scene;
 import com.fdkankan.deploy.entity.SceneReceiveLog;
 import com.fdkankan.deploy.mapper.SceneMapper;
@@ -22,7 +19,6 @@ import com.fdkankan.deploy.request.ScenePageRequest;
 import com.fdkankan.deploy.response.PageInfo;
 import com.fdkankan.deploy.service.SceneReceiveLogService;
 import com.fdkankan.deploy.service.SceneService;
-import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.fdkankan.web.response.ResultData;
 import lombok.extern.slf4j.Slf4j;
 import net.lingala.zip4j.core.ZipFile;
@@ -34,11 +30,12 @@ import javax.servlet.http.HttpServletRequest;
 import java.io.File;
 import java.io.InputStream;
 import java.nio.charset.StandardCharsets;
-import java.util.*;
-import java.util.function.Function;
+import java.util.ArrayList;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.List;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
-import java.util.stream.Collectors;
 
 /**
  * <p>
@@ -51,109 +48,16 @@ import java.util.stream.Collectors;
 @Slf4j
 @Service
 public class SceneServiceImpl extends ServiceImpl<SceneMapper, Scene> implements SceneService {
-    @Value("${scene.download.waiting-path}")
-    private String waitingFilePath;
     @Value("${scene.download.zip-dir}")
     private String zipDir;
     @Value("${scene.deploy.kankan}")
     private String delpoyKankan;
     @Value("${scene.deploy.laser}")
     private String delpoyLaser;
-    @Value("${scene.download.completed-file}")
-    private String completedFile;
-    @Value("${scene.download.fail-file}")
-    private String failFile;
-    @Value("${scene.list-file-path}")
-    private String listFilePath;
     private static final String  pattern = "^\\[\\d{2}:\\d{2}:\\d{2}\\]";
     @Autowired
     private SceneReceiveLogService sceneReceiveLogService;
-    @Value("${scene.log.path}")
-    private String sceneLogPath;
-
-
-
-    @Override
-    public ResultData receive(Map<String, Object> params) {
-
-        String num = (String)params.get("num");
-
-        String waitingFileName =  Calendar.getInstance().getTimeInMillis() + "-" + num + ".txt";
-
-        FileUtil.mkParentDirs(waitingFilePath + waitingFileName);
-        FileUtil.writeUtf8String(JSON.toJSONString(params), FileUtil.file(waitingFilePath + waitingFileName));
-
-        return ResultData.ok();
-    }
-
-    @Override
-    public void donloadAndDeploy() {
-        FileUtil.mkdir(waitingFilePath);
-        List<String> waitingFileList = FileUtil.listFileNames(waitingFilePath);
-        if(CollUtil.isEmpty(waitingFileList)){
-            return;
-        }
-        String fileName = waitingFileList.stream().sorted(Comparator.comparing(String::toString)).collect(Collectors.toList()).get(0);
-        String taskStr = FileUtil.readUtf8String(waitingFilePath + fileName);
-        try {
-            JSONObject taskObj = JSON.parseObject(taskStr);
-            String num = taskObj.getString("num");
-            String downloadUrl = taskObj.getString("downloadUrl");
-            String zipType = taskObj.getString("zipType");
-            String title = taskObj.getString("title");
-            int version = taskObj.getIntValue("version");
-
-            String zipPath = zipDir + FileUtil.getPrefix(fileName) + "/";
-            String zipFilePath = zipPath + num + ".zip";
-            FileUtil.mkParentDirs(zipFilePath);
-            HttpUtil.downloadFile(downloadUrl, zipFilePath);
-            ZipUtil.unzip(zipFilePath);
-
-            if("kankan".equals(zipType)){
-                FileUtil.copy(zipPath + num+ "/wwwroot/scene_view_data/" + num, delpoyKankan, true);
-            }else{
-                FileUtil.copy(zipPath + num + "/www/" + num, delpoyLaser, true);
-            }
-
-            //写入部署清单文件
-            SceneDeployBean deployBean = SceneDeployBean.builder()
-                    .num(num).title(title).sceneType(zipType).calcTime(DateExtUtil.format(new Date(), DateExtUtil.dateStyle8))
-                    .syncTime(DateExtUtil.format(new Date(), DateExtUtil.dateStyle8)).build();
-            this.writeListJson(deployBean);
-
-            //部署完毕后写入已下载任务,删除待下载任务
-            FileUtil.mkParentDirs(completedFile);
-            FileUtil.appendUtf8String(JSON.toJSONString(taskObj) + "\n", completedFile);
-
-        }catch (Exception e){
-            log.error("部署失败,task:{}", taskStr, e);
-            FileUtil.mkParentDirs(failFile);
-            FileUtil.appendUtf8String(taskStr + "\n", failFile);
-        } finally {
-            FileUtil.del(waitingFilePath + fileName);
-        }
-    }
-
-    private synchronized void writeListJson(SceneDeployBean deployBean){
-
-        Map<String, SceneDeployBean> sceneMap = null;
-        if(FileUtil.exist(listFilePath)){
-            String listStr = FileUtil.readUtf8String(listFilePath);
-            List<SceneDeployBean> sceneDeployBeans = JSON.parseArray(listStr, SceneDeployBean.class);
-            if(CollUtil.isNotEmpty(sceneDeployBeans)){
-                sceneMap = sceneDeployBeans.stream().collect(Collectors.toMap(SceneDeployBean::getKey, Function.identity()));
-            }
-        }
-        if(CollUtil.isEmpty(sceneMap)){
-            sceneMap = new HashMap<>();
-        }
-
-        sceneMap.put(deployBean.getNum() + "-" + deployBean.getSceneType(), deployBean);
-
-        Collection<SceneDeployBean> values = sceneMap.values();
 
-        FileUtil.writeUtf8String(JSON.toJSONString(values), listFilePath);
-    }
 
     @Override
     public ResultData receive3(HttpServletRequest request) throws Exception {
@@ -183,26 +87,16 @@ public class SceneServiceImpl extends ServiceImpl<SceneMapper, Scene> implements
                 FileUtil.mkParentDirs(zipFilePath);
                 FileUtil.writeFromStream(ins,zipFilePath);
             }else{
-                ZipFile zipFile = new ZipFile(new File(zipFilePath));
-                zipFile.extractAll(zipPath);
-                String scenePath = "kankan".equals(zipType) ? delpoyKankan : delpoyLaser;
-                FileUtil.copy(zipPath + num, scenePath, true);
-                Scene scene = lambdaQuery().eq(Scene::getNum, num).eq(Scene::getType, zipType).one();
-                if(scene == null){
-                    scene = new Scene();
-                }
-                scene.setNum(num);
-                scene.setTitle(title);
-                scene.setType(zipType);
-                scene.setStatus(CommonSuccessStatus.SUCCESS.code());
-                scene.setUploadTime(DateExtUtil.format(new Date(), DateExtUtil.dateStyle));
-                scene.setShootCount(Integer.valueOf(shootCount));
-                scene.setSpace(FileUtil.size(new File(scenePath + num)));
-                this.saveOrUpdate(scene);
-
-                sceneReceiveLog.setStatus(CommonSuccessStatus.SUCCESS.code());
-                sceneReceiveLog.setCompletedTime(DateExtUtil.format(new Date(), DateExtUtil.dateStyle));
-
+                JSONObject extObj = new JSONObject();
+                extObj.put("zipPath", zipPath);
+                extObj.put("title", title);
+                extObj.put("shootCount", shootCount);
+                extObj.put("num", num);
+                extObj.put("fileName", fileName);
+                extObj.put("zipType", zipType);
+
+                sceneReceiveLog.setStatus(2);//传输完毕,等待部署中
+                sceneReceiveLog.setExt(extObj.toJSONString());
             }
         }catch (Exception e){
             log.error("场景部署失败,num:{}", num, e);
@@ -213,137 +107,8 @@ public class SceneServiceImpl extends ServiceImpl<SceneMapper, Scene> implements
         return ResultData.ok();
     }
 
-    @Override
-    public ResultData receive2(HttpServletRequest request) throws Exception {
-        String id = request.getParameter("id");
-        String action = request.getParameter("action");
-        String fileName = request.getParameter("fileName");
-        String num = request.getParameter("num");
-        String zipPath = zipDir + id + "/";
-        String zipFilePath = zipPath.concat(fileName);
-
-        if("upload".equals(action)){
-            InputStream ins = request.getPart("file").getInputStream();
-            FileUtil.mkParentDirs(zipFilePath);
-            FileUtil.writeFromStream(ins,zipFilePath);
-        }else{
-            ZipFile zipFile = new ZipFile(new File(zipFilePath));
-            zipFile.extractAll(zipPath);
-            String zipType = request.getParameter("zipType");
-            String title = request.getParameter("title");
-            int computeTime = Integer.valueOf(request.getParameter("computeTime"));
-            if("kankan".equals(zipType)){
-                FileUtil.copy(zipPath + num, delpoyKankan, true);
-            }else{
-                FileUtil.copy(zipPath + num, delpoyLaser, true);
-            }
-
-            //重写日志文件
-            this.reWriteLog(num, zipPath + num + "/console.log", computeTime);
-
-            //写入部署清单文件
-            SceneDeployBean deployBean = SceneDeployBean.builder()
-                    .num(num).title(title).sceneType(zipType).calcTime(DateExtUtil.format(new Date(), DateExtUtil.dateStyle8))
-                    .syncTime(DateExtUtil.format(new Date(), DateExtUtil.dateStyle8)).build();
-            this.writeListJson(deployBean);
-        }
-
-        return ResultData.ok();
-    }
-
-    private void reWriteLog(String num, String logPath, int time){
-        List<String> strings = FileUtil.readLines(logPath, StandardCharsets.UTF_8);
-        Date endTime = Calendar.getInstance().getTime();
-        Date startTime = DateUtil.offsetSecond(endTime, -time);
-        int flag = strings.size()/time;
-        int skipIndex = time/(strings.size()%time);
-        if(time%(strings.size()%time) != 0){
-            skipIndex++;
-        }
-        String targetLog = String.format(sceneLogPath, num);
-        List<String> list = new ArrayList<>();
-        int index = 1;
-
-        int count = 0;
-        for (String str : strings) {
-            if(this.checkStartWithTime(str)){
-                list.add("[" + DateExtUtil.format(startTime, DateExtUtil.dateStyle2) + "]" + str.substring(10));
-            }else{
-                list.add(str);
-            }
-            if(count == 0){
-                if(index%flag==0){
-                    if(index%skipIndex != 0){
-                        startTime = DateUtil.offsetSecond(startTime, 1);
-                    }else{
-                        count = (++index)%skipIndex;
-                    }
-                }
-            }else{
-                if(count-- == 0){
-                    startTime = DateUtil.offsetSecond(startTime, 1);
-                }
-            }
-
-            index++;
-        }
-
-        FileUtil.writeUtf8Lines(list, targetLog);
-
 
 
-    }
-
-    public static void main(String[] args) throws Exception{
-//        net.lingala.zip4j.core.ZipFile zipFile = new ZipFile(new File("D:\\test\\KJ-t-WypZvHxCR4X.zip"));
-//        zipFile.extractAll("D:\\test\\");
-//        FileUtils.unZip("D:\\test\\aa\\KJ-t-WypZvHxCR4X.zip", "D:\\test\\aa\\");
-//        List<String> strings = FileUtil.readLines("D:\\test\\console.log", StandardCharsets.UTF_8);
-//        int time = 20 * 60;
-//        Date endTime = Calendar.getInstance().getTime();
-//        Date startTime = DateUtil.offsetSecond(endTime, -time);
-//        System.out.println("开始时间:" + DateExtUtil.format(startTime, DateExtUtil.dateStyle2));
-//        System.out.println("结束时间:" + DateExtUtil.format(endTime, DateExtUtil.dateStyle2));
-//        int flag = strings.size()/time;
-//        int test = time/(strings.size()%time);
-//        if(time%(strings.size()%time) != 0){
-//            test++;
-//        }
-//        String targetLog = "D:\\test\\aaa.log";
-//        List<String> list = new ArrayList<>();
-//        int index = 1;
-//
-//        for (String str : strings) {
-//            if(checkStartWithTime(str)){
-//                list.add("[" + DateExtUtil.format(startTime, DateExtUtil.dateStyle2) + "]" + str.substring(10));
-//            }else{
-//                list.add(str);
-//            }
-//            if(index%flag==0){
-//                if(index%test != 0){
-//                    startTime = DateUtil.offsetSecond(startTime, 1);
-//                }
-//            }
-//
-//            index++;
-//
-//        }
-//
-//        FileUtil.writeUtf8Lines(list, targetLog);
-
-
-    }
-
-    public boolean checkStartWithTime(String content){
-        Pattern r = Pattern.compile(pattern);
-        Matcher m = r.matcher(content);
-        if (m.find()) {
-            return true;
-        } else {
-            return false;
-        }
-    }
-
     @Override
     public PageInfo<Scene> listScene(ScenePageRequest request) {
         Page<Scene> page = lambdaQuery().like(Scene::getTitle, request.getTitle()).orderByDesc(Scene::getUploadTime).page(Page.of(request.getPageNum(), request.getPageSize()));
@@ -372,4 +137,53 @@ public class SceneServiceImpl extends ServiceImpl<SceneMapper, Scene> implements
         }
         this.removeById(scene.getId());
     }
+
+    @Override
+    public void deploy() {
+        List<SceneReceiveLog> list = sceneReceiveLogService.list(new LambdaQueryWrapper<SceneReceiveLog>().eq(SceneReceiveLog::getStatus, 2));
+        if(CollUtil.isEmpty(list)){
+            return;
+        }
+        for (SceneReceiveLog sceneReceiveLog : list) {
+            try {
+                //先将接收记录改为部署中
+                sceneReceiveLog.setStatus(3);
+                sceneReceiveLogService.saveOrUpdate(sceneReceiveLog);
+
+                JSONObject jsonObject = JSON.parseObject(sceneReceiveLog.getExt());
+                String num = jsonObject.getString("num");
+                String title = jsonObject.getString("title");
+                String zipPath = jsonObject.getString("zipPath");
+                String fileName = jsonObject.getString("fileName");
+                String zipType = jsonObject.getString("zipType");
+                String zipFilePath = zipPath.concat(fileName);
+                ZipFile zipFile = new ZipFile(new File(zipFilePath));
+                zipFile.extractAll(zipPath);
+                String scenePath = "kankan".equals(zipType) ? delpoyKankan : delpoyLaser;
+                FileUtil.copy(zipPath + num, scenePath, true);
+                Scene scene = lambdaQuery().eq(Scene::getNum, num).eq(Scene::getType, zipType).one();
+                if(scene == null){
+                    scene = new Scene();
+                }
+                scene.setStatus(CommonSuccessStatus.SUCCESS.code());
+                scene.setNum(num);
+                scene.setTitle(title);
+                scene.setType(zipType);
+                scene.setUploadTime(DateExtUtil.format(new Date(), DateExtUtil.dateStyle));
+                scene.setShootCount(jsonObject.getInteger("shootCount"));
+                scene.setSpace(FileUtil.size(new File(scenePath + num)));
+                this.saveOrUpdate(scene);
+
+                sceneReceiveLog.setStatus(CommonSuccessStatus.SUCCESS.code());
+                sceneReceiveLog.setCompletedTime(DateExtUtil.format(new Date(), DateExtUtil.dateStyle));
+            }catch (Exception e){
+                log.error("部署失败, num:{}", sceneReceiveLog.getNum(), e);
+                sceneReceiveLog.setStatus(CommonSuccessStatus.FAIL.code());
+            }finally {
+                sceneReceiveLogService.saveOrUpdate(sceneReceiveLog);
+            }
+        }
+
+
+    }
 }

+ 4 - 0
src/main/java/com/fdkankan/deploy/service/impl/UserServiceImpl.java

@@ -70,4 +70,8 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements Us
         return dto;
     }
 
+    public static void main(String[] args) {
+        System.out.println(BCrypt.hashpw("Wwjc123456"));
+    }
+
 }

+ 5 - 2
src/main/resources/application.yml

@@ -65,8 +65,11 @@ knife4j:
   enable: true
 
 # 不配默认为false
-  tlog:
-    enable-invoke-time-print: true
+tlog:
+  enable-invoke-time-print: true
+
+logging:
+  path: /home/backend/4dkankan_v4
 
 
 

+ 2 - 1
src/main/resources/db/schema.sql

@@ -37,7 +37,8 @@ CREATE TABLE IF NOT EXISTS "t_scene_receive_log"  (
   "completed_time" text,
   "type" text NOT NULL,
   "status" integer NOT NULL,
-  "task_id" text NOT NULL
+  "task_id" text NOT NULL,
+  "ext" text
 );
 
 CREATE INDEX IF NOT EXISTS"IDX_TASKID"

+ 1 - 1
src/main/resources/logback-spring.xml

@@ -9,7 +9,7 @@
 
 	<contextName>logback</contextName>
 	<!-- name的值是变量的名称,value的值时变量定义的值。通过定义的值会被插入到logger上下文中。定义变量后,可以使“${}”来使用变量。 -->
-	<property name="log.path" value="${LOG_PATH}/deploy/logs" />
+	<property name="log.path" value="/home/backend/4dkankan_v4/deploy/logs" />
 
 	<!-- 彩色日志 -->
 	<!-- 彩色日志依赖的渲染类 -->