package com.shkj.wcs.vo;

import lombok.Data;
import org.apache.poi.ss.formula.functions.T;

import java.util.Collections;
import java.util.List;

@Data
public class WcsResponseVo<T> {
    private ResponseHeader header;
    private List<T> body;

    @Data
    public static class ResponseHeader {
        private Integer msgCode;
        private String message;
    }

    // 成功
    public static <T> WcsResponseVo<T> success(List<T> data) {
        WcsResponseVo<T> response = new WcsResponseVo<>();
        response.setHeader(createSuccessHeader());
        response.setBody(data != null ? data : Collections.emptyList());
        return response;
    }

    // 无数据的成功响应
    public static <T> WcsResponseVo<T> success() {
        return success(null);
    }

    // 单对象成功响应（非List场景）
    public static <T> WcsResponseVo<T> success(T item) {
        WcsResponseVo<T> response = new WcsResponseVo<>();
        response.setHeader(createSuccessHeader());
        response.setBody(item != null ?
                Collections.singletonList(item) :
                Collections.emptyList());
        return response;
    }

    public static Boolean isOk(WcsResponseVo result) {
        if (result == null){
            return false;
        }
        Integer msgCode = result.getHeader().getMsgCode();
        if (msgCode == null) {
            return false;
        }
        if (msgCode == 200) {
            return true;
        }
        return false;
    }

    // 错误响应
    public static <T> WcsResponseVo<T> error(int code, String msg) {
        WcsResponseVo<T> response = new WcsResponseVo<>();
        ResponseHeader header = new ResponseHeader();
        header.setMsgCode(code);
        header.setMessage(msg);
        response.setHeader(header);
        response.setBody(Collections.emptyList()); // 错误时body默认为空数组
        return response;
    }

    private static ResponseHeader createSuccessHeader() {
        ResponseHeader header = new ResponseHeader();
        header.setMsgCode(200);
        header.setMessage("调用成功");
        return header;
    }
}