乐趣区

关于java:Activity工作流交互demo简单实现

前端代码

<template>
  <div>
    <el-table :data="tableData" style="width: 100%" max-height="250">
      <el-table-column fixed prop="id" label="ID" width="150">
      </el-table-column>
      <el-table-column prop="processInstanceId" label="流 id" width="120">
      </el-table-column>
      <el-table-column prop="processInstanceStatus" label="流状态" width="120">
      </el-table-column>
      <el-table-column prop="businessKey" label="业务 key" width="120">
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <span v-if="scope.row.processInstanceStatus==''">
            <el-button type="primary" size="mini" @click="commit(scope.row)"> 提交 </el-button>
          </span>
          <span v-if="scope.row.processInstanceStatus==' 审批中 '||scope.row.processInstanceStatus==' 审核实现 '">
            <el-button type="primary" size="mini" @click="look(scope.row)"> 查看 </el-button>
          </span>
        </template>
      </el-table-column>
    </el-table>
    <!-- 新增弹窗 -->
    <el-dialog title="编辑信息" :visible.sync="dialogVisible" width="30%">
      <el-form ref="form" :model="form" label-width="100px">
        <el-form-item label="ID">
          <el-input v-model="form.id" :disabled="true" style="width: 200px;"></el-input>
        </el-form-item>
        <el-form-item label="流 id">
          <el-input v-model="form.processInstanceId" :disabled="true" style="width: 200px;"></el-input>
        </el-form-item>
        <el-form-item label="流状态">
          <el-input v-model="form.processInstanceStatus" :disabled="true" style="width: 200px;"></el-input>
        </el-form-item>
        <el-form-item label="业务 key">
          <el-input v-model="form.businessKey" :disabled="true" style="width: 200px;"></el-input>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
  import axios from '@/axios';
  export default {data() {
      return {tableData: [],
        dialogVisible: false,
        form: {
          id: '',
          processInstanceId: '',
          processInstanceStatus: '',
          businessKey: ''
        }
      }
    },
    mounted() {this.queryObject()
    },
    methods: {queryObject() {
        var url = 'wzhB/queryObject.do'
        var param = {}
        axios.get(url, param).then(response => {if (response.data.code == '0') {
            this.tableData = response.data.data
            console.log(response.data.data)
          } else {
            this.$message({
              type: 'error',
              message: '操作失败!' + response.data.msg
            });
          }
        });
      },
      look(row) {
        this.dialogVisible = true
        this.form = row
      },
      commit(row) {
        var url = 'wzhB/start.do'
        var param = {id: row.id}
        axios.get(url, param).then(response => {if (response.data.code == '0') {this.queryObject();
          } else {
            this.$message({
              type: 'error',
              message: '操作失败!' + response.data.msg
            });
          }
        });
      }
    }
  }
</script>

<style>
</style>

后端

controller

package com.slxx.controller.develop;
import com.slxx.model.system.Type;
import com.slxx.service.develop.WangZhaoHuiDemoBService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
@RestController
@RequestMapping(value = "/wzhB")
public class wangZhaoHuiDemoBController {
    @Autowired
 WangZhaoHuiDemoBService wangZhaoHuiDemoBService;
 @RequestMapping(value = "/queryObject.do")
    public String queryObject(){
        String result = "";
 try {result = wangZhaoHuiDemoBService.queryObject();
 } catch (Exception ex) {result = Type.FAIL + ex.getMessage();
 ex.printStackTrace();} finally {return result;}
    }
    @RequestMapping(value = "/start.do")
    public String start(String id, HttpServletRequest request) {
        String result = "";
 String userId = (String) request.getAttribute("userId");
 try {result = wangZhaoHuiDemoBService.start(id,userId);
 } catch (Exception ex) {result = Type.FAIL + ex.getMessage();
 ex.printStackTrace();} finally {return result;}
    }
}

serviceImpl

package com.slxx.service.impl.develop;
import com.alibaba.druid.proxy.jdbc.JdbcParameter;
import com.alibaba.fastjson.JSON;
import com.slxx.mapping.develop.WangZhaoHuiDemoBMapper;
import com.slxx.model.develop.WangZhaoHuiDemo;
import com.slxx.model.develop.WangZhaoHuiDemoB;
import com.slxx.model.process.ProcessInfo;
import com.slxx.model.system.Type;
import com.slxx.service.develop.WangZhaoHuiDemoBService;
import com.slxx.service.process.ProcessService;
import com.slxx.tools.JSONTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service("WangZhaoHuiDemoBService")
public class WangZhaoHuiDemoBServiceImpl implements WangZhaoHuiDemoBService {
    @Autowired
 WangZhaoHuiDemoBMapper wangZhaoHuiDemoBMapper;
 @Autowired
 ProcessService processService;
 String result = Type.FAIL;
 @Override
 public String queryObject() {List<WangZhaoHuiDemoB> list = wangZhaoHuiDemoBMapper.queryObject();
 result = JSONTools.toStringyyyyMMddHHmmss(list);
 return result;
 }
    @Override
 public String start(String id, String userId) throws Exception {WangZhaoHuiDemoB demo = wangZhaoHuiDemoBMapper.selectPrimaryKeyId(id);
 Map<String, Object> map = new HashMap<>();
 String processInstanceId= processService.startWithProcessKeyWithOutFirstTask("test2", userId, map);
 demo.setProcessInstanceId(processInstanceId);
 demo.setProcessInstanceStatus("审批中");
 wangZhaoHuiDemoBMapper.updateByPrimaryKey(demo);
 processService.completeFirstTask(processInstanceId,userId);
 return JSONTools.toString(Type.SUCCESS);
 }
    @Override
 public String selectPrimaryKey(String id) {WangZhaoHuiDemoB demoB = wangZhaoHuiDemoBMapper.selectPrimaryKeyId(id);
 result = JSONTools.toStringyyyyMMddHHmmss(demoB);
 return result;
 }
}

service

package com.slxx.service.develop;
public interface WangZhaoHuiDemoBService {String queryObject();
 String start(String id,String userId) throws Exception;
 String selectPrimaryKey(String id);
}

Mapper

package com.slxx.mapping.develop;
import com.slxx.model.develop.WangZhaoHuiDemoB;
import java.util.List;
public interface WangZhaoHuiDemoBMapper {List<WangZhaoHuiDemoB> queryObject();
 WangZhaoHuiDemoB selectPrimaryKeyId(String id);
 WangZhaoHuiDemoB selectPrimaryKey(String processInstanceId);
 int updateByPrimaryKey(WangZhaoHuiDemoB record);
}

监听器

