继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

追加搭建Activiti5+Mybatis+Mysql+Spring+springmvc+freemarker项目

爱总结的小仙女
关注TA
已关注
手记 47
粉丝 57
获赞 437

业务代码

//controller层
package com.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.entity.Student;
import com.services.LoginService;

@Controller
public class LoginController {
    int flag=0;
    @Autowired
    private LoginService loginService;

    @Autowired
    private RepositoryService repositoryService; 

    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private TaskService taskService;
    //根据登录的用户的种类执行不同的业务,跳转不同的页面
        用户分为三类:(1)学生(2)班长(3)班主任
    @RequestMapping("login.do")
    public String LoginByNamePwd(String userName,String pwd,ModelMap map){
        Student s =new Student();
        //学生1登录
        if (userName.equals("lmy") && pwd.equals("123")) {
            s.setName("lmy");
                 //根据登录的用户名查询对应的请假申请列表
            List<Map<String,Object>> studentList = loginService.queryUsers(s);
            if (studentList.size()>0) {
                map.put("isHave", true);
            }else{
                map.put("isHave", false);
            }
            map.put("sList", studentList);
            return "user";
            //学生2登录
        }else if (userName.equals("qyl") && pwd.equals("123")) {
            s.setName("qyl");
            List<Map<String,Object>> studentList = loginService.queryUsers(s);
            if (studentList.size()>0) {
                map.put("isHave", true);
            }else{
                map.put("isHave", false);
            }
            map.put("sList", studentList);
            return "user";
            //班长登录
        }else if ((userName.equals("王班长") || userName.equals("大班长") || userName.equals("郝班长") )  && pwd.equals("123")){
             if (userName.equals("王班长")) {
                    flag=1;
             } else if (userName.equals("郝班长")) {
                   flag=2;
             }
                        //获取流程变量
                this.getVaribles(userName,map);
                   return "MyHtml";
         //主任登录
        }else{
              this.getVaribles("刘主任",map);
              return "ZR";
        }

    }

    /**
     * 学生提交申请单
     * */
    @RequestMapping("studentLeaveApply.do")
    @ResponseBody
    public Map<String,Object> studentLeaveApply(Integer id,String name,String day,String reason,String time,String createTime,ModelMap map){
          Map<String,Object> param = new HashMap<String, Object>();
          Task task1 = null;
          //根据id查询出学生的详细信息
          Student student = loginService.queryUserList(id);
          student.setStatus("已提交申请,提交给班长处理");
          //更新申请的状态信息
          loginService.queryUserByUserId(student);
          if (student.getTaskId()==null) {
              //部署流程
               repositoryService.createDeployment()
              .addClasspathResource("com/diagrams/1.bpmn")
              .name("学生请假流程")
              .deploy();
              //启动一个流程
              ProcessInstance pi=runtimeService.startProcessInstanceByKey("newStudentLeaveProcess");
              //开始第一个任务(学生请假申请)
             task1 = taskService
                           .createTaskQuery()
                           .processInstanceId(pi.getProcessInstanceId()).singleResult();

           //申请被驳回,重新根据任务id提交申请
          } else {

                 task1 = taskService.createTaskQuery()
                         .taskId(student.getTaskId().toString()).singleResult();
          }
        if (task1!=null) {
            //设置流程变量
            /* variables.put("student", student);
             taskService.setVariables(task1.getId(),variables);*/
             taskService.setVariable(task1.getId(), "name", name);
             taskService.setVariable(task1.getId(), "reason", reason);
             taskService.setVariable(task1.getId(), "time", time);
             taskService.setVariable(task1.getId(), "day",  day);
             taskService.setVariable(task1.getId(), "createTime",createTime);
             taskService.setVariable(task1.getId(), "status","已提交申请,提交给班长处理");
             taskService.setVariable(task1.getId(), "studentId",id);
             taskService.setVariable(task1.getId(), "userIds","王班长,大班长,郝班长");
             //完成第一个任务
             taskService.complete(task1.getId());
             param.put("mes","提交申请成功!");
        }else{
            param.put("mes","提交申请失败!");
        } 
        return param;
    }

