Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
3.9 kB
2
Indexable
Never
package br.com.senior.erpxcomven.pedido.predicate;

import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Arrays;
import java.util.Date;
import java.util.Objects;
import java.util.UUID;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import br.com.senior.erpxcomven.foundation.QE085pesEntity;
import br.com.senior.erpxcomven.pedido.Customer;
import br.com.senior.erpxcomven.pedido.EnumSaleIntegrationStatus;
import br.com.senior.erpxcomven.pedido.EnumSitPed;
import br.com.senior.erpxcomven.pedido.OrderStatus;
import br.com.senior.erpxcomven.pedido.QE120pedEntity;
import br.com.senior.erpxcomven.pedido.QE120pedIntegrationEntity;
import br.com.senior.erpxcomven.utils.PredicateBase;

@Component
@Scope("prototype")
public class ListOrdersPredicate extends PredicateBase<ListOrdersPredicate> {

    private static final int MINUTES = 59;
    private static final int HOUR = 23;
    private static final int SECONDS = 59;

    public ListOrdersPredicate filterFinalIssueDate(LocalDate datEmi) {
        if (Objects.nonNull(datEmi)) {
            var dateFromLocalDate = Date.from(datEmi.atTime(HOUR, MINUTES, SECONDS).atZone(ZoneId.systemDefault()).toInstant());
            getBooleanBuilder().and(QE120pedEntity.e120pedEntity.dthEmi.loe(dateFromLocalDate));
        }
        return this;
    }

    public ListOrdersPredicate filterInitialIssueDate(LocalDate datEmi) {
        if (Objects.nonNull(datEmi)) {
            var dateFromLocalDate = Date.from(datEmi.atStartOfDay(ZoneId.systemDefault()).toInstant());
            getBooleanBuilder().and(QE120pedEntity.e120pedEntity.dthEmi.goe(dateFromLocalDate));
        }
        return this;
    }

    public ListOrdersPredicate filterCliente(Customer e001pesCli) {
        if (Objects.nonNull(e001pesCli)) {
            if (Objects.nonNull(e001pesCli.id)) {
                getBooleanBuilder().and(QE120pedEntity.e120pedEntity.e001pesCli.eq(UUID.fromString(e001pesCli.id)));
            } else {
                if(Objects.nonNull(e001pesCli.code)) {
                    getBooleanBuilder().and(QE085pesEntity.e085pesEntity.e001pes.codPes.eq(Long.valueOf(e001pesCli.code)));
                }
            }
        }
        return this;
    }

    public ListOrdersPredicate filterSituacaoPedido(OrderStatus status) {
        if (Objects.nonNull(status)) {
            if (status == OrderStatus.CANCELED) {
                getBooleanBuilder().and(QE120pedEntity.e120pedEntity.sitPed.eq(EnumSitPed.V5));
            } else if (status == OrderStatus.INVOICED) {
                getBooleanBuilder().and(QE120pedEntity.e120pedEntity.sitPed.eq(EnumSitPed.V4));
            } else if (status == OrderStatus.NOT_PAID) {
                getBooleanBuilder().and(QE120pedEntity.e120pedEntity.sitPed.in(Arrays.asList(EnumSitPed.V1, EnumSitPed.V2)).and(QE120pedEntity.e120pedEntity.pedBlo.isTrue()));
            } else if (status == OrderStatus.PAID) {
                getBooleanBuilder().and(QE120pedEntity.e120pedEntity.sitPed.in(Arrays.asList(EnumSitPed.V1, EnumSitPed.V2)).and(QE120pedEntity.e120pedEntity.pedBlo.isFalse()));
            } else if (status == OrderStatus.IN_PROGRESS) {
                getBooleanBuilder().and(QE120pedEntity.e120pedEntity.sitPed.notIn(Arrays.asList(EnumSitPed.V5, EnumSitPed.V4)));
                getBooleanBuilder().and(QE120pedEntity.e120pedEntity.sitPed.in(Arrays.asList(EnumSitPed.V1, EnumSitPed.V2)).and(QE120pedEntity.e120pedEntity.pedBlo.isFalse()).not());
            } else {
                if (status == OrderStatus.ERROR) {
                    getBooleanBuilder().and(QE120pedIntegrationEntity.e120pedIntegrationEntity.sitInt.eq(EnumSaleIntegrationStatus.INTEGRACAO_FINALIZADA_ERRO));
                }
            }
        }
        return this;
    }

}