package com.hotwj.platform.config.hiddenDanger.strategy;

import com.hotwj.platform.flow.domain.SysFlowInstance;
import com.hotwj.platform.flow.domain.dto.FlowNextDto;
import com.hotwj.platform.flow.strategy.IFlowStrategy;
import com.hotwj.platform.securityManagement.hiddenDanger.domain.HotHiddenDanger;
import com.hotwj.platform.securityManagement.hiddenDanger.mapper.HotHiddenDangerMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;

/**
 * 隐患治理流程策略
 *
 * @author shihongwei
 * @date 2026-01-25
 */
@Component
public class HiddenDangerGovernanceStrategy implements IFlowStrategy {

    @Resource
    private HotHiddenDangerMapper mapper;

    @Override
    public String getFlowCode() {
        return "HIDDEN_DANGER_GOVERNANCE";
    }

    @Override
    public String getFlowName() {
        return "隐患治理";
    }

    @Override
    public FlowNextDto next(SysFlowInstance instance, String currentNodeCode) {
        // 简单流程：只有一级审批，审批通过即结束
        return getNextNode(instance.getBusinessId(), currentNodeCode, false);
    }

    @Override
    public FlowNextDto reject(SysFlowInstance instance, String currentNodeCode) {
        // 如果是复查不通过，退回到治理
        HotHiddenDanger danger = getHiddenDanger(instance.getBusinessId());
        String sourceName = danger != null ? danger.getSourceName() : "未知来源";

        if ("NODE_GOVERNANCE_AUDIT_FC".equals(currentNodeCode)) {
            // 回退给治理人
            String approverId = danger != null && danger.getGovernPersonId() != null ? String.valueOf(danger.getGovernPersonId()) : null;
            return new FlowNextDto("NODE_GOVERNANCE_ZL", String.format("（%s）%s", sourceName, "治理"), approverId);
        } else {
            return null;
        }
    }

    @Override
    public FlowNextDto getInitialNode(String businessId) {
        // 根据不同类型返回不同的名称
        return getNextNode(businessId, null, true);
    }


    private FlowNextDto getNextNode(String businessId, String currentNodeCode, boolean initialNode) {
        HotHiddenDanger danger = getHiddenDanger(businessId);
        String sourceName = danger != null ? danger.getSourceName() : "未知来源";

        // 初始节点
        if (initialNode) {
            return new FlowNextDto("NODE_GOVERNANCE_AUDIT_PG",
                    String.format("（%s）评估", sourceName), null);
        }

        // 流程流转：评估 -> 治理 -> 复查
        String nodeCode = null;
        String state = null;
        String approverId = null;

        if ("NODE_GOVERNANCE_AUDIT_PG".equals(currentNodeCode)) {
            nodeCode = "NODE_GOVERNANCE_ZL";
            state = "治理";
            // 待办给治理人
            if (danger != null && danger.getGovernPersonId() != null) {
                approverId = String.valueOf(danger.getGovernPersonId());
            }
        } else if ("NODE_GOVERNANCE_ZL".equals(currentNodeCode)) {
            nodeCode = "NODE_GOVERNANCE_AUDIT_FC";
            state = "复查";
            // 待办给复查人
            if (danger != null && danger.getReviewerId() != null) {
                approverId = String.valueOf(danger.getReviewerId());
            }
        } else if ("NODE_GOVERNANCE_AUDIT_FC".equals(currentNodeCode)) {
            // 流程结束
            return null;
        }

        if (nodeCode != null) {
            return new FlowNextDto(nodeCode,
                    String.format("（%s）%s", sourceName, state), approverId);
        }

        return null;
    }

    /**
     * 获取隐患对象
     */
    private HotHiddenDanger getHiddenDanger(String businessId) {
        if (businessId == null) {
            return null;
        }
        try {
            Long id = Long.parseLong(businessId);
            return mapper.selectById(id);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 获取来源名称 (安全获取)
     */
    private String getSourceName(String businessId) {
        HotHiddenDanger danger = getHiddenDanger(businessId);
        return danger != null && danger.getSourceName() != null ? danger.getSourceName() : "未知来源";
    }
}
