remove magic numbers, dry code
This commit is contained in:
@@ -10,7 +10,6 @@ services:
|
|||||||
depends_on:
|
depends_on:
|
||||||
- service-db
|
- service-db
|
||||||
environment:
|
environment:
|
||||||
- SERVER_PORT=8181
|
|
||||||
- SERVER_PORT=8181
|
- SERVER_PORT=8181
|
||||||
- SPRING_CLOUD_CONFIG_URI=http://192.168.2.22:10022
|
- SPRING_CLOUD_CONFIG_URI=http://192.168.2.22:10022
|
||||||
- SPRING_CLOUD_CONFIG_PROFILE=prod
|
- SPRING_CLOUD_CONFIG_PROFILE=prod
|
||||||
|
|||||||
@@ -30,6 +30,18 @@ import static java.util.Objects.nonNull;
|
|||||||
public class DownloadHeadlessService {
|
public class DownloadHeadlessService {
|
||||||
|
|
||||||
private final Logger logger = LoggerFactory.getLogger(DownloadHeadlessService.class);
|
private final Logger logger = LoggerFactory.getLogger(DownloadHeadlessService.class);
|
||||||
|
private static final int COL_TYPE = 1;
|
||||||
|
private static final int COL_NOTICE = 3;
|
||||||
|
private static final int COL_BID_START = 6;
|
||||||
|
private static final int COL_BID_END = 7;
|
||||||
|
private static final int COL_TRADES_DATE = 9;
|
||||||
|
private static final int COL_LOT_NUMBER = 16;
|
||||||
|
private static final int COL_STATUS = 17;
|
||||||
|
private static final int COL_INIT_MONEY = 24;
|
||||||
|
private static final int COL_STEP = 25;
|
||||||
|
private static final int COL_START_PRICE = 26;
|
||||||
|
private static final int COL_FINAL_PRICE = 27;
|
||||||
|
private static final int COL_FULL_INFO = 29;
|
||||||
|
|
||||||
@SneakyThrows
|
@SneakyThrows
|
||||||
public Map<String, Lot> downloadExcel(int retryCount, String link) {
|
public Map<String, Lot> downloadExcel(int retryCount, String link) {
|
||||||
@@ -41,7 +53,8 @@ public class DownloadHeadlessService {
|
|||||||
connection.setRequestProperty("Accept-Language", "ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7");
|
connection.setRequestProperty("Accept-Language", "ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7");
|
||||||
connection.setConnectTimeout(5000);
|
connection.setConnectTimeout(5000);
|
||||||
connection.setReadTimeout(10000);
|
connection.setReadTimeout(10000);
|
||||||
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
|
int responseCode = connection.getResponseCode();
|
||||||
|
if (responseCode == HttpURLConnection.HTTP_OK) {
|
||||||
Map<String, Lot> data = new HashMap<>();
|
Map<String, Lot> data = new HashMap<>();
|
||||||
Workbook wb = new XSSFWorkbook(connection.getInputStream());
|
Workbook wb = new XSSFWorkbook(connection.getInputStream());
|
||||||
Sheet sheetWithData = wb.getSheetAt(0);
|
Sheet sheetWithData = wb.getSheetAt(0);
|
||||||
@@ -62,12 +75,12 @@ public class DownloadHeadlessService {
|
|||||||
}
|
}
|
||||||
return data;
|
return data;
|
||||||
} else {
|
} else {
|
||||||
logger.error("Error in line 99 ");
|
logger.error("Wrong response code {} during call link {} ", responseCode, link);
|
||||||
Thread.sleep(1000);
|
Thread.sleep(1000);
|
||||||
return downloadExcel(retryCount + 1, link);
|
return downloadExcel(retryCount + 1, link);
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
logger.error("Error in line 103 " + e);
|
logger.error("Error during processing link {} ", link, e);
|
||||||
if (retryCount > 10) {
|
if (retryCount > 10) {
|
||||||
return new HashMap<>();
|
return new HashMap<>();
|
||||||
}
|
}
|
||||||
@@ -78,19 +91,19 @@ public class DownloadHeadlessService {
|
|||||||
|
|
||||||
private Lot extractDataFromRow(Row row, String cadastral) {
|
private Lot extractDataFromRow(Row row, String cadastral) {
|
||||||
Lot lot = new Lot();
|
Lot lot = new Lot();
|
||||||
lot.setBidStart(row.getCell(6).getStringCellValue());
|
lot.setNotice(row.getCell(COL_NOTICE).getStringCellValue());
|
||||||
lot.setBidEnd(row.getCell(7).getStringCellValue());
|
lot.setBidStart(row.getCell(COL_BID_START).getStringCellValue());
|
||||||
|
lot.setBidEnd(row.getCell(COL_BID_END).getStringCellValue());
|
||||||
|
lot.setTradesDate(row.getCell(COL_TRADES_DATE).getStringCellValue());
|
||||||
lot.setCadastral(cadastral);
|
lot.setCadastral(cadastral);
|
||||||
lot.setLotNumber((int) row.getCell(16).getNumericCellValue());
|
lot.setLotNumber((int) row.getCell(COL_LOT_NUMBER).getNumericCellValue());
|
||||||
lot.setFinalPrice(row.getCell(27).getStringCellValue());
|
lot.setStatus(row.getCell(COL_STATUS).getStringCellValue());
|
||||||
lot.setNotice(row.getCell(3).getStringCellValue());
|
lot.setInitMoney(row.getCell(COL_INIT_MONEY).getStringCellValue());
|
||||||
lot.setInitMoney(row.getCell(24).getStringCellValue());
|
lot.setStep(row.getCell(COL_STEP).getStringCellValue());
|
||||||
lot.setStep(row.getCell(25).getStringCellValue());
|
lot.setStartPrice(row.getCell(COL_START_PRICE).getStringCellValue());
|
||||||
lot.setStartPrice(row.getCell(26).getStringCellValue());
|
lot.setFinalPrice(row.getCell(COL_FINAL_PRICE).getStringCellValue());
|
||||||
lot.setStatus(row.getCell(17).getStringCellValue());
|
lot.setFullInfo(row.getCell(COL_FULL_INFO).getStringCellValue());
|
||||||
lot.setTradesDate(row.getCell(9).getStringCellValue());
|
lot.setType(row.getCell(COL_TYPE).getStringCellValue());
|
||||||
lot.setFullInfo(row.getCell(29).getStringCellValue());
|
|
||||||
lot.setType(row.getCell(1).getStringCellValue());
|
|
||||||
return lot;
|
return lot;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -126,31 +139,6 @@ public class DownloadHeadlessService {
|
|||||||
return cadastrals;
|
return cadastrals;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void createNewDocument(TorgiEventRepository eventRepository, LocalDate now) {
|
|
||||||
try (XSSFWorkbook workbook = new XSSFWorkbook()) {
|
|
||||||
XSSFSheet sheet = workbook.createSheet("sheet");
|
|
||||||
setRowHead(sheet);
|
|
||||||
logger.error("before write body");
|
|
||||||
int i = 0;
|
|
||||||
int currentRow = 1;
|
|
||||||
while (true) {
|
|
||||||
List<Lot> partition = eventRepository.findAllByOrderByCreatedAtDesc(PageRequest.of(i, 1000)).stream().map(Lot::new).toList();
|
|
||||||
if (partition.isEmpty()) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
currentRow = setRowBodyNew(sheet, currentRow, partition);
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
//setRowWith(sheet);
|
|
||||||
logger.error("before write to file");
|
|
||||||
try (FileOutputStream fileOut = new FileOutputStream(getFileToSave(now))) {
|
|
||||||
workbook.write(fileOut);
|
|
||||||
}
|
|
||||||
} catch (Exception ex) {
|
|
||||||
logger.error("in createNewDocument " + ex.getMessage());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void createNewDocument(TorgiEventRepository eventRepository, List<String> cadastrals, LocalDate now) {
|
public void createNewDocument(TorgiEventRepository eventRepository, List<String> cadastrals, LocalDate now) {
|
||||||
try (XSSFWorkbook workbook = new XSSFWorkbook()) {
|
try (XSSFWorkbook workbook = new XSSFWorkbook()) {
|
||||||
XSSFSheet sheet = workbook.createSheet("sheet");
|
XSSFSheet sheet = workbook.createSheet("sheet");
|
||||||
@@ -160,8 +148,6 @@ public class DownloadHeadlessService {
|
|||||||
int currentRow = 1;
|
int currentRow = 1;
|
||||||
setRowBodyNew(sheet, currentRow, data);
|
setRowBodyNew(sheet, currentRow, data);
|
||||||
|
|
||||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
||||||
workbook.write(out);
|
|
||||||
logger.error("before write to file");
|
logger.error("before write to file");
|
||||||
try (FileOutputStream fileOut = new FileOutputStream(getFileToSave(now))) {
|
try (FileOutputStream fileOut = new FileOutputStream(getFileToSave(now))) {
|
||||||
workbook.write(fileOut);
|
workbook.write(fileOut);
|
||||||
|
|||||||
Reference in New Issue
Block a user