Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.iqplatform.project.entities.setout.subscriber;
- import com.fasterxml.jackson.core.JsonProcessingException;
- import com.google.common.base.Joiner;
- import com.google.common.collect.Lists;
- import com.iqmen.apiJson.jackson.JacksonObjectMapper;
- import com.iqplatform.dictionaries.model.ThSafeDictionaryItem;
- import com.iqplatform.model.inner.InnerObject;
- import com.iqplatform.providers.DictionaryProvider;
- import org.joda.time.LocalDateTime;
- import org.springframework.util.Assert;
- import javax.annotation.Nonnull;
- import javax.annotation.Nullable;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Objects;
- import java.util.function.Supplier;
- import java.util.stream.Collectors;
- /**
- * Created by vyaz on 25.12.17.
- 1 col_abnId ABN_ID String
- 2 col_dateConnect DATA_CONNECT LocalDate
- 3 col_dateClosing DATA_CLOSE_SERV LocalDate
- 4 col_dateContract DATA_CONTR LocalDate
- 5 col_addressDevice ADRESS_DEV String
- 6 col_dateContractClose DATA_CLOSE LocalDate
- 7 col_accountNum ACCOUNT String
- 8 col_serviceName SERV_NAME_REF String
- 8 col_serviceId SERV_ID Long
- 10 col_techName TECH_NAME_REF String
- 11 col_techId TECH_ID Long
- 12 col_codeMRF MRF_ID Long
- 13 col_codeRF RF_ID Long
- 14 col_subsAddress ADRESS String
- 15 col_dateSnap DATE_SNAP LocalDateTime
- 16 col_tariffPlan RTPL_NAME String
- 17 col_inn INN String
- 18 col_companyName CONTR_NAME String
- 19 col_login SA_LOGIN String
- 20 rf Dict("RF","branchCode",codeRF || 'Не определен')
- 21 mrf Dict('mrf','Code',codeMRF || 'Не определен')
- 22 service Dict('serviceType','id',serviceId)
- service Dict('serviceType','Name',serviceName)
- service Dict('serviceType','id','-1')
- 23 tech Dict('techType','id',techId)
- tech Dict('techType','Name,Name2',techName)
- tech Dict('techType','id','-1')
- 24 changeDateTime LocalDateTime.now()
- 25 debug
- 26 idClient CL_ID
- **/
- @SuppressWarnings({"WeakerAccess", "BooleanMethodIsAlwaysInverted"})
- public class SubscriberAide extends AbstractAide {
- public SubscriberAide() {}
- public SubscriberAide(@Nonnull DictionaryProvider dictionaryProvider) {
- super(dictionaryProvider);
- }
- /** **/
- boolean hasChanges(@Nonnull ResultSet rs, @Nonnull SubscriberInfo item, @Nonnull DictionaryProvider dictionaryProvider) throws SQLException {
- String col_abnId = rs.getString("col_abnId");
- String col_inn = rs.getString("col_inn");
- Assert.isTrue(Objects.equals(col_abnId, item.ABN_ID + ""));
- Assert.isTrue(Objects.equals(col_inn, item.INN));
- boolean hasChanges;
- final List<String> changedColumns = Lists.newArrayList();
- hasChanges = !equals(rs, "col_dateConnect", item.DATA_CONNECT, changedColumns); // 1
- hasChanges |= !equals(rs, "col_dateClosing", item.DATA_CLOSE_SERV, changedColumns); // 2
- hasChanges |= !equals(rs, "col_dateContract", item.DATA_CONTR, changedColumns); // 3
- hasChanges |= !equals(rs, "col_addressDevice", item.ADRESS_DEV, changedColumns); // 4
- hasChanges |= !equals(rs, "col_dateContractClose", item.DATA_CLOSE, changedColumns); // 5
- hasChanges |= !equals(rs, "col_accountNum", item.ACCOUNT, changedColumns); // 6
- hasChanges |= !equals(rs, "col_accountNum", item.ACCOUNT, changedColumns); // 7
- hasChanges |= !equals(rs, "col_serviceName", item.SERV_NAME_REF, changedColumns); // 8
- hasChanges |= !equals(rs, "col_serviceId", item.SERV_ID, changedColumns); // 9
- hasChanges |= !equals(rs, "col_techName", item.TECH_NAME_REF, changedColumns); // 10
- hasChanges |= !equals(rs, "col_techId", item.TECH_ID, changedColumns); // 11
- hasChanges |= !equals(rs, "col_codeMRF", item.MRF_ID, changedColumns); // 12
- hasChanges |= !equals(rs, "col_codeRF", item.RF_ID, changedColumns); // 13
- hasChanges |= !equals(rs, "col_subsAddress", item.ADRESS, changedColumns); // 14
- hasChanges |= !equals(rs, "col_dateSnap", item.DATE_SNAP, changedColumns); // 15
- hasChanges |= !equals(rs, "col_tariffPlan", item.RTPL_NAME, changedColumns); // 16
- hasChanges |= !equals(rs, "col_inn", item.INN, changedColumns); // 17
- hasChanges |= !equals(rs, "col_companyName", item.CONTR_NAME, changedColumns); // 18
- hasChanges |= !equals(rs, "col_login", item.SA_LOGIN, changedColumns); // 19
- hasChanges |= !equals(rs, "col_idClient", item.CL_ID, changedColumns); // 26
- return hasChanges;
- }
- String prepareSelectSql(@Nonnull List<SubscriberInfo> items) {
- Assert.isTrue(!items.isEmpty());
- List<String> where = new ArrayList<>(items.size());
- StringBuilder builder = new StringBuilder("select id, " +
- " col_abnId,\n" + // ABN_ID
- " col_dateConnect,\n" + // DATA_CONNECT
- " col_dateClosing,\n" + // DATA_CLOSE_SERV
- " col_dateContract,\n" + // DATA_CONTR
- " col_addressDevice,\n" + // ADRESS_DEV
- " col_dateContractClose,\n" + // DATA_CLOSE
- " col_accountNum,\n" + // ACCOUNT
- " col_serviceName,\n" + // SERV_NAME_REF
- " col_serviceId,\n" + // SERV_ID
- " col_techName,\n" + // TECH_NAME_REF
- " col_techId,\n" + // TECH_ID
- " col_codeMRF,\n" + // MRF_ID
- " col_codeRF,\n" + // RF_ID
- " col_subsAddress,\n" + // ADRESS
- " col_dateSnap,\n" + // DATE_SNAP
- " col_tariffPlan,\n" + // RTPL_NAME
- " col_inn,\n" + // INN
- " col_companyName,\n" + // CONTR_NAME
- " col_login,\n" + // SA_LOGIN
- " col_idClient,\n" + // CL_ID
- //" col_client_data_contr,\n" +
- //" col_client_data_close,\n" +
- " true as true \n" +
- "from iqp_subscriber_t where ");
- for (int i = 0; i < items.size(); i++) {
- SubscriberInfo item = items.get(i);
- Assert.isTrue(item.ABN_ID != null);
- where.add(" col_abnId = '" + item.ABN_ID + "' ");
- }
- builder.append("\n").append(Joiner.on(" or \n ").join(where));
- return builder.toString();
- }
- InnerObject createSubscriber(@Nonnull InnerObject subscriber, @Nonnull SubscriberInfo info) {
- Assert.isTrue(Objects.equals("Subscriber", subscriber.getSchema().getAlias()));
- /*
- 1 abnId
- 2 dateConnect
- 3 dateClosing
- 4 dateContract
- 5 addressDevice
- 6 dateContractClose
- 7 accountNum
- 8 serviceName
- 9 serviceId
- 10 techName
- 11 techId
- 12 codeMRF
- 13 codeRF
- 14 subsAddress
- 15 dateSnap
- 16 tariffPlan
- 17 inn
- 18 companyName
- 19 login
- 20 client_data_contr ???
- 21 client_data_close ???
- 26 idClient CL_ID
- */
- subscriber.set("abnId", info.ABN_ID + ""); //1
- subscriber.set("dateConnect", info.DATA_CONNECT); //2
- subscriber.set("dateClosing", info.DATA_CLOSE_SERV); //3
- subscriber.set("dateContract", info.DATA_CONTR); //4
- subscriber.set("addressDevice", info.ADRESS_DEV); //5
- subscriber.set("dateContractClose", info.DATA_CLOSE); //6
- subscriber.set("accountNum", info.ACCOUNT); //7
- subscriber.set("serviceName", info.SERV_NAME_REF); //8
- subscriber.set("serviceId", castAsString(info.SERV_ID)); //10
- subscriber.set("techName", info.TECH_NAME_REF); //10
- subscriber.set("techId", info.TECH_ID); //11
- subscriber.set("codeMRF", info.MRF_ID); //12
- subscriber.set("codeRF", info.RF_ID); //13
- subscriber.set("subsAddress", info.ADRESS); //14
- subscriber.set("dateSnap", info.DATE_SNAP); //15
- subscriber.set("tariffPlan", info.RTPL_NAME); //16
- subscriber.set("inn", info.INN); //17
- subscriber.set("companyName", info.CONTR_NAME); //18
- subscriber.set("login", info.SA_LOGIN); //19
- //subscriber.set("client_data_contr", null); //20
- //subscriber.set("client_data_close", null); //21
- subscriber.set("idClient", info.CL_ID); //19
- return subscriber;
- }
- /**
- * возвращает null если нет применимых для апдейта данных (например все поля item == null)
- **/
- @Nullable
- public String prepareUpdateSql(@Nonnull ResultSet rs, @Nonnull SubscriberInfo item) throws SQLException {
- Long id = rs.getLong("id");
- Assert.notNull(id);
- Assert.notNull(item.ABN_ID);
- String col_abnId = rs.getString("col_abnId");
- String col_inn = rs.getString("col_inn");
- String col_accountNum = rs.getString("col_accountNum");
- Assert.isTrue(Objects.equals(col_abnId, item.ABN_ID + ""));
- Assert.isTrue(Objects.equals(col_inn, item.INN));
- Assert.isTrue(Objects.equals(col_accountNum, item.ACCOUNT));
- return prepareUpdateSql(item, () -> id);
- }
- @Nullable
- String prepareUpdateSql(@Nonnull SubscriberInfo item, @Nonnull Supplier<Long> identitySupplier) {
- List<String> list = Lists.newArrayList();
- list.add(_column("col_dateConnect", item.DATA_CONNECT)); // 2
- list.add(_column("col_dateClosing", item.DATA_CLOSE_SERV)); // 3
- list.add(_column("col_dateContract", item.DATA_CLOSE_SERV)); // 4
- list.add(_column("col_addressDevice", item.ADRESS_DEV)); // 5
- list.add(_column("col_dateContractClose", item.DATA_CLOSE)); // 6
- list.add(_column("col_accountNum", item.ACCOUNT)); // 7
- list.add(_column("col_serviceName", item.SERV_NAME_REF)); // 8
- list.add(_column("col_serviceId", castAsString(item.SERV_ID))); // 9
- list.add(_column("col_techName", item.TECH_NAME_REF)); // 10
- list.add(_column("col_techId", castAsString(item.TECH_ID))); // 11
- list.add(_column("col_codeMRF", castAsString(item.MRF_ID))); // 12
- list.add(_column("col_codeRF", castAsString(item.RF_ID))); // 13
- list.add(_column("col_subsAddress", item.ADRESS)); // 14
- list.add(_column("col_dateSnap", item.DATE_SNAP)); // 15
- list.add(_column("col_tariffPlan", item.RTPL_NAME)); // 16
- list.add(_column("col_inn", item.INN)); // 17
- list.add(_column("col_companyName", item.CONTR_NAME)); // 18
- list.add(_column("col_login", item.SA_LOGIN)); // 19
- list = list.stream().filter(it -> it != null && !it.isEmpty()).collect(Collectors.toList());
- if (list.isEmpty()) {
- return null;
- }
- list.add(_column("col_rf",
- dict("RF", "branchCode", castAsString(item.RF_ID), "Не определен"))); // 20
- ThSafeDictionaryItem service = null;
- ThSafeDictionaryItem tech = null;
- if (item.SERV_ID != null) {
- service = dict("serviceType","id", castAsString(item.SERV_ID));
- } else if (item.SERV_NAME_REF != null) {
- service = dict("serviceType","Name", item.SERV_NAME_REF);
- }
- if (service == null) {
- service = dict("serviceType","id", "-1");
- }
- list.add(_column("col_service", service)); // 22
- if (item.SERV_ID != null) {
- tech = dict("techType","id", castAsString(item.TECH_ID));
- } else if (item.TECH_NAME_REF != null) {
- tech = dict("techType","Name,Name2", item.TECH_NAME_REF);
- }
- if (tech == null) {
- tech = dict("techType","id", "-1");
- }
- list.add(_column("col_tech", tech)); // 23
- list.add(_column("col_changeDateTime", LocalDateTime.now())); // 24
- list.add(_column("col_debug", "subscriber-loader " + LocalDateTime.now().toString(SubscriberInfoLoader._debug_fmt_))); // 25
- String columns = Joiner.on(", \n ").skipNulls().join(list);
- return "update iqp_subscriber_t set " + columns + " where id = " + identitySupplier.get();
- }
- public static SubscriberAide aide(@Nonnull DictionaryProvider dictionaryProvider) {
- return new SubscriberAide(dictionaryProvider);
- }
- public static class SubscriberId {
- public final String ABN_ID;
- public final String INN;
- public final String CONTR_NAME;
- private SubscriberId(@Nonnull SubscriberInfo item) {
- Assert.notNull(item.ABN_ID);
- ABN_ID = item.ABN_ID + "";
- INN = item.INN;
- CONTR_NAME = item.CONTR_NAME;
- Assert.notNull(ABN_ID);
- }
- private SubscriberId(@Nonnull ResultSet rs) throws SQLException {
- String col_abnId = rs.getString("col_abnId");
- String col_inn = rs.getString("col_inn");
- String col_companyName = rs.getString("col_companyName");
- ABN_ID = col_abnId;
- INN = col_inn;
- CONTR_NAME = col_companyName;
- Assert.notNull(ABN_ID);
- }
- @Override
- public boolean equals(Object that) {
- if (this == that) {
- return true;
- }
- if (that == null || getClass() != that.getClass()) {
- return false;
- }
- SubscriberAide.SubscriberId subscriberId = (SubscriberAide.SubscriberId) that;
- if (INN != null) {
- return Objects.equals(ABN_ID, subscriberId.ABN_ID) &&
- Objects.equals(INN, subscriberId.INN);
- } else {
- //noinspection ConstantConditions
- return Objects.equals(ABN_ID, subscriberId.ABN_ID) &&
- Objects.equals(INN, subscriberId.INN) &&
- Objects.equals(CONTR_NAME, subscriberId.CONTR_NAME);
- }
- }
- @Override
- public int hashCode() {
- if (INN != null) {
- return Objects.hash(ABN_ID, INN);
- } else {
- //noinspection ConstantConditions
- return Objects.hash(ABN_ID, INN, CONTR_NAME);
- }
- }
- @Override
- public String toString() {
- try {
- return JacksonObjectMapper.INSTANCE.writeValueAsString(this);
- } catch (JsonProcessingException e) {
- throw new RuntimeException(e);
- }
- }
- }
- public static SubscriberAide.SubscriberId subscriberId(@Nonnull SubscriberInfo item) {
- return new SubscriberAide.SubscriberId(item);
- }
- public static SubscriberAide.SubscriberId subscriberId(@Nonnull ResultSet rs) throws SQLException {
- return new SubscriberAide.SubscriberId(rs);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement