From 98e4c499221c0069af9632c4fd08f096fb8006d6 Mon Sep 17 00:00:00 2001
From: cuilei <ray_tsu1@163.com>
Date: 星期五, 23 五月 2025 11:45:59 +0800
Subject: [PATCH] 工具管理-工具出库流程启动、审批

---
 lxzn-module-tms/src/main/java/org/jeecg/modules/tms/service/impl/OutboundOrderServiceImpl.java |  187 +++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 181 insertions(+), 6 deletions(-)

diff --git a/lxzn-module-tms/src/main/java/org/jeecg/modules/tms/service/impl/OutboundOrderServiceImpl.java b/lxzn-module-tms/src/main/java/org/jeecg/modules/tms/service/impl/OutboundOrderServiceImpl.java
index 70bb424..d0f5668 100644
--- a/lxzn-module-tms/src/main/java/org/jeecg/modules/tms/service/impl/OutboundOrderServiceImpl.java
+++ b/lxzn-module-tms/src/main/java/org/jeecg/modules/tms/service/impl/OutboundOrderServiceImpl.java
@@ -2,19 +2,36 @@
 
 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;
@@ -24,10 +41,7 @@
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.transaction.annotation.Transactional;
 import java.io.Serializable;
-import java.util.List;
-import java.util.Collection;
-import java.util.Map;
-import java.util.Objects;
+import java.util.*;
 
 /**
  * @Description: tms_outbound_order
@@ -35,13 +49,24 @@
  * @Date:   2025-05-16
  * @Version: V1.0
  */
+@Slf4j
 @Service
-public class OutboundOrderServiceImpl extends ServiceImpl<OutboundOrderMapper, OutboundOrder> implements IOutboundOrderService {
+public class OutboundOrderServiceImpl extends ServiceImpl<OutboundOrderMapper, OutboundOrder> 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
@@ -73,6 +98,7 @@
 		save(order);
 		List<OutboundDetail> detailList = CollectionUtil.newArrayList();
 		outboundOrder.getOutboundDetailList().forEach(item->{
+			item.setId(null);
 			item.setOutStorehouseId(order.getId());
 			detailList.add(item);
 		});
@@ -122,7 +148,96 @@
 		outboundDetailService.saveBatch(detailList);
 	}
 
-	private LoginUser getCurrentUser() {
+    @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<String, Object> 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<String> 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()) {
@@ -133,4 +248,64 @@
 		}
 		return null;
 	}
+
+	private FlowMyBusiness getFlowMyBusiness(String instanceId) {
+		List<FlowMyBusiness> businessList = flowMyBusinessService.list(
+				new LambdaQueryWrapper<FlowMyBusiness>().eq(FlowMyBusiness::getProcessInstanceId, instanceId));
+		return businessList.isEmpty() ? null : businessList.get(0);
+	}
+
+	private boolean isUserAuthorized(FlowMyBusiness flowMyBusiness, LoginUser user) {
+		List<String> 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<String, Object> 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<String, Object> flowValuesOfTask(String taskNameId, Map<String, Object> values) {
+		return Collections.emptyMap();
+	}
+
+	@Override
+	public List<String> flowCandidateUsernamesOfTask(String taskNameId, Map<String, Object> values) {
+		//涓氬姟鏄惁骞查娴佺▼锛屼笟鍔″共棰勶紝娴佺▼骞查锛屾寚瀹氫汉鍛樿繘琛屽鐞�
+		//鑾峰彇涓嬩竴姝ュ鐞嗕汉
+		Object object = values.get("NextAssignee");
+		return (List<String>) object;
+	}
 }

--
Gitblit v1.9.3