Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ru.pochta.abon.service.tarificator;
- import com.vaadin.spring.annotation.SpringComponent;
- import com.vaadin.spring.annotation.UIScope;
- import org.apache.commons.lang3.exception.ExceptionUtils;
- import org.apache.log4j.Logger;
- import org.springframework.web.client.RestTemplate;
- import org.springframework.web.util.UriComponentsBuilder;
- import java.time.LocalDate;
- import java.time.ZoneId;
- import java.time.ZonedDateTime;
- import java.time.format.DateTimeFormatter;
- import java.util.*;
- import static java.time.temporal.ChronoUnit.MONTHS;
- @SpringComponent
- @UIScope
- public class TarificatorService {
- private final String TARIFICATOR_HOST = "http://tariff.russianpost.ru/tariff/v1/calculate?json";
- private final String ABON_TARIFICATION_CODE = "312000";
- private final String CLIENT_PHYSICAL = "1";
- private final String CLIENT_LEGAL = "2";
- private final String CLIENT_PRIVELEGED = "3";
- private final String ACTION_FIRST_ORDER_1_MONTH = "1"; //Первичное заключение на 1 месяц
- private final String ACTION_PROLONGATION_1_MONTH = "2"; //Пролонгация на 1 месяц
- private final String ACTION_MONTHLY = "3"; //Каждый последующий месяц
- private final String PROLONGATION_KEY = "service";
- private final String PROLONGATION_VALUE = "45";
- private static final Logger LOG = Logger.getLogger(TarificatorService.class);
- private final String datePattern = "yyyyMMdd";
- private TarificatorResponse getTarificatorResponse(
- String region, String client, String month, String date, boolean prolongation) {
- UriComponentsBuilder urlBuilder = UriComponentsBuilder.fromHttpUrl(TARIFICATOR_HOST);
- urlBuilder.queryParam("object", ABON_TARIFICATION_CODE);
- urlBuilder.queryParam("region", region);
- urlBuilder.queryParam("client", client);
- urlBuilder.queryParam("month", month);
- urlBuilder.queryParam("date", date);
- if (prolongation) {
- urlBuilder.queryParam(PROLONGATION_KEY, PROLONGATION_VALUE);
- }
- String url = urlBuilder.build(false).encode().toUriString();
- System.out.println("link:" + url);
- RestTemplate rst = new RestTemplate();
- TarificatorResponse tarificatorResponse = null;
- try {
- tarificatorResponse = rst.getForObject(url, TarificatorResponse.class);
- } catch (Exception e) {
- LOG.error(ExceptionUtils.getStackTrace(e));
- }
- return tarificatorResponse;
- }
- public long getPaymentPhysical(String region, int month, LocalDate date, boolean prolongate) {
- String formattedDate = date.format(DateTimeFormatter.ofPattern(datePattern));
- TarificatorResponse tarificatorResponse = getTarificatorResponse(
- region,
- CLIENT_PHYSICAL,
- String.valueOf(month),
- formattedDate,
- prolongate);
- return null != tarificatorResponse.getPaynds() ?
- Long.valueOf(tarificatorResponse.getPaynds()) : -1L;
- }
- public long getPaymentLegal(String region, int month, LocalDate date, boolean prolongate) {
- String formattedDate = date.format(DateTimeFormatter.ofPattern(datePattern));
- TarificatorResponse tarificatorResponse = getTarificatorResponse(
- region,
- CLIENT_LEGAL,
- String.valueOf(month),
- formattedDate,
- prolongate);
- return null != tarificatorResponse.getPaynds() ?
- Long.valueOf(tarificatorResponse.getPaynds()) : -1L;
- }
- public long getPaymentPriveleged(String region, int month, LocalDate date, boolean prolongate) {
- String formattedDate = date.format(DateTimeFormatter.ofPattern(datePattern));
- TarificatorResponse tarificatorResponse = getTarificatorResponse(
- region,
- CLIENT_PRIVELEGED,
- String.valueOf(month),
- formattedDate,
- prolongate);
- return null != tarificatorResponse.getPaynds() ?
- Long.valueOf(tarificatorResponse.getPaynds()) : -1L;
- }
- public long getPaymentRefuse() {
- /*ZonedDateTime now = ZonedDateTime.now();
- ZonedDateTime startDate = claim.getStartDate();
- int reservationTerm = claim.getReservationTerm();
- ZonedDateTime endDate = serviceManager.getClaimService().calculateReservationDate(startDate, reservationTerm, ZoneId.systemDefault());
- long refundMonths = now.withDayOfMonth(1).until(endDate, MONTHS);
- Long claimTariff = getClaimTariff(claim);
- return ((now.isBefore(startDate)) ? reservationTerm : refundMonths) * claimTariff;*/
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment