|
|
|
@ -15,6 +15,7 @@ import org.dromara.common.core.domain.R;
|
|
|
|
|
import org.dromara.common.core.utils.MapstructUtils;
|
|
|
|
|
import org.dromara.common.core.utils.ObjectUtils;
|
|
|
|
|
import org.dromara.common.core.utils.uuid.Seq;
|
|
|
|
|
import org.dromara.common.tenant.helper.TenantHelper;
|
|
|
|
|
import org.dromara.dms.domain.*;
|
|
|
|
|
import org.dromara.dms.domain.bo.*;
|
|
|
|
|
import org.dromara.dms.domain.vo.*;
|
|
|
|
@ -40,7 +41,7 @@ import java.util.concurrent.CompletableFuture;
|
|
|
|
|
@DubboService
|
|
|
|
|
@Slf4j
|
|
|
|
|
public class RemoteLubeInstanceServiceImpl implements RemoteLubeInstanceService {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private final DmsBillsLubeInstanceMapper dmsBillsLubeInstanceMapper;
|
|
|
|
|
|
|
|
|
|
private final DmsBillsLubeDetailMapper dmsBillsLubeDetailMapper;
|
|
|
|
@ -68,7 +69,10 @@ public class RemoteLubeInstanceServiceImpl implements RemoteLubeInstanceService
|
|
|
|
|
public R<Void> insertLubeInstance(String planLubeCode, String tenantId, Long userId) {
|
|
|
|
|
// 【重要】在主线程中先登录,确保后续所有调用都有认证上下文
|
|
|
|
|
StpUtil.login(userId, "login");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 【关键修复】设置租户上下文,确保工作流能获取到正确的租户ID
|
|
|
|
|
TenantHelper.setDynamic(tenantId);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
R<Long> RLubeStanceId = LubeStance(planLubeCode, tenantId, userId);
|
|
|
|
|
Long LubeStanceId = RLubeStanceId.getData();
|
|
|
|
@ -79,6 +83,10 @@ public class RemoteLubeInstanceServiceImpl implements RemoteLubeInstanceService
|
|
|
|
|
RemoteStartProcess remoteStartProcess = new RemoteStartProcess();
|
|
|
|
|
remoteStartProcess.setBusinessId(businessId);
|
|
|
|
|
remoteStartProcess.setFlowCode(DmsConstants.DMS_BILLS_LUBE_INSTANCE_WF_CODE);
|
|
|
|
|
|
|
|
|
|
// 【重要】通过Dubbo隐式参数传递租户ID
|
|
|
|
|
RpcContext.getContext().setAttachment("tenantId", tenantId);
|
|
|
|
|
|
|
|
|
|
RemoteStartProcessReturn result = remoteWorkflowService.startWorkFlow(remoteStartProcess);
|
|
|
|
|
//TODO:赋值给工单的wfid
|
|
|
|
|
if(ObjectUtils.isEmpty(result)){
|
|
|
|
@ -91,20 +99,30 @@ public class RemoteLubeInstanceServiceImpl implements RemoteLubeInstanceService
|
|
|
|
|
// 【关键】在异步线程中重新登录相同用户
|
|
|
|
|
// 这样可以确保生成有效的Token并自动设置到Sa-Token上下文中
|
|
|
|
|
StpUtil.login(userId, "login");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 【关键修复】在异步线程中重新设置租户上下文
|
|
|
|
|
TenantHelper.setDynamic(tenantId);
|
|
|
|
|
|
|
|
|
|
log.info("异步线程中,当前租户ID: {}", TenantHelper.getTenantId());
|
|
|
|
|
|
|
|
|
|
// 【执行业务】更新润滑工单的工作流信息
|
|
|
|
|
updateWarmFlow(LubeStanceId, taskId);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
log.info("润滑工单 {} 异步工作流处理完成", LubeStanceId);
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
log.error("润滑工单 {} 异步工作流处理失败: {}", LubeStanceId, e.getMessage(), e);
|
|
|
|
|
} finally {
|
|
|
|
|
// 【清理】清理登录状态,避免线程池复用时的状态污染
|
|
|
|
|
// 【清理】清理登录状态和租户上下文,避免线程池复用时的状态污染
|
|
|
|
|
try {
|
|
|
|
|
StpUtil.logout();
|
|
|
|
|
} catch (Exception ignored) {
|
|
|
|
|
// 忽略登出时的异常
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
TenantHelper.clearDynamic();
|
|
|
|
|
} catch (Exception ignored) {
|
|
|
|
|
// 忽略清理时的异常
|
|
|
|
|
}
|
|
|
|
|
// 清理RpcContext
|
|
|
|
|
try {
|
|
|
|
|
RpcContext.removeContext();
|
|
|
|
@ -117,13 +135,18 @@ public class RemoteLubeInstanceServiceImpl implements RemoteLubeInstanceService
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} finally {
|
|
|
|
|
// 【主线程清理】确保主线程执行完成后清理登录状态
|
|
|
|
|
// 【主线程清理】确保主线程执行完成后清理登录状态和租户上下文
|
|
|
|
|
// 注意:这里不影响异步线程,因为每个线程都有自己的ThreadLocal
|
|
|
|
|
try {
|
|
|
|
|
StpUtil.logout();
|
|
|
|
|
} catch (Exception ignored) {
|
|
|
|
|
// 忽略登出异常
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
TenantHelper.clearDynamic();
|
|
|
|
|
} catch (Exception ignored) {
|
|
|
|
|
// 忽略清理异常
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -203,12 +226,26 @@ public class RemoteLubeInstanceServiceImpl implements RemoteLubeInstanceService
|
|
|
|
|
|
|
|
|
|
private void updateWarmFlow(Long businessId,Long taskId){
|
|
|
|
|
try {
|
|
|
|
|
// 【关键修复】确保在工作流操作前有正确的租户上下文
|
|
|
|
|
String currentTenantId = TenantHelper.getTenantId();
|
|
|
|
|
log.info("工作流更新前,当前租户ID: {}", currentTenantId);
|
|
|
|
|
|
|
|
|
|
// 1. 更新工作流ID
|
|
|
|
|
DmsBillsLubeInstanceVo dmsBillsLubeInstanceVo = dmsBillsLubeInstanceService.queryById(businessId);
|
|
|
|
|
if (dmsBillsLubeInstanceVo == null) {
|
|
|
|
|
log.error("润滑工单 {} 不存在,无法更新工作流ID", businessId);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DmsBillsLubeInstance billsLubeInstance = MapstructUtils.convert(dmsBillsLubeInstanceVo, DmsBillsLubeInstance.class);
|
|
|
|
|
billsLubeInstance.setWfDefinitionId(taskId);
|
|
|
|
|
dmsBillsLubeInstanceMapper.updateById(billsLubeInstance);
|
|
|
|
|
log.info("润滑工单 {} 工作流ID更新成功: {}", businessId, taskId);
|
|
|
|
|
int updateResult = dmsBillsLubeInstanceMapper.updateById(billsLubeInstance);
|
|
|
|
|
if (updateResult > 0) {
|
|
|
|
|
log.info("润滑工单 {} 工作流ID更新成功: {}", businessId, taskId);
|
|
|
|
|
} else {
|
|
|
|
|
log.error("润滑工单 {} 工作流ID更新失败", businessId);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 2. 推进工作流状态
|
|
|
|
|
RemoteCompleteTask completeTask = new RemoteCompleteTask();
|
|
|
|
@ -218,11 +255,21 @@ public class RemoteLubeInstanceServiceImpl implements RemoteLubeInstanceService
|
|
|
|
|
List<String> messageTypes = new ArrayList<>();
|
|
|
|
|
messageTypes.add("system"); // 系统消息
|
|
|
|
|
completeTask.setMessageType(messageTypes);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 【关键修复】在调用工作流推进前再次确认租户上下文
|
|
|
|
|
log.info("工作流推进前,当前租户ID: {}", TenantHelper.getTenantId());
|
|
|
|
|
|
|
|
|
|
// 【重要】通过Dubbo隐式参数传递租户ID
|
|
|
|
|
RpcContext.getContext().setAttachment("tenantId", currentTenantId);
|
|
|
|
|
|
|
|
|
|
// 【关键】调用工作流推进,可能会因为Token问题失败
|
|
|
|
|
remoteWorkflowService.completeTask(completeTask);
|
|
|
|
|
log.info("润滑工单 {} 工作流推进成功", businessId);
|
|
|
|
|
|
|
|
|
|
boolean completeResult = remoteWorkflowService.completeTask(completeTask);
|
|
|
|
|
if (completeResult) {
|
|
|
|
|
log.info("润滑工单 {} 工作流推进成功", businessId);
|
|
|
|
|
} else {
|
|
|
|
|
log.warn("润滑工单 {} 工作流推进失败,但工单已创建成功", businessId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
log.error("润滑工单 {} 工作流更新失败: {}", businessId, e.getMessage(), e);
|
|
|
|
|
// 注意:这里不抛出异常,避免影响主流程
|
|
|
|
|