package com.dora.util;

import java.util.HashMap;
import java.util.Map;

/**
 * API响应工具类
 * 统一处理API响应格式
 */
public class ApiResponseUtil {

    /**
     * 成功响应
     */
    public static Map<String, Object> success(Object data) {
        Map<String, Object> response = new HashMap<>();
        response.put("code", 200);
        response.put("message", "操作成功");
        response.put("data", data);
        return response;
    }

    /**
     * 成功响应（自定义消息）
     */
    public static Map<String, Object> success(String message, Object data) {
        Map<String, Object> response = new HashMap<>();
        response.put("code", 200);
        response.put("message", message);
        response.put("data", data);
        return response;
    }

    /**
     * 失败响应
     */
    public static Map<String, Object> error(int code, String message) {
        Map<String, Object> response = new HashMap<>();
        response.put("code", code);
        response.put("message", message);
        response.put("data", null);
        return response;
    }

    /**
     * 失败响应（默认500错误）
     */
    public static Map<String, Object> error(String message) {
        return error(500, message);
    }

    /**
     * 未授权响应
     */
    public static Map<String, Object> unauthorized() {
        return error(401, "用户未登录");
    }

    /**
     * 参数错误响应
     */
    public static Map<String, Object> badRequest(String message) {
        return error(400, message);
    }

    /**
     * 分页响应
     */
    public static Map<String, Object> successWithPagination(Object data, long total, int page, int size) {
        Map<String, Object> response = new HashMap<>();
        response.put("code", 200);
        response.put("message", "操作成功");
        response.put("data", data);
        response.put("total", total);
        response.put("page", page);
        response.put("size", size);
        return response;
    }
} 