    //班长审批任务
    @RequestMapping("applyByMonitor.do")
    @ResponseBody
    public Map<String,Object> applyByMonitor(String msg,String taskId,ModelMap map,Integer studentId){
        Map<String,Object> variables = new HashMap<String, Object>();
        Map<String,Object> param = new HashMap<String, Object>();
         Student s = new Student();
         s.setId(studentId);
        if (msg.equals("2")) {
            variables.put("mes","一般情况");
            s.setStatus("班长已同意申请,申请成功!");
            param.put("mes","您已同意该申请!");
        }else{
            variables.put("mes","重要情况");
            s.setStatus("班长已提交该申请,提交给班主任处理");
            param.put("mes","已提交该申请,提交给班主任处理!");
        }
        loginService.queryUserByUserId(s);
        taskService.complete(taskId,variables);//完成任务时设置流程变量数据
        return param;
    }

//主任审批任务
@RequestMapping("agree.do") 
@ResponseBody
public Map<String,Object> queryByZR(String msg,String taskId,ModelMap map,Integer studentId,String description){
    Map<String,Object> variables = new HashMap<String, Object>();
    Map<String,Object> param = new HashMap<String, Object>();
     Student s = new Student();
     s.setId(studentId);
    if (msg.equals("2")) {
        variables.put("msg","重新申请");
        s.setStatus("申请失败!");
        s.setDescription(description);
        s.setTaskId(Integer.parseInt(taskId));
        param.put("mes","您已驳回该申请!");
    }else{
        variables.put("msg","同意申请");
        s.setStatus("主任已同意申请,申请成功!");
        param.put("mes","您已同意该申请!");
    }
    loginService.queryUserByUserId(s);
    taskService.complete(taskId,variables);//完成任务时设置流程变量数据
    return param;

}   

/**
 * 跳转添加申请单的页面
 * */
@RequestMapping("addApply.do")
public String addApplyHtml(Integer id,ModelMap map){
    Student student = new Student();
    if (id!=null) {
         student = loginService.queryUserList(id); 
     }
    map.put("student",student);
    return "studentLeaveApply";
}

/**
 * 添加申请单
 * */
@RequestMapping("addUserApply.do")
@ResponseBody
public Map<String,Object> addApply(Student student){
    Map<String,Object> map = new HashMap<String, Object>();
    student.setStatus("未提交申请");
     //实行更新操作
     if (student.getId()!=null) {
         loginService.updateUser(student);
         map.put("mes", "更新成功!");
     //实行插入操作
     } else {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        student.setCreateTime(sdf.format(new Date()));
        loginService.insertUser(student);//插入学生请假信息
        map.put("mes", "提交成功!");
     }
    return map;        
}
/**
 * 跳转主任驳回申请页面
 * 
 * */
@RequestMapping("refuseApply.do")
public String refuseApply(String taskId,Integer msg,Integer studentId,ModelMap map){
    map.put("taskId", taskId);
    map.put("studentId", studentId);
    map.put("msg", msg);
    return "RefuseApply";   
}   

/**
 * 查看驳回原因
 * 
 * */
@RequestMapping("queryRefuseReason.do")
@ResponseBody
public Map<String,Object> queryRefuseReason(Integer id){
    Map<String,Object> map = new HashMap<String, Object>();
    Student student = loginService.queryUserList(id);
    map.put("mes",student.getDescription());
    return  map;
}

/**
 * 跳转到转移他人的页面
 * 
 * */
@RequestMapping("move.do")
public String Move(String taskId,ModelMap map){
    map.put("taskId", taskId);
    map.put("flag", flag);
    return "move";
}

/**
 * 转交给他人
 * 
 * */
@RequestMapping("moveToPerson.do")
@ResponseBody
public Map<String,Object> MoveToPerson(String taskId,String person){
    Map<String,Object> map = new HashMap<String, Object>();
    taskService.setAssignee(taskId, person);
    map.put("mes","转交成功!");
    return map;
}

//根据设置的任务代理人获取流程变量
public void getVaribles(String assignee,ModelMap map){
    List<Map<String,Object>> list =new ArrayList<Map<String,Object>>();
      List<Task> taskList = taskService
               .createTaskQuery()
               .taskCandidateUser(assignee)//查询组任务的任务
               .list();
      List<Task> taskList2 = taskService
               .createTaskQuery()
               .taskAssignee(assignee)//查询个人任务的任务
               .list();
      taskList.addAll(taskList2);
      if (taskList.size()>0) {
          for (Task task:taskList){
              Map<String,Object> paramMap = new HashMap<String, Object>();
              String  time = (String)taskService.getVariable(task.getId(), "time");
              String  reason  = (String) taskService.getVariable(task.getId(), "reason");
              String  name  = (String) taskService.getVariable(task.getId(), "name");
              String  day = (String) taskService.getVariable(task.getId(), "day");
              String  createTime = (String) taskService.getVariable(task.getId(), "createTime");
              String  status=(String) taskService.getVariable(task.getId(), "status");
              Integer studentId =(Integer) taskService.getVariable(task.getId(), "studentId");
              paramMap.put("time",time);
              paramMap.put("reason", reason);
              paramMap.put("name", name);
              paramMap.put("day", day);
              paramMap.put("taskId", task.getId());
              paramMap.put("createTime", createTime);
              paramMap.put("status", status);
              paramMap.put("studentId", studentId);
            /*  System.out.println("任务ID:"+task.getId());    
              System.out.println("任务的办理人:"+task.getAssignee());    
              System.out.println("任务名称:"+task.getName());    
              System.out.println("任务的创建时间:"+task.getCreateTime());    
              System.out.println("流程实例ID:"+task.getProcessInstanceId());    
              System.out.println("#######################################");    */
              list.add(paramMap);
          }
          map.put("isHave",true); 
      }else{
          map.put("isHave",false);
      }
      map.put("list", list);
}

dao层写一些查询数据的接口,mapper包下面是sql语句查询

package com.dao;

import java.util.List;
import java.util.Map;

import com.entity.Student;
//根据用户名查询申请列表(dao层接口)
public interface queryByNameDao {

    public List<Map<String,Object>> queryUsers(Student s);

}

//mapper文件(一个dao对应一个mapper文件)
<?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"> 
 <!--namespace是对应的dao层接口的路径 --> 

 <mapper namespace="com.dao.queryByNameDao">
 //id对应的是dao层的方法名
 <select id="queryUsers" parameterType="com.entity.Student" resultType="java.util.HashMap"> 
 select u.name name,u.id id,u.reason reason,u.time time,u.status status,u.day day,u.createTime createTime from Users u  where u.name=#{name}
 </select>

 </mapper> 

package com.services;

import java.util.List;
import java.util.Map;

import com.entity.Student;

//services层代码
public interface LoginService {

    public void insertUser(Student s);
    public List<Map<String,Object>> queryUsers(Student s);
    public void queryUserByUserId(Student s);
    public Student queryUserList(Integer id); 
    public void updateUser(Student s);

}
打开App,阅读手记
1人推荐
发表评论
随时随地看视频慕课网APP

热门评论

老师出点activiti项目实战课程嘛

查看全部评论