Ladies_Man

tariff

Feb 7th, 2017
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.80 KB | None | 0 0
  1. package ru.pochta.abon.service.tarificator;
  2.  
  3. import com.vaadin.spring.annotation.SpringComponent;
  4. import com.vaadin.spring.annotation.UIScope;
  5. import org.apache.commons.lang3.exception.ExceptionUtils;
  6. import org.apache.log4j.Logger;
  7. import org.springframework.web.client.RestTemplate;
  8. import org.springframework.web.util.UriComponentsBuilder;
  9.  
  10. import java.time.LocalDate;
  11. import java.time.ZoneId;
  12. import java.time.ZonedDateTime;
  13. import java.time.format.DateTimeFormatter;
  14. import java.util.*;
  15.  
  16. import static java.time.temporal.ChronoUnit.MONTHS;
  17.  
  18. @SpringComponent
  19. @UIScope
  20. public class TarificatorService {
  21.  
  22.     private final String TARIFICATOR_HOST = "http://tariff.russianpost.ru/tariff/v1/calculate?json";
  23.  
  24.     private final String ABON_TARIFICATION_CODE = "312000";
  25.  
  26.     private final String CLIENT_PHYSICAL = "1";
  27.     private final String CLIENT_LEGAL = "2";
  28.     private final String CLIENT_PRIVELEGED = "3";
  29.  
  30.     private final String ACTION_FIRST_ORDER_1_MONTH = "1";  //Первичное заключение на 1 месяц
  31.     private final String ACTION_PROLONGATION_1_MONTH = "2"; //Пролонгация на 1 месяц
  32.     private final String ACTION_MONTHLY = "3";              //Каждый последующий месяц
  33.  
  34.     private final String PROLONGATION_KEY = "service";
  35.     private final String PROLONGATION_VALUE = "45";
  36.  
  37.     private static final Logger LOG = Logger.getLogger(TarificatorService.class);
  38.  
  39.     private final String datePattern = "yyyyMMdd";
  40.  
  41.     private TarificatorResponse getTarificatorResponse(
  42.             String region, String client, String month, String date, boolean prolongation) {
  43.  
  44.         UriComponentsBuilder urlBuilder = UriComponentsBuilder.fromHttpUrl(TARIFICATOR_HOST);
  45.  
  46.         urlBuilder.queryParam("object", ABON_TARIFICATION_CODE);
  47.         urlBuilder.queryParam("region", region);
  48.         urlBuilder.queryParam("client", client);
  49.         urlBuilder.queryParam("month", month);
  50.         urlBuilder.queryParam("date", date);
  51.         if (prolongation) {
  52.             urlBuilder.queryParam(PROLONGATION_KEY, PROLONGATION_VALUE);
  53.         }
  54.  
  55.         String url = urlBuilder.build(false).encode().toUriString();
  56.  
  57.         System.out.println("link:" + url);
  58.  
  59.         RestTemplate rst = new RestTemplate();
  60.         TarificatorResponse tarificatorResponse = null;
  61.  
  62.         try {
  63.             tarificatorResponse = rst.getForObject(url, TarificatorResponse.class);
  64.         } catch (Exception e) {
  65.             LOG.error(ExceptionUtils.getStackTrace(e));
  66.         }
  67.         return tarificatorResponse;
  68.     }
  69.  
  70.  
  71.     public long getPaymentPhysical(String region, int month, LocalDate date, boolean prolongate) {
  72.         String formattedDate = date.format(DateTimeFormatter.ofPattern(datePattern));
  73.         TarificatorResponse tarificatorResponse = getTarificatorResponse(
  74.                 region,
  75.                 CLIENT_PHYSICAL,
  76.                 String.valueOf(month),
  77.                 formattedDate,
  78.                 prolongate);
  79.  
  80.         return null != tarificatorResponse.getPaynds() ?
  81.                 Long.valueOf(tarificatorResponse.getPaynds()) : -1L;
  82.     }
  83.  
  84.     public long getPaymentLegal(String region, int month, LocalDate date, boolean prolongate) {
  85.         String formattedDate = date.format(DateTimeFormatter.ofPattern(datePattern));
  86.         TarificatorResponse tarificatorResponse = getTarificatorResponse(
  87.                 region,
  88.                 CLIENT_LEGAL,
  89.                 String.valueOf(month),
  90.                 formattedDate,
  91.                 prolongate);
  92.  
  93.         return null != tarificatorResponse.getPaynds() ?
  94.                 Long.valueOf(tarificatorResponse.getPaynds()) : -1L;
  95.     }
  96.  
  97.     public long getPaymentPriveleged(String region, int month, LocalDate date, boolean prolongate) {
  98.         String formattedDate = date.format(DateTimeFormatter.ofPattern(datePattern));
  99.         TarificatorResponse tarificatorResponse = getTarificatorResponse(
  100.                 region,
  101.                 CLIENT_PRIVELEGED,
  102.                 String.valueOf(month),
  103.                 formattedDate,
  104.                 prolongate);
  105.  
  106.         return null != tarificatorResponse.getPaynds() ?
  107.                 Long.valueOf(tarificatorResponse.getPaynds()) : -1L;
  108.     }
  109.  
  110.     public long getPaymentRefuse() {
  111.  
  112.        
  113.        
  114.         /*ZonedDateTime now = ZonedDateTime.now();
  115.         ZonedDateTime startDate = claim.getStartDate();
  116.         int reservationTerm = claim.getReservationTerm();
  117.         ZonedDateTime endDate = serviceManager.getClaimService().calculateReservationDate(startDate, reservationTerm, ZoneId.systemDefault());
  118.         long refundMonths = now.withDayOfMonth(1).until(endDate, MONTHS);
  119.         Long claimTariff = getClaimTariff(claim);
  120.         return ((now.isBefore(startDate)) ? reservationTerm : refundMonths) * claimTariff;*/
  121.     }
  122.  
  123. }
Advertisement
Add Comment
Please, Sign In to add comment