package com.nuliji.tools;

import com.github.pagehelper.Page;
import com.nuliji.tools.exception.InsideException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 统一返回消息
 * Created by gaojie on 2017/5/11.
 */
@Service
public class MessageHelp<T> {
    private static final Logger logger = LoggerFactory.getLogger(MessageHelp.class);
    public static <T> Response<T> success(T result){
        logger.debug("success: "+result.toString());
        return new Response<>(0, "", result);
    }
    public static <T> Response<PageMessage<T>> success(List<T> result, int page, int size, long count){
        logger.debug("success: "+result.toString());
        return new Response<PageMessage<T>>(0, "", new PageMessage<T>(result, page, size, count));
    }
    public static <T> Response<PageMessage<T>> success(T result, Page<T> page){
        logger.debug("success: "+result.toString());
        return new Response<PageMessage<T>>(0, "", new PageMessage<T>(page, page.getPageNum(), page.getPageSize(), page.getTotal()));
    }

    public static <T> Response<T> error(int status, String message){
        logger.debug("error:" + status + " " + message);
        return new Response<>(status, message, null);
    }

    public static <T> Response<T> exception(InsideException e){
        logger.debug("exception:" + e.getCode() + " " +e.getMessage());
        return new Response<>(e.getCode(), e.getMessage(), null);
    }

}