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) {
        if ("NODE_CHECK_EDIT".equals(currentNodeCode)) {
            return buildAuditNode(instance.getBusinessId());
        }
        // 审核通过即结束
        return null;
    }

    @Override
    public FlowNextDto reject(SysFlowInstance instance, String currentNodeCode) {
        if (!"NODE_CHECK_AUDIT".equals(currentNodeCode)) {
            return null;
        }
        InspectionNodeMeta meta = getInspectionNodeMeta(instance.getBusinessId());
        String editApproverId = meta.inspection != null && meta.inspection.getCheckerId() != null
                ? meta.inspection.getCheckerId() : instance.getInitiatorId();
        return buildEditNode(instance.getBusinessId(), editApproverId);
    }

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

    private FlowNextDto buildAuditNode(String id) {
        InspectionNodeMeta meta = getInspectionNodeMeta(id);
        String approverId = meta.inspection != null && meta.inspection.getApproverId() != null
                ? String.valueOf(meta.inspection.getApproverId()) : null;
        String nodeName = String.format("（%s）（%s）审核", meta.planName, meta.inspectItemName);
        return new FlowNextDto("NODE_CHECK_AUDIT", nodeName, approverId);
    }

    private FlowNextDto buildEditNode(String id, String approverId) {
        InspectionNodeMeta meta = getInspectionNodeMeta(id);
        String nodeName = String.format("（%s）（%s）处理", meta.planName, meta.inspectItemName);
        return new FlowNextDto("NODE_CHECK_EDIT", nodeName, approverId);
    }

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

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

        try {
            if (id != null) {
                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) {
            // 忽略异常，使用默认值
        }
        return new InspectionNodeMeta(inspection, inspectItemName, planName);
    }

    private record InspectionNodeMeta(HotHiddenDangerInspection inspection, String inspectItemName, String planName) {
    }
}
