Как получить список данных, на конкретную дату только 6 записей на каждую дату, не более - PullRequest
1 голос
/ 23 апреля 2020

Я использую весеннюю загрузку с защитой в java. Я должен попытаться склеить весь список событий между startDate и endDate и order By startDate, но с каждой даты только 6 записей получают не больше. Предположим, в этом месяце дата мудрая - нет. события там. Но я хочу, чтобы только шесть записей на каждую дату не требовали больше записей. В этом eventsListTwoDates объекте у меня есть Список всех событий.

Проблема - Получить список событий null

Я пытаюсь использовать следующий код

    public GenericResponse getAllList() {
        {
            List<HashMap<String, Object>> eventsListTwoDates = eventsDao.findAllListOfEventsWithoutEventTypeIdWithOnlyTwoDate(startDate, endDate);
            int count = 0;
            List<HashMap<String, Object>> newList = new ArrayList<>();
            for (int i = 0; i < eventsListTwoDates.size(); i++) {
                if (map.contains(eventsListTwoDates.get(i).containsValue("startDate"))) {
                    if (count < 6) {
                        newList.add(i, map.get(i));
                        count++;
                    } else {
                        count = 0;
                    }
                }
            }
            return APIResponseBuilder.build(true, newList, commonMessages.findAllTrue);
        }

2. Запрос

Query("SELECT new map(e.id as id, e.eventImg as eventImg, e.multipleDays as multipleDays, e.startDate as startDate, e.endDate as endDate, e.startTime as startTime, e.endTime as endTime, e.eventStatus.status as status, e.paid as paid, e.fee as fee, e.passportTickets as passportTickets, e.location as location, e.city.name as city, e.myFavourite as myFavourite, e.remainingTickets as remainingTickets, e.eventName as eventName, e.passport as passport, e.typesOfEvents.type as eventType, e.eventPassportOptions.event_passport_option_id as passportOption, e.promoCode as promoCode, e.subTitle as subTitle, e.expectedAttendance as expectedAttendance)" +
            " from Events e WHERE e.startDate BETWEEN :startDate AND :endDate AND e.endDate BETWEEN :startDate AND :endDate ORDER BY e.startDate")
    List<HashMap<String, Object>> findAllListOfEventsWithoutEventTypeIdWithOnlyTwoDate(String startDate, String endDate);

3. Ответ

{
  "success": true,
  "message": "Record's find successfully.",
  "data": [
    {
      "passportTickets": 20,
      "endDate": "2020/04/17",
      "city": "Bombuflat",
      "fee": 50,
      "remainingTickets": 40,
      "expectedAttendance": "34",
      "passportOption": 1,
      "eventType": "Product Launches",
      "multipleDays": false,
      "myFavourite": false,
      "passport": true,
      "subTitle": " Celebration of World Wildlife Day",
      "paid": true,
      "eventName": "sales",
      "eventImg": "https://newsd.in/wp-content/uploads/2019/03/Arua-to-Host-National-Wildlife-Day-Celebrations.png",
      "startTime": "20:04:45",
      "location": "Rajpath club",
      "promoCode": "NetPh123",
      "id": 3,
      "endTime": "15:30:45",
      "startDate": "2020/04/15",
      "status": "Draft"
    },
    {
      "passportTickets": 20,
      "endDate": "2020/04/30",
      "city": "Rapar",
      "fee": 50,
      "remainingTickets": 40,
      "expectedAttendance": "34",
      "passportOption": 1,
      "eventType": "Incentive Trips",
      "multipleDays": false,
      "myFavourite": false,
      "passport": true,
      "subTitle": " Celebration of World Wildlife Day",
      "paid": true,
      "eventName": "Wild animal day",
      "eventImg": "https://newsd.in/wp-content/uploads/2019/03/Arua-to-Host-National-Wildlife-Day-Celebrations.png",
      "startTime": "20:04:45",
      "location": "Rajpath club",
      "promoCode": "NetPh123",
      "id": 5,
      "endTime": "15:30:45",
      "startDate": "2020/04/15",
      "status": "Draft"
    },
    {
      "passportTickets": 20,
      "endDate": "2020/04/30",
      "city": "Rapar",
      "fee": 50,
      "remainingTickets": 40,
      "expectedAttendance": "34",
      "passportOption": 1,
      "eventType": "Incentive Trips",
      "multipleDays": false,
      "myFavourite": false,
      "passport": true,
      "subTitle": " Celebration of World Wildlife Day",
      "paid": true,
      "eventName": "Wild animal day",
      "eventImg": "https://newsd.in/wp-content/uploads/2019/03/Arua-to-Host-National-Wildlife-Day-Celebrations.png",
      "startTime": "20:04:45",
      "location": "Rajpath club",
      "promoCode": "NetPh123",
      "id": 6,
      "endTime": "15:30:45",
      "startDate": "2020/04/16",
      "status": "Draft"
    },
    {
      "passportTickets": 20,
      "endDate": "2020/04/30",
      "city": "Rapar",
      "fee": 50,
      "remainingTickets": 40,
      "expectedAttendance": "34",
      "passportOption": 1,
      "eventType": "Dancing",
      "multipleDays": false,
      "myFavourite": false,
      "passport": true,
      "subTitle": " Celebration of World Wildlife Day",
      "paid": true,
      "eventName": "Wild animal day",
      "eventImg": "https://newsd.in/wp-content/uploads/2019/03/Arua-to-Host-National-Wildlife-Day-Celebrations.png",
      "startTime": "20:04:45",
      "location": "Rajpath club",
      "promoCode": "NetPh123",
      "id": 7,
      "endTime": "15:30:45",
      "startDate": "2020/04/16",
      "status": "Draft"
    },
    {
      "passportTickets": 20,
      "endDate": "2020/04/30",
      "city": "Rapar",
      "fee": 50,
      "remainingTickets": 40,
      "expectedAttendance": "34",
      "passportOption": 1,
      "eventType": "Dancing",
      "multipleDays": false,
      "myFavourite": false,
      "passport": true,
      "subTitle": " Celebration of World Wildlife Day",
      "paid": true,
      "eventName": "Wild animal day",
      "eventImg": "https://newsd.in/wp-content/uploads/2019/03/Arua-to-Host-National-Wildlife-Day-Celebrations.png",
      "startTime": "20:04:45",
      "location": "Rajpath club",
      "promoCode": "NetPh123",
      "id": 8,
      "endTime": "15:30:45",
      "startDate": "2020/04/16",
      "status": "Draft"
    },
    {
      "passportTickets": 20,
      "endDate": "2020/04/30",
      "city": "Rapar",
      "fee": 50,
      "remainingTickets": 40,
      "expectedAttendance": "34",
      "passportOption": 1,
      "eventType": "Party",
      "multipleDays": false,
      "myFavourite": false,
      "passport": true,
      "subTitle": " Celebration of World Wildlife Day",
      "paid": true,
      "eventName": "Wild animal day",
      "eventImg": "https://newsd.in/wp-content/uploads/2019/03/Arua-to-Host-National-Wildlife-Day-Celebrations.png",
      "startTime": "20:04:45",
      "location": "Rajpath club",
      "promoCode": "NetPh123",
      "id": 9,
      "endTime": "15:30:45",
      "startDate": "2020/04/16",
      "status": "Draft"
    },
    {
      "passportTickets": 20,
      "endDate": "2020/04/19",
      "city": "Bombuflat",
      "fee": 50,
      "remainingTickets": 40,
      "expectedAttendance": "34",
      "passportOption": 1,
      "eventType": "Musician1",
      "multipleDays": false,
      "myFavourite": false,
      "passport": true,
      "subTitle": " Celebration of World Wildlife Day",
      "paid": true,
      "eventName": "sales",
      "eventImg": "https://newsd.in/wp-content/uploads/2019/03/Arua-to-Host-National-Wildlife-Day-Celebrations.png",
      "startTime": "20:04:45",
      "location": "Rajpath club",
      "promoCode": "NetPh123",
      "id": 1,
      "endTime": "15:30:45",
      "startDate": "2020/04/17",
      "status": "Draft"
    },
    {
      "passportTickets": 20,
      "endDate": "2020/04/26",
      "city": "Rapar",
      "fee": 50,
      "remainingTickets": 40,
      "expectedAttendance": "34",
      "passportOption": 1,
      "eventType": "Historical/Remembrance",
      "multipleDays": false,
      "myFavourite": false,
      "passport": true,
      "subTitle": " Celebration of World Wildlife Day",
      "paid": true,
      "eventName": "Wild animal day",
      "eventImg": "https://newsd.in/wp-content/uploads/2019/03/Arua-to-Host-National-Wildlife-Day-Celebrations.png",
      "startTime": "20:04:45",
      "location": "Rajpath club",
      "promoCode": "NetPh123",
      "id": 17,
      "endTime": "15:30:45",
      "startDate": "2020/04/17",
      "status": "Draft"
    },
    {
      "passportTickets": 20,
      "endDate": "2020/04/30",
      "city": "Rapar",
      "fee": 50,
      "remainingTickets": 40,
      "expectedAttendance": "34",
      "passportOption": 1,
      "eventType": "Party",
      "multipleDays": false,
      "myFavourite": false,
      "passport": true,
      "subTitle": " Celebration of World Wildlife Day",
      "paid": true,
      "eventName": "Wild animal day",
      "eventImg": "https://newsd.in/wp-content/uploads/2019/03/Arua-to-Host-National-Wildlife-Day-Celebrations.png",
      "startTime": "20:04:45",
      "location": "Rajpath club",
      "promoCode": "NetPh123",
      "id": 10,
      "endTime": "15:30:45",
      "startDate": "2020/04/17",
      "status": "Draft"
    },
    {
      "passportTickets": 20,
      "endDate": "2020/04/30",
      "city": "Rapar",
      "fee": 50,
      "remainingTickets": 40,
      "expectedAttendance": "34",
      "passportOption": 1,
      "eventType": "Party",
      "multipleDays": false,
      "myFavourite": false,
      "passport": true,
      "subTitle": " Celebration of World Wildlife Day",
      "paid": true,
      "eventName": "Wild animal day",
      "eventImg": "https://newsd.in/wp-content/uploads/2019/03/Arua-to-Host-National-Wildlife-Day-Celebrations.png",
      "startTime": "20:04:45",
      "location": "Rajpath club",
      "promoCode": "NetPh123",
      "id": 11,
      "endTime": "15:30:45",
      "startDate": "2020/04/17",
      "status": "Draft"
    },
    {
      "passportTickets": 20,
      "endDate": "2020/04/30",
      "city": "Rapar",
      "fee": 50,
      "remainingTickets": 40,
      "expectedAttendance": "34",
      "passportOption": 1,
      "eventType": "Charity",
      "multipleDays": false,
      "myFavourite": false,
      "passport": true,
      "subTitle": " Celebration of World Wildlife Day",
      "paid": true,
      "eventName": "Wild animal day",
      "eventImg": "https://newsd.in/wp-content/uploads/2019/03/Arua-to-Host-National-Wildlife-Day-Celebrations.png",
      "startTime": "20:04:45",
      "location": "Rajpath club",
      "promoCode": "NetPh123",
      "id": 12,
      "endTime": "15:30:45",
      "startDate": "2020/04/17",
      "status": "Draft"
    },
    {
      "passportTickets": 20,
      "endDate": "2020/04/30",
      "city": "Rapar",
      "fee": 50,
      "remainingTickets": 40,
      "expectedAttendance": "34",
      "passportOption": 1,
      "eventType": "Charity",
      "multipleDays": false,
      "myFavourite": false,
      "passport": true,
      "subTitle": " Celebration of World Wildlife Day",
      "paid": true,
      "eventName": "Wild animal day",
      "eventImg": "https://newsd.in/wp-content/uploads/2019/03/Arua-to-Host-National-Wildlife-Day-Celebrations.png",
      "startTime": "20:04:45",
      "location": "Rajpath club",
      "promoCode": "NetPh123",
      "id": 13,
      "endTime": "15:30:45",
      "startDate": "2020/04/17",
      "status": "Draft"
    },
    {
      "passportTickets": 20,
      "endDate": "2020/04/26",
      "city": "Rapar",
      "fee": 50,
      "remainingTickets": 40,
      "expectedAttendance": "34",
      "passportOption": 1,
      "eventType": "Non-profit",
      "multipleDays": false,
      "myFavourite": false,
      "passport": true,
      "subTitle": " Celebration of World Wildlife Day",
      "paid": true,
      "eventName": "Wild animal day",
      "eventImg": "https://newsd.in/wp-content/uploads/2019/03/Arua-to-Host-National-Wildlife-Day-Celebrations.png",
      "startTime": "20:04:45",
      "location": "Rajpath club",
      "promoCode": "NetPh123",
      "id": 14,
      "endTime": "15:30:45",
      "startDate": "2020/04/17",
      "status": "Draft"
    },
    {
      "passportTickets": 20,
      "endDate": "2020/04/26",
      "city": "Rapar",
      "fee": 50,
      "remainingTickets": 40,
      "expectedAttendance": "34",
      "passportOption": 1,
      "eventType": "Non-profit",
      "multipleDays": false,
      "myFavourite": false,
      "passport": true,
      "subTitle": " Celebration of World Wildlife Day",
      "paid": true,
      "eventName": "Wild animal day",
      "eventImg": "https://newsd.in/wp-content/uploads/2019/03/Arua-to-Host-National-Wildlife-Day-Celebrations.png",
      "startTime": "20:04:45",
      "location": "Rajpath club",
      "promoCode": "NetPh123",
      "id": 15,
      "endTime": "15:30:45",
      "startDate": "2020/04/17",
      "status": "Draft"
    },
    {
      "passportTickets": 20,
      "endDate": "2020/04/26",
      "city": "Rapar",
      "fee": 50,
      "remainingTickets": 40,
      "expectedAttendance": "34",
      "passportOption": 1,
      "eventType": "Rotary - Service Club",
      "multipleDays": false,
      "myFavourite": false,
      "passport": true,
      "subTitle": " Celebration of World Wildlife Day",
      "paid": true,
      "eventName": "Wild animal day",
      "eventImg": "https://newsd.in/wp-content/uploads/2019/03/Arua-to-Host-National-Wildlife-Day-Celebrations.png",
      "startTime": "20:04:45",
      "location": "Rajpath club",
      "promoCode": "NetPh123",
      "id": 16,
      "endTime": "15:30:45",
      "startDate": "2020/04/17",
      "status": "Draft"
    },
    {
      "passportTickets": 20,
      "endDate": "2020/04/30",
      "city": "Rapar",
      "fee": 50,
      "remainingTickets": 40,
      "expectedAttendance": "34",
      "passportOption": 1,
      "eventType": "Product Launches",
      "multipleDays": false,
      "myFavourite": false,
      "passport": true,
      "subTitle": " Celebration of World Wildlife Day",
      "paid": true,
      "eventName": "Wild animal day",
      "eventImg": "https://newsd.in/wp-content/uploads/2019/03/Arua-to-Host-National-Wildlife-Day-Celebrations.png",
      "startTime": "20:04:45",
      "location": "Rajpath club",
      "promoCode": "NetPh123",
      "id": 4,
      "endTime": "15:30:45",
      "startDate": "2020/04/20",
      "status": "Draft"
    }
  ]
}

1 Ответ

0 голосов
/ 24 апреля 2020

Вы можете использовать оператор sql, подобный этому, чтобы выбрать необходимые данные в одном go.

select * from (
    select e.*,
        count(*) over (partition by eventDate order by id) cnt 
    from Events e where eventDate between :startDate and :endDate
) e where e.cnt <= 6

Конечно, вам придется адаптировать имена столбцов. Возможно, вам придется использовать trunc или format на eventDate, чтобы превратить разные метки времени в одну и ту же дату, в зависимости от того, как хранятся ваши данные.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...