package com.ozs.account.util;

import java.util.List;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

/**
 * @author 宋显哲
 * @version 1.0.0 创建于 2017/12/27
 **/
@Component
@Scope("prototype")
@ApiModel(description = "请求返回实体类", value = "返回值")
public class R<T> {
	@ApiModelProperty(dataType = "int", name = "code", value = "请求返回状态码")
	@JsonInclude(JsonInclude.Include.NON_EMPTY)
	private String code = S.OK;
	@ApiModelProperty(dataType = "String", name = "msg", value = "请求返回描述信息")
	@JsonInclude(JsonInclude.Include.NON_EMPTY)
	private String msg = S.getMsg(S.OK);
	@ApiModelProperty(dataType = "T", name = "data", value = "返回数据")
	T data;
	@ApiModelProperty(dataType = "Integer", name = "page", value = "当前页码")
	@JsonInclude(JsonInclude.Include.NON_EMPTY)
	private Integer page;
	@ApiModelProperty(dataType = "Integer", name = "pageSize", value = "每页行数")
	@JsonInclude(JsonInclude.Include.NON_EMPTY)
	private Integer pageSize;
	@ApiModelProperty(dataType = "Integer", name = "totalNumber", value = "数据总数")
	@JsonInclude(JsonInclude.Include.NON_EMPTY)
	private Integer totalNumber;
	@ApiModelProperty(dataType = "Integer", name = "totalPageCount", value = "总页数")
	@JsonInclude(JsonInclude.Include.NON_EMPTY)
	private Integer totalPageCount;

	public R() {
	}

	public R(String code) {
		this.code = code;
		this.msg = S.getMsg(code);
	}

	public R(T data) {
		this.data = data;
	}

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

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

	public String getCode() {
		return code;
	}

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

	public String getMsg() {
		return msg;
	}

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

	public T getData() {
		return data;
	}

	public R<T> setData(T data) {
		this.data = data;
		return this;
	}

	public Integer getPage() {
		return page;
	}

	public void setPage(Integer page) {
		this.page = page;
	}

	public Integer getPageSize() {
		return pageSize;
	}

	public void setPageSize(Integer pageSize) {
		this.pageSize = pageSize;
	}

	public Integer getTotalNumber() {
		return totalNumber;
	}

	public void setTotalNumber(Integer totalNumber) {
		this.totalNumber = totalNumber;
	}

	public Integer getTotalPageCount() {
		return totalPageCount;
	}

	public void setTotalPageCount(Integer totalPageCount) {
		this.totalPageCount = totalPageCount;
	}

	@Override
	public String toString() {
		return "ReturnData [code=" + code + ", msg=" + msg + ", data=" + data + ", page=" + page + ", pageSize="
				+ pageSize + ", totalNumber=" + totalNumber + ", totalPageCount=" + totalPageCount + "]";
	}

	public R<List<T>> page(List<T> result) {
		PageInfo<T> pageInfo = new PageInfo<>(result);
		R<List<T>> r = new R<>(result);
		r.setPage(pageInfo.getPageNum());
		r.setPageSize(pageInfo.getPageSize());
		r.setTotalNumber((int) pageInfo.getTotal());
		r.setTotalPageCount(pageInfo.getPages());
		return r;
	}

	/**
	 * 
	 * @Title: pageDataTotal
	 * @Description: 存在合计数据的分页展示
	 * @param result
	 * @param page
	 *            数据当前查询页数
	 * @return
	 * @return: R<List<T>>
	 */
	public R<List<T>> pageDataTotal(List<T> result, int page, T total) {
		PageInfo<T> pageInfo = new PageInfo<>(result);
		int pageSize = pageInfo.getPageSize();
		int totalNumber = (int) pageInfo.getTotal() + 1;
		int pageNum = pageInfo.getPageNum();
		int totalPageCount = (totalNumber - 1) / pageSize + 1;
		// 当前页为最后一页，加入合计数据
		if (page >= totalPageCount) {
			if (result.size() == pageSize) {
				result.removeAll(result);
				pageNum++;
			}
			if (total != null) {
				result.add(total);
			}
		}
		R<List<T>> r = new R<>(result);
		r.setPage(pageNum);
		r.setPageSize(pageSize);
		r.setTotalNumber(totalNumber);
		r.setTotalPageCount(totalPageCount);
		return r;
	}

	public void pageStart(String sort, String order, int page, int pageSize) {
		if (sort != null && order != null) {
			sort = ParamUtil.camelToUnderline(sort);
			order = ParamUtil.getDefaultOrder(order);
			PageHelper.startPage(page, pageSize, sort + " " + order);
		} else {
			PageHelper.startPage(page, pageSize);
		}

	}

}
