package com.ruoyi.oa.enums;

import lombok.Getter;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 数据库表名映射枚举
 * 用于将中文描述映射到对应的数据库表名
 *
 * @author ruoyi
 * @date 2024-12-19
 */
@Getter
public enum TableMappingEnum {

    // 员工相关表
    EMPLOYEE_FILES("employee_files", "员工档案表", new String[]{"员工文件", "档案"}),
    EMPLOYEE_OFFBOARDING("employee_offboarding", "员工离职表", new String[]{"离职", "员工离职"}),
    EMPLOYEE_ONBOARDING("employee_onboarding", "员工入职表", new String[]{"入职", "员工入职"}),

    // 文章相关表
    EXPORT_ARTICLE("export_article", "文章表", new String[]{"文章", "内容"}),
    EXPORT_ARTICLE_CATEGORY("export_article_category", "文章分类表", new String[]{"文章分类", "分类"}),
    EXPORT_CAROUSEL("export_carousel", "轮播图表", new String[]{"轮播图", "轮播"}),
    EXPORT_CATEGORY("export_category", "分类表", new String[]{"分类", "类别"}),
    EXPORT_CONTACT("export_contact", "联系方式表", new String[]{"联系方式", "联系"}),
    EXPORT_ITEM("export_item", "展示品表", new String[]{"展示品", "展示"}),
    EXPORT_LANGUAGE("export_language", "语言管理表", new String[]{"语言", "多语言"}),

    // OA应用相关表
    OA_APPLICATION("oa_application", "应用集成表", new String[]{"应用", "集成"}),
    OA_ATTENDANCE_RECORD("oa_attendance_record", "考勤记录表", new String[]{"考勤", "打卡", "出勤"}),
    OA_BINDING_ITEM_DETAIL("oa_binding_item_detail", "绑定记录明细表", new String[]{"绑定", "明细"}),
    OA_BUSINESS("oa_business", "CRM商机表", new String[]{"商机", "业务", "CRM"}),
    OA_BUSINESS_PRODUCT("oa_business_product", "CRM商机产品关联表", new String[]{"商机产品", "产品关联"}),
    OA_CLUE("oa_clue", "CRM线索表", new String[]{"线索", "客户线索"}),
    OA_CUSTOMER("oa_customer", "CRM客户表", new String[]{"客户", "客户信息"}),
    OA_EMAIL_ACCOUNT("oa_email_account", "发件人邮箱账号管理", new String[]{"邮箱", "邮件账号"}),
    OA_EMAIL_TEMPLATE("oa_email_template", "邮件模板表", new String[]{"邮件模板", "模板"}),
    OA_EMPLOYEE("oa_employee", "员工基础信息", new String[]{"员工", "员工信息"}),
    OA_EMPLOYEE_TEMPLATE_BINDING("oa_employee_template_binding", "员工模板绑定及月度发放记录表", new String[]{"员工模板", "绑定"}),
    OA_EXPRESS("oa_express", "快递表", new String[]{"快递", "物流"}),
    OA_EXPRESS_QUESTION("oa_express_question", "快递问题表", new String[]{"快递问题", "问题"}),
    OA_FEEDBACK("oa_feedback", "反馈表", new String[]{"反馈", "意见"}),
    OA_FEEDBACK_ITEM("oa_feedback_item", "反馈项目表", new String[]{"反馈项目", "反馈详情"}),
    OA_FOLLOW_UP_RECORD("oa_follow_up_record", "CRM跟进记录", new String[]{"跟进", "跟进记录"}),
    OA_FURNITURE_TABLE("oa_furniture_table", "存储家具相关业务数据", new String[]{"家具", "家具数据"}),
    OA_INSURANCE_TEMPLATE("oa_insurance_template", "社保公积金模板主表", new String[]{"社保", "公积金", "保险"}),
    OA_INSURANCE_TEMPLATE_DETAIL("oa_insurance_template_detail", "社保公积金模板明细表", new String[]{"社保明细", "公积金明细"}),
    OA_PAYMENT_PROGRESS("oa_payment_progress", "项目付款进度表", new String[]{"付款", "付款进度"}),
    OA_PRODUCT("oa_product", "CRM产品表", new String[]{"产品", "产品信息"}),
    OA_PROGRESS("oa_progress", "项目进度主表", new String[]{"项目进度", "进度"}),
    OA_PROGRESS_DETAIL("oa_progress_detail", "项目进度付款进度扩展表", new String[]{"进度详情", "付款详情"}),
    OA_PROJECT_REPORT("oa_project_report", "报工记录表", new String[]{"报工", "报工记录"}),
    OA_PROJECT_SCHEDULE("oa_project_schedule", "项目进度主表", new String[]{"项目排期", "排期"}),
    OA_PROJECT_SCHEDULE_STEP("oa_project_schedule_step", "项目进度步骤跟踪表", new String[]{"项目步骤", "步骤跟踪"}),
    OA_REPORT_DETAIL("oa_report_detail", "设计项目汇报详情表", new String[]{"汇报详情", "项目汇报"}),
    OA_REPORT_SCHEDULE("oa_report_schedule", "项目排产表", new String[]{"排产", "生产排期"}),
    OA_REPORT_SUMMARY("oa_report_summary", "设计项目汇报概述表", new String[]{"汇报概述", "项目概述"}),
    OA_REQUIREMENTS("oa_requirements", "OA需求表", new String[]{"需求", "OA需求"}),
    OA_SALARY("oa_salary", "薪水表", new String[]{"薪水", "工资", "薪资"}),
    OA_SALARY_ITEM("oa_salary_item", "薪水详情表", new String[]{"薪水详情", "工资详情"}),
    OA_SALARY_TEMPLATE("oa_salary_template", "薪资模板主表", new String[]{"薪资模板", "工资模板"}),
    OA_SALARY_TEMPLATE_DETAIL("oa_salary_template_detail", "薪资模板明细表", new String[]{"薪资模板明细", "工资模板明细"}),
    OA_SCHEDULE_TEMPLATE("oa_schedule_template", "进度模板主表", new String[]{"进度模板", "排期模板"}),
    OA_SCHEDULE_TEMPLATE_STEP("oa_schedule_template_step", "进度模板步骤表", new String[]{"进度模板步骤", "模板步骤"}),

