package com.joju.datamanager.common.result;

/**
 * Created with IntelliJ IDEA.
 *
 * @author： Mr.zs
 * @date： 2023/12/25
 * @description：
 * @modifiedBy：
 * @version: 1.0
 */


import com.joju.datamanager.common.annotation.ResultAnnotation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

import javax.validation.constraints.NotNull;


/**
 * 描述：
 * 使用 @ControllerAdvice & ResponseBodyAdvice
 * 拦截Controller方法默认返回参数，统一处理返回值/响应体
 */
@Slf4j
@ControllerAdvice
public class ResultResponseBodyAdviceImpl implements ResponseBodyAdvice<Object> {

    @Override
    public boolean supports(@NotNull MethodParameter returnType, @NotNull Class<? extends HttpMessageConverter<?>> converterType) {

        /*支持所有方法*/
        ResultAnnotation methodAnnotation = returnType.getMethodAnnotation(ResultAnnotation.class);
        if (methodAnnotation != null) {
            return methodAnnotation.required();
        }
        return false;
    }

    @Override
    public Object beforeBodyWrite(Object body,
                                  @NotNull MethodParameter returnType,
                                  @NotNull MediaType selectedContentType,
                                  @NotNull Class<? extends HttpMessageConverter<?>> selectedConverterType,
                                  @NotNull ServerHttpRequest request,
                                  @NotNull ServerHttpResponse response) {

        if (log.isDebugEnabled()) {
            log.debug("ResultResponseBodyAdviceImpl.beforeBodyWrite:{}", body);
        }
        if (body instanceof Result) {
            return body;
        }
        return ResultUtil.successData(body);
    }
}