package org.jeecg.modules.tms.service.impl; import cn.hutool.core.bean.BeanUtil; import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.date.DateUtil; import cn.hutool.core.util.StrUtil; import com.alibaba.fastjson.JSON; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import lombok.extern.slf4j.Slf4j; import org.apache.shiro.SecurityUtils; import org.apache.shiro.subject.Subject; import org.flowable.engine.TaskService; import org.flowable.task.api.Task; import org.jeecg.common.api.vo.Result; import org.jeecg.common.exception.JeecgBootException; import org.jeecg.common.system.vo.LoginUser; import org.jeecg.modules.flowable.apithird.business.entity.FlowMyBusiness; import org.jeecg.modules.flowable.apithird.business.service.IFlowMyBusinessService; import org.jeecg.modules.flowable.apithird.business.service.impl.FlowMyBusinessServiceImpl; import org.jeecg.modules.flowable.apithird.service.FlowCallBackServiceI; import org.jeecg.modules.flowable.apithird.service.FlowCommonService; import org.jeecg.modules.flowable.service.IFlowDefinitionService; import org.jeecg.modules.flowable.service.IFlowTaskService; import org.jeecg.modules.system.service.ISysBusinessCodeRuleService; import org.jeecg.modules.tms.entity.OutboundOrder; import org.jeecg.modules.tms.entity.OutboundDetail; import org.jeecg.modules.tms.entity.dto.OutBoundOrderFlowDto; import org.jeecg.modules.tms.entity.dto.OutboundOrderAndDetailDto; import org.jeecg.modules.tms.enums.OutBillStatus; import org.jeecg.modules.tms.enums.OutBoundStatusEnum; import org.jeecg.modules.tms.mapper.OutboundDetailMapper; import org.jeecg.modules.tms.mapper.OutboundOrderMapper; import org.jeecg.modules.tms.service.IOutboundDetailService; import org.jeecg.modules.tms.service.IOutboundOrderService; import org.springframework.stereotype.Service; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.transaction.annotation.Transactional; import java.io.Serializable; import java.util.*; /** * @Description: tms_outbound_order * @Author: jeecg-boot * @Date: 2025-05-16 * @Version: V1.0 */ @Slf4j @Service public class OutboundOrderServiceImpl extends ServiceImpl implements IOutboundOrderService, FlowCallBackServiceI { @Autowired private IOutboundDetailService outboundDetailService; @Autowired private ISysBusinessCodeRuleService businessCodeRuleService; @Autowired private IFlowDefinitionService flowDefinitionService; @Autowired private IFlowMyBusinessService flowMyBusinessService; @Autowired private IFlowTaskService flowTaskService; @Autowired private TaskService taskService; @Autowired private FlowCommonService flowCommonService; @Autowired private OutboundOrderMapper outboundOrderMapper; @Autowired private OutboundDetailMapper outboundDetailMapper; @Override @Transactional(rollbackFor = Exception.class) public void delMain(String id) { outboundDetailMapper.deleteByMainId(id); outboundOrderMapper.deleteById(id); } @Override @Transactional(rollbackFor = Exception.class) public void delBatchMain(Collection idList) { for(Serializable id:idList) { outboundDetailMapper.deleteByMainId(id.toString()); outboundOrderMapper.deleteById(id); } } @Override @Transactional(rollbackFor = Exception.class) public void addTotal(OutboundOrderAndDetailDto outboundOrder) { OutboundOrder order = BeanUtil.copyProperties(outboundOrder, OutboundOrder.class); order.setHandler(Objects.requireNonNull(getCurrentUser()).getId()); order.setOutNum(businessCodeRuleService.generateBusinessCodeSeq("outBoundOrder")); order.setOrderStatus(OutBillStatus.DRAFT.getValue()); save(order); List detailList = CollectionUtil.newArrayList(); outboundOrder.getOutboundDetailList().forEach(item->{ item.setId(null); item.setOutStorehouseId(order.getId()); detailList.add(item); }); outboundDetailService.saveBatch(detailList); } @Override public IPage queryPageList(Page page, Map parameterMap) { QueryWrapper queryWrapper = Wrappers.query(); String[] outNums = parameterMap.get("outNum"); if (outNums != null && outNums.length > 0) { queryWrapper.like("t.out_num", outNums[0]); } String[] outStorehouseTypes = parameterMap.get("outStorehouseType"); if (outStorehouseTypes != null && outStorehouseTypes.length > 0) { queryWrapper.eq("t.out_storehouse_type", outStorehouseTypes[0]); } String[] statuses = parameterMap.get("orderStatus"); if (statuses != null && statuses.length > 0) { queryWrapper.eq("t.order_status", statuses[0]); } String[] startTimes = parameterMap.get("startTime"); if (startTimes != null && startTimes.length > 0) { queryWrapper.ge("t.create_time", startTimes[0]); } String[] endTimes = parameterMap.get("endTime"); if (endTimes != null && endTimes.length > 0) { queryWrapper.le("t.create_time", endTimes[0]); } queryWrapper.orderByDesc("t.create_time"); return this.baseMapper.queryPageList(page, queryWrapper); } @Override @Transactional(rollbackFor = Exception.class) public void editTotal(OutboundOrderAndDetailDto outboundOrder) { //删除所有明细 outboundDetailService.remove(new LambdaQueryWrapper() .eq(OutboundDetail::getOutStorehouseId, outboundOrder.getId())); OutboundOrder outboundOrderUpdate = BeanUtil.copyProperties(outboundOrder, OutboundOrder.class); outboundOrderMapper.updateById(outboundOrderUpdate); List detailList = CollectionUtil.newArrayList(); outboundOrder.getOutboundDetailList().forEach(item->{ item.setOutStorehouseId(outboundOrderUpdate.getId()); detailList.add(item); }); outboundDetailService.saveBatch(detailList); } @Override public void submit(String id) { OutboundOrder outboundOrder = getById(id); if (outboundOrder == null) { throw new JeecgBootException("出库单申请单不存在,无法提交!"); } if (!Objects.equals(outboundOrder.getOrderStatus(), OutBillStatus.DRAFT.getValue())) { throw new JeecgBootException("无法提交非草稿状态的出库申请单!"); } //启动流程 if (triggerProcess(outboundOrder)) { outboundOrder.setOrderStatus(OutBillStatus.SUBMITTED.getValue()); } updateById(outboundOrder); } @Transactional(rollbackFor = Exception.class) @Override public void approvalProcess(OutBoundOrderFlowDto outBoundOrderFlowDto) { if (StrUtil.isBlank(outBoundOrderFlowDto.getTaskId()) || StrUtil.isBlank(outBoundOrderFlowDto.getDataId())) { throw new JeecgBootException("非法参数!"); } // 获取当前登录用户 LoginUser user = getCurrentUser(); if (user == null || StrUtil.isBlank(user.getId())) { throw new JeecgBootException("账号不存在"); } //获取出库申请单信息 OutboundOrder outboundOrder = getById(outBoundOrderFlowDto.getDataId()); if (outboundOrder == null) { throw new JeecgBootException("未找到对应的出库申请单!"); } //获取流程业务记录 FlowMyBusiness flowMyBusiness = getFlowMyBusiness(outBoundOrderFlowDto.getInstanceId()); if (flowMyBusiness == null) { throw new JeecgBootException("流程记录不存在"); } // 检查用户是否有权限操作任务 if (!isUserAuthorized(flowMyBusiness, user)) { throw new JeecgBootException("用户无权操作此任务"); } // 认领任务 if (!claimTask(flowMyBusiness.getTaskId(), user)) { throw new JeecgBootException("任务不存在、已完成或已被他人认领"); } //设置流程变量 setupProcessVariables(outBoundOrderFlowDto, outboundOrder, user); //完成流程任务 Result result = flowTaskService.complete(outBoundOrderFlowDto); //根据任务完成结果更新申请单状态 if (result.isSuccess()) { outboundOrder.setOrderStatus(outBoundOrderFlowDto.getStatus()); if (OutBillStatus.APPROVED.getValue().equals(outBoundOrderFlowDto.getStatus())) { outboundOrder.setOutStatus(OutBoundStatusEnum.NOT_OUTBOUND.getValue()); } outboundOrder.setAuditDate(new Date()); outboundOrder.setApprovalOpinion(outBoundOrderFlowDto.getApprovalOpinion()); updateById(outboundOrder); } } public boolean triggerProcess(OutboundOrder outboundOrder) { flowCommonService.initActBusiness("单号为:" + outboundOrder.getOutNum() + " 的出库申请,开始进行审批", outboundOrder.getId(), "outboundOrderServiceImpl", "tool_out_storage", null); Map variables = new HashMap<>(); variables.put("dataId", outboundOrder.getId()); if (StrUtil.isEmpty(outboundOrder.getRemark())) { variables.put("organization", "新增出库申请单默认启动流程"); variables.put("comment", "新增出库申请单默认启动流程"); } else { variables.put("organization", outboundOrder.getRemark()); variables.put("comment", outboundOrder.getRemark()); } variables.put("proofreading", true); List usernames = new ArrayList<>(); usernames.add(outboundOrder.getReviewer()); variables.put("NextAssignee", usernames); Result result = flowDefinitionService.startProcessInstanceByKey("tool_out_storage", variables); return result.isSuccess(); } private LoginUser getCurrentUser() { // 获取当前认证的登录用户信息 Subject currentUser = SecurityUtils.getSubject(); if (currentUser != null && currentUser.isAuthenticated()) { Object principal = currentUser.getPrincipal(); if (principal instanceof LoginUser) { return (LoginUser) principal; } } return null; } private FlowMyBusiness getFlowMyBusiness(String instanceId) { List businessList = flowMyBusinessService.list( new LambdaQueryWrapper().eq(FlowMyBusiness::getProcessInstanceId, instanceId)); return businessList.isEmpty() ? null : businessList.get(0); } private boolean isUserAuthorized(FlowMyBusiness flowMyBusiness, LoginUser user) { List todoUsers = JSON.parseArray(flowMyBusiness.getTodoUsers(), String.class); return todoUsers != null && todoUsers.contains(user.getUsername()); } private boolean claimTask(String taskId, LoginUser user) { Task task = taskService.createTaskQuery().taskId(taskId).singleResult(); if (task == null) { return false; } if (task.getAssignee() != null && !task.getAssignee().equals(user.getUsername())) { return false; } taskService.claim(taskId, user.getUsername()); return true; } private void setupProcessVariables(OutBoundOrderFlowDto outBoundOrderFlowDto, OutboundOrder outboundOrder, LoginUser user) { if (OutBillStatus.SUBMITTED.getValue().equals(outboundOrder.getOrderStatus()) && user.getUsername().equals(outboundOrder.getReviewer())) { Map values = new HashMap<>(); values.put("dataId", outboundOrder.getId()); values.put("organization", outBoundOrderFlowDto.getApprovalOpinion()); values.put("comment", outBoundOrderFlowDto.getApprovalOpinion()); values.put("status", outBoundOrderFlowDto.getStatus()); values.put("NextAssignee", Collections.singletonList(outboundOrder.getReviewer())); outBoundOrderFlowDto.setValues(values); } } @Override public void afterFlowHandle(FlowMyBusiness business) { business.getTaskNameId();//接下来审批的节点 business.getValues();//前端传进来的参数 business.getActStatus(); } @Override public Object getBusinessDataById(String dataId) { return this.getById(dataId); } @Override public Map flowValuesOfTask(String taskNameId, Map values) { return Collections.emptyMap(); } @Override public List flowCandidateUsernamesOfTask(String taskNameId, Map values) { //业务是否干预流程,业务干预,流程干预,指定人员进行处理 //获取下一步处理人 Object object = values.get("NextAssignee"); return (List) object; } }