package com.tiesheng.core.pojos.dto;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.Map;

/**
 * @author hao
 */
public class PageDTO {

    private Long pageNum;
    private Long pageSize;
    private String keyword;
    private String between;
    private String equals;
    private String order;


    /**
     * 启用分页
     *
     * @return
     */
    public <T> Page<T> pageObj() {
        Page<T> page = new Page<>();
        page.setSize(getPageSize());
        page.setCurrent(getPageNum());
        return page;
    }


    /**
     * 启用分页
     *
     * @return
     */
    public Page<Map<String, Object>> pageMap() {
        Page<Map<String, Object>> page = new Page<>();
        page.setSize(getPageSize());
        page.setCurrent(getPageNum());
        return page;
    }

    /***
     * 启用排序
     * @param queryWrapper
     * @param <T>
     */
    public <T> void doOrder(QueryWrapper<T> queryWrapper) {
        if (!StrUtil.isEmpty(order)) {
            if (order.contains("-")) {
                queryWrapper.orderByDesc(StrUtil.toUnderlineCase(order.replace("-", "")));
            } else {
                queryWrapper.orderByAsc(StrUtil.toUnderlineCase(order));
            }
        }

    }

    /**
     * 关键字查询
     *
     * @param queryWrapper
     * @param columns
     * @param <T>
     */
    public <T> void likeColumns(QueryWrapper<T> queryWrapper, String... columns) {
        keyword = StrUtil.trim(keyword);
        if (!StrUtil.isEmpty(keyword) && columns != null && columns.length > 0) {
            queryWrapper.and(s -> {
                for (String col : columns) {
                    s.like(col, keyword).or();
                }
            });
        }
        betweenColumns(queryWrapper);
        equalsColumns(queryWrapper);
    }

    /**
     * 区间
     *
     * @param queryWrapper
     * @param <T>
     */
    public <T> void betweenColumns(QueryWrapper<T> queryWrapper) {
        if (!StrUtil.isEmpty(between)) {
            String[] bets = between.split("=");
            if (bets.length == 2) {
                String[] vals = bets[1].split(",");
                if (vals.length == 2) {
                    queryWrapper.between(StrUtil.toUnderlineCase(bets[0]), vals[0], vals[1]);
                }
            }
        }
    }

    /**
     * 匹配列
     *
     * @param <T>
     */
    public <T> void equalsColumns(QueryWrapper<T> queryWrapper) {
        if (!StrUtil.isEmpty(equals)) {
            String[] kv = equals.split(",");
            for (String key : kv) {
                String[] cols = key.split("=");
                if (cols.length == 2) {
                    queryWrapper.eq(StrUtil.toUnderlineCase(cols[0]), cols[1]);
                }
            }
        }
    }


    /**
     * between
     *
     * @param alias
     * @return
     */
    public String betweenSql(String alias) {
        String sql = "";
        if (!StrUtil.isEmpty(between)) {
            String[] bets = between.split("=");
            if (bets.length == 2) {
                String[] vals = bets[1].split(",");
                if (vals.length == 2) {
                    sql = sql + " and (" + alias + "." + StrUtil.toUnderlineCase(bets[0])
                            + " between '" + vals[0] + "' and '" + vals[1] + "')";
                }
            }
        }
        return sql;
    }

    /**
     * 获取排序的sql
     *
     * @return
     */
    public String getOrderSql(String alias) {
        String sql = "";
        if (!StrUtil.isEmpty(order)) {
            if (order.contains("-")) {
                sql = StrUtil.toUnderlineCase(order.replace("-", "")) + " desc";
            } else {
                sql = StrUtil.toUnderlineCase(order) + " asc";
            }
            sql = "order by " + alias + "." + sql;
        }
        return sql;
    }

    public String getEqualsSql(String alias) {
        StringBuilder sql = new StringBuilder();
        if (!StrUtil.isEmpty(equals)) {
            String[] kv = equals.split(",");
            for (String key : kv) {
                String[] cols = key.split("=");
                if (cols.length == 2) {
                    sql.append(" and ").append(alias).append(".").append(StrUtil.toUnderlineCase(cols[0])).append("=").append(cols[1]);
                }
            }
        }
        return sql.toString();
    }


    ///////////////////////////////////////////////////////////////////////////
    // setter\getter
    ///////////////////////////////////////////////////////////////////////////


    public Long getPageNum() {
        return pageNum == null ? 1 : pageNum;
    }

    public void setPageNum(Long pageNum) {
        this.pageNum = pageNum;
    }

    public Long getPageSize() {
        return pageSize == null ? 10 : pageSize;
    }

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

    public String getKeyword() {
        return keyword;
    }

    public void setKeyword(String keyword) {
        this.keyword = keyword;
    }

    public String getBetween() {
        return between;
    }

    public void setBetween(String between) {
        this.between = between;
    }

    public String getEquals() {
        return equals;
    }

    public void setEquals(String equals) {
        this.equals = equals;
    }

    public String getOrder() {
        return order;
    }

    public void setOrder(String order) {
        this.order = order;
    }

}