package com.slxx.service.impl.develop;
import com.slxx.mapping.develop.WangZhaoHuiDemoBMapper;
import com.slxx.model.develop.WangZhaoHuiDemo;
import com.slxx.model.develop.WangZhaoHuiDemoB;
import com.slxx.service.common.CommonWaitToDoService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.ExecutionListener;
import org.activiti.engine.delegate.TaskListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service("wangZhaoHuiDemoBListener")
public class WangZhaoHuiDemoBListener implements ExecutionListener, TaskListener {
    @Autowired
 WangZhaoHuiDemoBMapper wangZhaoHuiDemoBMapper;
 @Autowired
 CommonWaitToDoService commonWaitToDoService;
 @Autowired
 RuntimeService runtimeService;
 @Override
 public void notify(DelegateExecution delegateExecution) throws Exception {
        try {
            String status = "";
 if (delegateExecution.getEventName().equals("start")) {} else if (delegateExecution.getEventName().equals("end")) {WangZhaoHuiDemoB payReq = wangZhaoHuiDemoBMapper.selectPrimaryKey(delegateExecution.getProcessInstanceId());
 if (payReq != null) {String deleteType = (String) (runtimeService.getVariable(delegateExecution.getProcessInstanceId(), "deleteType") == null ? "": runtimeService.getVariable(delegateExecution.getProcessInstanceId(),"deleteType"));
 if (payReq.getProcessInstanceStatus().equals("驳回")) {if (deleteType.equals("manual")) {
                            status = "手动敞开";
 payReq.setProcessInstanceStatus("");
 } else {
                            status = "驳回";
 //payReq.setStatus("");
 payReq.setProcessInstanceStatus("");
 }
                    } else if (deleteType.equals("manual")) {status = "手动敞开";} else {status = "审批通过";}
                    payReq.setProcessInstanceStatus(status);
 wangZhaoHuiDemoBMapper.updateByPrimaryKey(payReq);
 }
            }
        } catch (Exception ex) {ex.printStackTrace();
 }
    }
    @Override
 public void notify(DelegateTask delegateTask) {WangZhaoHuiDemoB payReq = wangZhaoHuiDemoBMapper.selectPrimaryKey(delegateTask.getProcessInstanceId());
 if (payReq != null) {
            //draft     step1
 if (delegateTask.getTaskDefinitionKey().equals("step1")) {if (payReq.getProcessInstanceStatus().equals("驳回")) {if (delegateTask.getEventName().equals("create")) {payReq.setProcessInstanceStatus("驳回");
 } else if (delegateTask.getEventName().equals("complete")) {payReq.setProcessInstanceStatus("审批中");
 }
                } else if (delegateTask.getEventName().equals("create")) {payReq.setProcessInstanceStatus("驳回");
 } else if (delegateTask.getEventName().equals("complete")) {payReq.setProcessInstanceStatus("审批中");
 }
                wangZhaoHuiDemoBMapper.updateByPrimaryKey(payReq);
 }
            if (delegateTask.getEventName().equals("create")) {commonWaitToDoService.beforeDeal(delegateTask);
 } else if (delegateTask.getEventName().equals("complete")) {commonWaitToDoService.afterDeal(delegateTask);
 }
        }
    }
}

mapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.slxx.mapping.develop.WangZhaoHuiDemoBMapper">
 <resultMap id="resultMap" type="com.slxx.model.develop.WangZhaoHuiDemoB">
 <id column="ID" property="id"></id>
 <result column="PROCESS_INSTANCE_ID" property="processInstanceId"></result>
 <result column="PROCESS_INSTANCE_STATUS" property="processInstanceStatus"></result>
 <result column="BUSINESS_KEY" property="businessKey"></result>
 </resultMap>
 <select id="queryObject" resultMap="resultMap">
 select * from W_ZH_DEMO_B
    </select>
 <select id="selectPrimaryKeyId" resultMap="resultMap">
 select * from W_ZH_DEMO_B where ID = #{id.jdbcType=VARCHAR}
     </select>
 <select id="selectPrimaryKey" resultMap="resultMap">
 select * from W_ZH_DEMO_B where PROCESS_INSTANCE_ID=#{processInstanceId,jdbcType=VARCHAR}
    </select>
 <update id="updateByPrimaryKey" parameterType="com.slxx.model.develop.WangZhaoHuiDemoB">
 update W_ZH_DEMO_B
    set PROCESS_INSTANCE_ID = #{processInstanceId,jdbcType=VARCHAR},
    PROCESS_INSTANCE_STATUS = #{processInstanceStatus,jdbcType=VARCHAR},
    BUSINESS_KEY = #{businessKey,jdbcType=VARCHAR}
    where ID = #{id,jdbcType=VARCHAR}
    </update>
</mapper>

总结: 创立各个字段必须有上面的字段, 流程 key 必须是惟一的

ID  PROCESS_INSTANCE_ID   PROCESS_INSTANCE_STATUS   BUSINESS_KEY(业务)
dev 表的字段
05E4B0553B1B4F2A1EDA34CED0CD0430    demo2    W_ZH_DEMO_B    test2    com.th.demo        2020-11-05 00:00:00        2020-11-05 00:00:00            0    0    /externalproj/tender/externalprojTenderShow.vue    select t.BUSINESS_KEY from W_ZH_DEMO_B t where t.PROCESS_INSTANCE_ID=#{param2,jdbcType=VARCHAR}    /pages/externalproj/tender/process/process            demoB
退出移动版