package com.wms_main.model.dto.response.ewm;

import lombok.Data;
import java.util.HashMap;
import java.util.Map;

@Data
public class EwmApiBackResponse {
    private String state;        // 修改字段名从status为state
    private String message;
    private Object content;
    //private Boolean successful;

    // 无参构造函数
    public EwmApiBackResponse() {
    }

    // 全参构造函数
    public EwmApiBackResponse(String state, String message, Object content, Boolean successful) {
        this.state = state;      // 修改字段名从status为state
        this.message = message;
        setContentWithSuccessful(content, successful);
        //this.successful = successful;
    }

    /**
     * 判断是否成功
     * @return 是否成功
     */
    public boolean getSuccess() {
        return "successfully".equals(state);
    }

    /**
     * 构建错误响应
     * @param message 错误信息
     * @return EwmApiBackResponse
     */
    public static EwmApiBackResponse error(String message) {
        EwmApiBackResponse response = new EwmApiBackResponse();
        response.setState("failed");         // 设置state为"failed"
        response.setMessage(message);
        response.setContentWithSuccessful(null, false);
        return response;
    }

    /**
     * 构建错误响应
     * @param state 状态码
     * @param message 错误信息
     * @return EwmApiBackResponse
     */
    public static EwmApiBackResponse error(String state, String message) {
        EwmApiBackResponse response = new EwmApiBackResponse();
        response.setState(state);            // 修改字段名从status为state
        response.setMessage(message);
        response.setContentWithSuccessful(null, false);
        return response;
    }

    /**
     * 构建成功响应
     * @return EwmApiBackResponse
     */
    public static EwmApiBackResponse success() {
        EwmApiBackResponse response = new EwmApiBackResponse();
        response.setState("successfully");   // 设置state为"successfully"
        response.setContentWithSuccessful(null, true);
        return response;
    }

    /**
     * 构建成功响应
     * @param message 成功信息
     * @return EwmApiBackResponse
     */
    public static EwmApiBackResponse success(String message) {
        EwmApiBackResponse response = new EwmApiBackResponse();
        response.setState("successfully");   // 设置state为"successfully"
        response.setMessage(message);
        response.setContentWithSuccessful(null, true);
        return response;
    }

    /**
     * 构建成功响应
     * @param message 成功信息
     * @param content 返回内容
     * @return EwmApiBackResponse
     */
    public static EwmApiBackResponse success(String message, Object content) {
        EwmApiBackResponse response = success(message);
        response.setContentWithSuccessful(content, true);
        return response;
    }

    /**
     * 设置content，包含successful字段
     * @param content 内容
     * @param successful 是否成功
     */
    private void setContentWithSuccessful(Object content, Boolean successful) {
        Map<String, Object> contentMap = new HashMap<>();
        contentMap.put("successful", successful);
        if (content != null) {
            contentMap.put("data", content);
        }
        this.content = contentMap;
    }
}
