package com.fs.company.service.impl.call.node;

import com.fs.common.utils.spring.SpringUtils;
import com.fs.company.domain.CompanyAiWorkflowExecLog;
import com.fs.company.domain.CompanyVoiceRobotic;
import com.fs.company.domain.CompanyVoiceRoboticBusiness;
import com.fs.company.mapper.CompanyWorkflowNodeMapper;
import com.fs.company.param.ExecutionContext;
import com.fs.company.vo.ExecutionResult;
import com.fs.enums.ExecutionStatusEnum;
import com.fs.enums.NodeTypeEnum;
import com.fs.enums.TaskTypeEnum;

import java.util.Map;

/**
 * @author MixLiu
 * @date 2026/1/28 10:59
 * @description 开始节点
 */
public class EndNode extends AbstractWorkflowNode {
    private static final CompanyWorkflowNodeMapper companyWorkflowNodeMapper = SpringUtils.getBean(CompanyWorkflowNodeMapper.class);
    public EndNode(String nodeKey, String nodeName, Map<String, Object> properties) {
        super(nodeKey, nodeName, properties);
    }

    @Override
    protected ExecutionResult doContinue(ExecutionContext context) {
        return null;
    }

    @Override
    public ExecutionResult doExecute(ExecutionContext context) {
        return ExecutionResult.success().nextNodeKey(null).outputData(null).build();
    }

    @Override
    public NodeTypeEnum getType() {
        return NodeTypeEnum.END;
    }

    /**
     * 执行后的后处理
     */
    @Override
    protected void postExecute(ExecutionContext context, ExecutionResult result) {
      super.postExecute(context, result);
      super.updateWorkflowStatus(context.getWorkflowInstanceId(), ExecutionStatusEnum.SUCCESS);
      //判定是否任务完成了更新任务的状态为执行完成
        CompanyVoiceRoboticBusiness roboticBusiness = getRoboticBusiness(context.getWorkflowInstanceId());
        if(null != roboticBusiness){
            Integer i = companyVoiceRoboticBusinessMapper.selectUnfinishedTaskCountByRoboticId(roboticBusiness.getRoboticId(), nodeKey);
            if(Integer.valueOf(0).equals(i)){
                CompanyVoiceRobotic robotic = new CompanyVoiceRobotic();
                if(robotic.getTaskType().equals(TaskTypeEnum.ORDINARY.getValue())){
                    robotic.setId(roboticBusiness.getRoboticId());
                    robotic.setTaskStatus(3);
                    companyVoiceRoboticMapper.updateById(robotic);
                }
            }
        }
    }
}
