package org.xyzh.common.core.domain;

import java.io.Serializable;
import java.util.List;

import org.springframework.http.HttpStatus;
import org.xyzh.common.core.page.PageDomain;

import lombok.Data;

/**
 * @description    结果域 通用返回结果
 * @filename    结果域
 * @author    yslg
 * @copyright yslg
 * @since 2025-11-02
 */
@Data
public class ResultDomain<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    private Integer code;
    private Boolean success;
    private String message;
    private T data;
    private List<T> dataList;
    private PageDomain<T> pageDomain;

    public ResultDomain(){
    }

    public ResultDomain(int code, String message) {
        this.code = code;
        this.message = message;
        this.success = false;
    }

    public ResultDomain(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.success = false;
        this.data = data;
    }
    
    public ResultDomain(int code, String message, List<T> dataList) {
        this.code = code;
        this.message = message;
        this.success = false;
        this.dataList = dataList;
    }
    
    public ResultDomain(int code, String message, PageDomain<T> pageDomain) {
        this.code = code;
        this.message = message;
        this.success = false;
        this.pageDomain = pageDomain;
        if(pageDomain != null) this.dataList = pageDomain.getDataList();
    }

    public void setPageDomain(PageDomain<T> pageDomain){
        this.pageDomain = pageDomain;
        if(pageDomain != null) this.dataList = pageDomain.getDataList();
    }

    // 静态工厂方法 - 推荐使用（简洁、清晰）
    public static <R> ResultDomain<R> success(String message) {
        ResultDomain<R> result = new ResultDomain<>();
        result.success = true;
        result.message = message;
        result.code = HttpStatus.OK.value();
        return result;
    }

    public static <R> ResultDomain<R> success(String message, R data) {
        ResultDomain<R> result = new ResultDomain<>();
        result.success = true;
        result.message = message;
        result.data = data;
        result.code = HttpStatus.OK.value();
        return result;
    }

    public static <R> ResultDomain<R> success(String message, List<R> dataList) {
        ResultDomain<R> result = new ResultDomain<>();
        result.success = true;
        result.message = message;
        result.dataList = dataList;
        result.code = HttpStatus.OK.value();
        return result;
    }

    public static <R> ResultDomain<R> success(String message, PageDomain<R> pageDomain) {
        ResultDomain<R> result = new ResultDomain<>();
        result.success = true;
        result.message = message;
        result.pageDomain = pageDomain;
        if(pageDomain != null) result.dataList = pageDomain.getDataList();
        result.code = HttpStatus.OK.value();
        return result;
    }

    public static <R> ResultDomain<R> failure(String message) {
        ResultDomain<R> result = new ResultDomain<>();
        result.success = false;
        result.message = message;
        result.code = HttpStatus.INTERNAL_SERVER_ERROR.value();
        return result;
    }

    public static <R> ResultDomain<R> failure(int code, String message) {
        ResultDomain<R> result = new ResultDomain<>();
        result.success = false;
        result.message = message;
        result.code = code;
        return result;
    }
}
