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.hiddenDangerInspection.domain.HotHiddenDangerInspection;
import com.hotwj.platform.securityManagement.hiddenDangerInspection.mapper.HotHiddenDangerInspectionMapper;
import com.hotwj.platform.securityManagement.hiddenDangerPlan.domain.HotHiddenDangerPlan;
import com.hotwj.platform.securityManagement.hiddenDangerPlan.mapper.HotHiddenDangerPlanMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;

/**
 * 隐患排查流程策略
 *
 * @author shihongwei
 * @date 2026-01-25
 */
@Component
public class HiddenDangerCheckStrategy implements IFlowStrategy {

    @Resource
    private HotHiddenDangerInspectionMapper inspectMapper;

    @Resource
    private HotHiddenDangerPlanMapper planMapper;

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

    @Override
    public String getFlowName() {
        return "隐患排查";
    }

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


    @Override
    public FlowNextDto getInitialNode(String businessId) {
        return getNextNode(businessId);
    }

    private FlowNextDto getNextNode(String id) {
        // 待办名称规则：（排查计划名称）（排查项名称）审核

        String inspectItemName = "未知排查项";
        String planName = "未知计划";

        try {
            if (id != null) {
                HotHiddenDangerInspection inspection = inspectMapper.selectById(id);
                if (inspection != null) {
                    // 处理排查项名称
                    if (inspection.getProjectName() != null) {
                        if (Long.valueOf(3L).equals(inspection.getProjectType())) {
                            // 刘浩然(513019199308195132)
                            // 人员特殊处理，保护隐私 ，支持中英文括号
                            inspectItemName = inspection.getProjectName().replaceAll("[\\(（].*?[\\)）]", "");
                        } else {
                            inspectItemName = inspection.getProjectName();
                        }
                    }

                    // 处理计划名称
                    if (inspection.getPlanId() != null) {
                        HotHiddenDangerPlan plan = planMapper.selectById(inspection.getPlanId());
                        if (plan != null && plan.getPlanName() != null) {
                            planName = plan.getPlanName();
                        }
                    }
                }
            }
        } catch (Exception e) {
            // 忽略异常，使用默认值
        }

        String nodeName = String.format("（%s）（%s）审核", planName, inspectItemName);
        return new FlowNextDto("NODE_CHECK_AUDIT", nodeName, null);
    }
}