    // 通信相关表
    SOCKET_CONTACT("socket_contact", "通信目录表", new String[]{"通信", "联系人"}),
    SOCKET_MESSAGE("socket_message", "对话信息表", new String[]{"对话", "消息"}),

    // 系统相关表
    SYS_CONFIG("sys_config", "参数配置表", new String[]{"配置", "系统配置"}),
    SYS_DEPT("sys_dept", "部门表", new String[]{"部门", "组织架构"}),
    SYS_DICT_DATA("sys_dict_data", "字典数据表", new String[]{"字典数据", "数据字典"}),
    SYS_DICT_TYPE("sys_dict_type", "字典类型表", new String[]{"字典类型", "字典"}),
    SYS_LOGININFOR("sys_logininfor", "系统访问记录", new String[]{"登录记录", "访问记录"}),
    SYS_MENU("sys_menu", "菜单权限表", new String[]{"菜单", "权限"}),
    SYS_NOTICE("sys_notice", "通知公告表", new String[]{"通知", "公告"}),
    SYS_OA_AI_CONFIG("sys_oa_ai_config", "AI配置表", new String[]{"AI配置", "人工智能配置"}),
    SYS_OA_AI_CONVERSATION("sys_oa_ai_conversation", "AI对话历史表", new String[]{"AI对话", "对话历史"}),
    SYS_OA_AI_MESSAGE("sys_oa_ai_message", "AI对话详情表", new String[]{"AI消息", "对话详情"}),
    SYS_OA_ARTICLE("sys_oa_article", "知识库表", new String[]{"知识库", "知识"}),
    SYS_OA_ATTENDANCE("sys_oa_attendance", "考勤表", new String[]{"考勤", "出勤"}),
    SYS_OA_BID("sys_oa_bid", "投标管理表", new String[]{"投标", "招标"}),
    SYS_OA_CATEGORY("sys_oa_category", "文章分类表", new String[]{"文章分类", "分类管理"}),
    SYS_OA_CLAIM("sys_oa_claim", "报销表", new String[]{"报销", "费用报销"}),
    SYS_OA_CLAIM_DETAIL("sys_oa_claim_detail", "报销明细表", new String[]{"报销明细", "费用明细"}),
    SYS_OA_CONTRACT("sys_oa_contract", "合同表", new String[]{"合同", "合同管理"}),
    SYS_OA_COST("sys_oa_cost", "成本表", new String[]{"成本", "费用"}),
    SYS_OA_DETAIL("sys_oa_detail", "进出账明细表", new String[]{"进出账明细", "账目明细"}),
    SYS_OA_FINANCE("sys_oa_finance", "进出账主表", new String[]{"进出账", "财务"}),
    SYS_OA_HOLIDAY("sys_oa_holiday", "节假日表", new String[]{"节假日", "假期"}),
    SYS_OA_PROJECT("sys_oa_project", "项目管理表", new String[]{"项目", "项目管理"}),
    SYS_OA_PURPOSE("sys_oa_purpose", "采购意向表", new String[]{"采购意向", "采购"}),
    SYS_OA_RECEIVE_ACCOUNT("sys_oa_receive_account", "收款账户表", new String[]{"收款账户", "账户"}),
    SYS_OA_REMIND("sys_oa_remind", "任务事件提醒表", new String[]{"提醒", "任务提醒"}),
    SYS_OA_TASK("sys_oa_task", "任务管理表", new String[]{"任务", "任务管理"}),
    SYS_OA_TASK_ITEM("sys_oa_task_item", "报工任务单元", new String[]{"任务单元", "报工单元"}),
    SYS_OA_TASK_USER("sys_oa_task_user", "任务用户表", new String[]{"任务用户", "用户任务"}),
    SYS_OA_WAREHOUSE("sys_oa_warehouse", "仓库表", new String[]{"仓库", "库存"}),
    SYS_OA_WAREHOUSE_DETAIL("sys_oa_warehouse_detail", "仓库明细表", new String[]{"仓库明细", "库存明细"}),
    SYS_OA_WAREHOUSE_LOG("sys_oa_warehouse_log", "仓库日志表", new String[]{"仓库日志", "库存日志"}),
    SYS_OA_WAREHOUSE_MASTER("sys_oa_warehouse_master", "仓库主表", new String[]{"仓库主表", "库存主表"}),
    SYS_OA_WAREHOUSE_TASK("sys_oa_warehouse_task", "采购计划表", new String[]{"采购计划", "采购任务"}),
    SYS_OA_WORK("sys_oa_work", "工作表", new String[]{"工作", "工作记录"}),
    SYS_OPER_LOG("sys_oper_log", "操作日志记录", new String[]{"操作日志", "日志"}),
    SYS_OSS("sys_oss", "OSS对象存储表", new String[]{"文件存储", "对象存储"}),
    SYS_OSS_ACL("sys_oss_acl", "OSS文件授权表", new String[]{"文件授权", "存储授权"}),
    SYS_OSS_CONFIG("sys_oss_config", "对象存储配置表", new String[]{"存储配置", "OSS配置"}),
    SYS_POST("sys_post", "岗位信息表", new String[]{"岗位", "职位"}),
    SYS_PREFIX_COUNTER("sys_prefix_counter", "前缀计数器表", new String[]{"计数器", "前缀"}),
    SYS_ROLE("sys_role", "角色信息表", new String[]{"角色", "权限角色"}),
    SYS_ROLE_DEPT("sys_role_dept", "角色和部门关联表", new String[]{"角色部门", "部门角色"}),
    SYS_ROLE_MENU("sys_role_menu", "角色和菜单关联表", new String[]{"角色菜单", "菜单角色"}),
    SYS_USER("sys_user", "用户信息表", new String[]{"用户", "用户信息"}),
    SYS_USER_POST("sys_user_post", "用户与岗位关联表", new String[]{"用户岗位", "岗位用户"}),
    SYS_USER_ROLE("sys_user_role", "用户和角色关联表", new String[]{"用户角色", "角色用户"}),

