package org.springblade.flow.base;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springblade.flow.base.vo.PageListVO;
import org.springblade.flow.base.vo.PaginationVO;

import com.fasterxml.jackson.annotation.JsonInclude;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 *
 * @author JNPF开发平台组
 * @version V3.1.0
 * @copyright 引迈信息技术有限公司
 * @date 2021/3/16 8:47
 */
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ActionResult<T> {

	@ApiModelProperty("状态码")
	private Integer code;

	@ApiModelProperty("返回信息")
	private String msg;

	@ApiModelProperty("返回数据")
	private T data;

	private Object ext;

	public ActionResult() {
	}

	public ActionResult(int code, String msg) {
		this.code = code;
		this.msg = msg;
	}

	public ActionResult(int code, String msg, T data) {
		this.code = code;
		this.msg = msg;
		this.data = data;
	}

	public static <T> ActionResult<T> success() {
		return ActionResult.success("操作成功");
	}

	public static <T> ActionResult<T> success(String message) {
		return new ActionResult<T>(200, message, null);
	}

	public static <T> ActionResult<T> success(T data) {
		return ActionResult.success("操作成功", data);
	}

	public static <T> ActionResult<T> success(T data, Object ext) {
		ActionResult<T> ret = ActionResult.success("操作成功", data);
		ret.setExt(ext);
		return ret;
	}

	public static <T> ActionResult<T> success(String message, T data) {
		return new ActionResult<T>(200, message, data);
	}

	public static ActionResult<Object> success(Object rows, PageModel pageModel) {
		Map<String, Object> data = new HashMap<>(16);
		data.put("page", pageModel.getPage());
		data.put("records", pageModel.getRecords());
		data.put("rows", rows);
		data.put("total", pageModel.getTotal());
		return new ActionResult<>(200, "操作成功", data);
	}

	public static <T> ActionResult<PageListVO<T>> page(List<T> list, PaginationVO pagination) {
		PageListVO<T> data = new PageListVO<>();
		data.setList(list);
		data.setPagination(pagination);
		return new ActionResult<>(200, "操作成功", data);
	}

	public static <T> ActionResult<T> fail() {
		return ActionResult.fail("操作失败");
	}

	public static <T> ActionResult<T> fail(String message) {
		return ActionResult.fail(message, null);
	}

	public static ActionResult<String> fail(Integer code, String message) {
		return new ActionResult<String>(code, message, null);
	}

	public static <T> ActionResult<T> fail(String message, T data) {
		return new ActionResult<T>(400, message, data);
	}

}
