Skip to content
Extraits de code Groupes Projets
Valider 9710c029 rédigé par Mohamed Lemine BAILLAHI's avatar Mohamed Lemine BAILLAHI
Parcourir les fichiers

Merge branch 'feature/MS-114' into 'develop'

MS-114/export sale session data to CSV and upload to FTP server

Closes MS-114

See merge request !120
parents 3a4f7ef5 835b6dcf
Branches
1 requête de fusion!120MS-114/export sale session data to CSV and upload to FTP server
Pipeline #15849 en échec avec les étapes
in 37 secondes
## [1.0.75-RELEASE]
### Added
- MS-114/Export sale session data to CSV and upload to FTP server
-
### Changed
- Changes in existing functionality.
### Deprecated
- Soon-to-be removed features.
### Removed
- Features that have been removed.
### Fixed
- Any bug fixes.
### Security
- Any security improvements.
## [1.0.74-RELEASE] ## [1.0.74-RELEASE]
### Added ### Added
- MYD-739/Fix List with sale sessions stats - MYD-739/Fix List with sale sessions stats
......
package com.marketingconfort.mydressin; package com.marketingconfort.mydressin;
import com.marketingconfort.mydressin.config.FTPConfig;
import com.marketingconfort.starter.core.config.props.CustomRestProperties; import com.marketingconfort.starter.core.config.props.CustomRestProperties;
import com.marketingconfort.starter.core.services.MCRestTemplateService; import com.marketingconfort.starter.core.services.MCRestTemplateService;
import com.marketingconfort.starter.core.services.implementations.*; import com.marketingconfort.starter.core.services.implementations.*;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan; import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.FilterType; import org.springframework.context.annotation.FilterType;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories; import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication(scanBasePackages = "com.marketingconfort.mydressin") @SpringBootApplication(scanBasePackages = "com.marketingconfort.mydressin")
@ComponentScan(basePackages = { @ComponentScan(basePackages = {
...@@ -25,6 +28,7 @@ import org.springframework.data.jpa.repository.config.EnableJpaRepositories; ...@@ -25,6 +28,7 @@ import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
"com.marketingconfort.mydressin.common.cart.models" "com.marketingconfort.mydressin.common.cart.models"
}) })
@Import({MCRestTemplateService.class, CustomRestProperties.class}) @Import({MCRestTemplateService.class, CustomRestProperties.class})
@EnableScheduling
public class Application { public class Application {
public static void main(String[] args) { public static void main(String[] args) {
SpringApplication.run(Application.class, args); SpringApplication.run(Application.class, args);
......
package com.marketingconfort.mydressin.config;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
@Primary
@Component
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
@ConfigurationProperties(prefix = "ftp")
public class FTPConfig {
private String host;
private String username;
private String password;
}
...@@ -120,4 +120,7 @@ public interface SaleSessionRepository extends JpaRepository<SaleSession, Long> ...@@ -120,4 +120,7 @@ public interface SaleSessionRepository extends JpaRepository<SaleSession, Long>
@Param("name") String name , @Param("name") String name ,
@Param("startDate") Date startDate , @Param("startDate") Date startDate ,
@Param("endDate") Date endDate); @Param("endDate") Date endDate);
@Query("SELECT s FROM SaleSession s WHERE CAST(s.startedDate AS date ) = :yesterdayDate ")
List<SaleSession> getAllYesterdaySaleSessions(@Param("yesterdayDate") Date yesterdayDate);
} }
...@@ -39,4 +39,6 @@ public interface SaleSessionService { ...@@ -39,4 +39,6 @@ public interface SaleSessionService {
Page<SaleSessionDTO> getDeletedSaleSessionsPage(String name , String type, Date startDate, Date endDate, Page<SaleSessionDTO> getDeletedSaleSessionsPage(String name , String type, Date startDate, Date endDate,
int page, int size, String sortBy, String order); int page, int size, String sortBy, String order);
List<String[]> getDeletedTypeCount(String name , String type, Date startDate, Date endDate); List<String[]> getDeletedTypeCount(String name , String type, Date startDate, Date endDate);
void exportYesterdaySaleSessions();
} }
...@@ -6,7 +6,7 @@ import com.marketingconfort.mydressin.common.cart.enumurations.ItemCartStatus; ...@@ -6,7 +6,7 @@ import com.marketingconfort.mydressin.common.cart.enumurations.ItemCartStatus;
import com.marketingconfort.mydressin.common.cart.enumurations.ItemSource; import com.marketingconfort.mydressin.common.cart.enumurations.ItemSource;
import com.marketingconfort.mydressin.common.cart.enumurations.OrderStatus; import com.marketingconfort.mydressin.common.cart.enumurations.OrderStatus;
import com.marketingconfort.mydressin.common.cart.models.*; import com.marketingconfort.mydressin.common.cart.models.*;
import com.marketingconfort.mydressin.config.FTPConfig;
import com.marketingconfort.mydressin.dtos.SaleSessionDTO; import com.marketingconfort.mydressin.dtos.SaleSessionDTO;
import com.marketingconfort.mydressin.dtos.SessionOrderDTO; import com.marketingconfort.mydressin.dtos.SessionOrderDTO;
import com.marketingconfort.mydressin.dtos.ItemRequestDTO; import com.marketingconfort.mydressin.dtos.ItemRequestDTO;
...@@ -18,18 +18,29 @@ import com.marketingconfort.mydressin.mappers.SaleSessionMapper; ...@@ -18,18 +18,29 @@ import com.marketingconfort.mydressin.mappers.SaleSessionMapper;
import com.marketingconfort.mydressin.repositories.SaleSessionRepository; import com.marketingconfort.mydressin.repositories.SaleSessionRepository;
import com.marketingconfort.mydressin.repositories.SessionOrderRepository; import com.marketingconfort.mydressin.repositories.SessionOrderRepository;
import com.marketingconfort.mydressin.services.*; import com.marketingconfort.mydressin.services.*;
import com.opencsv.CSVWriter;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.net.ftp.FTPClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort; import org.springframework.data.domain.Sort;
import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Value;
import java.io.*;
import java.time.LocalDate;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.ZoneId;
import java.util.*; import java.util.*;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors; import java.util.stream.Collectors;
...@@ -38,6 +49,8 @@ import java.util.stream.Collectors; ...@@ -38,6 +49,8 @@ import java.util.stream.Collectors;
@Service @Service
@Slf4j @Slf4j
public class SaleSessionServiceImp implements SaleSessionService { public class SaleSessionServiceImp implements SaleSessionService {
private static final Logger logger = LoggerFactory.getLogger(SaleSessionServiceImp.class);
private final SaleSessionRepository saleSessionRepository; private final SaleSessionRepository saleSessionRepository;
private final SaleSessionMapper saleSessionMapper; private final SaleSessionMapper saleSessionMapper;
private final ItemCartService itemCartService; private final ItemCartService itemCartService;
...@@ -45,8 +58,11 @@ public class SaleSessionServiceImp implements SaleSessionService { ...@@ -45,8 +58,11 @@ public class SaleSessionServiceImp implements SaleSessionService {
private final ProductStockService productStockService; private final ProductStockService productStockService;
private final SessionOrderRepository orderRepository; private final SessionOrderRepository orderRepository;
private final SessionOrderMapper sessionOrderMapper; private final SessionOrderMapper sessionOrderMapper;
private final SessionOrderService sessionOrderService;
private final UnregisteredCartService unregisteredCartService; private final UnregisteredCartService unregisteredCartService;
private final SessionOrderRepository sessionOrderRepository; private final SessionOrderRepository sessionOrderRepository;
private FTPConfig ftpConfig;
@Override @Override
...@@ -309,4 +325,124 @@ public class SaleSessionServiceImp implements SaleSessionService { ...@@ -309,4 +325,124 @@ public class SaleSessionServiceImp implements SaleSessionService {
return stringList; return stringList;
} }
@Scheduled(cron = "0 0 7 * * ?")
@Override
public void exportYesterdaySaleSessions() {
LocalDate yesterday = LocalDate.now().minusDays(1);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
String formattedDate = yesterday.format(formatter);
logger.info("Starting export of yesterday's sale sessions for date: {}", formattedDate);
List<SaleSession> saleSessions = saleSessionRepository.getAllYesterdaySaleSessions(
Date.from(yesterday.atStartOfDay(ZoneId.systemDefault()).toInstant())
);
logger.info("Sale sessions count: {}", saleSessions.size());
String fileName = "sale_sessions_" + formattedDate + ".csv";
try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
CSVWriter writer = new CSVWriter(new OutputStreamWriter(byteArrayOutputStream))) {
writer.writeNext(new String[]{
"idUtilisateur", "idCommande", "idSessionVente", "nameSession", "startDate", "expirationDate", "description",
"lastModification", "typeLive", "liveId", "statutPanier", "statutCommande", "prenom", "nom", "email", "telephone",
"nomUtilisateur", "nomProduit", "UGS", "quantite", "prixProduit", "montant", "dateCreationProduitCommande", "dateExpiration"
});
logger.info("CSV header written successfully for date: {}", formattedDate);
for (SaleSession saleSession : saleSessions) {
logger.debug("Processing SaleSession with ID: {}", saleSession.getId());
List<SessionOrderDTO> sessionOrderDTOS = sessionOrderService.getAllOrdersBySessionId(saleSession.getId());
for (SessionOrderDTO order : sessionOrderDTOS) {
List<String> row = new ArrayList<>();
String clientUid = order.getClient() != null ? String.valueOf(order.getClient().getUid()) : "N/A";
String firstName = order.getClient() != null ? order.getClient().getFirstName() : "N/A";
String lastName = order.getClient() != null ? order.getClient().getLastName() : "N/A";
String email = order.getClient() != null ? order.getClient().getEmail() : "N/A";
String phoneNumber = order.getClient() != null ? order.getClient().getPhoneNumber() : "N/A";
String pseudo = order.getClient() != null ? order.getClient().getPseudo() : "N/A";
row.add(clientUid);
row.add(String.valueOf(order.getId()));
row.add(String.valueOf(saleSession.getId()));
row.add(saleSession.getName());
row.add(saleSession.getStartedDate() != null ? saleSession.getStartedDate().toString() : "");
row.add(saleSession.getExpirationDate() != null ? saleSession.getExpirationDate().toString() : "");
row.add(saleSession.getDescription() != null ? saleSession.getDescription() : "");
row.add(saleSession.getLastModification() != null ? saleSession.getLastModification().toString() : "");
row.add(saleSession.getType() != null ? saleSession.getType().name() : "");
row.add(String.valueOf(saleSession.getLive()));
row.add(order.getStatus().name());
row.add(order.getStatus().name());
row.add(firstName);
row.add(lastName);
row.add(email);
row.add(phoneNumber);
row.add(pseudo);
row.add(order.getItemCartDTO() != null ? order.getItemCartDTO().getProduct().getName() : "");
row.add(order.getItemCartDTO() != null ? order.getItemCartDTO().getUgs() : "");
row.add(String.valueOf(order.getItemCartDTO() != null ? order.getItemCartDTO().getQuantity() : 0));
row.add(order.getCreatedDate() != null ? order.getCreatedDate().toString() : "");
row.add(String.valueOf(order.getPrice()));
row.add(String.valueOf(order.getPrice()));
row.add(order.getItemCartDTO() != null && order.getItemCartDTO().getExpirationDate() != null ? order.getItemCartDTO().getExpirationDate().toString() : "");
writer.writeNext(row.toArray(new String[0]));
}
}
writer.flush();
logger.info("Export completed successfully for date: {}", formattedDate);
// Now upload the data to the FTP server
uploadFileToFTPServer(fileName, byteArrayOutputStream.toByteArray());
} catch (IOException e) {
logger.error("Error occurred during export for date: {}", formattedDate, e);
}
}
private void uploadFileToFTPServer(String fileName, byte[] fileData) {
FTPClient ftpClient = new FTPClient();
try {
ftpClient.connect(ftpConfig.getHost());
ftpClient.login(ftpConfig.getUsername(), ftpConfig.getPassword());
logger.info("Connected to FTP server.");
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
ftpClient.enterLocalPassiveMode();
logger.info("FTP Response Code: {}", ftpClient.getReplyCode());
logger.info("FTP Reply String: {}", ftpClient.getReplyString());
try (ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(fileData)) {
boolean done = ftpClient.storeFile("/sale_sessions/" + fileName, byteArrayInputStream);
if (done) {
logger.info("File uploaded successfully to FTP server.");
} else {
int replyCode = ftpClient.getReplyCode();
logger.error("Failed to upload file, FTP server reply: {}", replyCode);
}
}
ftpClient.logout();
logger.info("Logged out from FTP server.");
} catch (IOException e) {
logger.error("Error occurred while uploading file to FTP server: ", e);
} finally {
try {
if (ftpClient.isConnected()) {
ftpClient.disconnect();
}
} catch (IOException e) {
logger.error("Error occurred while disconnecting from FTP server: ", e);
}
}
}
} }
0% ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter