Un peu de tout
This commit is contained in:
@@ -8,6 +8,9 @@ import fr.eni.enchere.controllers.AccueilController;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageImpl;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.jdbc.core.RowMapper;
|
||||
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
|
||||
@@ -87,7 +90,7 @@ public class ArticleRepositoryImpl implements ArticleRepository {
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Article> searchArticle(SearchArticleCritere critere) {
|
||||
public Page<Article> searchArticlePageable(SearchArticleCritere critere, Pageable pageable) {
|
||||
StringBuilder sql = new StringBuilder("SELECT DISTINCT a.*, u.* FROM ARTICLES_VENDUS a ");
|
||||
sql.append("JOIN UTILISATEURS u ON a.no_utilisateur = u.no_utilisateur ");
|
||||
sql.append("LEFT JOIN ENCHERES e ON a.no_article = e.no_article ");
|
||||
@@ -165,8 +168,104 @@ public class ArticleRepositoryImpl implements ArticleRepository {
|
||||
sql.append(")");
|
||||
}
|
||||
|
||||
System.out.println();
|
||||
// Compte le nombre total d'articles
|
||||
int totalCount = countArticlePageable(critere);
|
||||
|
||||
// Ajoute la pagination à la requête SQL
|
||||
sql.append(" LIMIT ? OFFSET ?");
|
||||
params.add(pageable.getPageSize());
|
||||
params.add(pageable.getOffset());
|
||||
|
||||
// Exécute la requête paginée
|
||||
List<Article> articles = jdbcTemplate.query(sql.toString(), new HomeArticleRowMapper(), params.toArray());
|
||||
|
||||
// Crée une Page<Article> à partir des résultats et des informations de pagination
|
||||
return new PageImpl<>(articles, pageable, totalCount);
|
||||
}
|
||||
|
||||
public int countArticlePageable(SearchArticleCritere critere) {
|
||||
StringBuilder sql = new StringBuilder("SELECT COUNT(a.no_article) FROM ARTICLES_VENDUS a ");
|
||||
sql.append("JOIN UTILISATEURS u ON a.no_utilisateur = u.no_utilisateur ");
|
||||
sql.append("LEFT JOIN ENCHERES e ON a.no_article = e.no_article ");
|
||||
sql.append("WHERE 1 = 1 AND a.isDelete = 0");
|
||||
List<Object> params = new ArrayList<>();
|
||||
|
||||
if (critere.getNoCategorie() != null) {
|
||||
sql.append(" AND a.no_categorie = ?");
|
||||
params.add(critere.getNoCategorie());
|
||||
}
|
||||
if (critere.getTitle() != null && !critere.getTitle().isEmpty()) {
|
||||
sql.append(" AND a.nom_article LIKE ?");
|
||||
params.add('%' + critere.getTitle() + '%');
|
||||
}
|
||||
|
||||
if (critere.getVenteOptions() != null && critere.getVenteOptions().length > 0) {
|
||||
sql.append(" AND (");
|
||||
boolean isFirstCondition = true;
|
||||
for (String option : critere.getVenteOptions()) {
|
||||
if (option.equals("venteEnCours")) {
|
||||
if (!isFirstCondition) {
|
||||
sql.append(" OR ");
|
||||
}
|
||||
sql.append(" (a.date_debut_encheres <= NOW() AND a.date_fin_encheres >= NOW()) ");
|
||||
isFirstCondition = false;
|
||||
}
|
||||
if (option.equals("ventesNonDebutees")) {
|
||||
if (!isFirstCondition) {
|
||||
sql.append(" OR ");
|
||||
}
|
||||
sql.append(" (a.date_debut_encheres > NOW()) ");
|
||||
isFirstCondition = false;
|
||||
}
|
||||
if (option.equals("ventesTerminees")) {
|
||||
if (!isFirstCondition) {
|
||||
sql.append(" OR ");
|
||||
}
|
||||
sql.append(" (a.date_fin_encheres < NOW()) ");
|
||||
isFirstCondition = false;
|
||||
}
|
||||
}
|
||||
sql.append(") AND a.no_utilisateur = ?");
|
||||
params.add(critere.getNoVendeur());
|
||||
}
|
||||
|
||||
if (critere.getAchatOptions() != null && critere.getAchatOptions().length > 0) {
|
||||
sql.append(" AND (");
|
||||
boolean isFirstCondition = true;
|
||||
for (String option : critere.getAchatOptions()) {
|
||||
if (option.equals("encheresOuvertes")) {
|
||||
if (!isFirstCondition) {
|
||||
sql.append(" OR ");
|
||||
}
|
||||
sql.append(" (a.date_debut_encheres <= NOW() AND a.date_fin_encheres >= NOW()) ");
|
||||
isFirstCondition = false;
|
||||
}
|
||||
if (option.equals("enchereEnCours")) {
|
||||
if (!isFirstCondition) {
|
||||
sql.append(" OR ");
|
||||
}
|
||||
sql.append(" (e.no_utilisateur = ? AND a.date_debut_encheres <= NOW() AND a.date_fin_encheres >= NOW()) ");
|
||||
isFirstCondition = false;
|
||||
params.add(critere.getNoVendeur());
|
||||
}
|
||||
if (option.equals("enchereRemportees")) {
|
||||
if (!isFirstCondition) {
|
||||
sql.append(" OR ");
|
||||
}
|
||||
sql.append(" (e.no_utilisateur = ? AND e.montant_enchere = (SELECT MAX(montant_enchere) FROM ENCHERES WHERE no_article = a.no_article)) ");
|
||||
isFirstCondition = false;
|
||||
params.add(critere.getNoVendeur());
|
||||
}
|
||||
}
|
||||
|
||||
sql.append(")");
|
||||
}
|
||||
|
||||
// Exécute la requête paginée
|
||||
int articles = jdbcTemplate.queryForObject(sql.toString(), Integer.class, params.toArray());
|
||||
|
||||
// Retourne le nombre d'articles
|
||||
return articles;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user