[Fix-17579][Workflow] Fix view variable error after setting startup parameters for workflow instance (#17583) (#17594)

(cherry picked from commit 0862515eee0af4feefb6d5dd24a98980c242e05d)

Co-authored-by: huangsheng <huangshengtx@163.com>
diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/WorkflowInstanceServiceImpl.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/WorkflowInstanceServiceImpl.java
index 1b3a63f..ecc9c71 100644
--- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/WorkflowInstanceServiceImpl.java
+++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/WorkflowInstanceServiceImpl.java
@@ -25,6 +25,7 @@
 import static org.apache.dolphinscheduler.common.constants.Constants.LOCAL_PARAMS;
 import static org.apache.dolphinscheduler.common.constants.Constants.TASK_LIST;
 import static org.apache.dolphinscheduler.common.constants.Constants.WORKFLOW_INSTANCE_STATE;
+import static org.apache.dolphinscheduler.common.utils.JSONUtils.parseObject;
 import static org.apache.dolphinscheduler.plugin.task.api.TaskPluginManager.checkTaskParameters;
 
 import org.apache.dolphinscheduler.api.constants.ApiFuncIdentificationConstant;
@@ -79,6 +80,7 @@
 import org.apache.dolphinscheduler.dao.repository.WorkflowInstanceDao;
 import org.apache.dolphinscheduler.dao.repository.WorkflowInstanceMapDao;
 import org.apache.dolphinscheduler.dao.utils.WorkflowUtils;
+import org.apache.dolphinscheduler.extract.master.command.ICommandParam;
 import org.apache.dolphinscheduler.plugin.task.api.model.Property;
 import org.apache.dolphinscheduler.plugin.task.api.utils.ParameterUtils;
 import org.apache.dolphinscheduler.plugin.task.api.utils.TaskTypeUtils;
@@ -663,13 +665,12 @@
             return result;
         }
 
-        //
-        Map<String, String> commandParamMap = JSONUtils.toMap(workflowInstance.getCommandParam());
-        String timezoneId = null;
-        if (commandParamMap == null || StringUtils.isBlank(commandParamMap.get(Constants.SCHEDULE_TIMEZONE))) {
+        String timezoneId;
+        final ICommandParam commandParam = parseObject(workflowInstance.getCommandParam(), ICommandParam.class);
+        if (commandParam == null || StringUtils.isEmpty(commandParam.getTimeZone())) {
             timezoneId = loginUser.getTimeZone();
         } else {
-            timezoneId = commandParamMap.get(Constants.SCHEDULE_TIMEZONE);
+            timezoneId = commandParam.getTimeZone();
         }
 
         setWorkflowInstance(workflowInstance, scheduleTime, globalParams, timeout, timezoneId);
@@ -873,11 +874,12 @@
             return result;
         }
 
-        Map<String, String> commandParam = JSONUtils.toMap(workflowInstance.getCommandParam());
         String timezone = null;
-        if (commandParam != null) {
-            timezone = commandParam.get(Constants.SCHEDULE_TIMEZONE);
+        final ICommandParam commandParam = parseObject(workflowInstance.getCommandParam(), ICommandParam.class);
+        if (commandParam != null && StringUtils.isNotEmpty(commandParam.getTimeZone())) {
+            timezone = commandParam.getTimeZone();
         }
+
         Map<String, String> timeParams = BusinessTimeUtils
                 .getBusinessTime(workflowInstance.getCmdTypeIfComplement(),
                         workflowInstance.getScheduleTime(), timezone);
diff --git a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/WorkflowInstanceServiceTest.java b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/WorkflowInstanceServiceTest.java
index e5d953a..138de28 100644
--- a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/WorkflowInstanceServiceTest.java
+++ b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/WorkflowInstanceServiceTest.java
@@ -67,9 +67,13 @@
 import org.apache.dolphinscheduler.dao.repository.TaskInstanceDao;
 import org.apache.dolphinscheduler.dao.repository.WorkflowInstanceDao;
 import org.apache.dolphinscheduler.dao.repository.WorkflowInstanceMapDao;
+import org.apache.dolphinscheduler.extract.master.command.RunWorkflowCommandParam;
 import org.apache.dolphinscheduler.plugin.task.api.TaskPluginManager;
+import org.apache.dolphinscheduler.plugin.task.api.enums.DataType;
 import org.apache.dolphinscheduler.plugin.task.api.enums.DependResult;
+import org.apache.dolphinscheduler.plugin.task.api.enums.Direct;
 import org.apache.dolphinscheduler.plugin.task.api.enums.TaskExecutionStatus;
+import org.apache.dolphinscheduler.plugin.task.api.model.Property;
 import org.apache.dolphinscheduler.service.expand.CuringParamsService;
 import org.apache.dolphinscheduler.service.model.TaskNode;
 import org.apache.dolphinscheduler.service.process.ProcessService;
@@ -643,6 +647,21 @@
                             taskRelationJson, taskDefinitionJson, "2020-02-21 00:00:00", true, "", "", 0);
             Assertions.assertEquals(Status.SUCCESS, processInstanceFinishRes.get(Constants.STATUS));
 
