Java - JPA - 每月创建计数增量

时间:2017-10-06 00:15:44

标签: java json jpa

我正在尝试创建一个json结构,它将显示每月显示成员数的折线图。

enter image description here

表格如下所示。

id  |  email            | registeredDate
1   |  email1@test.com  | 2017-09-04
2   |  email2@test.com  | 2017-09-14
3   |  email3@test.com  | 2017-09-21
4   |  email4@test.com  | 2017-10-01
5   |  email5@test.com  | 2017-10-03
6   |  email6@test.com  | 2017-10-12
7   |  email7@test.com  | 2017-10-15
8   |  email8@test.com  | 2017-10-16
9   |  email9@test.com  | 2017-11-06
10  |  email10@test.com | 2017-11-07
11  |  email11@test.com | 2017-12-02

因此尝试创建此类图表的json结构看起来类似于以下

[   
    {
        "value": 3,
        "date": "2017-09-01"
    },
    {
        "value": 8,
        "date": "2017-10-01"
    },
    {
        "value": 10,
        "date": "2017-11-01"
    },
    {
        "value": 11,
        "date": "2017-12-01"
    }
]

我遇到的问题是如何使用或创建各种JPA查询来构建此类结果。

所以这是一个JPA,可以在日期范围之间获得成员数。

long countByRegisteredDateBetween(Date thresholdDate1, Date thresholdDate2) throws Exception;

找到最小和最大注册日期和青蛙跳来创建这个json结构似乎有点热门?

minReg = 2017-09-04
maxReg = 2017-12-02

...循环

x = 0;

-------------------------------------------
//count in sep 2017
date1 = "2017-09-01";
date2 = "2017-10-01";
countPerMonth = countByRegisteredDateBetween(date1, date2);

x+=countPerMonth;

JSONObject chartObj = new JSONObject();
chartObj.put("value", x);
chartObj.put("date", date1);
-------------------------------------------

//count in oct 2017
date1 = "2017-10-01";
date2 = "2017-11-01";
countPerMonth = countByRegisteredDateBetween(date1, date2);

x+=countPerMonth;

JSONObject chartObj = new JSONObject();
chartObj.put("value", x);
chartObj.put("date", date1);
-------------------------------------------

//count in nov 2017
date1 = "2017-11-01";
date2 = "2017-12-01";
countPerMonth = countByRegisteredDateBetween(date1, date2);

x+=countPerMonth;

JSONObject chartObj = new JSONObject();
chartObj.put("value", x);
chartObj.put("date", date1);
-------------------------------------------

//count in dec 2017
date1 = "2017-12-01";
date2 = "2018-01-01";
countPerMonth = countByRegisteredDateBetween(date1, date2);
x+=countPerMonth;

JSONObject chartObj = new JSONObject();
chartObj.put("value", x);
chartObj.put("date", date1);
-------------------------------------------

0 个答案:

没有答案