持续创作,加速成长!这是我参与「掘金日新计划 · 10 月更文挑战」的第15天,点击查看活动详情
审批业务处理
1.定义更新操作方法
流程更新和查询方法
<select id="selectByFormId" parameterType="long" resultType="com.imooc.oa.entity.ProcessFlow">
SELECT * from adm_process_flow where form_id = #{formId}
</select>
<update id="update" parameterType="com.imooc.oa.entity.ProcessFlow">
UPDATE adm_process_flow SET form_id = #{formId}, operator_id = #{operatorId}, action = #{action}, result = #{result}, reason = #{reason}, create_time = #{createTime}, audit_time = #{auditTime}, order_no = #{orderNo}, state = #{state}, is_last = #{isLast}
WHERE process_id = #{processId}
</update>
请假单更新和查询方法
<update id="update">
UPDATE adm_leave_form SET employee_id=#{employeeId},form_type=#{formType},start_time=#{startTime},end_time=#{endTime},reason=#{reason},
create_time=#{createTime},state=#{state} WHERE form_id=#{formId}
</update>
<select id="selectByFormId" resultType="com.imooc.oa.entity.LeaveForm">
SELECT * from adm_leave_form WHERE form_id = #{formId}
</select>
2.service层
public void audit(Long formId,Long operatorId,String reason,String result){
MybatisUtils.executeUpdate(sqlSession -> {
ProcessFlowDao dao = sqlSession.getMapper(ProcessFlowDao.class);
List<ProcessFlow> flow = dao.selectByFormId(formId);
LeaveFormDao leaveFormDao = sqlSession.getMapper(LeaveFormDao.class);
LeaveForm form = leaveFormDao.selectByFormId(formId);
List<ProcessFlow> item = flow.stream().filter(p->p.getState().equals("process") && p.getOperatorId()==operatorId).collect(Collectors.toList());
ProcessFlow flow1=null;
if(item.size()==0){
throw new BusinessException("pf002","未找到待审批的流程");
}else {
//更新为complete
flow1 = item.get(0);
flow1.setResult(result);
flow1.setReason(reason);
flow1.setState("complete");
flow1.setAuditTime(new Date());
flow1.setCreateTime(new Date());
dao.update(flow1);
}
if(flow1.getIsLast()==1){
form.setReason(reason);
form.setState(result);
leaveFormDao.update(form);
}else{
List<ProcessFlow> flow2 = flow.stream().filter(p->p.getState().equals("ready")).collect(Collectors.toList());
if(result.equals("approved")){
ProcessFlow flow3 = flow2.get(0);
flow3.setState("process");
dao.update(flow3);
}else if(result.equals("refused")){
for(ProcessFlow p:flow2){
p.setState("cancle");
dao.update(p);
}
form.setState("refused");
leaveFormDao.update(form);
}
}
return null;
});
}
代码说明:
- flow.stream().filter(p->p.getState().equals("process") && p.getOperatorId()==operatorId).collect(Collectors.toList()):查询出需要审批的流程并且把状态改成complete
- flow1.getIsLast()==1:判断当前流程是不是最后一个流程,如果是最后一个流程,更新请假单的状态
- flow.stream().filter(p->p.getState().equals("ready")).collect(Collectors.toList()):查询出下一个流程,如果审批结果为同意,则把该流程状态改成process,如果驳回,这把所有流程改成cancel,并且更改请假单状态refused
3.controller
private void audit(HttpServletRequest request, HttpServletResponse response) throws IOException {
long formId = Long.parseLong(request.getParameter("formId"));
String reason = request.getParameter("reason");
String result = request.getParameter("result");
HttpSession session = request.getSession();
User user = (User) session.getAttribute("login_user");
Map json = new HashMap<>();
try {
leaveFormService.audit(formId,user.getEmployeeId(),reason,result);
json.put("code",0);
json.put("message","");
}catch (Exception e){
json.put("code",e.getClass().getSimpleName());
json.put("message",e.getMessage());
}
String str = JSON.toJSONString(json);
response.getWriter().println(str);
}
处理校验
4.审批界面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>请假审批</title>
<link rel="stylesheet" href="/resources/layui/css/layui.css">
<style>
.form-item{
padding: 10px;
}
.form-item-value{
padding: 10px;
}
</style>
</head>
<body>
<div class="layui-row">
<blockquote class="layui-elem-quote">
<h1>请假审批</h1>
</blockquote>
<!--待审批列表-->
<table id="grdFormList" lay-filter="grdFormList"></table>
</div>
<!--请假详情对话框-->
<div id="divDialog" style="display: none;padding: 10px">
<form class="layui-form">
<div class="layui-form-item">
<div class="layui-row">
<div class="layui-col-xs2 form-item">部门</div>
<div class="layui-col-xs4 form-item-value" id="dname"></div>
<div class="layui-col-xs2 form-item">姓名</div>
<div class="layui-col-xs4 form-item-value" id="name"></div>
</div>
<div class="layui-row">
<div class="layui-col-xs2 form-item">起始时间</div>
<div class="layui-col-xs4 form-item-value" id="startTime"></div>
<div class="layui-col-xs2 form-item">结束时间</div>
<div class="layui-col-xs4 form-item-value" id="endTime"></div>
</div>
<div class="layui-row">
<div class="layui-col-xs2 form-item">请假原因</div>
<div class="layui-col-xs10 form-item-value" id="reason"></div>
</div>
<!--表单Id-->
<input type="hidden" name="formId" id="formId">
<!--审批结果-->
<select name="result" lay-verfity="required">
<option value="approved">同意</option>
<option value="refused">驳回</option>
</select>
</div>
<div class="layui-form-item">
<!--审批意见-->
<input type="text" name="reason" placeholder="请输入审批意见"
autocomplete="off" class="layui-input"/>
</div>
<div class="layui-form-item">
<button class="layui-btn layui-btn-fluid " lay-submit lay-filter="audit">确认提交</button>
</div>
</form>
</div>
<script src="/resources/layui/layui.js"></script>
<script src="/resources/sweetalert2.all.min.js"></script>
<script>
var $ = layui.$;
//将毫秒数转换为"yyyy-MM-dd HH时"字符串格式
function formatDate(time){
var newDate = new Date(time);
return newDate.getFullYear() + "-" +
(newDate.getMonth() + 1) + "-" + newDate.getDate()
+ " " + newDate.getHours() + "时";
}
// 将table渲染为数据表格
layui.table.render({
elem : "#grdFormList" , //选择器
id : "grdFormList" , //id
url : "/leave/list" , //ajax请求url
page : false , //是否分页 true-是 false-否
cols :[[ //列描述
{title : "" , width:70 , style : "height:60px" , type:"numbers"}, // numbers代表序号列
{field : "create_time" , title : "申请时间" , width : 150 , templet: function (d) {
//templet代表对数据进行加工后再显示
return formatDate(d.create_time)
}},
{field : "form_type" , title : "类型" , width : 100 , templet: function(d){
switch (d.form_type) {
case 1:
return "事假";
case 2:
return "病假";
case 3:
return "工伤假";
case 4:
return "婚假";
case 5:
return "产假";
case 6:
return "丧假";
}
}},
{field : "department_name" , title : "部门" , width : 100},
{field : "name" , title : "员工" , width : 100},
{field : "start_time" , title : "起始时间" , width : 150, templet: function (d) {
return formatDate(d.start_time)
}},
{field : "end_time" , title : "结束时间" , width : 150 , templet: function (d) {
return formatDate(d.end_time)
}},
{field : "reason" , title : "请假原因" , width : 350 },
{title : "" , width:150 ,type:"space" , templet : function(d){
var strRec = JSON.stringify(d);
console.info("请假单数据", strRec);
//将请假单数据存放至data-laf属性中
return "<button class='layui-btn layui-btn-danger layui-btn-sm btn-audit' data-laf=" + strRec + " >审批</button>";
}}
]]
})
// 绑定每一行的审批按钮
$(document).on("click" , ".btn-audit" , function(){
//初始化表单
$("#divDialog form")[0].reset();
$("#divDialog form form-item-value").text("");
//获取当前点击按钮的请假单数据,回填至显示项
var laf = $(this).data("laf");
$("#dname").text(laf.department_name);
$("#name").text(laf.name);
$("#startTime").text(formatDate(laf.start_time));
$("#endTime").text(formatDate(laf.end_time));
$("#reason").text(laf.reason);
$("#formId").val(laf.form_id);
//弹出layui对话框
layui.layer.open({
type : "1" , //页面层
title : "请假审批" , //标题
content : $("#divDialog") , //指定对话框容器对象
area : ["500px" , "400px"] , //尺寸
end : function(){ //销毁后触发事件
$("#divDialog").hide();
}
})
})
/**
* 提交审批数据
*/
layui.form.on("submit(audit)" , function(data){
$.ajax({
url : "/leave/audit", //审核URL
data : data.field ,
type : "post" ,
dataType : "json" ,
success: function (json) {
//关闭所有layui对话框
layui.layer.closeAll();
//显示处理结果
if(json.code == "0"){
swal({
type: 'success',
html: "<h2>请假已审批完毕</h2>",
confirmButtonText: "确定"
}).then(function (result) {
window.location.href="/forward/notice";
});
}else{
swal({
type: 'warning',
html: "<h2>" + json.msg + "</h2>",
confirmButtonText: "确定"
});
}
}
})
return false;
})
</script>
</body>
</html>