Advertisement
Guest User

Untitled

a guest
Jan 17th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Groovy 16.85 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.dictionaries.model.ThSafeDictionaryItem;
  8. import com.iqplatform.model.inner.InnerObject;
  9. import com.iqplatform.providers.DictionaryProvider;
  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 25.12.17.
  25.         1 col_abnId                  ABN_ID              String
  26.         2 col_dateConnect            DATA_CONNECT        LocalDate
  27.         3 col_dateClosing            DATA_CLOSE_SERV     LocalDate
  28.         4 col_dateContract           DATA_CONTR          LocalDate
  29.         5 col_addressDevice          ADRESS_DEV          String
  30.         6 col_dateContractClose      DATA_CLOSE          LocalDate
  31.         7 col_accountNum             ACCOUNT             String
  32.         8 col_serviceName            SERV_NAME_REF       String
  33.         8 col_serviceId              SERV_ID             Long
  34.         10 col_techName              TECH_NAME_REF       String
  35.         11 col_techId                TECH_ID             Long
  36.         12 col_codeMRF               MRF_ID              Long
  37.         13 col_codeRF                RF_ID               Long
  38.         14 col_subsAddress           ADRESS              String
  39.         15 col_dateSnap              DATE_SNAP           LocalDateTime
  40.         16 col_tariffPlan            RTPL_NAME           String
  41.         17 col_inn                   INN                 String
  42.         18 col_companyName           CONTR_NAME          String
  43.         19 col_login                 SA_LOGIN            String
  44.  
  45.         20 rf           Dict("RF","branchCode",codeRF || 'Не определен')
  46.         21 mrf          Dict('mrf','Code',codeMRF || 'Не определен')
  47.  
  48.         22 service      Dict('serviceType','id',serviceId)
  49.            service      Dict('serviceType','Name',serviceName)
  50.            service      Dict('serviceType','id','-1')
  51.  
  52.         23 tech         Dict('techType','id',techId)
  53.            tech         Dict('techType','Name,Name2',techName)
  54.            tech         Dict('techType','id','-1')
  55.  
  56.         24 changeDateTime LocalDateTime.now()
  57.         25 debug
  58.  
  59.         26 idClient                 CL_ID
  60.  
  61.  **/
  62. @SuppressWarnings({"WeakerAccess", "BooleanMethodIsAlwaysInverted"})
  63. public class SubscriberAide extends AbstractAide {
  64.  
  65.     public SubscriberAide() {}
  66.  
  67.     public SubscriberAide(@Nonnull DictionaryProvider dictionaryProvider) {
  68.         super(dictionaryProvider);
  69.     }
  70.  
  71.     /**  **/
  72.     boolean hasChanges(@Nonnull ResultSet rs, @Nonnull SubscriberInfo item, @Nonnull DictionaryProvider dictionaryProvider) throws SQLException {
  73.         String col_abnId = rs.getString("col_abnId");
  74.         String col_inn = rs.getString("col_inn");
  75.         Assert.isTrue(Objects.equals(col_abnId, item.ABN_ID + ""));
  76.         Assert.isTrue(Objects.equals(col_inn, item.INN));
  77.  
  78.         boolean hasChanges;
  79.         final List<String> changedColumns = Lists.newArrayList();
  80.  
  81.         hasChanges = !equals(rs, "col_dateConnect", item.DATA_CONNECT, changedColumns);          // 1
  82.         hasChanges |= !equals(rs, "col_dateClosing", item.DATA_CLOSE_SERV, changedColumns);      // 2
  83.         hasChanges |= !equals(rs, "col_dateContract", item.DATA_CONTR, changedColumns);          // 3
  84.         hasChanges |= !equals(rs, "col_addressDevice", item.ADRESS_DEV, changedColumns);         // 4
  85.         hasChanges |= !equals(rs, "col_dateContractClose", item.DATA_CLOSE, changedColumns);     // 5
  86.         hasChanges |= !equals(rs, "col_accountNum", item.ACCOUNT, changedColumns);               // 6
  87.         hasChanges |= !equals(rs, "col_accountNum", item.ACCOUNT, changedColumns);               // 7
  88.         hasChanges |= !equals(rs, "col_serviceName", item.SERV_NAME_REF, changedColumns);        // 8
  89.         hasChanges |= !equals(rs, "col_serviceId", item.SERV_ID, changedColumns);                // 9
  90.         hasChanges |= !equals(rs, "col_techName", item.TECH_NAME_REF, changedColumns);           // 10
  91.         hasChanges |= !equals(rs, "col_techId", item.TECH_ID, changedColumns);                   // 11
  92.         hasChanges |= !equals(rs, "col_codeMRF", item.MRF_ID, changedColumns);                   // 12
  93.         hasChanges |= !equals(rs, "col_codeRF", item.RF_ID, changedColumns);                     // 13
  94.         hasChanges |= !equals(rs, "col_subsAddress", item.ADRESS, changedColumns);               // 14
  95.         hasChanges |= !equals(rs, "col_dateSnap", item.DATE_SNAP, changedColumns);               // 15
  96.         hasChanges |= !equals(rs, "col_tariffPlan", item.RTPL_NAME, changedColumns);             // 16
  97.         hasChanges |= !equals(rs, "col_inn", item.INN, changedColumns);                          // 17
  98.         hasChanges |= !equals(rs, "col_companyName", item.CONTR_NAME, changedColumns);           // 18
  99.         hasChanges |= !equals(rs, "col_login", item.SA_LOGIN, changedColumns);                   // 19
  100.         hasChanges |= !equals(rs, "col_idClient", item.CL_ID, changedColumns);                   // 26
  101.  
  102.         return hasChanges;
  103.     }
  104.    
  105.     String prepareSelectSql(@Nonnull List<SubscriberInfo> items) {
  106.         Assert.isTrue(!items.isEmpty());
  107.         List<String> where = new ArrayList<>(items.size());
  108.  
  109.         StringBuilder builder = new StringBuilder("select id, " +
  110.                 " col_abnId,\n" +                // ABN_ID
  111.                 " col_dateConnect,\n" +          // DATA_CONNECT
  112.                 " col_dateClosing,\n" +          // DATA_CLOSE_SERV
  113.                 " col_dateContract,\n" +         // DATA_CONTR
  114.                 " col_addressDevice,\n" +        // ADRESS_DEV
  115.                 " col_dateContractClose,\n" +    // DATA_CLOSE
  116.                 " col_accountNum,\n" +           // ACCOUNT
  117.                 " col_serviceName,\n" +          // SERV_NAME_REF
  118.                 " col_serviceId,\n" +            // SERV_ID
  119.                 " col_techName,\n" +             // TECH_NAME_REF
  120.                 " col_techId,\n" +               // TECH_ID
  121.                 " col_codeMRF,\n" +              // MRF_ID
  122.                 " col_codeRF,\n" +               // RF_ID
  123.                 " col_subsAddress,\n" +          // ADRESS
  124.                 " col_dateSnap,\n" +             // DATE_SNAP
  125.                 " col_tariffPlan,\n" +           // RTPL_NAME
  126.                 " col_inn,\n" +                  // INN
  127.                 " col_companyName,\n" +          // CONTR_NAME
  128.                 " col_login,\n" +                // SA_LOGIN
  129.                 " col_idClient,\n" +             // CL_ID
  130.                 //" col_client_data_contr,\n" +
  131.                 //" col_client_data_close,\n" +
  132.                 " true as true \n" +
  133.                 "from iqp_subscriber_t where ");
  134.  
  135.         for (int i = 0; i < items.size(); i++) {
  136.             SubscriberInfo item = items.get(i);
  137.             Assert.isTrue(item.ABN_ID != null);
  138.             where.add(" col_abnId = '" + item.ABN_ID + "' ");
  139.         }
  140.  
  141.         builder.append("\n").append(Joiner.on(" or \n ").join(where));
  142.         return builder.toString();
  143.     }
  144.  
  145.     InnerObject createSubscriber(@Nonnull InnerObject subscriber, @Nonnull SubscriberInfo info) {
  146.         Assert.isTrue(Objects.equals("Subscriber", subscriber.getSchema().getAlias()));
  147.         /*
  148.                1 abnId
  149.                2 dateConnect
  150.                3 dateClosing
  151.                4 dateContract
  152.                5 addressDevice
  153.                6 dateContractClose
  154.                7 accountNum
  155.                8 serviceName
  156.                9 serviceId
  157.                10 techName
  158.                11 techId
  159.                12 codeMRF
  160.                13 codeRF
  161.                14 subsAddress
  162.                15 dateSnap
  163.                16 tariffPlan
  164.                17 inn
  165.                18 companyName
  166.                19 login
  167.  
  168.                20 client_data_contr   ???
  169.                21 client_data_close   ???
  170.  
  171.                26 idClient CL_ID
  172.           */
  173.  
  174.         subscriber.set("abnId", info.ABN_ID + "");                //1
  175.         subscriber.set("dateConnect", info.DATA_CONNECT);         //2
  176.         subscriber.set("dateClosing", info.DATA_CLOSE_SERV);      //3
  177.         subscriber.set("dateContract", info.DATA_CONTR);          //4
  178.         subscriber.set("addressDevice", info.ADRESS_DEV);         //5
  179.         subscriber.set("dateContractClose", info.DATA_CLOSE);     //6
  180.         subscriber.set("accountNum", info.ACCOUNT);               //7
  181.         subscriber.set("serviceName", info.SERV_NAME_REF);        //8
  182.         subscriber.set("serviceId", castAsString(info.SERV_ID));  //10
  183.         subscriber.set("techName", info.TECH_NAME_REF);           //10
  184.         subscriber.set("techId", info.TECH_ID);                   //11
  185.         subscriber.set("codeMRF", info.MRF_ID);                   //12
  186.         subscriber.set("codeRF", info.RF_ID);                     //13
  187.         subscriber.set("subsAddress", info.ADRESS);               //14
  188.         subscriber.set("dateSnap", info.DATE_SNAP);               //15
  189.         subscriber.set("tariffPlan", info.RTPL_NAME);             //16
  190.         subscriber.set("inn", info.INN);                          //17
  191.         subscriber.set("companyName", info.CONTR_NAME);           //18
  192.         subscriber.set("login", info.SA_LOGIN);                   //19
  193.  
  194.         //subscriber.set("client_data_contr", null);     //20
  195.         //subscriber.set("client_data_close", null);     //21
  196.         subscriber.set("idClient", info.CL_ID);          //19
  197.  
  198.         return subscriber;
  199.  
  200.     }
  201.  
  202.     /**
  203.      * возвращает null если нет применимых для апдейта данных (например все поля item == null)
  204.      **/
  205.     @Nullable
  206.     public String prepareUpdateSql(@Nonnull ResultSet rs, @Nonnull SubscriberInfo item) throws SQLException {
  207.         Long id = rs.getLong("id");
  208.         Assert.notNull(id);
  209.         Assert.notNull(item.ABN_ID);
  210.  
  211.         String col_abnId = rs.getString("col_abnId");
  212.         String col_inn = rs.getString("col_inn");
  213.         String col_accountNum = rs.getString("col_accountNum");
  214.  
  215.         Assert.isTrue(Objects.equals(col_abnId, item.ABN_ID + ""));
  216.         Assert.isTrue(Objects.equals(col_inn, item.INN));
  217.         Assert.isTrue(Objects.equals(col_accountNum, item.ACCOUNT));
  218.  
  219.         return prepareUpdateSql(item, () -> id);
  220.     }
  221.  
  222.     @Nullable
  223.     String prepareUpdateSql(@Nonnull SubscriberInfo item, @Nonnull Supplier<Long> identitySupplier) {
  224.         List<String> list = Lists.newArrayList();
  225.  
  226.         list.add(_column("col_dateConnect", item.DATA_CONNECT));                    // 2
  227.         list.add(_column("col_dateClosing", item.DATA_CLOSE_SERV));                 // 3
  228.         list.add(_column("col_dateContract", item.DATA_CLOSE_SERV));                // 4
  229.         list.add(_column("col_addressDevice", item.ADRESS_DEV));                    // 5
  230.         list.add(_column("col_dateContractClose", item.DATA_CLOSE));                // 6
  231.         list.add(_column("col_accountNum", item.ACCOUNT));                          // 7
  232.         list.add(_column("col_serviceName", item.SERV_NAME_REF));                   // 8
  233.         list.add(_column("col_serviceId", castAsString(item.SERV_ID)));             // 9
  234.         list.add(_column("col_techName", item.TECH_NAME_REF));                      // 10
  235.         list.add(_column("col_techId", castAsString(item.TECH_ID)));                // 11
  236.         list.add(_column("col_codeMRF", castAsString(item.MRF_ID)));                // 12
  237.         list.add(_column("col_codeRF", castAsString(item.RF_ID)));                  // 13
  238.         list.add(_column("col_subsAddress", item.ADRESS));                          // 14
  239.         list.add(_column("col_dateSnap", item.DATE_SNAP));                          // 15
  240.         list.add(_column("col_tariffPlan", item.RTPL_NAME));                        // 16
  241.         list.add(_column("col_inn", item.INN));                                     // 17
  242.         list.add(_column("col_companyName", item.CONTR_NAME));                      // 18
  243.         list.add(_column("col_login", item.SA_LOGIN));                              // 19
  244.  
  245.         list = list.stream().filter(it -> it != null && !it.isEmpty()).collect(Collectors.toList());
  246.  
  247.         if (list.isEmpty()) {
  248.             return null;
  249.         }
  250.  
  251.         list.add(_column("col_rf",
  252.                 dict("RF", "branchCode", castAsString(item.RF_ID), "Не определен")));  // 20
  253.  
  254.         ThSafeDictionaryItem service = null;
  255.         ThSafeDictionaryItem tech = null;
  256.  
  257.         if (item.SERV_ID != null) {
  258.             service = dict("serviceType","id", castAsString(item.SERV_ID));
  259.         } else if (item.SERV_NAME_REF != null) {
  260.             service = dict("serviceType","Name", item.SERV_NAME_REF);
  261.         }
  262.         if (service == null) {
  263.             service = dict("serviceType","id", "-1");
  264.         }
  265.  
  266.         list.add(_column("col_service", service));                                                           // 22
  267.  
  268.  
  269.         if (item.SERV_ID != null) {
  270.             tech = dict("techType","id", castAsString(item.TECH_ID));
  271.         } else if (item.TECH_NAME_REF != null) {
  272.             tech = dict("techType","Name,Name2", item.TECH_NAME_REF);
  273.         }
  274.         if (tech == null) {
  275.             tech = dict("techType","id", "-1");
  276.         }
  277.  
  278.         list.add(_column("col_tech", tech));                                                                  // 23
  279.  
  280.         list.add(_column("col_changeDateTime", LocalDateTime.now()));                                                               // 24
  281.         list.add(_column("col_debug", "subscriber-loader " + LocalDateTime.now().toString(SubscriberInfoLoader._debug_fmt_)));  // 25
  282.  
  283.         String columns = Joiner.on(", \n ").skipNulls().join(list);
  284.  
  285.         return "update iqp_subscriber_t set " + columns + " where id = " + identitySupplier.get();
  286.     }
  287.  
  288.     public static SubscriberAide aide(@Nonnull DictionaryProvider dictionaryProvider) {
  289.         return new SubscriberAide(dictionaryProvider);
  290.     }
  291.  
  292.  
  293.     public static class SubscriberId {
  294.         public final String ABN_ID;
  295.         public final String INN;
  296.         public final String CONTR_NAME;
  297.  
  298.         private SubscriberId(@Nonnull SubscriberInfo item) {
  299.             Assert.notNull(item.ABN_ID);
  300.             ABN_ID = item.ABN_ID + "";
  301.             INN = item.INN;
  302.             CONTR_NAME = item.CONTR_NAME;
  303.  
  304.             Assert.notNull(ABN_ID);
  305.         }
  306.  
  307.         private SubscriberId(@Nonnull ResultSet rs) throws SQLException {
  308.             String col_abnId = rs.getString("col_abnId");
  309.             String col_inn = rs.getString("col_inn");
  310.             String col_companyName = rs.getString("col_companyName");
  311.  
  312.             ABN_ID = col_abnId;
  313.             INN = col_inn;
  314.             CONTR_NAME = col_companyName;
  315.  
  316.             Assert.notNull(ABN_ID);
  317.         }
  318.  
  319.         @Override
  320.         public boolean equals(Object that) {
  321.             if (this == that) {
  322.                 return true;
  323.             }
  324.  
  325.             if (that == null || getClass() != that.getClass()) {
  326.                 return false;
  327.             }
  328.  
  329.             SubscriberAide.SubscriberId subscriberId = (SubscriberAide.SubscriberId) that;
  330.             if (INN != null) {
  331.                 return Objects.equals(ABN_ID, subscriberId.ABN_ID) &&
  332.                         Objects.equals(INN, subscriberId.INN);
  333.             } else {
  334.                 //noinspection ConstantConditions
  335.                 return Objects.equals(ABN_ID, subscriberId.ABN_ID) &&
  336.                         Objects.equals(INN, subscriberId.INN) &&
  337.                         Objects.equals(CONTR_NAME, subscriberId.CONTR_NAME);
  338.             }
  339.         }
  340.  
  341.         @Override
  342.         public int hashCode() {
  343.             if (INN != null) {
  344.                 return Objects.hash(ABN_ID, INN);
  345.             } else {
  346.                 //noinspection ConstantConditions
  347.                 return Objects.hash(ABN_ID, INN, CONTR_NAME);
  348.             }
  349.         }
  350.  
  351.         @Override
  352.         public String toString() {
  353.             try {
  354.                 return JacksonObjectMapper.INSTANCE.writeValueAsString(this);
  355.             } catch (JsonProcessingException e) {
  356.                 throw new RuntimeException(e);
  357.             }
  358.         }
  359.     }
  360.  
  361.     public static SubscriberAide.SubscriberId subscriberId(@Nonnull SubscriberInfo item) {
  362.         return new SubscriberAide.SubscriberId(item);
  363.     }
  364.  
  365.     public static SubscriberAide.SubscriberId subscriberId(@Nonnull ResultSet rs) throws SQLException {
  366.         return new SubscriberAide.SubscriberId(rs);
  367.     }
  368. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement