package cn.stylefeng.team.project.modular.filing.pojo.flux;

import cn.stylefeng.roses.kernel.rule.annotation.ChineseDescription;
import cn.stylefeng.team.project.modular.filing.pojo.flux.enums.FluxResponseTypeEnum;
import lombok.Data;
import reactor.core.publisher.FluxSink;

@Data
public class FluxResponse {

    @ChineseDescription("响应状态码")
    private Integer code;

    @ChineseDescription("响应信息，响应成功的提示和响应错误的提示都用这个")
    private String message;

    @ChineseDescription("附加的信息")
    private Object attachmentObject;

    public static FluxResponse writeJson(String message) {
        FluxResponse response = new FluxResponse();
        response.setCode(FluxResponseTypeEnum.ASSISTANT_RESPONSE.getCode());
        response.setMessage(message);
        return response;
    }

    public static FluxResponse error(String message) {
        FluxResponse response = new FluxResponse();
        response.setCode(FluxResponseTypeEnum.ERROR.getCode());
        response.setMessage(message);
        return response;
    }

    public static FluxResponse attachment(FluxResponseTypeEnum fluxResponseTypeEnum, Object attachmentObject) {
        FluxResponse response = new FluxResponse();
        response.setCode(fluxResponseTypeEnum.getCode());
        response.setMessage(fluxResponseTypeEnum.getMessage());
        response.setAttachmentObject(attachmentObject);
        return response;
    }

    public static void sinkNext(FluxSink<FluxResponse> sink, FluxResponse fluxResponse) {
        if (sink.isCancelled()) {
            return;
        }
        sink.next(fluxResponse);
    }

}
