package com.qmth.teachcloud.common.util;

import cn.hutool.http.HttpStatus;
import com.qmth.boot.api.exception.ApiException;
import com.qmth.teachcloud.common.contant.SystemConstant;
import com.qmth.teachcloud.common.enums.ExceptionResultEnum;

import java.util.Collections;

/**
 * @Description: 自定义消息工具
 * @Param:
 * @return:
 * @Author: wangliang
 * @Date: 2019/10/11
 */
public class ResultUtil {

    public static Result success(Object object) {
        return new Result(ExceptionResultEnum.SUCCESS.getStatus().value(), ExceptionResultEnum.SUCCESS.getMessage(), object);
    }

    public static Result success() {
        return success(null);
    }

    public static Result error(int code, String message) {
        return new Result(code, message);
    }

    public static Result error() {
        throw ExceptionResultEnum.EXCEPTION_ERROR.exception();
    }

    public static Result error(ApiException e, String message) {
        throw new ApiException(e.getStatus(), e.getCode(), message, message);
    }

    public static Result error(ExceptionResultEnum e, int code, String message) {
        throw e.exception(code, message);
    }

    public static Result error(String message) {
        throw ExceptionResultEnum.ERROR.exception(message);
    }

    public static Result ok(String message) {
        return new Result(ExceptionResultEnum.SUCCESS.getStatus().value(), message);
    }

    public static Result ok(Object data) {
        return new Result(ExceptionResultEnum.SUCCESS.getStatus().value(), data, ExceptionResultEnum.SUCCESS.getMessage());
    }

    public static Result ok(boolean success) {
        return new Result(ExceptionResultEnum.SUCCESS.getStatus().value(), Collections.singletonMap(SystemConstant.SUCCESS, success), ExceptionResultEnum.SUCCESS.getMessage());
    }

    public static Result ok(Object data, String message) {
        return new Result(ExceptionResultEnum.SUCCESS.getStatus().value(), data, message);
    }

    public static Result ok() {
        return new Result(HttpStatus.HTTP_OK, Collections.singletonMap(SystemConstant.UPDATE_TIME, System.currentTimeMillis()), ExceptionResultEnum.SUCCESS.getMessage());
    }
}
