审核之后,数据库置有result更新了,审核时间,state,reason都没有变,而且页面上审核完之后没有消失,是不是也是因为state没有变的原因?
相关代码:
<?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.imooc.oa.dao.ProcessFlowDao">
<insert id="insert" parameterType="com.imooc.oa.entity.ProcessFlow" useGeneratedKeys="true" keyProperty="processId" keyColumn="process_id">
INSERT INTO adm_process_flow (form_id , operator_id , action , result , reason , create_time , audit_time , order_no , state , is_last ) VALUES (#{formId}, #{operatorId}, #{action}, #{ result}, #{reason}, #{createTime}, #{auditTime},#{orderNo}, #{state}, #{isLast});
</insert>
<update id="update" parameterType="com.imooc.oa.entity.ProcessFlow">
UPDATE adm_process_flow SET state = #{state}, form_id = #{formId}, operator_id = #{operatorId}, action = #{action }, result = #{result}, reason = #{reason}, create_time = #{createTime}, audit_time = #{auditTime}, order_no = #{orderNo}, is_last = #{isLast} WHERE process_id = #{processId};
</update>
<select id="selectById" parameterType="Long" resultType="com.imooc.oa.entity.ProcessFlow">
select * from adm_process_flow where form_id = #{value } order by order_no;
</select>
</mapper>
相关代码:
package com.imooc.oa.dao;
import com.imooc.oa.entity.ProcessFlow;
import java.util.List;
public interface ProcessFlowDao {
public void insert(ProcessFlow processFlow);
public void update(ProcessFlow processFlow);
public List<ProcessFlow> selectById(Long formId);
}
相关代码:
package com.imooc.oa.controller;
import com.alibaba.fastjson.JSON;
import com.imooc.oa.entity.LeaveForm;
import com.imooc.oa.entity.User;
import com.imooc.oa.service.LeaveFormService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@WebServlet(name = "LeaveFormServlet", urlPatterns = "/leave/*")
public class LeaveFormServlet extends HttpServlet {
//实例化LeaveFormService
private LeaveFormService leaveFormService = new LeaveFormService();
//引入日志文件
private Logger logger = LoggerFactory.getLogger(LeaveFormServlet.class);
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
//得到当前URL字符串
String uri = request.getRequestURI();
//得到http://localhost/leave/*的那个*是什么
String methodName = uri.substring(uri.lastIndexOf("/") + 1);
if (methodName.equals("create")) {
this.create(request, response);//创建请假单
} else if (methodName.equals("list")) {
this.getLeaveForm(request,response);//审核时查看请假单
} else if (methodName.equals("audit")) {
this.audit(request, response);//审核
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
/**
* 创建请假单
* @param request
* @param response
* @throws IOException
*/
private void create(HttpServletRequest request, HttpServletResponse response) throws IOException {
//再session中获取到用户信息
HttpSession session = request.getSession();
User user = (User) session.getAttribute("login_user");
//得到请假单信息
String formType = request.getParameter("formType");
String strStartTime = request.getParameter("startTime");
String strEndStTime = request.getParameter("endTime");
String reason = request.getParameter("reason");
//字符串日期转化为Date
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-HH");
//返回集
Map resolts = new HashMap();
try {
//封装信息
LeaveForm leaveForm = new LeaveForm();
leaveForm.setEmployeeId(user.getEmployeeId());
leaveForm.setStartTime(sdf.parse(strStartTime));
leaveForm.setEndTime(sdf.parse(strEndStTime));
leaveForm.setFormType(Integer.parseInt(formType));
leaveForm.setReason(reason);
leaveForm.setCreateTime(new Date());
//调用业务逻辑方法
leaveFormService.CreateLeaveForm(leaveForm);
resolts.put("code", "0");
resolts.put("message", "success");
} catch (Exception e) {
e.printStackTrace();
logger.error("请假申请异常",e);
resolts.put("code", e.getClass().getSimpleName());
resolts.put("message", e.getMessage());
}
//组织响应结果
//转化为JSON
String json = JSON.toJSONString(resolts);
//对外输出
response.getWriter().println(json);
}
public void getLeaveForm(HttpServletRequest request,HttpServletResponse response) {
User user = (User) request.getSession().getAttribute("login_user");
List<Map> list = leaveFormService.getLeaveForm("process", user.getEmployeeId());
//封装输出结果
Map map = new HashMap();
map.put("code", "0");
map.put("msg", "");
map.put("count", list.size());
map.put("data", list);
//序列化结果
String json = JSON.toJSONString(map);
try {
response.getWriter().println(json);
} catch (IOException e) {
e.printStackTrace();
}
}
public void audit(HttpServletRequest request,HttpServletResponse response) throws IOException {
String formId = request.getParameter("formId");
String result = request.getParameter("result");
String reason = request.getParameter("reason");
User user = (User) request.getSession().getAttribute("login_user");
//结果集
Map mresult = new HashMap();
try {
leaveFormService.audit(Long.parseLong(formId), user.getEmployeeId(), result, reason);
mresult.put("code", "0");
mresult.put("message", "success");
} catch (Exception e) {
logger.error("请假单审核失败", e);
mresult.put("code", e.getClass().getSimpleName());
mresult.put("message", e.getMessage());
}
String json = JSON.toJSONString(mresult);
response.getWriter().println(json);
}
}
相关代码:
package com.imooc.oa.service;
import com.imooc.oa.dao.EmployeeDao;
import com.imooc.oa.dao.LeaveFormDao;
import com.imooc.oa.dao.ProcessFlowDao;
import com.imooc.oa.entity.Employee;
import com.imooc.oa.entity.LeaveForm;
import com.imooc.oa.entity.ProcessFlow;
import com.imooc.oa.service.exception.BussinessException;
import com.imooc.oa.utils.MybatisUtils;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
//请假单service
public class LeaveFormService {
/**
* 创建请假单
* @param leaveForm
* @return
*/
public LeaveForm CreateLeaveForm(LeaveForm leaveForm) {
LeaveForm savedForm = (LeaveForm) MybatisUtils.excuteUpdate(sqlSession -> {
LeaveFormDao leaveFormDao = sqlSession.getMapper(LeaveFormDao.class);
//1.获取当前创建请假单的员工的级别,并根据不同级别处理业务
EmployeeDao employeeDao = sqlSession.getMapper(EmployeeDao.class);
Employee employee = employeeDao.selectById(leaveForm.getEmployeeId());
if (employee.getLevel()==8){//总经理直接通过
leaveForm.setState("approved");
}else {
leaveForm.setState("processing");
}
//持久化表单
leaveFormDao.insert(leaveForm);
//2.创建第一条流程数据,说明表单已提交
ProcessFlowDao processFlowDao = sqlSession.getMapper(ProcessFlowDao.class);
ProcessFlow f1 = new ProcessFlow();
f1.setFormId(leaveForm.getFormId());
f1.setOrderNo(1);//任务序号
f1.setOperatorId(employee.getEmployeeId());//经手人ID
f1.setAction("apply");//申请行为
f1.setCreateTime(new Date());
f1.setState("complete");//提交行文完成
f1.setIsLast(0);//不是最后一个节点
processFlowDao.insert(f1);
//3.分情况处理其他流程数据
//3.1 七级一下员工生成部门经理审批,36小时以上生成总经理审批
if (employee.getLevel()<7){
//获取上级领导
Employee leader = employeeDao.selectLeader(employee);
//生成领导审批的流程
ProcessFlow f2 = new ProcessFlow();
f2.setFormId(leaveForm.getFormId());
f2.setOperatorId(leader.getEmployeeId());//领导经办
f2.setAction("audit");//审批惹我你
f2.setOrderNo(2);//流程号
f2.setCreateTime(new Date());
f2.setState("process");//领导正在审批
//判断还用不用项总经理审批
Long diff = leaveForm.getEndTime().getTime() - leaveForm.getStartTime().getTime();//得到时间差
Float hours = diff / (1000 * 60 * 60) * 1f;//毫秒数转换为浮点小时
if(hours>=BussinessContants.MANAGER_AUDIT_HOURS){
f2.setIsLast(0);//不是最后一个节点
processFlowDao.insert(f2);//持久化
//总经理审批
Employee manager = employeeDao.selectLeader(leader);//得到总经理
ProcessFlow f3 = new ProcessFlow();//总经理审批流程
f3.setFormId(leaveForm.getFormId());
f3.setOperatorId(leader.getEmployeeId());//领导经办
f3.setAction("audit");//审批惹我你
f3.setOrderNo(3);//流程号
f3.setCreateTime(new Date());
f3.setIsLast(1);//是最后一个节点
f3.setState("ready");//部门经理正在审批,总经理就要先等待
processFlowDao.insert(f3);
}else {//不用总经理
f2.setIsLast(1);
processFlowDao.insert(f2);
}
}else if (employee.getLevel()==7){//部门经理
//生成总经理审批
Employee manager = employeeDao.selectLeader(employee);
ProcessFlow f = new ProcessFlow();
f.setFormId(leaveForm.getFormId());
f.setOperatorId(manager.getEmployeeId());//领导经办
f.setAction("audit");//审批惹我你
f.setOrderNo(2);//流程号
f.setCreateTime(new Date());
f.setIsLast(1);//是最后一个节点
f.setState("process");//部门经理正在
processFlowDao.insert(f);
} else if (employee.getLevel() == 8) {//总经理请假
ProcessFlow f = new ProcessFlow();
f.setFormId(leaveForm.getFormId());
f.setOperatorId(employee.getEmployeeId());//领导经办
f.setAction("audit");//审批惹我你
f.setResult("approved");//自动审批通过
f.setReason("自动通过");
f.setCreateTime(new Date());
f.setAuditTime(new Date());
f.setState("complete");//完成
f.setOrderNo(2);//第二部
f.setIsLast(1);//最后一个节点
processFlowDao.insert(f);
}
return leaveForm;
});
return savedForm;//返回最终的请假单
}
public List<Map> getLeaveForm(String state, Long id) {
return (List<Map>) MybatisUtils.excuteQuery(sqlSession -> {
LeaveFormDao leaveFormDao = sqlSession.getMapper(LeaveFormDao.class);
List<Map> list = leaveFormDao.selectByParam(state, id);
return list;
});
}
public void audit(Long formId,Long operatorId,String result,String reason){
MybatisUtils.excuteUpdate(sqlSession -> {
ProcessFlow process = null;
//1.无论统一还是驳回,当前任务状态都变为complete
ProcessFlowDao processFlowDao = sqlSession.getMapper(ProcessFlowDao.class);//实例化
List<ProcessFlow> list = processFlowDao.selectById(formId);//得到进程流
if (list.size()==0){//没有进程
throw new BussinessException("PF001", "无效进程");
}
//获取当前进程(筛选条件为:1.状态为process。2.操作员为operatorId)并将筛选后的结果生成为新的List
List<ProcessFlow> pfList = list.stream().filter(p -> (p.getState().equals("process") )&&( p.getOperatorId() == operatorId)).collect(Collectors.toList());
if (pfList.size()==0){//没有进程
throw new BussinessException("PF002", "未找到待处理任务");
}else{
process = pfList.get(0);
process.setState("complete");
process.setAuditTime(new Date());
process.setReason(reason);
process.setResult(result);
processFlowDao.update(process);
}
//2.当前是最后一个节点,更新请假单为approved/refused
LeaveFormDao leaveFormDao = sqlSession.getMapper(LeaveFormDao.class);
LeaveForm leaveForm = new LeaveForm();
if(process.getIsLast()==1){
leaveForm.setState(result);//更新请假单为approved/refused
leaveFormDao.update(leaveForm);
}else{//有后续节点
//3.当前不是最后一个节点,下一个节点从ready变为process
// 从进程流中获取后续所有的任务节点(进程为ready的就是后续进程)
List<ProcessFlow> readyList = list.stream().filter(p -> p.getState().equals("ready")).collect(Collectors.toList());
if (result.equals("approved")){//审批结果为统一,进行下一个节点
ProcessFlow fistReady = readyList.get(0);//获取第一个后继节点
fistReady.setState("process");
processFlowDao.update(fistReady);
}else if(result.equals("refused")){//当前节点拒绝
//4.当前不是最后一个节点,但是当前节点是驳回,则之后所有任务变为cancel,请假单状态变为refused
for (ProcessFlow p:readyList){//对每个后继结点修改
p.setState("cancel");
processFlowDao.update(p);
}
//修改表单状态
leaveForm.setState("refused");
leaveFormDao.update(leaveForm);
}
}
return null;
});
}
}
相关截图:
29
收起
正在回答 回答被采纳积分+1
2回答
java工程师2020版
- 参与学习 人
- 提交作业 9393 份
- 解答问题 16556 个
综合就业常年第一,编程排行常年霸榜,无需脱产即可学习,北上广深月薪过万 无论你是未就业的学生还是想转行的在职人员,不需要基础,只要你有梦想,想高薪
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星