    // 工作流相关表
    WF_CATEGORY("wf_category", "流程分类表", new String[]{"流程分类", "工作流分类"}),
    WF_COPY("wf_copy", "流程抄送表", new String[]{"流程抄送", "抄送"}),
    WF_DEPLOY_FORM("wf_deploy_form", "流程实例关联表单", new String[]{"流程表单", "部署表单"}),
    WF_FORM("wf_form", "流程表单信息表", new String[]{"流程表单", "表单信息"});

    private final String tableName;
    private final String description;
    private final String[] keywords;

    TableMappingEnum(String tableName, String description, String[] keywords) {
        this.tableName = tableName;
        this.description = description;
        this.keywords = keywords;
    }

    /**
     * 根据关键词匹配表名
     *
     * @param keyword 关键词
     * @return 匹配的表名列表
     */
    public static List<String> getTableNamesByKeyword(String keyword) {
        if (keyword == null || keyword.trim().isEmpty()) {
            return new java.util.ArrayList<>();
        }

        String lowerKeyword = keyword.toLowerCase();
        return Arrays.stream(values())
                .filter(table -> {
                    // 检查表名是否包含关键词
                    if (table.getTableName().toLowerCase().contains(lowerKeyword)) {
                        return true;
                    }
                    // 检查描述是否包含关键词
                    if (table.getDescription().toLowerCase().contains(lowerKeyword)) {
                        return true;
                    }
                    // 检查关键词数组是否包含关键词
                    return Arrays.stream(table.getKeywords())
                            .anyMatch(kw -> kw.toLowerCase().contains(lowerKeyword));
                })
                .map(TableMappingEnum::getTableName)
                .collect(Collectors.toList());
    }

    /**
     * 根据多个关键词匹配表名
     *
     * @param keywords 关键词数组
     * @return 匹配的表名列表
     */
    public static List<String> getTableNamesByKeywords(String[] keywords) {
        if (keywords == null || keywords.length == 0) {
            return new java.util.ArrayList<>();
        }

        java.util.Set<String> matchedTables = new java.util.HashSet<>();
        for (String keyword : keywords) {
            matchedTables.addAll(getTableNamesByKeyword(keyword));
        }
        return new java.util.ArrayList<>(matchedTables);
    }

    /**
     * 获取所有表名和描述的映射
     *
     * @return 表名到描述的映射
     */
    public static Map<String, String> getAllTableDescriptions() {
        return Arrays.stream(values())
                .collect(Collectors.toMap(
                        TableMappingEnum::getTableName,
                        TableMappingEnum::getDescription
                ));
    }

    /**
     * 根据表名获取描述
     *
     * @param tableName 表名
     * @return 描述
     */
    public static String getDescriptionByTableName(String tableName) {
        return Arrays.stream(values())
                .filter(table -> table.getTableName().equals(tableName))
                .findFirst()
                .map(TableMappingEnum::getDescription)
                .orElse("");
    }
} 