gssong
2024-03-25 b5ad057f0697e1bdbb88d26cd5ac72f5f55f7995
ruoyi-modules/ruoyi-workflow/src/main/java/org/dromara/workflow/service/impl/ActModelServiceImpl.java
@@ -15,12 +15,16 @@
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.tenant.helper.TenantHelper;
import org.dromara.workflow.common.constant.FlowConstant;
import org.dromara.workflow.domain.bo.ModelBo;
import org.dromara.workflow.domain.bo.WfFormDefinitionBo;
import org.dromara.workflow.domain.vo.ModelVo;
import org.dromara.workflow.domain.vo.WfFormDefinitionVo;
import org.dromara.workflow.service.IActModelService;
import org.dromara.workflow.service.IWfFormDefinitionService;
import org.dromara.workflow.utils.ModelUtils;
import org.dromara.workflow.utils.QueryUtils;
import org.flowable.bpmn.model.BpmnModel;
@@ -51,6 +55,7 @@
public class ActModelServiceImpl implements IActModelService {
    private final RepositoryService repositoryService;
    private final IWfFormDefinitionService iWfFormDefinitionService;
    /**
     * 分页查询模型
@@ -59,7 +64,7 @@
     * @return 返回分页列表
     */
    @Override
    public TableDataInfo<Model> page(ModelBo modelBo) {
    public TableDataInfo<Model> page(ModelBo modelBo, PageQuery pageQuery) {
        ModelQuery query = QueryUtils.modelQuery();
        if (StringUtils.isNotEmpty(modelBo.getName())) {
            query.modelNameLike("%" + modelBo.getName() + "%");
@@ -74,7 +79,7 @@
        // 创建时间降序排列
        query.orderByCreateTime().desc();
        // 分页查询
        List<Model> modelList = query.listPage(modelBo.getPageNum(), modelBo.getPageSize());
        List<Model> modelList = query.listPage(pageQuery.getFirstNum(), pageQuery.getPageSize());
        // 总记录数
        long total = query.count();
        return new TableDataInfo<>(modelList, total);
@@ -248,6 +253,7 @@
            }
            // 查询模型的基本信息
            Model model = repositoryService.getModel(id);
            ProcessDefinition processDefinition = QueryUtils.definitionQuery().processDefinitionKey(model.getKey()).latestVersion().singleResult();
            // xml资源的名称 ,对应act_ge_bytearray表中的name_字段
            String processName = model.getName() + ".bpmn20.xml";
            // 调用部署相关的api方法进行部署流程定义
@@ -270,6 +276,17 @@
            // 更新分类
            ProcessDefinition definition = QueryUtils.definitionQuery().deploymentId(deployment.getId()).singleResult();
            repositoryService.setProcessDefinitionCategory(definition.getId(), model.getCategory());
            if (processDefinition != null) {
                WfFormDefinitionVo definitionVo = iWfFormDefinitionService.getByDefId(processDefinition.getId());
                if (definitionVo != null) {
                    WfFormDefinitionBo wfFormDefinition = new WfFormDefinitionBo();
                    wfFormDefinition.setDefinitionId(definition.getId());
                    wfFormDefinition.setProcessKey(definition.getKey());
                    wfFormDefinition.setPath(definitionVo.getPath());
                    wfFormDefinition.setRemark(definitionVo.getRemark());
                    iWfFormDefinitionService.saveOrUpdate(wfFormDefinition);
                }
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
@@ -280,35 +297,37 @@
    /**
     * 导出模型zip压缩包
     *
     * @param modelId  模型id
     * @param modelIds 模型id
     * @param response 相应
     */
    @Override
    public void exportZip(String modelId, HttpServletResponse response) {
    public void exportZip(List<String> modelIds, HttpServletResponse response) {
        ZipOutputStream zos = null;
        try {
            zos = ZipUtil.getZipOutputStream(response.getOutputStream(), StandardCharsets.UTF_8);
            // 压缩包文件名
            String zipName = "模型不存在";
            // 查询模型基本信息
            Model model = repositoryService.getModel(modelId);
            byte[] xmlBytes = repositoryService.getModelEditorSource(modelId);
            if (ObjectUtil.isNotNull(model)) {
                if (JSONUtil.isTypeJSON(IOUtils.toString(xmlBytes, StandardCharsets.UTF_8.toString())) && ArrayUtil.isEmpty(ModelUtils.bpmnJsonToXmlBytes(xmlBytes))) {
                    zipName = "模型不能为空,请至少设计一条主线流程!";
                    zos.putNextEntry(new ZipEntry(zipName + ".txt"));
                    zos.write(zipName.getBytes(StandardCharsets.UTF_8));
                } else if (ArrayUtil.isEmpty(xmlBytes)) {
                    zipName = "模型数据为空,请先设计流程定义模型,再进行部署!";
                    zos.putNextEntry(new ZipEntry(zipName + ".txt"));
                    zos.write(zipName.getBytes(StandardCharsets.UTF_8));
                } else {
                    String fileName = model.getName() + "-" + model.getKey();
                    // 压缩包文件名
                    zipName = fileName + ".zip";
                    // 将xml添加到压缩包中(指定xml文件名:请假流程.bpmn20.xml
                    zos.putNextEntry(new ZipEntry(fileName + ".bpmn20.xml"));
                    zos.write(xmlBytes);
            for (String modelId : modelIds) {
                Model model = repositoryService.getModel(modelId);
                byte[] xmlBytes = repositoryService.getModelEditorSource(modelId);
                if (ObjectUtil.isNotNull(model)) {
                    if (JSONUtil.isTypeJSON(IOUtils.toString(xmlBytes, StandardCharsets.UTF_8.toString())) && ArrayUtil.isEmpty(ModelUtils.bpmnJsonToXmlBytes(xmlBytes))) {
                        zipName = "模型不能为空,请至少设计一条主线流程!";
                        zos.putNextEntry(new ZipEntry(zipName + ".txt"));
                        zos.write(zipName.getBytes(StandardCharsets.UTF_8));
                    } else if (ArrayUtil.isEmpty(xmlBytes)) {
                        zipName = "模型数据为空,请先设计流程定义模型,再进行部署!";
                        zos.putNextEntry(new ZipEntry(zipName + ".txt"));
                        zos.write(zipName.getBytes(StandardCharsets.UTF_8));
                    } else {
                        String fileName = model.getName() + "-" + model.getKey();
                        // 压缩包文件名
                        zipName = fileName + ".zip";
                        // 将xml添加到压缩包中(指定xml文件名:请假流程.bpmn20.xml
                        zos.putNextEntry(new ZipEntry(fileName + ".bpmn20.xml"));
                        zos.write(xmlBytes);
                    }
                }
            }
            response.setHeader("Content-Disposition",