+            final RunWorkflowCommandParam runWorkflowCommandParam = RunWorkflowCommandParam.builder()
+                    .commandParams(Lists.newArrayList(Property.builder()
+                            .prop("name")
+                            .direct(Direct.IN)
+                            .type(DataType.VARCHAR)
+                            .value("commandParam")
+                            .build()))
+                    .timeZone("Asia/Shanghai")
+                    .build();
+            workflowInstance.setCommandParam(JSONUtils.toJsonString(runWorkflowCommandParam));
+            Map<String, Object> processInstanceFinishRes2 =
+                    workflowInstanceService.updateWorkflowInstance(loginUser, projectCode, 1,
+                            taskRelationJson, taskDefinitionJson, "2020-02-21 00:00:00", true, "", "", 0);
+            Assertions.assertEquals(Status.SUCCESS, processInstanceFinishRes2.get(Constants.STATUS));
+
             // success
             when(workflowDefinitionMapper.queryByCode(46L)).thenReturn(workflowDefinition);
             putMsg(result, Status.SUCCESS, projectCode);
@@ -798,6 +817,42 @@
         Assertions.assertEquals(Status.WORKFLOW_INSTANCE_NOT_EXIST, processNotExist.get(Constants.STATUS));
     }
 
+    @Test
+    public void testViewVariablesWithStartingParam() {
+        final RunWorkflowCommandParam runWorkflowCommandParam = RunWorkflowCommandParam.builder()
+                .commandParams(Lists.newArrayList(Property.builder()
+                        .prop("name")
+                        .direct(Direct.IN)
+                        .type(DataType.VARCHAR)
+                        .value("commandParam")
+                        .build()))
+                .timeZone("Asia/Shanghai")
+                .build();
+        WorkflowInstance workflowInstance = getProcessInstance();
+        workflowInstance.setCommandType(CommandType.SCHEDULER);
+        workflowInstance.setScheduleTime(new Date());
+        workflowInstance.setCommandParam(JSONUtils.toJsonString(runWorkflowCommandParam));
+
+        when(workflowInstanceMapper.queryDetailById(1)).thenReturn(workflowInstance);
+        Map<String, Object> successRes = workflowInstanceService.viewVariables(1L, 1);
+        Assertions.assertEquals(Status.SUCCESS, successRes.get(Constants.STATUS));
+
+        final RunWorkflowCommandParam commandParamWithEmptyTimeZone = RunWorkflowCommandParam.builder()
+                .commandParams(Lists.newArrayList(Property.builder()
+                        .prop("name")
+                        .direct(Direct.IN)
+                        .type(DataType.VARCHAR)
+                        .value("commandParam")
+                        .build()))
+                .build();
+        workflowInstance.setCommandType(CommandType.SCHEDULER);
+        workflowInstance.setScheduleTime(new Date());
+        workflowInstance.setCommandParam(JSONUtils.toJsonString(commandParamWithEmptyTimeZone));
+        Map<String, Object> successRes2 = workflowInstanceService.viewVariables(1L, 1);
+        Assertions.assertEquals(Status.SUCCESS, successRes2.get(Constants.STATUS));
+
+    }
+
     /**
      * get Mock Admin User
      *