All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.afu.utils.common.dto.BaseRespDto Maven / Gradle / Ivy

There is a newer version: 0.0.55-RELEASE
Show newest version
package io.afu.utils.common.dto;


import io.afu.utils.common.constant.CodeConstant;
import io.afu.utils.common.constant.ConstantEnum;
import io.afu.utils.common.constant.MsgConstant;
import io.afu.utils.exception.BaseException;

import java.util.List;

/**
 * @author RffanLAB.方露宇
 */
public class BaseRespDto {
    private Boolean status;
    private Integer code;
    private String msg;
    private T data;


    public Boolean getStatus() {
        return status;
    }

    public void setStatus(Boolean status) {
        this.status = status;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        this.status = true;
        this.code = CodeConstant.OPERATION_SUCCESS;
        this.msg = MsgConstant.OPERATION_SUCCESS;
        return data;
    }

    public void setData(T data) {
        this.status = true;
        this.code = CodeConstant.OPERATION_SUCCESS;
        this.msg = MsgConstant.OPERATION_SUCCESS;
        this.data = data;
    }

    public void setTrue(){
        this.status = true;
        this.code = ConstantEnum.OPERATION_SUCCESS.getCode();
    }

    public void setFalse(){
        this.status = false;
        this.code = ConstantEnum.OPERATION_FAILED.getCode();
    }

    public void setTrueMsg(String msg){
        this.status = true;
        this.code = ConstantEnum.OPERATION_SUCCESS.getCode();
        this.msg = msg;
    }

    public void setErrorMsg(String msg){
        this.status = false;
        this.code = ConstantEnum.OPERATION_FAILED.getCode();
        this.msg = msg;
    }

    public void setTrue(ConstantEnum constantEnum){
        this.status = true;
        this.code =  constantEnum.getCode();
        this.msg = constantEnum.getMsg();
    }

    public void setFalse(ConstantEnum constantEnum){
        this.status = false;
        this.code = constantEnum.getCode();
        this.msg = constantEnum.getMsg();
    }

    public void setFalse(BaseException e){
        this.status = false;
        this.code = e.getCode();
        this.msg = e.getMessage();
    }


    /**
     * 添加自定义fail方法
     * @param msg 消息
     * @param code 代码
     * @return 返回实体类
     */
    public static BaseRespDto fail(String msg,Integer code){
        BaseRespDto baseRespDto = new BaseRespDto<>();
        baseRespDto.setStatus(false);
        baseRespDto.setCode(code);
        baseRespDto.setMsg(msg);
        return baseRespDto;
    }


    public static BaseRespDto fail(BaseException e){
        BaseRespDto baseRespDto = new BaseRespDto<>();
        baseRespDto.setStatus(false);
        baseRespDto.setCode(e.getCode());
        baseRespDto.setMsg(e.getMessage());
        return baseRespDto;
    }

    public static BaseRespDto fail(String msg) {
        BaseRespDto baseRespDto = new BaseRespDto<>();
        baseRespDto.setStatus(false);
        baseRespDto.setCode(-1);
        baseRespDto.setMsg(msg);
        return baseRespDto;
    }

    public static BaseRespDto fail(ConstantEnum constantEnum){
        BaseRespDto baseRespDto = new BaseRespDto<>();
        baseRespDto.setStatus(false);
        baseRespDto.setCode(constantEnum.getCode());
        baseRespDto.setMsg(constantEnum.getMsg());
        return baseRespDto;
    }

    public static BaseRespDto fail(Object o){
        BaseRespDto baseRespDto = new BaseRespDto<>();
        baseRespDto.setStatus(false);
        baseRespDto.setData(o);
        baseRespDto.setMsg(ConstantEnum.OPERATION_FAILED.getMsg());
        baseRespDto.setCode(ConstantEnum.OPERATION_FAILED.getCode());
        return baseRespDto;
    }

    public static BaseRespDto fail(List objects){
        BaseRespDto> baseRespDto = new BaseRespDto<>();
        baseRespDto.setStatus(false);
        baseRespDto.setCode(ConstantEnum.OPERATION_FAILED.getCode());
        baseRespDto.setMsg(ConstantEnum.OPERATION_FAILED.getMsg());
        baseRespDto.setData(objects);
        return baseRespDto;
    }

    public static BaseRespDto success(){
        BaseRespDto baseRespDto = new BaseRespDto<>();
        baseRespDto.setStatus(true);
        baseRespDto.setCode(ConstantEnum.OPERATION_SUCCESS.getCode());
        baseRespDto.setMsg(ConstantEnum.OPERATION_SUCCESS.getMsg());
        return baseRespDto;
    }

    public static BaseRespDto success(String data){
        BaseRespDto baseRespDto = new BaseRespDto<>();
        baseRespDto.setStatus(true);
        baseRespDto.setCode(ConstantEnum.OPERATION_SUCCESS.getCode());
        baseRespDto.setMsg(ConstantEnum.OPERATION_SUCCESS.getMsg());
        baseRespDto.setData(data);
        return baseRespDto;
    }

    /**
     * 通过泛型来进行参数的返回的解析
     * @param k 泛型传入的data
     * @param  泛型定义传入的data的类型
     * @return 返回返回实体类
     */
    public static  BaseRespDto success(K k){
        BaseRespDto baseRespDto = new BaseRespDto<>();
        baseRespDto.setTrue();
        baseRespDto.setCode(ConstantEnum.OPERATION_SUCCESS.getCode());
        baseRespDto.setMsg(ConstantEnum.OPERATION_SUCCESS.getMsg());
        baseRespDto.setData(k);
        return baseRespDto;
    }






}