Advertisement
Guest User

AccountAide

a guest
Jan 17th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Groovy 10.38 KB | None | 0 0
  1. package com.iqplatform.project.entities.setout.subscriber;
  2.  
  3. import com.fasterxml.jackson.core.JsonProcessingException;
  4. import com.google.common.base.Joiner;
  5. import com.google.common.collect.Lists;
  6. import com.iqmen.apiJson.jackson.JacksonObjectMapper;
  7. import com.iqplatform.model.inner.InnerObject;
  8. import com.iqplatform.providers.DictionaryProvider;
  9. import org.apache.commons.lang.StringEscapeUtils;
  10. import org.joda.time.LocalDateTime;
  11. import org.springframework.util.Assert;
  12.  
  13. import javax.annotation.Nonnull;
  14. import javax.annotation.Nullable;
  15. import java.sql.ResultSet;
  16. import java.sql.SQLException;
  17. import java.util.ArrayList;
  18. import java.util.List;
  19. import java.util.Objects;
  20. import java.util.function.Supplier;
  21. import java.util.stream.Collectors;
  22.  
  23. /**
  24.  * Created by vyaz on 26.12.17.
  25.  * <p>
  26.  * 1 accountNum         ACCOUNT
  27.  * 2 inn                INN
  28.  * 3 companyName        CONTR_NAME
  29.  * 5 dateClosing        DATA_CLOSE
  30.  * 6 dateContract       DATA_CONTR
  31.  * 7 dateSnap           DATE_SNAP
  32.  * <p>
  33.  * 8 client_data_contr
  34.  * 9 client_data_close
  35.  * <p>
  36.  * <p>
  37.  * 10 rf                  Dict("RF","branchCode",codeRF)
  38.  * 11 mrf                 Dict('mrf','Code',codeMRF)
  39.  * 12 kSegment            Dict('kSegment','Code,Code2',kSegmentStr || '-')
  40.  * 13 mSegment            Dict('mSegment','Name,Name2,Name3,Name4,Name5',segmentStr || 'Не определен')
  41.  * 14 macrosegment        Dict('macrosegment','Name',macrosegmentStr || 'Не определен')
  42.  * 15 serviceClassDict    Dict('serviceClass','Type,Type2',serviceClass || 'Не определен')
  43.  * 16 subsegment          Dict('Subsegment','Name,Name2,Name3,Name4',subsegmentStr || 'Не определен')
  44.  * <p>
  45.  * 17 changeDateTime LocalDateTime.now()
  46.  * 18 debug
  47.  **/
  48.  
  49. public class AccountAide extends AbstractAide {
  50.     public AccountAide() {
  51.         super();
  52.     }
  53.  
  54.     public AccountAide(@Nonnull DictionaryProvider dictionaryProvider) {
  55.         super(dictionaryProvider);
  56.     }
  57.  
  58.     String prepareSelectSql(@Nonnull List<SubscriberInfo> items) {
  59.         Assert.isTrue(!items.isEmpty());
  60.         List<String> where = new ArrayList<>(items.size());
  61.  
  62.         StringBuilder builder = new StringBuilder("select id, " +
  63.                 " col_accountNum,\n" +           // ACCOUNT             1
  64.                 " col_inn,\n" +                  // inn                 2
  65.                 " col_companyName,\n" +          // CONTR_NAME          3
  66.                 " col_dateClosing,\n" +          // DATA_CLOSE_SERV     5
  67.                 " col_dateContract,\n" +         // DATA_CONTR          6
  68.                 " col_dateSnap,\n" +             // DATE_SNAP           7
  69.  
  70.                 //" col_client_data_contr,\n" +
  71.                 //" col_client_data_close,\n" +
  72.                 " true as true \n" +
  73.                 "from iqp_account_t where ");
  74.  
  75.         for (int i = 0; i < items.size(); i++) {
  76.             SubscriberInfo item = items.get(i);
  77.             Assert.isTrue(item.ACCOUNT != null);
  78.             if (item.INN != null) {
  79.                 where.add(" ( (col_accountNum = '" + item.ACCOUNT + "') and (col_inn = '" + item.INN + "') ) ");
  80.             } else {
  81.                 if (item.CONTR_NAME != null) {
  82.                     where.add(" ( (col_accountNum = '" + item.ACCOUNT + "')  and (col_companyName = '" + StringEscapeUtils.escapeSql(item.CONTR_NAME) + "') and (col_inn is null) ) ");
  83.                 } else {
  84.                     where.add(" ( (col_accountNum = '" + item.ACCOUNT + "')  and (col_companyName is null) and (col_inn is null) ) ");
  85.                 }
  86.             }
  87.         }
  88.  
  89.         builder.append("\n").append(Joiner.on(" or \n ").join(where));
  90.         return builder.toString();
  91.     }
  92.  
  93.     InnerObject createAccount(@Nonnull InnerObject account, @Nonnull SubscriberInfo info) {
  94.         Assert.isTrue(Objects.equals("Account", account.getSchema().getAlias()));
  95.         /*
  96.              1 accountNum         ACCOUNT
  97.              2 inn                INN
  98.              3 companyName        CONTR_NAME
  99.              5 dateClosing        DATA_CLOSE_SERV
  100.              6 dateContract       DATA_CONTR
  101.              7 dateSnap           DATE_SNAP
  102.  
  103.              8 client_data_contr
  104.              9 client_data_close
  105.           */
  106.  
  107.         account.set("accountNum", info.ACCOUNT);              // 1
  108.         account.set("inn", info.INN);                         // 2
  109.         account.set("companyName", info.CONTR_NAME);          // 3
  110.         account.set("dateClosing", info.DATA_CLOSE);          // 5
  111.         account.set("dateContract", info.DATA_CONTR);         // 6
  112.         account.set("dateSnap", info.DATE_SNAP);              // 7
  113.  
  114.         //subscriber.set("client_data_contr", null);     // 8
  115.         //subscriber.set("client_data_close", null);     // 9
  116.  
  117.         return account;
  118.     }
  119.  
  120.     /**  **/
  121.     boolean hasChanges(@Nonnull ResultSet rs, @Nonnull SubscriberInfo item, @Nonnull DictionaryProvider dictionaryProvider) throws SQLException {
  122.         String col_accountNum = rs.getString("col_accountNum");
  123.         String col_inn = rs.getString("col_inn");
  124.  
  125.         Assert.isTrue(Objects.equals(col_accountNum, item.ACCOUNT));
  126.         Assert.isTrue(Objects.equals(col_inn, item.INN));
  127.  
  128.         boolean hasChanges;
  129.         final List<String> changedColumns = Lists.newArrayList();
  130.  
  131.         hasChanges = !equals(rs, "col_companyName", item.CONTR_NAME, changedColumns);       // 3
  132.         hasChanges |= !equals(rs, "col_dateClosing", item.DATA_CLOSE, changedColumns);      // 5
  133.         hasChanges |= !equals(rs, "col_dateContract", item.DATA_CONTR, changedColumns);     // 6
  134.  
  135.         if (item.DATE_SNAP != null) {
  136.             hasChanges |= !equals(rs, "col_dateSnap", item.DATE_SNAP.toLocalDate(), changedColumns); // 7
  137.         }
  138.  
  139.         return hasChanges;
  140.     }
  141.  
  142.     /**
  143.      * возвращает null если нет применимых для апдейта данных (например все поля item == null)
  144.      **/
  145.     @Nullable
  146.     public String prepareUpdateSql(@Nonnull ResultSet rs, @Nonnull SubscriberInfo item) throws SQLException {
  147.         Long id = rs.getLong("id");
  148.         Assert.notNull(id);
  149.  
  150.         Assert.notNull(item.ACCOUNT);
  151.         Assert.notNull(item.INN);
  152.  
  153.         String col_accountNum = rs.getString("col_accountNum");
  154.         String col_inn = rs.getString("col_inn");
  155.  
  156.         Assert.isTrue(Objects.equals(col_accountNum, item.ACCOUNT));
  157.         Assert.isTrue(Objects.equals(col_inn, item.INN));
  158.  
  159.         return prepareUpdateSql(item, () -> id);
  160.     }
  161.  
  162.     @Nullable
  163.     String prepareUpdateSql(@Nonnull SubscriberInfo item, @Nonnull Supplier<Long> identitySupplier) {
  164.         List<String> list = Lists.newArrayList();
  165.  
  166.         if (item.INN != null && item.CONTR_NAME != null) {
  167.             list.add(_column("col_companyName", item.CONTR_NAME));       // 3
  168.         }
  169.  
  170.         list.add(_column("col_dateClosing", item.DATA_CLOSE_SERV));  // 5
  171.         list.add(_column("col_dateContract", item.DATA_CLOSE_SERV)); // 6
  172.  
  173.         if (item.DATE_SNAP != null) {
  174.             list.add(_column("col_dateSnap", item.DATE_SNAP.toLocalDate()));/** для абонентов dateSnap это дата и время а вот для счетов и организаций - только дата. приятно правда? **/
  175.         }
  176.  
  177.         list = list.stream().filter(it -> it != null && !it.isEmpty()).collect(Collectors.toList());
  178.  
  179.         if (list.isEmpty()) {
  180.             return null;
  181.         }
  182.  
  183.         list.add(_column("col_changeDateTime", LocalDateTime.now()));                                                               // 17
  184.         list.add(_column("col_debug", "subscriber-loader " + LocalDateTime.now().toString(SubscriberInfoLoader._debug_fmt_)));  // 18
  185.  
  186.         String columns = Joiner.on(", \n ").skipNulls().join(list);
  187.  
  188.         return "update iqp_account_t set " + columns + " where id = " + identitySupplier.get();
  189.     }
  190.  
  191.     public static AccountAide aide(@Nonnull DictionaryProvider dictionaryProvider) {
  192.         return new AccountAide(dictionaryProvider);
  193.     }
  194.  
  195.     public static class AccountId {
  196.         public final String ACCOUNT;
  197.         public final String INN;
  198.         public final String CONTR_NAME;
  199.  
  200.         private AccountId(@Nonnull SubscriberInfo item) {
  201.             ACCOUNT = item.ACCOUNT;
  202.             INN = item.INN;
  203.             CONTR_NAME = item.CONTR_NAME;
  204.  
  205.             Assert.notNull(ACCOUNT);
  206.         }
  207.  
  208.         private AccountId(@Nonnull ResultSet rs) throws SQLException {
  209.             String col_accountNum = rs.getString("col_accountNum");
  210.             String col_inn = rs.getString("col_inn");
  211.             String col_companyName = rs.getString("col_companyName");
  212.  
  213.  
  214.             ACCOUNT = col_accountNum;
  215.  
  216.             INN = col_inn;
  217.             CONTR_NAME = col_companyName;
  218.  
  219.             Assert.notNull(ACCOUNT);
  220.         }
  221.  
  222.         @Override
  223.         public boolean equals(Object that) {
  224.             if (this == that) {
  225.                 return true;
  226.             }
  227.  
  228.             if (that == null || getClass() != that.getClass()) {
  229.                 return false;
  230.             }
  231.  
  232.             AccountId accountId = (AccountId) that;
  233.             if (INN != null) {
  234.                 return Objects.equals(ACCOUNT, accountId.ACCOUNT) &&
  235.                         Objects.equals(INN, accountId.INN);
  236.             } else {
  237.                 //noinspection ConstantConditions
  238.                 return Objects.equals(ACCOUNT, accountId.ACCOUNT) &&
  239.                         Objects.equals(INN, accountId.INN) &&
  240.                         Objects.equals(CONTR_NAME, accountId.CONTR_NAME);
  241.             }
  242.         }
  243.  
  244.         @Override
  245.         public int hashCode() {
  246.             if (INN != null) {
  247.                 return Objects.hash(ACCOUNT, INN);
  248.             } else {
  249.                 //noinspection ConstantConditions
  250.                 return Objects.hash(ACCOUNT, INN, CONTR_NAME);
  251.             }
  252.         }
  253.  
  254.         @Override
  255.         public String toString() {
  256.             try {
  257.                 return JacksonObjectMapper.INSTANCE.writeValueAsString(this);
  258.             } catch (JsonProcessingException e) {
  259.                 throw new RuntimeException(e);
  260.             }
  261.         }
  262.     }
  263.  
  264.     public static AccountId accountId(@Nonnull SubscriberInfo item) {
  265.         return new AccountId(item);
  266.     }
  267.  
  268.     public static AccountId accountId(@Nonnull ResultSet rs) throws SQLException {
  269.         return new AccountId(rs);
  270.     }
  271. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement