wangliang пре 1 година
родитељ
комит
c4045d0716

+ 4 - 3
sop-business/src/main/java/com/qmth/sop/business/activiti/service/ActivitiService.java

@@ -36,7 +36,7 @@ public interface ActivitiService {
      * @return
      * @throws IOException
      */
-    public Map<String, Object> upload(MultipartFile file) throws IOException;
+    public Map<String, Object> upload(MultipartFile file) throws Exception;
 
     /**
      * 部署流程
@@ -45,7 +45,7 @@ public interface ActivitiService {
      * @return
      * @throws IOException
      */
-    public Map<String, Object> createDeployment(List<TFCustomTypeEnum> tfCustomTypeEnumList) throws IOException;
+    public Map<String, Object> createDeployment(List<TFCustomTypeEnum> tfCustomTypeEnumList) throws Exception;
 
     /**
      * 审批任务
@@ -61,9 +61,10 @@ public interface ActivitiService {
      * @param flowDeploymentId
      * @param flowId
      * @param taskId
+     * @param dynamicTable
      * @return
      */
-    public Map<String, Object> formPropertiesGet(String flowDeploymentId, Long flowId, Long taskId);
+    public Map<String, Object> formPropertiesGet(String flowDeploymentId, Long flowId, Long taskId, Boolean dynamicTable) throws Exception;
 
     /**
      * 获取用户待办

+ 14 - 7
sop-business/src/main/java/com/qmth/sop/business/activiti/service/impl/ActivitiServiceImpl.java

@@ -10,6 +10,7 @@ import com.qmth.sop.business.bean.params.FlowApproveParam;
 import com.qmth.sop.business.bean.result.*;
 import com.qmth.sop.business.cache.CommonCacheService;
 import com.qmth.sop.business.entity.*;
+import com.qmth.sop.business.exec.MySQLExec;
 import com.qmth.sop.business.mapper.SysUserMapper;
 import com.qmth.sop.business.service.*;
 import com.qmth.sop.common.contant.SpringContextHolder;
@@ -105,6 +106,9 @@ public class ActivitiServiceImpl implements ActivitiService {
     @Resource
     TDFormWidgetMetadataService tdFormWidgetMetadataService;
 
+    @Resource
+    MySQLExec mySQLExec;
+
     /**
      * 根据deploymentId查找processDefinitionId
      *
@@ -124,13 +128,13 @@ public class ActivitiServiceImpl implements ActivitiService {
      * @throws IOException
      */
     @Override
-    public Map<String, Object> upload(MultipartFile file) throws IOException {
+    public Map<String, Object> upload(MultipartFile file) throws Exception {
         basicAttachmentService.validateAttachment(file);
         Map<String, Object> map = new HashMap<>();
         DeploymentBuilder builder = repositoryService.createDeployment();
         ZipInputStream zip = new ZipInputStream(file.getInputStream());
         builder.addZipInputStream(zip);
-        Map<String, Object> mapData = formPropertiesGet(builder.deploy().getId(), null, null);
+        Map<String, Object> mapData = formPropertiesGet(builder.deploy().getId(), null, null, true);
         map.put(file.getName(), mapData);
         return map;
     }
@@ -143,7 +147,7 @@ public class ActivitiServiceImpl implements ActivitiService {
      * @throws IOException
      */
     @Override
-    public Map<String, Object> createDeployment(List<TFCustomTypeEnum> tfCustomTypeEnumList) throws IOException {
+    public Map<String, Object> createDeployment(List<TFCustomTypeEnum> tfCustomTypeEnumList) throws Exception {
         Map<String, Object> map = new HashMap<>();
         for (TFCustomTypeEnum tfCustomTypeEnum : tfCustomTypeEnumList) {
             List<TFCustomFlow> tfCustomFlowList = tfCustomFlowService.list(new QueryWrapper<TFCustomFlow>().lambda().eq(TFCustomFlow::getType, tfCustomTypeEnum));
@@ -152,10 +156,10 @@ public class ActivitiServiceImpl implements ActivitiService {
                 ClassPathResource resource = new ClassPathResource(tfCustomTypeEnum.getFileName());
                 ZipInputStream zip = new ZipInputStream(resource.getInputStream());
                 builder.addZipInputStream(zip);
-                Map<String, Object> mapData = formPropertiesGet(builder.deploy().getId(), null, null);
+                Map<String, Object> mapData = formPropertiesGet(builder.deploy().getId(), null, null, true);
                 map.put(tfCustomTypeEnum.name(), mapData);
             } else {
-                Map<String, Object> mapData = formPropertiesGet(tfCustomFlowList.get(0).getFlowDeploymentId(), null, null);
+                Map<String, Object> mapData = formPropertiesGet(tfCustomFlowList.get(0).getFlowDeploymentId(), null, null, true);
                 map.put(tfCustomTypeEnum.name(), mapData);
             }
         }
@@ -396,13 +400,15 @@ public class ActivitiServiceImpl implements ActivitiService {
      * @param flowDeploymentId
      * @param flowId
      * @param taskId
+     * @param dynamicTable
      * @return
      */
     @Override
     @Transactional
     public Map<String, Object> formPropertiesGet(String flowDeploymentId,
                                                  Long flowId,
-                                                 Long taskId) {
+                                                 Long taskId,
+                                                 Boolean dynamicTable) throws Exception {
         Map<String, Object> map = new HashMap<>();
         FlowResult flowResult = null;
         TFCustomFlow tfCustomFlow = null;
@@ -418,7 +424,7 @@ public class ActivitiServiceImpl implements ActivitiService {
             flowResult = JSONObject.parseObject(tfCustomFlow.getFlowProcessVar(), FlowResult.class);
             if (tfCustomFlow.getType() == TFCustomTypeEnum.OFFICE_SOP_FLOW || tfCustomFlow.getType() == TFCustomTypeEnum.CLOUD_MARK_SOP_FLOW || tfCustomFlow.getType() == TFCustomTypeEnum.QUALITY_PROBLEM_FLOW) {
                 map = this.getFlowFormPropertie(flowResult, map, 1);
-                if (tfCustomFlow.getType() == TFCustomTypeEnum.OFFICE_SOP_FLOW || tfCustomFlow.getType() == TFCustomTypeEnum.CLOUD_MARK_SOP_FLOW) {
+                if ((Objects.nonNull(dynamicTable) && dynamicTable) && (tfCustomFlow.getType() == TFCustomTypeEnum.OFFICE_SOP_FLOW || tfCustomFlow.getType() == TFCustomTypeEnum.CLOUD_MARK_SOP_FLOW)) {
                     Map<String, FlowTaskResult> setupMap = flowResult.getSetupMap();
                     Set<TDFormWidgetMetadata> tdFormWidgetMetadataSet = new LinkedHashSet<>();
                     for (Map.Entry<String, FlowTaskResult> entry : setupMap.entrySet()) {
@@ -439,6 +445,7 @@ public class ActivitiServiceImpl implements ActivitiService {
                         tdFormWidgetMetadataService.deleteAllData(tfCustomFlow.getType());
                         tdFormWidgetMetadataService.saveBatch(tdFormWidgetMetadataSet);
                     }
+                    mySQLExec.execCreateSopDynamicTable(SystemConstant.SOP_DYNAMIC_TABLE_FILE_NAME, tfCustomFlow.getType());
                 }
             }
         } else if (Objects.nonNull(flowId)) {

+ 70 - 55
sop-business/src/main/java/com/qmth/sop/business/exec/MySQLExec.java

@@ -2,7 +2,10 @@ package com.qmth.sop.business.exec;
 
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.qmth.sop.business.entity.TDFormWidgetMetadata;
+import com.qmth.sop.business.entity.TFCustomFlow;
+import com.qmth.sop.business.service.TBSopInfoService;
 import com.qmth.sop.business.service.TDFormWidgetMetadataService;
+import com.qmth.sop.business.service.TFCustomFlowService;
 import com.qmth.sop.common.contant.SystemConstant;
 import com.qmth.sop.common.enums.ExceptionResultEnum;
 import com.qmth.sop.common.enums.TFCustomTypeEnum;
@@ -51,6 +54,12 @@ public class MySQLExec {
     @Resource
     TDFormWidgetMetadataService tdFormWidgetMetadataService;
 
+    @Resource
+    TFCustomFlowService tfCustomFlowService;
+
+    @Resource
+    TBSopInfoService tbSopInfoService;
+
     /**
      * 执行动态创建sop字段表
      *
@@ -62,67 +71,73 @@ public class MySQLExec {
         Optional.ofNullable(fileName).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("文件名不能为空"));
         Optional.ofNullable(type).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("流程类型不能为空"));
 
-        InputStream inputStream = MySQLExec.class.getClassLoader().getResourceAsStream(fileName);
-        Optional.ofNullable(inputStream).orElseThrow(() -> ExceptionResultEnum.ERROR.exception(fileName + "未找到"));
-        File file = SystemConstant.getFileTempVar(".sql");
-        FileUtils.copyInputStreamToFile(inputStream, file);
-
-        ByteArrayOutputStream ou = new ByteArrayOutputStream();
-        IOUtils.copy(new FileInputStream(file), ou);
-        String string = new String(ou.toByteArray(), StandardCharsets.UTF_8);
-        if (Objects.nonNull(string)) {
-            List<TDFormWidgetMetadata> tdFormWidgetMetadataList = tdFormWidgetMetadataService.list(new QueryWrapper<TDFormWidgetMetadata>().lambda().eq(TDFormWidgetMetadata::getType, type));
-            StringJoiner stringJoiner = new StringJoiner(",\r\n");
-            for (TDFormWidgetMetadata t : tdFormWidgetMetadataList) {
-                if (t.getCode() == WidgetCodeEnum.TABLE) {
-                    stringJoiner.add("`" + t.getFieldId() + "`" + " mediumtext CHARACTER SET utf8mb4 COMMENT '" + t.getFieldTitle() + "'");
-                } else if (t.getCode() == WidgetCodeEnum.TEXTAREA) {
-                    stringJoiner.add("`" + t.getFieldId() + "`" + " varchar(500) NOT NULL COMMENT '" + t.getFieldTitle() + "'");
-                } else if (t.getCode() == WidgetCodeEnum.NUMBER) {
-                    stringJoiner.add("`" + t.getFieldId() + "`" + " int DEFAULT NULL COMMENT '" + t.getFieldTitle() + "'");
-                } else {
-                    stringJoiner.add("`" + t.getFieldId() + "`" + " varchar(100) NOT NULL COMMENT '" + t.getFieldTitle() + "'");
+        TFCustomFlow tfCustomFlow = tfCustomFlowService.getOne(new QueryWrapper<TFCustomFlow>().lambda().eq(TFCustomFlow::getType, type));
+        String tableName = "t_b_sop_dynamic_" + type.getTableName() + "_" + tfCustomFlow.getVersion();
+        int count = tbSopInfoService.selectSopDynamicExist(databaseName, tableName);
+        if (count == 0) {
+            InputStream inputStream = MySQLExec.class.getClassLoader().getResourceAsStream(fileName);
+            Optional.ofNullable(inputStream).orElseThrow(() -> ExceptionResultEnum.ERROR.exception(fileName + "未找到"));
+            File file = SystemConstant.getFileTempVar(".sql");
+            FileUtils.copyInputStreamToFile(inputStream, file);
+
+            ByteArrayOutputStream ou = new ByteArrayOutputStream();
+            IOUtils.copy(new FileInputStream(file), ou);
+            String string = new String(ou.toByteArray(), StandardCharsets.UTF_8);
+
+            if (Objects.nonNull(string)) {
+                List<TDFormWidgetMetadata> tdFormWidgetMetadataList = tdFormWidgetMetadataService.list(new QueryWrapper<TDFormWidgetMetadata>().lambda().eq(TDFormWidgetMetadata::getType, type));
+                StringJoiner stringJoiner = new StringJoiner(",\r\n");
+                for (TDFormWidgetMetadata t : tdFormWidgetMetadataList) {
+                    if (t.getCode() == WidgetCodeEnum.TABLE) {
+                        stringJoiner.add("`" + t.getFieldId() + "`" + " mediumtext CHARACTER SET utf8mb4 COMMENT '" + t.getFieldTitle() + "'");
+                    } else if (t.getCode() == WidgetCodeEnum.TEXTAREA) {
+                        stringJoiner.add("`" + t.getFieldId() + "`" + " varchar(500) NOT NULL COMMENT '" + t.getFieldTitle() + "'");
+                    } else if (t.getCode() == WidgetCodeEnum.NUMBER) {
+                        stringJoiner.add("`" + t.getFieldId() + "`" + " int DEFAULT NULL COMMENT '" + t.getFieldTitle() + "'");
+                    } else {
+                        stringJoiner.add("`" + t.getFieldId() + "`" + " varchar(100) NOT NULL COMMENT '" + t.getFieldTitle() + "'");
+                    }
                 }
-            }
-            string = string.replaceAll("\\#\\{dynamicTable\\}", "`t_b_sop_dynamic_" + type.getTableName() + "`");
-            string = string.replaceAll("\\#\\{dynamicFields\\}", stringJoiner.toString());
-        }
-        IOUtils.write(string.getBytes(StandardCharsets.UTF_8), new FileOutputStream(file));
-
-        String cmdarray[] = {"mysql -h" + this.host + " -P" + this.port + " -u" + this.username + " -p" + this.password + " " + this.databaseName, "source " + file.getPath()};
-        Runtime runtime = Runtime.getRuntime();
-        Process process = null;
-        try {
-            process = runtime.exec(cmdarray[0]);//cmd之后执行数组的第一个条件进入数据库
-            //执行了第一条命令以后已经登录到mysql了
-            Process finalProcess = process;
-            new Thread(() -> {
-                OutputStream os = finalProcess.getOutputStream();
-                OutputStreamWriter writer = new OutputStreamWriter(os);
+                string = string.replaceAll("\\#\\{dynamicTable\\}", "`" + tableName + "`");
+                string = string.replaceAll("\\#\\{dynamicFields\\}", stringJoiner.toString());
+                IOUtils.write(string.getBytes(StandardCharsets.UTF_8), new FileOutputStream(file));
+
+                String cmdarray[] = {"mysql -h" + this.host + " -P" + this.port + " -u" + this.username + " -p" + this.password + " " + this.databaseName, "source " + file.getPath()};
+                Runtime runtime = Runtime.getRuntime();
+                Process process = null;
                 try {
-                    IOUtils.write(cmdarray[1], writer);
-                } catch (IOException e) {
-                    e.printStackTrace();
-                } finally {
-                    try {
-                        if (Objects.nonNull(writer)) {
-                            writer.close();
+                    process = runtime.exec(cmdarray[0]);//cmd之后执行数组的第一个条件进入数据库
+                    //执行了第一条命令以后已经登录到mysql了
+                    Process finalProcess = process;
+                    new Thread(() -> {
+                        OutputStream os = finalProcess.getOutputStream();
+                        OutputStreamWriter writer = new OutputStreamWriter(os);
+                        try {
+                            IOUtils.write(cmdarray[1], writer);
+                        } catch (IOException e) {
+                            e.printStackTrace();
+                        } finally {
+                            try {
+                                if (Objects.nonNull(writer)) {
+                                    writer.close();
+                                }
+                                if (Objects.nonNull(os)) {
+                                    os.close();
+                                }
+                            } catch (Exception e) {
+                                log.error(SystemConstant.LOG_ERROR, e);
+                            }
                         }
-                        if (Objects.nonNull(os)) {
-                            os.close();
+                    }).start();
+                } catch (Exception e) {
+                    log.error(SystemConstant.LOG_ERROR, e);
+                } finally {
+                    if (process.waitFor() == 0 && Objects.nonNull(file)) {
+                        if (Objects.nonNull(file)) {
+                            file.delete();
                         }
-                    } catch (Exception e) {
-                        log.error(SystemConstant.LOG_ERROR, e);
                     }
                 }
-            }).start();
-        } catch (Exception e) {
-            log.error(SystemConstant.LOG_ERROR, e);
-        } finally {
-            if (process.waitFor() == 0 && Objects.nonNull(file)) {
-                if (Objects.nonNull(file)) {
-                    file.delete();
-                }
             }
         }
     }

+ 10 - 1
sop-business/src/main/java/com/qmth/sop/business/mapper/TBSopInfoMapper.java

@@ -1,7 +1,8 @@
 package com.qmth.sop.business.mapper;
 
-import com.qmth.sop.business.entity.TBSopInfo;
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import com.qmth.sop.business.entity.TBSopInfo;
+import org.apache.ibatis.annotations.Param;
 
 /**
  * <p>
@@ -13,4 +14,12 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
  */
 public interface TBSopInfoMapper extends BaseMapper<TBSopInfo> {
 
+    /**
+     * 查询动态sop表名是否存在
+     *
+     * @param db
+     * @param tableName
+     * @return
+     */
+    int selectSopDynamicExist(@Param("db") String db, @Param("tableName") String tableName);
 }

+ 9 - 1
sop-business/src/main/java/com/qmth/sop/business/service/TBSopInfoService.java

@@ -1,7 +1,7 @@
 package com.qmth.sop.business.service;
 
-import com.qmth.sop.business.entity.TBSopInfo;
 import com.baomidou.mybatisplus.extension.service.IService;
+import com.qmth.sop.business.entity.TBSopInfo;
 
 /**
  * <p>
@@ -13,4 +13,12 @@ import com.baomidou.mybatisplus.extension.service.IService;
  */
 public interface TBSopInfoService extends IService<TBSopInfo> {
 
+    /**
+     * 查询动态sop表名是否存在
+     *
+     * @param db
+     * @param tableName
+     * @return
+     */
+    int selectSopDynamicExist(String db, String tableName);
 }

+ 12 - 1
sop-business/src/main/java/com/qmth/sop/business/service/impl/TBSopInfoServiceImpl.java

@@ -1,9 +1,9 @@
 package com.qmth.sop.business.service.impl;
 
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.qmth.sop.business.entity.TBSopInfo;
 import com.qmth.sop.business.mapper.TBSopInfoMapper;
 import com.qmth.sop.business.service.TBSopInfoService;
-import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import org.springframework.stereotype.Service;
 
 /**
@@ -17,4 +17,15 @@ import org.springframework.stereotype.Service;
 @Service
 public class TBSopInfoServiceImpl extends ServiceImpl<TBSopInfoMapper, TBSopInfo> implements TBSopInfoService {
 
+    /**
+     * 查询动态sop表名是否存在
+     *
+     * @param db
+     * @param tableName
+     * @return
+     */
+    @Override
+    public int selectSopDynamicExist(String db, String tableName) {
+        return this.baseMapper.selectSopDynamicExist(db, tableName);
+    }
 }

+ 3 - 0
sop-business/src/main/resources/mapper/TBSopInfoMapper.xml

@@ -2,4 +2,7 @@
 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 <mapper namespace="com.qmth.sop.business.mapper.TBSopInfoMapper">
 
+    <select id="selectSopDynamicExist" resultType="java.lang.Integer">
+        SELECT count(1) FROM information_schema.tables WHERE table_schema = #{db} AND table_name = #{tableName}
+    </select>
 </mapper>

+ 0 - 9
sop-server/src/main/java/com/qmth/sop/server/api/TDFormWidgetController.java

@@ -10,7 +10,6 @@ import com.qmth.sop.business.bean.result.FormWidgetResult;
 import com.qmth.sop.business.cache.CommonCacheService;
 import com.qmth.sop.business.entity.TDFormWidget;
 import com.qmth.sop.business.entity.TDTableProp;
-import com.qmth.sop.business.exec.MySQLExec;
 import com.qmth.sop.business.service.TDFormWidgetService;
 import com.qmth.sop.common.contant.SystemConstant;
 import com.qmth.sop.common.enums.TFCustomTypeEnum;
@@ -44,9 +43,6 @@ public class TDFormWidgetController {
     @Resource
     TDFormWidgetService tdFormWidgetService;
 
-    @Resource
-    MySQLExec mySQLExec;
-
     @ApiOperation(value = "控件查询列表")
     @ApiResponses({@ApiResponse(code = 200, message = "控件查询列表信息", response = FormWidgetResult.class)})
     @RequestMapping(value = "/list", method = RequestMethod.POST)
@@ -59,11 +55,6 @@ public class TDFormWidgetController {
     @RequestMapping(value = "/flow/select", method = RequestMethod.POST)
     public Result flowSelect(@ApiParam(value = "流程类型", required = true) @RequestParam TFCustomTypeEnum flowType,
                              @ApiParam(value = "表单步骤") @RequestParam(required = false) Integer formSetup) {
-        try {
-            mySQLExec.execCreateSopDynamicTable(SystemConstant.SOP_DYNAMIC_TABLE_FILE_NAME, TFCustomTypeEnum.OFFICE_SOP_FLOW);
-        } catch (Exception e) {
-            e.printStackTrace();
-        }
         QueryWrapper<TDFormWidget> tdFormWidgetQueryWrapper = new QueryWrapper<>();
         tdFormWidgetQueryWrapper.lambda().eq(TDFormWidget::getFlowType, flowType).eq(TDFormWidget::getType, WidgetTypeEnum.FORM);
         if (Objects.nonNull(formSetup)) {

+ 4 - 5
sop-server/src/main/java/com/qmth/sop/server/api/TFCustomFlowController.java

@@ -27,7 +27,6 @@ import javax.annotation.Resource;
 import javax.validation.Valid;
 import javax.validation.constraints.Max;
 import javax.validation.constraints.Min;
-import java.io.IOException;
 import java.util.Arrays;
 import java.util.List;
 import java.util.Objects;
@@ -55,7 +54,7 @@ public class TFCustomFlowController {
     @ApiOperation(value = "创建流程接口")
     @RequestMapping(value = "/createDeployment", method = RequestMethod.POST)
     @ApiResponses({@ApiResponse(code = 200, message = "返回信息", response = Object.class)})
-    public Result createDeployment() throws IOException {
+    public Result createDeployment() throws Exception {
         return ResultUtil.ok(activitiService.createDeployment(Arrays.asList(TFCustomTypeEnum.OFFICE_SOP_FLOW, TFCustomTypeEnum.CLOUD_MARK_SOP_FLOW, TFCustomTypeEnum.DING_EXCEPTION_FLOW, TFCustomTypeEnum.PROJECT_EXCHANGE_FLOW, TFCustomTypeEnum.QUALITY_PROBLEM_FLOW)));
 //        return ResultUtil.ok(activitiService.createDeployment(Arrays.asList(TFCustomTypeEnum.QUALITY_PROBLEM_FLOW)));
     }
@@ -63,7 +62,7 @@ public class TFCustomFlowController {
     @ApiOperation(value = "上传流程接口")
     @RequestMapping(value = "/upload", method = RequestMethod.POST)
     @ApiResponses({@ApiResponse(code = 200, message = "返回信息", response = Object.class)})
-    public Result upload(@ApiParam(required = true, value = "上传文件") @RequestParam MultipartFile file) throws IOException {
+    public Result upload(@ApiParam(required = true, value = "上传文件") @RequestParam MultipartFile file) throws Exception {
         return ResultUtil.ok(activitiService.upload(file));
     }
 
@@ -79,8 +78,8 @@ public class TFCustomFlowController {
     @ApiResponses({@ApiResponse(code = 200, message = "返回信息", response = Object.class)})
     public Result formPropertiesGet(@ApiParam(value = "流程部署id") @RequestParam(required = false) String flowDeploymentId,
                                     @ApiParam(value = "流程id") @RequestParam(required = false) Long flowId,
-                                    @ApiParam(value = "流程任务id") @RequestParam(required = false) Long taskId) {
-        return ResultUtil.ok(activitiService.formPropertiesGet(flowDeploymentId, flowId, taskId));
+                                    @ApiParam(value = "流程任务id") @RequestParam(required = false) Long taskId) throws Exception {
+        return ResultUtil.ok(activitiService.formPropertiesGet(flowDeploymentId, flowId, taskId, false));
     }
 
     @ApiOperation(value = "流程审批接口")