Untitled

mail@pastecode.io avatar
unknown
plain_text
24 days ago
4.0 kB
4
Indexable
Never
 @Override
    public Page<RequestEntity> searchC06Report(RequestReq requestForm, Pageable pageable) throws Exception {
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<RequestEntity> cq = cb.createQuery(RequestEntity.class);
        Root<RequestEntity> requestRoot = cq.from(RequestEntity.class);

        List<Predicate> predicates = new ArrayList<>();

        if (StringUtils.isNotBlank(requestForm.getCif())) {
            predicates.add(cb.like(requestRoot.get("cif").as(String.class), "%" + requestForm.getCif().trim()  + "%"));
        }

        if (StringUtils.isNotBlank(requestForm.getRequestId())) {
            predicates.add(cb.like(requestRoot.get("requestId").as(String.class), "%" + requestForm.getRequestId().trim()  + "%"));
        }

        if(StringUtils.isEmpty(requestForm.getFromDate()) || StringUtils.isEmpty(requestForm.getToDate())) {
            LocalDateTime currentDate = LocalDateTime.now();
            predicates.add(cb.greaterThanOrEqualTo(requestRoot.get("time"), currentDate.minusDays(30)));
            predicates.add(cb.lessThanOrEqualTo(requestRoot.get("time"), currentDate));
        } else {
            LocalDateTime fromDate = WebUtils.convertStringToLocalDateTime01(requestForm.getFromDate());
            LocalDateTime toDate = Objects.requireNonNull(WebUtils.convertStringToLocalDateTime01(requestForm.getToDate())).plusHours(23).plusMinutes(59);
            assert fromDate != null;
            if (fromDate.isAfter(toDate)) {
                throw new Exception(Commons.DATE_ERROR);
            }
            predicates.add(cb.greaterThanOrEqualTo(requestRoot.get("time"), fromDate));
            predicates.add(cb.lessThanOrEqualTo(requestRoot.get("time"), toDate));
        }

        predicates.add(cb.isNull(requestRoot.get("lstErrorCode")));
        cq.where(predicates.toArray(new Predicate[]{}));
        cq.orderBy(cb.desc(requestRoot.get("time")));

        List<RequestEntity> results = em.createQuery(cq)
                .setFirstResult((int) pageable.getOffset())
                .setMaxResults(pageable.getPageSize())
                .getResultList();

//        CriteriaQuery<Long> countQuery = cb.createQuery(Long.class);
//        countQuery.select(cb.count(countQuery.from(RequestEntity.class)));
//        countQuery.where(predicates.toArray(new Predicate[]{}));
//        Root<RequestEntity> countRoot = countQuery.from(RequestEntity.class);
//        countQuery.select(cb.count(countRoot));
//        countQuery.where(predicates.toArray(new Predicate[]{}));
        long total =  em.createQuery(cq).getResultList().size();

        return new PageImpl<>(results, pageable, total);
    }



... 
// controller
@PreAuthorize("@permissionServiceImpl.magReportEkycRisk")
@RequestMapping("/ekyc_c06_report")
@RestController
@RequiredArgsConstructor
@Scope("request")
public class ReportC06Controller {
    private static final Logger LOGGER = LoggerFactory.getLogger(ConfigController.class);
    private final RequestService requestService;

    @GetMapping
    @PreAuthorize("@permissionServiceImpl.isGrantView('REPORT_BY_C06')")
    public ResponseEntity<?> search(RequestReq requestForm) {
        LOGGER.debug("Call api search ekyc c06 report: {}", requestForm);
        BaseResponse response = new BaseResponse();
        try {
            Pageable pageable = PageRequest.of(
                    requestForm.getPage(),
                    requestForm.getSize());
            Page<RequestEntity> requests = requestService.searchC06Report(requestForm, pageable);
            response.setCode(Commons.OK);
            response.setData(requests);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            response.setCode(Commons.ERROR);
            response.setMessage(e.getMessage());
            return ResponseEntity.ok(response);
        }
    }
}
Leave a Comment