package cn.ele6.catalyzer.ruoyi.vue.enhance;

import cn.ele6.catalyzer.ruoyi.vue.custom.Ele6RYResponse;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.constant.HttpStatus;
import org.springframework.beans.BeanUtils;

import java.io.Serializable;
import java.util.List;

/**
 * 同名类增强改造。
 */
public class TableDataInfo<T> extends com.ruoyi.common.core.page.TableDataInfo implements Serializable, Ele6RYResponse {

    public static final String MSG_OK = "查询成功";
    public static final long TOTAL_0 = 0L;

    @SuppressWarnings({"rawtypes", "unchecked"})
    public static <T> TableDataInfo<T> buildOk(final List<T> rows) {
        return build(HttpStatus.SUCCESS, MSG_OK, rows, new PageInfo(rows).getTotal());
    }

    public static <T> TableDataInfo<T> buildError(final String msg) {
        return build(HttpStatus.ERROR, msg, null, TOTAL_0);
    }

    public static <T> TableDataInfo<T> build(final int code, final String msg, final List<T> rows, final long total) {
        TableDataInfo<T> t = new TableDataInfo<>();
        t
                .eSetCode(code)
                .eSetMsg(msg)
                .eSetRows(rows)
                .eSetTotal(total);
        return t;
    }

    public TableDataInfo<T> eSetCode(final int code) {
        super.setCode(code);
        return this;
    }

    public TableDataInfo<T> eSetMsg(final String msg) {
        super.setMsg(msg);
        return this;
    }

    public TableDataInfo<T> eSetRows(final List<T> rows) {
        super.setRows(rows);
        return this;
    }

    public TableDataInfo<T> eSetTotal(final long total) {
        super.setTotal(total);
        return this;
    }

    /**
     * convert type.
     *
     * @param o
     * @return
     */
    public static TableDataInfo<?> convertOldToNew(com.ruoyi.common.core.page.TableDataInfo o) {
        TableDataInfo<?> t = new TableDataInfo<>();
        BeanUtils.copyProperties(o, t);
        return t;
    }

    /**
     * convert type.
     *
     * @param o
     * @return
     */
    public static com.ruoyi.common.core.page.TableDataInfo convertNewToOld(TableDataInfo<?> o) {
        com.ruoyi.common.core.page.TableDataInfo t = new com.ruoyi.common.core.page.TableDataInfo();
        BeanUtils.copyProperties(o, t);
        return t;
    }
}
