java-数据库分表后分页查询算法

public class PunchTheClock {

    private SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");

    @Autowired
    private PunchTheClockMapper punchTheClockMapper;

    /**
     * 查询分表分页数据
     *
     * @param start 开始时间
     * @param end   结束时间
     * @return
     */
    @PostMapping("/test")
    public Object getList(@RequestParam("start") Date start, @RequestParam("end") Date end,@RequestParam("page")  int page) {

        String startMonth = sdf.format(start);
        String endMonth = sdf.format(end);

        long count = 0;

        JSONArray totalNum = new JSONArray();
        Map<String, Object> param = new HashMap<>();
        param.put("stime", start);
        param.put("etime", end);

        long tableCount = 0;

        //判断开始结束时间是否在同一月份
        if (startMonth.equals(endMonth)) {
            param.put("month", startMonth);
            tableCount = punchTheClockMapper.getMiddleMonthCount(param);
            JSONObject obj = new JSONObject();
            obj.put("tableName", startMonth);
            obj.put("countNum", tableCount);
            totalNum.add(obj);
        } else {
            //查询首月
            param.put("month", startMonth);
            param.put("time", start);
            tableCount = punchTheClockMapper.getFirstMonthCount(param);
            count += tableCount;
            JSONObject obj = new JSONObject();
            obj.put("tableName", startMonth);
            obj.put("countNum", tableCount);
            totalNum.add(obj);

            //查询中间月份
            List<String> months = getMonthBetween(start, end);
            for (String month : months) {
                param.put("month", month);
                tableCount = punchTheClockMapper.getMiddleMonthCount(param);
                count += tableCount;
                JSONObject objm = new JSONObject();
                objm.put("tableName", month);
                objm.put("countNum", tableCount);
                totalNum.add(objm);
            }

            //查询最后月份
            param.put("month", endMonth);
            param.put("time", end);
            tableCount = punchTheClockMapper.getLastMonthCount(param);
            count += tableCount;
            JSONObject objm = new JSONObject();
            objm.put("tableName", endMonth);
            objm.put("countNum", tableCount);
            totalNum.add(objm);
        }

        // 获取需要查询表列表
        JSONArray tables = tableCount(totalNum, 10, page);

        List<Map> list = new ArrayList<>();

        for (int j = 0; j < tables.size(); j++) {
            JSONObject json = tables.getJSONObject(j);
            String tableName = json.getString("tableName");
            int limits = (int)json.get("limits");
            int limite = (int)json.get("limite");
            param.put("month", tableName);
            param.put("limits", limits);
            param.put("limite", limite);
            list.addAll(punchTheClockMapper.getList(param));
        }

        return list;

    }

    /**
     * 获取开始到结束月份-不包含前后月份
     *
     * @param minDate
     * @param maxDate
     * @return
     */
    public List<String> getMonthBetween(Date minDate, Date maxDate) {
        String startMonth = sdf.format(minDate);
        String endMonth = sdf.format(maxDate);
        ArrayList<String> result = new ArrayList<String>();
        //格式化为年月

        Calendar min = Calendar.getInstance();
        Calendar max = Calendar.getInstance();

        min.setTime(minDate);
        min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);

        max.setTime(maxDate);
        max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);

        Calendar curr = min;
        while (curr.before(max)) {
            String month = sdf.format(curr.getTime());
            if (!startMonth.equals(month) && !endMonth.equals(month)) {
                result.add(month);
            }
            curr.add(Calendar.MONTH, 1);
        }
        return result;
    }


    /**
     * 分页计算方法
     *
     * @param totalNum 符合条件的各个表数据数据条数
     * @param pageSize 每页总数
     * @param page     当前第几页
     * @return array
     */
    private JSONArray tableCount(JSONArray totalNum, int pageSize, int page) {
        JSONArray resArr = new JSONArray();

        int prevNum = (page - 1) * pageSize; //前页面总数据
        int lastNum = prevNum + pageSize; //需要到的最后那条数据

        int nowNum = 0;
        int upNum = 0; //以上表占据的数量
        int nowHave = 0; //已够条数

        int arrNum = totalNum.size();
        for (int j = 0; j < totalNum.size(); j++) {
            JSONObject json = totalNum.getJSONObject(j);
            String tableName = json.getString("tableName");
            int countNum = json.getInteger("countNum");
            nowNum += countNum; //当前总条数
            if (nowNum > prevNum) {
                //开始数 = 前页面总数据 + 已够数 -上表总数
                int start = prevNum + nowHave - upNum;
                //已够数量 = 当前总数 - 开始数
                int end = 0;
                if (nowNum < lastNum) {
                    nowHave += nowNum - upNum - start;
                    end = nowNum - upNum - start;
                } else {
                    end = pageSize - nowHave;
                }
                nowHave = nowHave > pageSize ? pageSize : nowHave;

                JSONObject obj = new JSONObject();
                obj.put("tableName", tableName);
                obj.put("limits", start);
                obj.put("limite", end);
                resArr.add(obj);
                if (nowNum >= lastNum) {
                    //如果当前计算位置总数 大于或等于 需要到达的位置 则退出循环
                    break;
                }
            }
            upNum += countNum; //增加以上表数量
        }
        return resArr;
    }
}

你可能感兴趣的:(java-数据库分表后分页查询算法)