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.model.inner.InnerObject;
- import com.iqplatform.providers.DictionaryProvider;
- import org.apache.commons.lang.StringEscapeUtils;
- 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 26.12.17.
- * <p>
- * 1 accountNum ACCOUNT
- * 2 inn INN
- * 3 companyName CONTR_NAME
- * 5 dateClosing DATA_CLOSE
- * 6 dateContract DATA_CONTR
- * 7 dateSnap DATE_SNAP
- * <p>
- * 8 client_data_contr
- * 9 client_data_close
- * <p>
- * <p>
- * 10 rf Dict("RF","branchCode",codeRF)
- * 11 mrf Dict('mrf','Code',codeMRF)
- * 12 kSegment Dict('kSegment','Code,Code2',kSegmentStr || '-')
- * 13 mSegment Dict('mSegment','Name,Name2,Name3,Name4,Name5',segmentStr || 'Не определен')
- * 14 macrosegment Dict('macrosegment','Name',macrosegmentStr || 'Не определен')
- * 15 serviceClassDict Dict('serviceClass','Type,Type2',serviceClass || 'Не определен')
- * 16 subsegment Dict('Subsegment','Name,Name2,Name3,Name4',subsegmentStr || 'Не определен')
- * <p>
- * 17 changeDateTime LocalDateTime.now()
- * 18 debug
- **/
- public class AccountAide extends AbstractAide {
- public AccountAide() {
- super();
- }
- public AccountAide(@Nonnull DictionaryProvider dictionaryProvider) {
- super(dictionaryProvider);
- }
- String prepareSelectSql(@Nonnull List<SubscriberInfo> items) {
- Assert.isTrue(!items.isEmpty());
- List<String> where = new ArrayList<>(items.size());
- StringBuilder builder = new StringBuilder("select id, " +
- " col_accountNum,\n" + // ACCOUNT 1
- " col_inn,\n" + // inn 2
- " col_companyName,\n" + // CONTR_NAME 3
- " col_dateClosing,\n" + // DATA_CLOSE_SERV 5
- " col_dateContract,\n" + // DATA_CONTR 6
- " col_dateSnap,\n" + // DATE_SNAP 7
- //" col_client_data_contr,\n" +
- //" col_client_data_close,\n" +
- " true as true \n" +
- "from iqp_account_t where ");
- for (int i = 0; i < items.size(); i++) {
- SubscriberInfo item = items.get(i);
- Assert.isTrue(item.ACCOUNT != null);
- if (item.INN != null) {
- where.add(" ( (col_accountNum = '" + item.ACCOUNT + "') and (col_inn = '" + item.INN + "') ) ");
- } else {
- if (item.CONTR_NAME != null) {
- where.add(" ( (col_accountNum = '" + item.ACCOUNT + "') and (col_companyName = '" + StringEscapeUtils.escapeSql(item.CONTR_NAME) + "') and (col_inn is null) ) ");
- } else {
- where.add(" ( (col_accountNum = '" + item.ACCOUNT + "') and (col_companyName is null) and (col_inn is null) ) ");
- }
- }
- }
- builder.append("\n").append(Joiner.on(" or \n ").join(where));
- return builder.toString();
- }
- InnerObject createAccount(@Nonnull InnerObject account, @Nonnull SubscriberInfo info) {
- Assert.isTrue(Objects.equals("Account", account.getSchema().getAlias()));
- /*
- 1 accountNum ACCOUNT
- 2 inn INN
- 3 companyName CONTR_NAME
- 5 dateClosing DATA_CLOSE_SERV
- 6 dateContract DATA_CONTR
- 7 dateSnap DATE_SNAP
- 8 client_data_contr
- 9 client_data_close
- */
- account.set("accountNum", info.ACCOUNT); // 1
- account.set("inn", info.INN); // 2
- account.set("companyName", info.CONTR_NAME); // 3
- account.set("dateClosing", info.DATA_CLOSE); // 5
- account.set("dateContract", info.DATA_CONTR); // 6
- account.set("dateSnap", info.DATE_SNAP); // 7
- //subscriber.set("client_data_contr", null); // 8
- //subscriber.set("client_data_close", null); // 9
- return account;
- }
- /** **/
- boolean hasChanges(@Nonnull ResultSet rs, @Nonnull SubscriberInfo item, @Nonnull DictionaryProvider dictionaryProvider) throws SQLException {
- String col_accountNum = rs.getString("col_accountNum");
- String col_inn = rs.getString("col_inn");
- Assert.isTrue(Objects.equals(col_accountNum, item.ACCOUNT));
- Assert.isTrue(Objects.equals(col_inn, item.INN));
- boolean hasChanges;
- final List<String> changedColumns = Lists.newArrayList();
- hasChanges = !equals(rs, "col_companyName", item.CONTR_NAME, changedColumns); // 3
- hasChanges |= !equals(rs, "col_dateClosing", item.DATA_CLOSE, changedColumns); // 5
- hasChanges |= !equals(rs, "col_dateContract", item.DATA_CONTR, changedColumns); // 6
- if (item.DATE_SNAP != null) {
- hasChanges |= !equals(rs, "col_dateSnap", item.DATE_SNAP.toLocalDate(), changedColumns); // 7
- }
- return hasChanges;
- }
- /**
- * возвращает 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.ACCOUNT);
- Assert.notNull(item.INN);
- String col_accountNum = rs.getString("col_accountNum");
- String col_inn = rs.getString("col_inn");
- Assert.isTrue(Objects.equals(col_accountNum, item.ACCOUNT));
- Assert.isTrue(Objects.equals(col_inn, item.INN));
- return prepareUpdateSql(item, () -> id);
- }
- @Nullable
- String prepareUpdateSql(@Nonnull SubscriberInfo item, @Nonnull Supplier<Long> identitySupplier) {
- List<String> list = Lists.newArrayList();
- if (item.INN != null && item.CONTR_NAME != null) {
- list.add(_column("col_companyName", item.CONTR_NAME)); // 3
- }
- list.add(_column("col_dateClosing", item.DATA_CLOSE_SERV)); // 5
- list.add(_column("col_dateContract", item.DATA_CLOSE_SERV)); // 6
- if (item.DATE_SNAP != null) {
- list.add(_column("col_dateSnap", item.DATE_SNAP.toLocalDate()));/** для абонентов dateSnap это дата и время а вот для счетов и организаций - только дата. приятно правда? **/
- }
- list = list.stream().filter(it -> it != null && !it.isEmpty()).collect(Collectors.toList());
- if (list.isEmpty()) {
- return null;
- }
- list.add(_column("col_changeDateTime", LocalDateTime.now())); // 17
- list.add(_column("col_debug", "subscriber-loader " + LocalDateTime.now().toString(SubscriberInfoLoader._debug_fmt_))); // 18
- String columns = Joiner.on(", \n ").skipNulls().join(list);
- return "update iqp_account_t set " + columns + " where id = " + identitySupplier.get();
- }
- public static AccountAide aide(@Nonnull DictionaryProvider dictionaryProvider) {
- return new AccountAide(dictionaryProvider);
- }
- public static class AccountId {
- public final String ACCOUNT;
- public final String INN;
- public final String CONTR_NAME;
- private AccountId(@Nonnull SubscriberInfo item) {
- ACCOUNT = item.ACCOUNT;
- INN = item.INN;
- CONTR_NAME = item.CONTR_NAME;
- Assert.notNull(ACCOUNT);
- }
- private AccountId(@Nonnull ResultSet rs) throws SQLException {
- String col_accountNum = rs.getString("col_accountNum");
- String col_inn = rs.getString("col_inn");
- String col_companyName = rs.getString("col_companyName");
- ACCOUNT = col_accountNum;
- INN = col_inn;
- CONTR_NAME = col_companyName;
- Assert.notNull(ACCOUNT);
- }
- @Override
- public boolean equals(Object that) {
- if (this == that) {
- return true;
- }
- if (that == null || getClass() != that.getClass()) {
- return false;
- }
- AccountId accountId = (AccountId) that;
- if (INN != null) {
- return Objects.equals(ACCOUNT, accountId.ACCOUNT) &&
- Objects.equals(INN, accountId.INN);
- } else {
- //noinspection ConstantConditions
- return Objects.equals(ACCOUNT, accountId.ACCOUNT) &&
- Objects.equals(INN, accountId.INN) &&
- Objects.equals(CONTR_NAME, accountId.CONTR_NAME);
- }
- }
- @Override
- public int hashCode() {
- if (INN != null) {
- return Objects.hash(ACCOUNT, INN);
- } else {
- //noinspection ConstantConditions
- return Objects.hash(ACCOUNT, INN, CONTR_NAME);
- }
- }
- @Override
- public String toString() {
- try {
- return JacksonObjectMapper.INSTANCE.writeValueAsString(this);
- } catch (JsonProcessingException e) {
- throw new RuntimeException(e);
- }
- }
- }
- public static AccountId accountId(@Nonnull SubscriberInfo item) {
- return new AccountId(item);
- }
- public static AccountId accountId(@Nonnull ResultSet rs) throws SQLException {
- return new AccountId(rs);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement