mirror of
https://git.imnavajas.es/jjimenez/erp-imprimelibros.git
synced 2026-01-13 00:48:49 +00:00
trabajando en devoluciones
This commit is contained in:
@ -4,20 +4,15 @@ import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.context.MessageSource;
|
||||
import org.springframework.data.jpa.domain.Specification;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
|
||||
import com.imprimelibros.erp.payments.model.PaymentTransactionStatus.*;
|
||||
|
||||
import com.imprimelibros.erp.common.Utils;
|
||||
import com.imprimelibros.erp.configuracion.margenes_presupuestos.MargenPresupuesto;
|
||||
import com.imprimelibros.erp.datatables.DataTable;
|
||||
import com.imprimelibros.erp.datatables.DataTablesParser;
|
||||
import com.imprimelibros.erp.datatables.DataTablesRequest;
|
||||
@ -25,100 +20,128 @@ import com.imprimelibros.erp.datatables.DataTablesResponse;
|
||||
import com.imprimelibros.erp.payments.model.Payment;
|
||||
import com.imprimelibros.erp.payments.model.PaymentTransaction;
|
||||
import com.imprimelibros.erp.payments.model.PaymentTransactionStatus;
|
||||
import com.imprimelibros.erp.payments.model.PaymentTransactionType;
|
||||
import com.imprimelibros.erp.payments.repo.PaymentTransactionRepository;
|
||||
import com.imprimelibros.erp.users.User;
|
||||
import com.imprimelibros.erp.users.UserDao;
|
||||
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
|
||||
|
||||
|
||||
@Controller
|
||||
@RequestMapping("/pagos")
|
||||
@PreAuthorize("hasRole('SUPERADMIN')")
|
||||
public class PaymentController {
|
||||
|
||||
private final MessageSource messageSource;
|
||||
|
||||
protected final PaymentTransactionRepository repoPaymentTransaction;
|
||||
protected final UserDao repoUser;
|
||||
|
||||
public PaymentController(PaymentTransactionRepository repoPaymentTransaction, UserDao repoUser) {
|
||||
public PaymentController(PaymentTransactionRepository repoPaymentTransaction, UserDao repoUser,
|
||||
MessageSource messageSource) {
|
||||
this.repoPaymentTransaction = repoPaymentTransaction;
|
||||
this.repoUser = repoUser;
|
||||
this.messageSource = messageSource;
|
||||
}
|
||||
|
||||
@GetMapping()
|
||||
public String index() {
|
||||
return "imprimelibros/pagos/gestion-pagos";
|
||||
}
|
||||
|
||||
|
||||
@GetMapping(value = "datatable/redsys", produces = "application/json")
|
||||
@ResponseBody
|
||||
public DataTablesResponse<Map<String, Object>> getDatatableRedsys(HttpServletRequest request,Locale locale) {
|
||||
|
||||
public DataTablesResponse<Map<String, Object>> getDatatableRedsys(HttpServletRequest request, Locale locale) {
|
||||
|
||||
DataTablesRequest dt = DataTablesParser.from(request);
|
||||
|
||||
List<String> searchable = List.of(
|
||||
);
|
||||
"payment.gatewayOrderId",
|
||||
"payment.orderId"
|
||||
// "client" no, porque lo calculas a posteriori
|
||||
);
|
||||
|
||||
// Campos ordenables
|
||||
List<String> orderable = List.of(
|
||||
|
||||
);
|
||||
|
||||
"payment.gatewayOrderId",
|
||||
"payment.orderId",
|
||||
"amountCents",
|
||||
"payment.amountRefundedCents",
|
||||
"createdAt");
|
||||
|
||||
Specification<PaymentTransaction> base = Specification.allOf(
|
||||
(root, query, cb) -> cb.equal(root.get("status"), PaymentTransactionStatus.succeeded));
|
||||
|
||||
base = base.and((root, query, cb) -> cb.equal(root.get("type"), PaymentTransactionType.CAPTURE));
|
||||
|
||||
String clientSearch = dt.getColumnSearch("client");
|
||||
|
||||
// 2) Si hay filtro, traducirlo a userIds y añadirlo al Specification
|
||||
if (clientSearch != null) {
|
||||
List<Long> userIds = repoUser.findIdsByFullNameLike(clientSearch.trim());
|
||||
|
||||
if (userIds.isEmpty()) {
|
||||
// Ningún usuario coincide → forzamos 0 resultados
|
||||
base = base.and((root, query, cb) -> cb.disjunction());
|
||||
} else {
|
||||
base = base.and((root, query, cb) -> root.join("payment").get("userId").in(userIds));
|
||||
}
|
||||
}
|
||||
Long total = repoPaymentTransaction.count(base);
|
||||
|
||||
return DataTable
|
||||
.of(repoPaymentTransaction, PaymentTransaction.class, dt, searchable) // 'searchable' en DataTable.java
|
||||
// edita columnas "reales":
|
||||
.of(repoPaymentTransaction, PaymentTransaction.class, dt, searchable)
|
||||
.orderable(orderable)
|
||||
.add("created_at", (pago) -> {
|
||||
return Utils.formatDateTime(pago.getCreatedAt(), locale);
|
||||
})
|
||||
.add("client", (pago) -> {
|
||||
.add("created_at", pago -> Utils.formatDateTime(pago.getCreatedAt(), locale))
|
||||
.add("client", pago -> {
|
||||
if (pago.getPayment() != null && pago.getPayment().getUserId() != null) {
|
||||
Payment payment = pago.getPayment();
|
||||
if(payment.getUserId() != null) {
|
||||
Optional<User> user = repoUser.findById(payment.getUserId());
|
||||
if (payment.getUserId() != null) {
|
||||
Optional<User> user = repoUser.findById(payment.getUserId().longValue());
|
||||
return user.map(User::getFullName).orElse("");
|
||||
}
|
||||
return "";
|
||||
} else {
|
||||
return "";
|
||||
}
|
||||
return "";
|
||||
})
|
||||
.add("gateway_order_id", (pago) -> {
|
||||
.add("gateway_order_id", pago -> {
|
||||
if (pago.getPayment() != null) {
|
||||
return pago.getPayment().getGatewayOrderId();
|
||||
} else {
|
||||
return "";
|
||||
}
|
||||
})
|
||||
.add("orderId", (pago) -> {
|
||||
.add("orderId", pago -> {
|
||||
if (pago.getPayment() != null && pago.getPayment().getOrderId() != null) {
|
||||
return pago.getPayment().getOrderId().toString();
|
||||
} else {
|
||||
return "";
|
||||
}
|
||||
})
|
||||
.add("amount_cents", (pago) -> {
|
||||
return Utils.formatCurrency(pago.getAmountCents() / 100.0, locale);
|
||||
.add("amount_cents", pago -> Utils.formatCurrency(pago.getAmountCents() / 100.0, locale))
|
||||
.add("amount_cents_refund", pago -> {
|
||||
Payment payment = pago.getPayment();
|
||||
if (payment != null) {
|
||||
return Utils.formatCurrency(payment.getAmountRefundedCents() / 100.0, locale);
|
||||
}
|
||||
return "";
|
||||
})
|
||||
.add("actions", (pago) -> {
|
||||
return "<div class=\"hstack gap-3 flex-wrap\">\n" +
|
||||
" <a href=\"javascript:void(0);\" data-id=\"" + pago.getId()
|
||||
+ "\" class=\"link-success btn-edit-pago fs-15\"><i class=\"ri-edit-2-line\"></i></a>\n"
|
||||
+ " <a href=\"javascript:void(0);\" data-id=\"" + pago.getId()
|
||||
+ "\" class=\"link-danger btn-delete-pago fs-15\"><i class=\"ri-delete-bin-5-line\"></i></a>\n"
|
||||
+ " </div>";
|
||||
.add("actions", pago -> {
|
||||
Payment p = pago.getPayment();
|
||||
if (p != null) {
|
||||
if (pago.getAmountCents() - p.getAmountRefundedCents() > 0) {
|
||||
return "<span class=\'badge bg-secondary btn-refund-payment \' data-dsOrderId=\'"
|
||||
+ p.getGatewayOrderId()
|
||||
+ "\' data-transactionId=\'" + pago.getPayment().getId()
|
||||
+ "\' style=\'cursor: pointer;\'>"
|
||||
+ messageSource.getMessage("pagos.table.devuelto", null, locale) + "</span>";
|
||||
}
|
||||
return "";
|
||||
} else {
|
||||
return "";
|
||||
}
|
||||
})
|
||||
.where(base)
|
||||
// Filtros custom:
|
||||
.toJson(total);
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@ -41,7 +41,6 @@ public class PaymentService {
|
||||
this.webhookEventRepo = webhookEventRepo;
|
||||
this.cartService = cartService;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Crea el Payment en BD y construye el formulario de Redsys usando la API
|
||||
@ -54,7 +53,7 @@ public class PaymentService {
|
||||
p.setOrderId(null);
|
||||
|
||||
Cart cart = this.cartService.findById(cartId);
|
||||
if(cart != null && cart.getUserId() != null) {
|
||||
if (cart != null && cart.getUserId() != null) {
|
||||
p.setUserId(cart.getUserId());
|
||||
}
|
||||
p.setCurrency(currency);
|
||||
@ -198,21 +197,22 @@ public class PaymentService {
|
||||
p.setFailedAt(LocalDateTime.now());
|
||||
}
|
||||
|
||||
if(authorized) {
|
||||
if (authorized) {
|
||||
// GENERAR PEDIDO A PARTIR DEL CARRITO
|
||||
Cart cart = this.cartService.findById(notif.cartId);
|
||||
if(cart != null) {
|
||||
if (cart != null) {
|
||||
// Bloqueamos el carrito
|
||||
this.cartService.lockCartById(cart.getId());
|
||||
// order ID es generado dentro de createOrderFromCart donde se marcan los presupuestos como no editables
|
||||
// Long orderId = this.cartService.pedidoService.createOrderFromCart(cart.getId(), p.getId());
|
||||
// order ID es generado dentro de createOrderFromCart donde se marcan los
|
||||
// presupuestos como no editables
|
||||
// Long orderId =
|
||||
// this.cartService.pedidoService.createOrderFromCart(cart.getId(), p.getId());
|
||||
// p.setOrderId(orderId);
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
payRepo.save(p);
|
||||
|
||||
|
||||
if (!authorized) {
|
||||
ev.setLastError("Payment declined (Ds_Response=" + notif.response + ")");
|
||||
@ -230,9 +230,8 @@ public class PaymentService {
|
||||
}
|
||||
}
|
||||
|
||||
// ---- refundViaRedsys y bank_transfer igual que antes, no tocan RedsysService
|
||||
// ---- refundViaRedsys
|
||||
// ----
|
||||
|
||||
@Transactional
|
||||
public void refundViaRedsys(Long paymentId, long amountCents, String idempotencyKey) {
|
||||
Payment p = payRepo.findById(paymentId)
|
||||
@ -240,6 +239,7 @@ public class PaymentService {
|
||||
|
||||
if (amountCents <= 0)
|
||||
throw new IllegalArgumentException("Importe inválido");
|
||||
|
||||
long maxRefundable = p.getAmountCapturedCents() - p.getAmountRefundedCents();
|
||||
if (amountCents > maxRefundable)
|
||||
throw new IllegalStateException("Importe de devolución supera lo capturado");
|
||||
@ -256,8 +256,18 @@ public class PaymentService {
|
||||
r.setRequestedAt(LocalDateTime.now());
|
||||
r = refundRepo.save(r);
|
||||
|
||||
String gatewayRefundId = "REF-" + UUID.randomUUID(); // aquí iría el ID real si alguna vez llamas a un API de
|
||||
// devoluciones
|
||||
String gatewayRefundId;
|
||||
try {
|
||||
// ⚠️ Usa aquí el mismo valor que mandaste en Ds_Merchant_Order al cobrar
|
||||
// por ejemplo, p.getGatewayOrderId() o similar
|
||||
String originalOrder = p.getGatewayOrderId(); // ajusta al nombre real del campo
|
||||
gatewayRefundId = redsysService.requestRefund(originalOrder, amountCents);
|
||||
} catch (Exception e) {
|
||||
r.setStatus(RefundStatus.failed);
|
||||
r.setProcessedAt(LocalDateTime.now());
|
||||
refundRepo.save(r);
|
||||
throw new IllegalStateException("Error al solicitar la devolución a Redsys", e);
|
||||
}
|
||||
|
||||
PaymentTransaction tx = new PaymentTransaction();
|
||||
tx.setPayment(p);
|
||||
@ -285,13 +295,14 @@ public class PaymentService {
|
||||
payRepo.save(p);
|
||||
}
|
||||
|
||||
|
||||
@Transactional
|
||||
public Payment createBankTransferPayment(Long cartId, long amountCents, String currency) {
|
||||
Payment p = new Payment();
|
||||
p.setOrderId(null);
|
||||
|
||||
Cart cart = this.cartService.findById(cartId);
|
||||
if(cart != null && cart.getUserId() != null) {
|
||||
if (cart != null && cart.getUserId() != null) {
|
||||
p.setUserId(cart.getUserId());
|
||||
}
|
||||
|
||||
|
||||
@ -8,6 +8,11 @@ import sis.redsys.api.ApiMacSha256;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import java.net.URI;
|
||||
import java.net.URLEncoder;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.MessageDigest;
|
||||
import java.util.Base64;
|
||||
@ -18,6 +23,10 @@ import java.util.Objects;
|
||||
public class RedsysService {
|
||||
|
||||
// ---------- CONFIG ----------
|
||||
@Value("${redsys.url}")
|
||||
private String url;
|
||||
@Value("${redsys.refund.url}")
|
||||
private String urlRefund;
|
||||
@Value("${redsys.merchant-code}")
|
||||
private String merchantCode;
|
||||
@Value("${redsys.terminal}")
|
||||
@ -37,6 +46,8 @@ public class RedsysService {
|
||||
@Value("${redsys.environment}")
|
||||
private String env;
|
||||
|
||||
private final HttpClient httpClient = HttpClient.newHttpClient();
|
||||
|
||||
// ---------- RECORDS ----------
|
||||
// Pedido a Redsys
|
||||
public record PaymentRequest(String order, long amountCents, String description, Long cartId) {
|
||||
@ -89,9 +100,11 @@ public class RedsysService {
|
||||
String merchantParameters = api.createMerchantParameters();
|
||||
String signature = api.createMerchantSignature(secretKeyBase64);
|
||||
|
||||
String action = "test".equalsIgnoreCase(env)
|
||||
? "https://sis-t.redsys.es:25443/sis/realizarPago"
|
||||
: "https://sis.redsys.es/sis/realizarPago";
|
||||
String action = url;
|
||||
/*
|
||||
* ? "https://sis-t.redsys.es:25443/sis/realizarPago"
|
||||
* : "https://sis.redsys.es/sis/realizarPago";
|
||||
*/
|
||||
|
||||
return new FormPayload(action, "HMAC_SHA256_V1", merchantParameters, signature);
|
||||
}
|
||||
@ -231,4 +244,84 @@ public class RedsysService {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Solicita a Redsys una devolución (TransactionType = 3)
|
||||
*
|
||||
* @param order El mismo Ds_Merchant_Order que se usó en el cobro.
|
||||
* @param amountCents Importe en céntimos a devolver.
|
||||
* @return gatewayRefundId (p.ej. Ds_AuthorisationCode o Ds_Order)
|
||||
*/
|
||||
public String requestRefund(String order, long amountCents) throws Exception {
|
||||
ApiMacSha256 api = new ApiMacSha256();
|
||||
|
||||
// Montar parámetros para el refund
|
||||
api.setParameter("DS_MERCHANT_MERCHANTCODE", merchantCode);
|
||||
api.setParameter("DS_MERCHANT_TERMINAL", terminal);
|
||||
api.setParameter("DS_MERCHANT_ORDER", order);
|
||||
api.setParameter("DS_MERCHANT_AMOUNT", String.valueOf(amountCents));
|
||||
api.setParameter("DS_MERCHANT_CURRENCY", currency);
|
||||
api.setParameter("DS_MERCHANT_TRANSACTIONTYPE", "3"); // 3 = devolución
|
||||
api.setParameter("DS_MERCHANT_MERCHANTURL", "");
|
||||
api.setParameter("DS_MERCHANT_URLOK", "");
|
||||
api.setParameter("DS_MERCHANT_URLKO", "");
|
||||
|
||||
// Crear parámetros y firma (como en tu PHP)
|
||||
String merchantParameters = api.createMerchantParameters();
|
||||
String signature = api.createMerchantSignature(secretKeyBase64);
|
||||
|
||||
// Montar el JSON para Redsys REST
|
||||
String json = """
|
||||
{
|
||||
"Ds_MerchantParameters": "%s",
|
||||
"Ds_Signature": "%s",
|
||||
"Ds_SignatureVersion": "HMAC_SHA256_V1"
|
||||
}
|
||||
""".formatted(merchantParameters, signature);
|
||||
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.uri(URI.create(urlRefund))
|
||||
.header("Content-Type", "application/json; charset=UTF-8")
|
||||
.POST(HttpRequest.BodyPublishers.ofString(json))
|
||||
.build();
|
||||
|
||||
HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
|
||||
|
||||
System.out.println("### Redsys refund REST request:\n" + json);
|
||||
System.out.println("### HTTP " + response.statusCode());
|
||||
System.out.println("### Redsys refund REST response:\n" + response.body());
|
||||
|
||||
if (response.statusCode() / 100 != 2)
|
||||
throw new IllegalStateException("HTTP error Redsys refund: " + response.statusCode());
|
||||
|
||||
if (response.body() == null || response.body().isBlank())
|
||||
throw new IllegalStateException("Respuesta vacía de Redsys refund REST");
|
||||
|
||||
// Parsear la respuesta JSON
|
||||
Map<String, Object> respMap = MAPPER.readValue(response.body(), new TypeReference<>() {
|
||||
});
|
||||
|
||||
// Redsys puede devolver "Ds_MerchantParameters" o "errorCode"
|
||||
if (respMap.containsKey("errorCode")) {
|
||||
throw new IllegalStateException("Error Redsys refund: " + respMap.get("errorCode"));
|
||||
}
|
||||
|
||||
String dsMerchantParametersResp = (String) respMap.get("Ds_MerchantParameters");
|
||||
if (dsMerchantParametersResp == null) {
|
||||
throw new IllegalStateException("Respuesta Redsys refund sin Ds_MerchantParameters");
|
||||
}
|
||||
|
||||
// Decodificar MerchantParameters de la respuesta
|
||||
Map<String, Object> decoded = decodeMerchantParametersToMap(dsMerchantParametersResp);
|
||||
System.out.println("### Redsys refund decoded response:\n" + decoded);
|
||||
|
||||
String dsResponse = String.valueOf(decoded.get("Ds_Response"));
|
||||
if (!"0900".equals(dsResponse)) {
|
||||
throw new IllegalStateException("Devolución rechazada, Ds_Response=" + dsResponse);
|
||||
}
|
||||
|
||||
return String.valueOf(decoded.getOrDefault("Ds_AuthorisationCode", order));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@ -19,60 +19,63 @@ import org.springframework.lang.Nullable;
|
||||
@Repository
|
||||
public interface UserDao extends JpaRepository<User, Long>, JpaSpecificationExecutor<User> {
|
||||
|
||||
// Aplicamos EntityGraph a la versión con Specification+Pageable
|
||||
@Override
|
||||
@EntityGraph(attributePaths = { "rolesLink", "rolesLink.role" })
|
||||
@NonNull
|
||||
Page<User> findAll(@Nullable Specification<User> spec, @NonNull Pageable pageable);
|
||||
// Aplicamos EntityGraph a la versión con Specification+Pageable
|
||||
@Override
|
||||
@EntityGraph(attributePaths = { "rolesLink", "rolesLink.role" })
|
||||
@NonNull
|
||||
Page<User> findAll(@Nullable Specification<User> spec, @NonNull Pageable pageable);
|
||||
|
||||
Optional<User> findByUserNameIgnoreCase(String userName);
|
||||
Optional<User> findByUserNameIgnoreCase(String userName);
|
||||
|
||||
boolean existsByUserNameIgnoreCase(String userName);
|
||||
boolean existsByUserNameIgnoreCase(String userName);
|
||||
|
||||
// Para comprobar si existe al hacer signup
|
||||
@Query(value = """
|
||||
SELECT id, deleted, enabled
|
||||
FROM users
|
||||
WHERE LOWER(username) = LOWER(:userName)
|
||||
LIMIT 1
|
||||
""", nativeQuery = true)
|
||||
Optional<UserLite> findLiteByUserNameIgnoreCase(@Param("userName") String userName);
|
||||
// Para comprobar si existe al hacer signup
|
||||
@Query(value = """
|
||||
SELECT id, deleted, enabled
|
||||
FROM users
|
||||
WHERE LOWER(username) = LOWER(:userName)
|
||||
LIMIT 1
|
||||
""", nativeQuery = true)
|
||||
Optional<UserLite> findLiteByUserNameIgnoreCase(@Param("userName") String userName);
|
||||
|
||||
boolean existsByUserNameIgnoreCaseAndIdNot(String userName, Long id);
|
||||
boolean existsByUserNameIgnoreCaseAndIdNot(String userName, Long id);
|
||||
|
||||
// Nuevo: para login/negocio "activo"
|
||||
@EntityGraph(attributePaths = { "rolesLink", "rolesLink.role" })
|
||||
Optional<User> findByUserNameIgnoreCaseAndEnabledTrueAndDeletedFalse(String userName);
|
||||
// Nuevo: para login/negocio "activo"
|
||||
@EntityGraph(attributePaths = { "rolesLink", "rolesLink.role" })
|
||||
Optional<User> findByUserNameIgnoreCaseAndEnabledTrueAndDeletedFalse(String userName);
|
||||
|
||||
// Para poder restaurar, necesitas leer ignorando @Where (native):
|
||||
@Query(value = "SELECT * FROM users WHERE id = :id", nativeQuery = true)
|
||||
Optional<User> findByIdIncludingDeleted(@Param("id") Long id);
|
||||
// Para poder restaurar, necesitas leer ignorando @Where (native):
|
||||
@Query(value = "SELECT * FROM users WHERE id = :id", nativeQuery = true)
|
||||
Optional<User> findByIdIncludingDeleted(@Param("id") Long id);
|
||||
|
||||
@Query(value = "SELECT * FROM users WHERE deleted = TRUE", nativeQuery = true)
|
||||
List<User> findAllDeleted();
|
||||
@Query(value = "SELECT * FROM users WHERE deleted = TRUE", nativeQuery = true)
|
||||
List<User> findAllDeleted();
|
||||
|
||||
@Query("select u.id from User u where lower(u.userName) = lower(:userName)")
|
||||
Optional<Long> findIdByUserNameIgnoreCase(@Param("userName") String userName);
|
||||
@Query("select u.id from User u where lower(u.userName) = lower(:userName)")
|
||||
Optional<Long> findIdByUserNameIgnoreCase(@Param("userName") String userName);
|
||||
|
||||
@Query(value = """
|
||||
SELECT DISTINCT u
|
||||
FROM User u
|
||||
JOIN u.rolesLink rl
|
||||
JOIN rl.role r
|
||||
WHERE (:role IS NULL OR r.name = :role)
|
||||
AND (:q IS NULL OR LOWER(u.fullName) LIKE LOWER(CONCAT('%', :q, '%'))
|
||||
OR LOWER(u.userName) LIKE LOWER(CONCAT('%', :q, '%')))
|
||||
""", countQuery = """
|
||||
SELECT COUNT(DISTINCT u.id)
|
||||
FROM User u
|
||||
JOIN u.rolesLink rl
|
||||
JOIN rl.role r
|
||||
WHERE (:role IS NULL OR r.name = :role)
|
||||
AND (:q IS NULL OR LOWER(u.fullName) LIKE LOWER(CONCAT('%', :q, '%'))
|
||||
OR LOWER(u.userName) LIKE LOWER(CONCAT('%', :q, '%')))
|
||||
""")
|
||||
Page<User> searchUsers(@Param("role") String role,
|
||||
@Param("q") String q,
|
||||
Pageable pageable);
|
||||
@Query(value = """
|
||||
SELECT DISTINCT u
|
||||
FROM User u
|
||||
JOIN u.rolesLink rl
|
||||
JOIN rl.role r
|
||||
WHERE (:role IS NULL OR r.name = :role)
|
||||
AND (:q IS NULL OR LOWER(u.fullName) LIKE LOWER(CONCAT('%', :q, '%'))
|
||||
OR LOWER(u.userName) LIKE LOWER(CONCAT('%', :q, '%')))
|
||||
""", countQuery = """
|
||||
SELECT COUNT(DISTINCT u.id)
|
||||
FROM User u
|
||||
JOIN u.rolesLink rl
|
||||
JOIN rl.role r
|
||||
WHERE (:role IS NULL OR r.name = :role)
|
||||
AND (:q IS NULL OR LOWER(u.fullName) LIKE LOWER(CONCAT('%', :q, '%'))
|
||||
OR LOWER(u.userName) LIKE LOWER(CONCAT('%', :q, '%')))
|
||||
""")
|
||||
Page<User> searchUsers(@Param("role") String role,
|
||||
@Param("q") String q,
|
||||
Pageable pageable);
|
||||
|
||||
@Query("select u.id from User u where lower(u.fullName) like lower(concat('%', :name, '%'))")
|
||||
List<Long> findIdsByFullNameLike(@Param("name") String name);
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user