/* * Copyright (c) 2018-2028, Chill Zhuang All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * Neither the name of the dreamlu.net developer nor the names of its * contributors may be used to endorse or promote products derived from * this software without specific prior written permission. * Author: Chill 庄骞 (smallchill@163.com) */ package org.springblade.desk.service.impl; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.metadata.IPage; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springblade.core.log.exception.ServiceException; import org.springblade.core.mp.base.BaseServiceImpl; import org.springblade.core.secure.utils.AuthUtil; import org.springblade.core.secure.utils.SecureUtil; import org.springblade.core.tool.api.R; import org.springblade.core.tool.support.Kv; import org.springblade.core.tool.utils.Func; import org.springblade.desk.entity.WorkOvertime; import org.springblade.desk.entity.WorkOvertimePeople; import org.springblade.desk.entity.WorkOvertimePeopleApprove; import org.springblade.desk.entity.WorkOvertimeTotalHours; import org.springblade.desk.mapper.WorkOvertimeMapper; import org.springblade.desk.service.IWorkOvertimeService; import org.springblade.desk.utils.DeptUserUtil; import org.springblade.flow.core.constant.ProcessConstant; import org.springblade.flow.core.entity.BladeFlow; import org.springblade.flow.core.feign.IFlowClient; import org.springblade.flow.core.utils.FlowUtil; import org.springblade.flow.core.utils.TaskUtil; import org.springblade.system.entity.Dept; import org.springblade.system.user.entity.User; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.math.BigDecimal; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; /** * 服务实现类 加班申请单 * * @author*/ @Slf4j @Service @AllArgsConstructor public class WorkOvertimeServiceImpl extends BaseServiceImpl<WorkOvertimeMapper, WorkOvertime> implements IWorkOvertimeService { private IFlowClient flowClient; private DeptUserUtil deptUserUtil; private OaSerialServiceImpl oaSerialService; private WorkOvertimePeopleServiceImpl workOvertimePeopleServiceImpl; private WorkOvertimeMapper workOvertimeMapper; @Override @Transactional(rollbackFor = Exception.class) public boolean startProcess(WorkOvertime bean) { //是否启动流程 boolean isStartProcess = false; //是否新增数据 boolean isAdd = false; if(bean!=null){ if(Func.isEmpty(bean.getProcessInstanceId())){ isStartProcess = true; } if(Func.isEmpty(bean.getId())){ isAdd = true; } //更新业务数据 if(isAdd){ Date now = new Date(); SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd"); String dateStr = format.format(now); if(Func.isNull(bean.getWorkOvertimeNumber())){ bean.setWorkOvertimeNumber(oaSerialService.getSerial(bean.getTenantId(),"WorkOvertime",dateStr,dateStr,5)); } } saveOrUpdate(bean); //加班人员 if(isAdd){ //新增 savePeople(bean.getWorkOvertimePeopleList(),bean); }else{ //修改从新发起 修改先删除旧的,插入新的 workOvertimePeopleServiceImpl.removeById(bean.getId()); //插入新的 savePeople(bean.getWorkOvertimePeopleList(),bean); } } //获取经理职位 Long managerUser=null; String roleId=deptUserUtil.getRoleId(AuthUtil.getTenantId(), "经理"); List<User>users=deptUserUtil.getUserListByRoleIdAndDeptId(roleId,String.valueOf(bean.getProposerDepartmentCode()),false); if(!Func.isEmpty(users)){ managerUser=users.get(0).getId(); } log.info("获取经理职位roleId====="+roleId+"------users========================"+users); //获取部门总监 String majorRoleId=deptUserUtil.getRoleId(AuthUtil.getTenantId(), "总监"); List<User>majordomoUsers=deptUserUtil.getUserListByRoleIdAndDeptId(majorRoleId,String.valueOf(bean.getProposerDepartmentCode()),true); Long generalManager=null; if(null!=majordomoUsers && majordomoUsers.size()>0){ generalManager =majordomoUsers.get(0).getId(); } log.info("获取总监majorRoleId===="+majorRoleId+"-----majordomoUsers========================"+majordomoUsers); //获取副总 String deputyPostId = deptUserUtil.getPostId("副总经理",bean.getTenantId()); List<User> deputyUsers = deptUserUtil.getUserListByPostIdAndDeptId(deputyPostId,String.valueOf(bean.getProposerDepartmentCode())); Long presidentUser =null; if(null!=deputyUsers && deputyUsers.size()>0){ presidentUser =deputyUsers.get(0).getId(); } log.info("获取副总deputyPostId====="+deputyPostId+"-------deputyUsers========================"+deputyUsers); //获取总经理 String companyBossPostId=deptUserUtil.getPostId("总经理",bean.getTenantId()); List<User>companyBossUsers=deptUserUtil.getUserListByPostId(companyBossPostId);//总经理不属于某个部门,不用根据部门id查询 Long companyBoss=null; if(null!=companyBossUsers && companyBossUsers.size()>0){ companyBoss=companyBossUsers.get(0).getId(); } log.info("获取总经理companyBossPostId====="+companyBossPostId+"-------companyBossUsers========================"+companyBossUsers); /** * 判断条件 */ //如果发起人是总监或者经理不是部门最高领导 没有的职位跳过经理职位 if(null!=managerUser){ if(bean.getProposerCode().longValue()==managerUser.longValue()){ managerUser=null;//经理 } } //跳过总监 if(null!=generalManager){ //跳过总监 if(bean.getProposerCode().longValue()==generalManager.longValue()){ //跳过经理 managerUser=null;//经理 //跳过总监职位 generalManager=null; } } //根据部门获取部门信息 Dept deptInfo= deptUserUtil.getDeptById(bean.getProposerDepartmentCode()); String isDepartmentHead="0";//0不是最高主管,1是最高主管 String isSell="其他"; String isManager="2";//2上级主管不是最大领导,3是部门最大领导 log.info("根据部门获取部门信息deptInfo================="+deptInfo); if(!Func.isEmpty(deptInfo)){ if(deptInfo.getManagerUser().longValue()==bean.getProposerCode().longValue()){ isDepartmentHead="1"; } //判断是不是销售部人员 if(deptInfo.getDeptName().equals("销售部")){ isSell="销售"; } //判断是不是部门最大领导 申请人编号和部门领导号相同 是部门最大领导 if(deptInfo.getManagerUser().longValue()==bean.getProposerCode()){ isManager="3"; } } // 启动流程 Kv variables = Kv.create() .set(ProcessConstant.TASK_VARIABLE_CREATE_USER, SecureUtil.getUserName()) .set("subject",bean.getProposerName()+","+bean.getProposerName())//主题 .set("orderCode",bean.getWorkOvertimeNumber())//流程编号 .set("taskUser", TaskUtil.getTaskUser(AuthUtil.getUser().getUserId().toString())) .set("isDepartmentHead",isDepartmentHead)//是不是部门最高领导 .set("isSell",isSell)//是不是销售部人员 .set("isManager",isManager)//2上级主管不是部门最大领导,3是最大领导 .set("overtimeType",bean.getOvertimeType())//申请类型 调休,付薪 .set("managerSkip", Func.isEmpty(managerUser)) .set("managerUser",TaskUtil.getTaskUser(String.valueOf(managerUser==null ? "" : managerUser))) .set("generalManagerSkip", Func.isEmpty(generalManager)) .set("generalManager", TaskUtil.getTaskUser(String.valueOf(generalManager==null ? "" : generalManager))) .set("presidentSkip",Func.isEmpty(presidentUser)) .set("presidentUser", TaskUtil.getTaskUser(String.valueOf(presidentUser==null ? "" : presidentUser))) .set("companyBossSkip",Func.isEmpty(companyBoss)) .set("companyBossUser", TaskUtil.getTaskUser(String.valueOf(companyBoss==null ? "" : companyBoss)))//总经理 .set("pass",true) .set("_FLOWABLE_SKIP_EXPRESSION_ENABLED",true); //启动流程 if (isStartProcess) { String businessTable = FlowUtil.getBusinessTable(ProcessConstant.WORK_OVERTIME); R<BladeFlow> result = flowClient.startProcessInstanceById(bean.getProcessDefinitionId(), FlowUtil.getBusinessKey(businessTable, String.valueOf(bean.getId())), variables); if (result.isSuccess()) { log.debug("流程已启动,流程ID:" + result.getData().getProcessInstanceId()); // 返回流程id写入业务表中 bean.setProcessInstanceId(result.getData().getProcessInstanceId()); updateById(bean); } else { throw new ServiceException("开启流程失败"); } } else{ if(!Func.isEmpty(bean.getFlow())&&!Func.isEmpty(bean.getFlow().getTaskId())&&!Func.isEmpty(bean.getProcessInstanceId())){ variables.put(ProcessConstant.PASS_KEY, true); flowClient.completeTask(bean.getFlow().getTaskId(),bean.getProcessInstanceId(),bean.getFlow().getComment(),variables); } } return true; } /** * 获取明细 * @param businessId * @return */ @Override public WorkOvertime getDtail(Long businessId) { //获取主表信息 WorkOvertime workOvertime=getById(businessId); Map<String,Object>map=new HashMap<String,Object>(); map.put("id",businessId); List<WorkOvertimePeople>list=workOvertimePeopleServiceImpl.listByMap(map); workOvertime.setWorkOvertimePeopleList(list); return workOvertime; } /** * 审批通过后根据流程实例id查询加班申请单信息 * @param processInstanceId * @return */ @Transactional(rollbackFor = Exception.class) @Override public boolean getWorkOvertimeServiceInfo(String processInstanceId) { WorkOvertime workOvertime=workOvertimeMapper.getWorkOvertimeServiceInfo(processInstanceId); //根据id获取加班人员信息 Map<String,Object>mapId=new HashMap<String,Object>(); mapId.put("id",workOvertime.getId()); List<WorkOvertimePeople>listPerson=workOvertimePeopleServiceImpl.listByMap(mapId); workOvertime.setWorkOvertimePeopleList(listPerson); log.info("审批通过后根据流程实例id查询加班申请单信息processInstanceId=================="+processInstanceId+"-----workOvertime========="+workOvertime); System.out.println(!Func.isEmpty(workOvertime)); if(!Func.isEmpty(workOvertime)){ if(!Func.isEmpty(workOvertime.getWorkOvertimePeopleList())){ String dateTime=new SimpleDateFormat("yyyy-MM-dd-HH:mm:ss:SSS").format(new Date()).toString(); log.info("dateTime=============="+dateTime); for (WorkOvertimePeople workOvertimePeople : workOvertime.getWorkOvertimePeopleList()){ //人员加班总时长表如果存在 则根据加班类型修改相应的时长,如果不存在则插入 根据用户号 //根据用户号查询人员加班总时长是否已经存在 WorkOvertimeTotalHours workOvertimeTotalHours=workOvertimeMapper.getWorkOvertimeTotalHoursInfo(workOvertimePeople.getUserCode()); //根据用户号查询人员加班总时长不存在 if(Func.isEmpty(workOvertimeTotalHours)){ WorkOvertimeTotalHours totalHoursDetail =new WorkOvertimeTotalHours(); if(workOvertimePeople.getOvertimeType().equals("调休")){ //调休加班总时长 totalHoursDetail.setTotalHours(workOvertimePeople.getDuration()); } //付薪加班总时长 if(workOvertimePeople.getOvertimeType().equals("付薪")){ totalHoursDetail.setPayTotalHours(workOvertimePeople.getDuration()); } //人员名称 totalHoursDetail.setUserName(workOvertimePeople.getUserName()); // 人员编号 totalHoursDetail.setUserCode(workOvertimePeople.getUserCode()); //人员部门名称 totalHoursDetail.setDepartmentName(workOvertimePeople.getDepartmentName()); //人员部门编号 totalHoursDetail.setDepartmentCode(workOvertimePeople.getDepartmentCode()); //租户id totalHoursDetail.setTenantId(workOvertimePeople.getTenantId()); //更新时间到时分秒 totalHoursDetail.setUpdateTime(dateTime); //信息写入加班总时长表 log.info("totalHoursDetail=============="+totalHoursDetail); workOvertimeMapper.saveWorkOvertimeTotalHoursInfo(totalHoursDetail); }else{ log.info("用户加班时长已经存在workOvertimeTotalHours============"+workOvertimeTotalHours); WorkOvertimeTotalHours totalHoursInfo =new WorkOvertimeTotalHours(); //用户加班总时长信息已经存在 if(workOvertimePeople.getOvertimeType().equals("调休")){ //如果调休加班时长为空 if(Func.isEmpty(workOvertimeTotalHours.getTotalHours())){ Double hours=workOvertimePeople.getDuration(); totalHoursInfo.setTotalHours(hours); }else{ //如果调休加班时长为不为空 Double hours=add(workOvertimeTotalHours.getTotalHours(),workOvertimePeople.getDuration()); totalHoursInfo.setTotalHours(hours); } } if(workOvertimePeople.getOvertimeType().equals("付薪")){ //如果付薪时长为空 if(Func.isEmpty(workOvertimeTotalHours.getPayTotalHours())){ Double hours1=workOvertimePeople.getDuration(); totalHoursInfo.setPayTotalHours(hours1); }else { //付薪时长不为空 Double hours1=add(workOvertimeTotalHours.getPayTotalHours(),workOvertimePeople.getDuration()); totalHoursInfo.setPayTotalHours(hours1); } } totalHoursInfo.setUserCode(workOvertimePeople.getUserCode()); log.info("totalHoursInfo======================"+totalHoursInfo); workOvertimeMapper.updateWorkOvertimeTotalHoursInfo(totalHoursInfo); } //将信息插入员加班明细表 // 将 实体类 转换为 Map Map<String,Object> maps= JSONObject.parseObject(JSONObject.toJSONString(workOvertimePeople), Map.class); maps.remove("id"); maps.remove("createTime"); maps.put("updateTime",dateTime); log.info("将信息插入员加班明细表maps==================="+maps); //将人员加班信息插入人员加班详情表 workOvertimeMapper.saveWorkOvertimePeopleApprove(maps); } } } return true; } /** * 根据用户号查询加班总时长详情 * @param userCode * @return */ @Override public WorkOvertimeTotalHours getWorkOvertimeTotalHoursDetail(Long userCode) { return workOvertimeMapper.getWorkOvertimeTotalHoursDetail(userCode); } @Override public IPage<WorkOvertimePeopleApprove> getWorkOvertimePeopleApprovePage(IPage<WorkOvertimePeopleApprove> page,WorkOvertimePeopleApprove workOvertimePeopleApprove) { log.info("workOvertimePeopleApprove================"+workOvertimePeopleApprove); String startTime=workOvertimePeopleApprove.getStartTime(); String endTime=workOvertimePeopleApprove.getEndTime(); String departmentName=workOvertimePeopleApprove.getDepartmentName(); String overtimeType=workOvertimePeopleApprove.getOvertimeType(); String tenantId=workOvertimePeopleApprove.getTenantId(); Long user_code=workOvertimePeopleApprove.getUserCode(); List<WorkOvertimePeopleApprove>list=workOvertimeMapper.getWorkOvertimePeopleApprovePage(page,startTime,endTime,departmentName,overtimeType,tenantId,user_code); return page.setRecords(list); } /** * @Description 两个Double数相加 * * @param d1 * @param d2 * @return Double */ public static Double add(Double d1,Double d2){ BigDecimal b1 = new BigDecimal(d1.toString()); BigDecimal b2 = new BigDecimal(d2.toString()); return b1.add(b2).doubleValue(); } /** * 保存加班人员 */ public void savePeople(List<WorkOvertimePeople>list,WorkOvertime bean){ log.info("保存加班人员bean=============="+bean+"-----list======"+list); if(null!=list && list.size()>0){ for (WorkOvertimePeople workOvertimePeople : list){ //加班时长 workOvertimePeople.setDuration(bean.getDuration()); workOvertimePeople.setStartTime(bean.getStartTime()); workOvertimePeople.setEndTime(bean.getEndTime()); workOvertimePeople.setId(bean.getId()); workOvertimePeople.setWorkOvertimeNumber(bean.getWorkOvertimeNumber()); workOvertimePeople.setOvertimeReason(bean.getOvertimeReason()); workOvertimePeople.setCreateTime(bean.getCreateTime()); workOvertimePeople.setOvertimeType(bean.getOvertimeType()); workOvertimePeopleServiceImpl.save(workOvertimePeople); } } } }