接上一個
開發(fā)多級審批流程
設計約束
每一個請假單對應一個審批流程
請假單創(chuàng)建后,按業(yè)務規(guī)則生成部門經(jīng)理、總經(jīng)理審批任務
審批任務的經(jīng)辦人只能審批自己轄區(qū)內的請假申請
所有審批任務"通過",代表請假已經(jīng)批準
任意審批任務"駁回"操作,其余審批任務取消,請假申請被駁回
請假流程中任意節(jié)點產(chǎn)生的操作都要生成對應的系統(tǒng)通知
請假申請表
請假流程表
請假通知表
相關的實體類都進行創(chuàng)建
請假單接口的創(chuàng)建
public interface LeaveFormDao {public void insert(LeaveForm form
);public List
<Map> selectByParams(@Param("pf_state") String pfState
,@Param("pf_operator_id") Long operatorId
);
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.imooc.oa.dao.LeaveFormDao"><insert id="insert" parameterType="com.imooc.oa.entity.LeaveForm"useGeneratedKeys="true" keyProperty="formId" keyColumn="form_id">INSERT INTO adm_leave_form(employee_id, form_type, start_time, end_time,reason, create_time, state)VALUES (#{employeeId}, #{formType}, #{startTime}, #{endTime},#{reason}, #{createTime}, #{state})
</insert>
</mapper>
在配置文件中進行注冊
剩下兩個表的操作都一樣
開發(fā)請假功能
請假單流程服務和消息提示 LeaveFormService
public LeaveForm
createLeaveForm(LeaveForm form
) {LeaveForm savedForm
= (LeaveForm
) MybatisUtils
.excuteUpdate(sqlSession
-> {EmployeeDao employeeDao
= sqlSession
.getMapper(EmployeeDao
.class);Employee employee
= employeeDao
.selectById(form
.getEmployeeId());if (employee
.getLevel() == 8) {form
.setState("approved");} else {form
.setState("processing");}LeaveFormDao leaveFormDao
= sqlSession
.getMapper(LeaveFormDao
.class);leaveFormDao
.insert(form
);ProcessFlowDao processFlowDao
= sqlSession
.getMapper(ProcessFlowDao
.class);ProcessFlow flow1
= new ProcessFlow();flow1
.setFormId(form
.getFormId());flow1
.setOperatorId(employee
.getEmployeeId());flow1
.setAction("apply");flow1
.setCreateTime(new Date());flow1
.setOrderNo(1);flow1
.setState("complete");flow1
.setIsLast(0);processFlowDao
.insert(flow1
);SimpleDateFormat sdf
= new SimpleDateFormat("yyyy-MM-dd-HH時");NoticeDao noticeDao
= sqlSession
.getMapper(NoticeDao
.class); if (employee
.getLevel() < 7) {Employee dmanager
= employeeDao
.selectLeader(employee
);ProcessFlow flow2
= new ProcessFlow();flow2
.setFormId(form
.getFormId());flow2
.setOperatorId(dmanager
.getEmployeeId());flow2
.setAction("audit");flow2
.setCreateTime(new Date());flow2
.setOrderNo(2);flow2
.setState("process");long diff
= form
.getEndTime().getTime() - form
.getStartTime().getTime();float hours
= diff
/ (1000 * 60 * 60) * 1f;if (hours
>= BussinessConstants
.MANAGER_AUDIT_HOURS
) {flow2
.setIsLast(0);processFlowDao
.insert(flow2
);Employee manager
= employeeDao
.selectLeader(dmanager
);ProcessFlow flow3
= new ProcessFlow();flow3
.setFormId(form
.getFormId());flow3
.setOperatorId(manager
.getEmployeeId());flow3
.setAction("audit");flow3
.setCreateTime(new Date());flow3
.setState("ready");flow3
.setOrderNo(3);flow3
.setIsLast(1);processFlowDao
.insert(flow3
);} else {flow2
.setIsLast(1);processFlowDao
.insert(flow2
);}String noticeContent
= String
.format("您的請假申請[%s-%s]已提交,請等待上級批準.",sdf
.format(form
.getStartTime()), sdf
.format(form
.getEndTime()));noticeDao
.insert(new Notice(employee
.getEmployeeId(), noticeContent
));noticeContent
= String
.format("%s-%s提起請假申請[%s-%s],請盡快審批",employee
.getTitle(), employee
.getName(), sdf
.format(form
.getStartTime()),sdf
.format(form
.getEndTime()));noticeDao
.insert(new Notice(dmanager
.getEmployeeId(), noticeContent
));} else if (employee
.getLevel() == 7) {Employee manager
= employeeDao
.selectLeader(employee
);ProcessFlow flow
= new ProcessFlow();flow
.setFormId(form
.getFormId());flow
.setOperatorId(manager
.getEmployeeId());flow
.setAction("audit");flow
.setCreateTime(new Date());flow
.setState("process");flow
.setOrderNo(2);flow
.setIsLast(1);processFlowDao
.insert(flow
);String noticeContent
= String
.format("您的請假申請[%s-%s]已提交,請等待上級批準.",sdf
.format(form
.getStartTime()), sdf
.format(form
.getEndTime()));noticeDao
.insert(new Notice(employee
.getEmployeeId(), noticeContent
));noticeContent
= String
.format("%s-%s提起請假申請[%s-%s],請盡快審批",employee
.getTitle(), employee
.getName(), sdf
.format(form
.getStartTime()),sdf
.format(form
.getEndTime()));noticeDao
.insert(new Notice(manager
.getEmployeeId(), noticeContent
));} else if (employee
.getLevel() == 8) {ProcessFlow flow
= new ProcessFlow();flow
.setFormId(form
.getFormId());flow
.setOperatorId(employee
.getEmployeeId());flow
.setAction("audit");flow
.setResult("approved");flow
.setReason("自動通過");flow
.setCreateTime(new Date());flow
.setAuditTime(new Date());flow
.setState("complete");flow
.setOrderNo(2);flow
.setIsLast(1);processFlowDao
.insert(flow
);String noticeContent
= String
.format("您的請假申請[%s-%s]系統(tǒng)已自動批準通過,請等待上級批準.",sdf
.format(form
.getStartTime()), sdf
.format(form
.getEndTime()));noticeDao
.insert(new Notice(employee
.getEmployeeId(), noticeContent
));}return form
;});return savedForm
;}
創(chuàng)建一個動態(tài)SQL根據(jù)傳入的員工獲取對應的上級主管對象
public Employee
selectLeader(@Param("emp") Employee employee
);
<select id="selectLeader" parameterType="com.imooc.oa.entity.Employee"resultType="com.imooc.oa.entity.Employee">select * from adm_employeewhere
<if test="emp.level < 7">level = 7 and department_id = #{emp.departmentId}
</if><if test="emp.level == 7">level = 8
</if><if test="emp.level == 8">employee_id = #{emp.employeeId}
</if></select>
Servlet
@WebServlet(name
= "LeaveFormServlet", urlPatterns
= "/leave/*")
public class LeaveFormServlet extends HttpServlet {private LeaveFormService leaveFormService
= new LeaveFormService();private Logger logger
= LoggerFactory
.getLogger(LeaveFormService
.class);@Overrideprotected void doPost(HttpServletRequest request
, HttpServletResponse response
) throws ServletException
,IOException
{request
.setCharacterEncoding("utf-8");response
.setContentType("text/html;charset=utf-8");String uri
= request
.getRequestURI();// 對url進行截取String methodName
= uri
.substring(uri
.lastIndexOf("/") + 1);// 創(chuàng)建請假表單
if (methodName
.equals("create")) {this.create(request
, response
);} else if (methodName
.equals("list")) {//獲取請假表單
this.getLeaveFormList(request
, response
);} else if (methodName
.equals("audit")) {//審核請假單
this.audit(request
, response
);}}@Overrideprotected void doGet(HttpServletRequest request
, HttpServletResponse response
) throws ServletException
,IOException
{this.doPost(request
, response
);}// 創(chuàng)建請假單
private void create(HttpServletRequest request
, HttpServletResponse response
) throws ServletException
,IOException
{// 1.接收各項請假單數(shù)據(jù)集HttpSession session
= request
.getSession();// 獲取登錄用戶
// 獲取用戶提交的請假單數(shù)據(jù)User user
= (User
) session
.getAttribute("login_user");String formType
= request
.getParameter("formType");String strStartTime
= request
.getParameter("startTime");String strEndTime
= request
.getParameter("endTime");String reason
= request
.getParameter("reason");SimpleDateFormat sdf
= new SimpleDateFormat("yyyy-MM-dd-HH");Map
<String, String> result
= new HashMap();try {LeaveForm form
= new LeaveForm();form
.setEmployeeId(user
.getEmployeeId());form
.setStartTime(sdf
.parse(strStartTime
));form
.setEndTime(sdf
.parse(strEndTime
));form
.setFormType(Integer
.parseInt(formType
));form
.setReason(reason
);//請假原因form
.setCreateTime(new Date());// 2.調用業(yè)務邏輯方法
// 創(chuàng)建請假單leaveFormService
.createLeaveForm(form
);result
.put("code", "0");result
.put("message", "success");} catch (Exception e
) {e
.printStackTrace();logger
.error("請假申請異常", e
);result
.put("code", e
.getClass().getCanonicalName());result
.put("message", e
.getMessage());}// 組織相應結果String json
= JSON
.toJSONString(result
);response
.getWriter().println(json
);}public void getLeaveFormList(HttpServletRequest request
, HttpServletResponse response
) throws ServletException
,IOException
{User user
= (User
) request
.getSession().getAttribute("login_user");List
<Map> formList
= leaveFormService
.getLeaveFormList("process", user
.getEmployeeId());Map result
= new HashMap();result
.put("code", "0");result
.put("msg", "");result
.put("count", formList
.size());result
.put("data", formList
);String json
= JSON
.toJSONString(result
);response
.getWriter().println(json
);}public void audit(HttpServletRequest request
, HttpServletResponse response
) throws ServletException
,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
<String, String> mpResult
= new HashMap<>();try {leaveFormService
.audit(Long
.parseLong(formId
), user
.getEmployeeId(), result
, reason
);mpResult
.put("code", "0");mpResult
.put("message", "success");} catch (Exception e
) {logger
.error("請假單審核失敗", e
);mpResult
.put("code", e
.getClass().getSimpleName());mpResult
.put("message", e
.getMessage());}String json
= JSON
.toJSONString(mpResult
);response
.getWriter().println(json
);}
}
前端代碼就不列出來了。
為了解決頁面顯示問題
@WebServlet(name
= "ForwardServlet", urlPatterns
= "/forward/*")
public class ForwardServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest request
, HttpServletResponse response
) throws ServletException
,IOException
{String uri
= request
.getRequestURI();String subUri
= uri
.substring(1);String page
= subUri
.substring(subUri
.indexOf("/"));request
.getRequestDispatcher(page
+ ".ftl").forward(request
, response
);}
}
請假審批功能
首先查詢自己要審批的表單條件是,審批人為自己和審批狀態(tài)為processing(正在處理)
<select id="selectByParams" parameterType="java.util.Map" resultType="java.util.Map">select f.*, e.name, d.*from adm_leave_form f,adm_process_flow pf,adm_employee e,adm_department dwhere f.form_id = pf.form_idand pf.state = #{pf_state}and pf.operator_id = #{pf_operator_id}and f.employee_id = e.employee_idand e.department_id = d.department_id
</select>
public List
<Map> getLeaveFormList(String pfState
, Long operatorId
) {return (List
<Map>) MybatisUtils
.executeQuery(sqlSession
-> {LeaveFormDao dao
= sqlSession
.getMapper(LeaveFormDao
.class);List
<Map> formList
= dao
.selectByParams(pfState
, operatorId
);return formList
;});}
public void getLeaveFormList(HttpServletRequest request
, HttpServletResponse response
) throws ServletException
,IOException
{User user
= (User
) request
.getSession().getAttribute("login_user");List
<Map> formList
= leaveFormService
.getLeaveFormList("process", user
.getEmployeeId());Map result
= new HashMap();result
.put("code", "0");result
.put("msg", "");result
.put("count", formList
.size());result
.put("data", formList
);String json
= JSON
.toJSONString(result
);response
.getWriter().println(json
);}
審批業(yè)務邏輯
情況1:普通員工請假,小于72小時,部門經(jīng)理審批
情況2:普通員工請假,大于72小時,部門經(jīng)理審批通過,總經(jīng)理需要再次審批
情況3:普通員工請假,大于72小時,部門經(jīng)理審批未通過,總經(jīng)理不用審批
情況4:部門經(jīng)理請假,總經(jīng)理審批
情況5:總經(jīng)理請假,直接通過
public void audit(Long formId
, Long operatorId
, String result
, String reason
) {MybatisUtils
.excuteUpdate(sqlSession
-> {ProcessFlowDao processFlowDao
= sqlSession
.getMapper(ProcessFlowDao
.class);List
<ProcessFlow> flowList
= processFlowDao
.selectByFormId(formId
);if (flowList
.size() == 0) {throw new BussinessException("PF001", "無效的審批流程");}List
<ProcessFlow> processList
= flowList
.stream().filter(p
-> p
.getOperatorId().equals(operatorId
) && p
.getState().equals("process")).collect(Collectors
.toList());ProcessFlow process
= null
;if (processList
.size() == 0) {throw new BussinessException("PF002", "未找到待處理任務");} else {process
= processList
.get(0);process
.setState("complete");process
.setResult(result
);process
.setReason(reason
);process
.setAuditTime(new Date());processFlowDao
.update(process
);}LeaveFormDao leaveFormDao
= sqlSession
.getMapper(LeaveFormDao
.class);LeaveForm form
= leaveFormDao
.selectById(formId
);SimpleDateFormat sdf
= new SimpleDateFormat("yyyy-MM-dd-HH時");EmployeeDao employeeDao
= sqlSession
.getMapper(EmployeeDao
.class);Employee employee
= employeeDao
.selectById(form
.getEmployeeId());Employee operator
= employeeDao
.selectById(operatorId
);NoticeDao noticeDao
= sqlSession
.getMapper(NoticeDao
.class);if (process
.getIsLast() == 1) {form
.setState(result
);leaveFormDao
.update(form
);String strResult
= null
;if (result
.equals("approved")) {strResult
= "批準";} else if (result
.equals("refused")) {strResult
= "駁回";}String noticeContent
= String
.format("您的請假申請[%s-%s]%s%s已%s,審批意見:%s,審批流程已結束",sdf
.format(form
.getStartTime()), sdf
.format(form
.getEndTime()), operator
.getTitle(),operator
.getName(), strResult
, reason
);noticeDao
.insert(new Notice(form
.getEmployeeId(), noticeContent
));noticeContent
= String
.format("%s-%s提起的請假申請[%s-%s]您已%s,審批意見:%s,審批流程已結束",employee
.getTitle(), employee
.getName(), sdf
.format(form
.getStartTime()),sdf
.format(form
.getEndTime()),strResult
, reason
);noticeDao
.insert(new Notice(operator
.getEmployeeId(), noticeContent
));} else {List
<ProcessFlow> readyList
=flowList
.stream().filter(p
-> p
.getState().equals("ready")).collect(Collectors
.toList());if (result
.equals("approved")) {ProcessFlow readyProcess
= readyList
.get(0);readyProcess
.setState("process");processFlowDao
.update(readyProcess
);String noticeContent1
= String
.format("您的請假申請[%s-%s]%s%s已批準,審批意見:%s,請等待上級領導審批",sdf
.format(form
.getStartTime()), sdf
.format(form
.getEndTime()),operator
.getTitle(), operator
.getName(), reason
);noticeDao
.insert(new Notice(form
.getEmployeeId(), noticeContent1
));String noticeContent2
= String
.format("%s-%s提起請假申請[%s-%s],請盡快審批",employee
.getTitle(), employee
.getName(), sdf
.format(form
.getStartTime()),sdf
.format(form
.getEndTime()));noticeDao
.insert(new Notice(readyProcess
.getOperatorId(), noticeContent2
));String noticeContent3
= String
.format("%s-%s提起的請假申請[%s-%s]您已批準,審批意見:%s,請等待上級領導審批",employee
.getTitle(), employee
.getName(), sdf
.format(form
.getStartTime()),sdf
.format(form
.getEndTime()),reason
);noticeDao
.insert(new Notice(operator
.getEmployeeId(), noticeContent3
));} else if (result
.equals("refused")) {for (ProcessFlow p
:readyList
) {p
.setState("cancel");processFlowDao
.update(p
);}form
.setState("refused");leaveFormDao
.update(form
);String noticeContent1
= String
.format("您的請假申請[%s-%s]%s%s已駁回,原因是%s",sdf
.format(form
.getStartTime()), sdf
.format(form
.getEndTime()),operator
.getTitle(), operator
.getName(), reason
);noticeDao
.insert(new Notice(form
.getEmployeeId(), noticeContent1
));String noticeContent3
= String
.format("%s-%s提起的請假申請[%s-%s]您已駁回,審批意見%s,審批流程已結束",employee
.getTitle(), employee
.getName(), sdf
.format(form
.getStartTime()),sdf
.format(form
.getEndTime()),reason
);noticeDao
.insert(new Notice(operator
.getEmployeeId(), noticeContent3
));}}return null
;});}
總結
以上是生活随笔為你收集整理的OA系统实现(请假审批,mybatis)-2的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。