From 145b286a2f29b293a818843e11032f097120cd59 Mon Sep 17 00:00:00 2001 From: "k.struzhkin" Date: Wed, 15 Apr 2020 16:58:31 +0300 Subject: [PATCH] Fix for father --- .../newway/dao/party/iface/ContractDao.java | 2 +- .../newway/dao/party/iface/ContractorDao.java | 2 +- .../newway/dao/party/iface/ShopDao.java | 2 +- .../dao/party/impl/ContractDaoImpl.java | 11 +++++----- .../dao/party/impl/ContractorDaoImpl.java | 13 ++++++------ .../newway/dao/party/impl/ShopDaoImpl.java | 10 ++++++---- .../party/PartyRevisionChangedHandler.java | 20 ++++++++++++++----- 7 files changed, 37 insertions(+), 23 deletions(-) diff --git a/src/main/java/com/rbkmoney/newway/dao/party/iface/ContractDao.java b/src/main/java/com/rbkmoney/newway/dao/party/iface/ContractDao.java index 9ec8fdf..642cb9b 100644 --- a/src/main/java/com/rbkmoney/newway/dao/party/iface/ContractDao.java +++ b/src/main/java/com/rbkmoney/newway/dao/party/iface/ContractDao.java @@ -18,7 +18,7 @@ public interface ContractDao extends GenericDao { void updateNotCurrent(List ids) throws DaoException; - void switchCurrent(List ids) throws DaoException; + void switchCurrent(List ids, String partyId) throws DaoException; List getByPartyId(String partyId); } diff --git a/src/main/java/com/rbkmoney/newway/dao/party/iface/ContractorDao.java b/src/main/java/com/rbkmoney/newway/dao/party/iface/ContractorDao.java index ee32789..fc63b49 100644 --- a/src/main/java/com/rbkmoney/newway/dao/party/iface/ContractorDao.java +++ b/src/main/java/com/rbkmoney/newway/dao/party/iface/ContractorDao.java @@ -18,7 +18,7 @@ public interface ContractorDao extends GenericDao { void updateNotCurrent(List ids) throws DaoException; - void switchCurrent(List ids) throws DaoException; + void switchCurrent(List ids, String partyId) throws DaoException; List getByPartyId(String partyId); } diff --git a/src/main/java/com/rbkmoney/newway/dao/party/iface/ShopDao.java b/src/main/java/com/rbkmoney/newway/dao/party/iface/ShopDao.java index 709c3c3..4c9c265 100644 --- a/src/main/java/com/rbkmoney/newway/dao/party/iface/ShopDao.java +++ b/src/main/java/com/rbkmoney/newway/dao/party/iface/ShopDao.java @@ -22,6 +22,6 @@ public interface ShopDao extends GenericDao { void saveWithUpdateCurrent(Shop shopSource, Long oldEventId, String eventName); - void switchCurrent(List ids); + void switchCurrent(List ids, String partyId); } diff --git a/src/main/java/com/rbkmoney/newway/dao/party/impl/ContractDaoImpl.java b/src/main/java/com/rbkmoney/newway/dao/party/impl/ContractDaoImpl.java index 297d099..a5ba0d3 100644 --- a/src/main/java/com/rbkmoney/newway/dao/party/impl/ContractDaoImpl.java +++ b/src/main/java/com/rbkmoney/newway/dao/party/impl/ContractDaoImpl.java @@ -15,11 +15,11 @@ import org.springframework.stereotype.Component; import javax.sql.DataSource; import java.util.List; +import java.util.Map; import java.util.Optional; import java.util.stream.Collectors; import static com.rbkmoney.newway.domain.Tables.CONTRACT; -import static com.rbkmoney.newway.domain.Tables.SHOP; @Component public class ContractDaoImpl extends AbstractGenericDao implements ContractDao { @@ -83,11 +83,12 @@ public class ContractDaoImpl extends AbstractGenericDao implements ContractDao { } @Override - public void switchCurrent(List ids) throws DaoException { + public void switchCurrent(List ids, String partyId) throws DaoException { ids.forEach(id -> - this.getNamedParameterJdbcTemplate().update("update nw.contract set current = false where id =:id and current;" + - "update nw.contract set current = true where id = (select max(id) from nw.contract where id =:id);", - new MapSqlParameterSource("id", id))); + this.getNamedParameterJdbcTemplate() + .update("update nw.contract set current = false where contract_id =:contract_id and party_id=:party_id and current;" + + "update nw.contract set current = true where id = (select max(id) from nw.contract where contract_id =:contract_id and party_id=:party_id);", + new MapSqlParameterSource(Map.of("contract_id", id, "party_id", partyId)))); } @Override diff --git a/src/main/java/com/rbkmoney/newway/dao/party/impl/ContractorDaoImpl.java b/src/main/java/com/rbkmoney/newway/dao/party/impl/ContractorDaoImpl.java index cb14591..db69888 100644 --- a/src/main/java/com/rbkmoney/newway/dao/party/impl/ContractorDaoImpl.java +++ b/src/main/java/com/rbkmoney/newway/dao/party/impl/ContractorDaoImpl.java @@ -15,11 +15,11 @@ import org.springframework.stereotype.Component; import javax.sql.DataSource; import java.util.List; +import java.util.Map; import java.util.Optional; import java.util.stream.Collectors; -import static com.rbkmoney.newway.domain.Tables.*; -import static com.rbkmoney.newway.domain.Tables.SHOP; +import static com.rbkmoney.newway.domain.Tables.CONTRACTOR; @Component public class ContractorDaoImpl extends AbstractGenericDao implements ContractorDao { @@ -82,11 +82,12 @@ public class ContractorDaoImpl extends AbstractGenericDao implements ContractorD } @Override - public void switchCurrent(List ids) throws DaoException { + public void switchCurrent(List ids, String partyId) throws DaoException { ids.forEach(id -> - this.getNamedParameterJdbcTemplate().update("update nw.contractor set current = false where id =:id and current;" + - "update nw.contractor set current = true where id = (select max(id) from nw.contractor where id =:id);", - new MapSqlParameterSource("id", id))); + this.getNamedParameterJdbcTemplate() + .update("update nw.contractor set current = false where contractor_id =:contractor_id and party_id=:party_id and current;" + + "update nw.contractor set current = true where id = (select max(id) from nw.contractor where contractor_id =:contractor_id and party_id=:party_id);", + new MapSqlParameterSource(Map.of("contractor_id", id, "party_id", partyId)))); } @Override diff --git a/src/main/java/com/rbkmoney/newway/dao/party/impl/ShopDaoImpl.java b/src/main/java/com/rbkmoney/newway/dao/party/impl/ShopDaoImpl.java index 86a6263..b649c54 100644 --- a/src/main/java/com/rbkmoney/newway/dao/party/impl/ShopDaoImpl.java +++ b/src/main/java/com/rbkmoney/newway/dao/party/impl/ShopDaoImpl.java @@ -16,6 +16,7 @@ import org.springframework.stereotype.Component; import javax.sql.DataSource; import java.util.List; +import java.util.Map; import java.util.Optional; import java.util.stream.Collectors; @@ -83,11 +84,12 @@ public class ShopDaoImpl extends AbstractGenericDao implements ShopDao { } @Override - public void switchCurrent(List ids) throws DaoException { + public void switchCurrent(List ids, String partyId) throws DaoException { ids.forEach(id -> - this.getNamedParameterJdbcTemplate().update("update nw.shop set current = false where id =:id and current;" + - "update nw.shop set current = true where id = (select max(id) from nw.shop where id =:id);", - new MapSqlParameterSource("id", id))); + this.getNamedParameterJdbcTemplate() + .update("update nw.shop set current = false where shop_id =:shop_id and party_id=:party_id and current;" + + "update nw.shop set current = true where id = (select max(id) from nw.shop where shop_id =:shop_id and party_id=:party_id);", + new MapSqlParameterSource(Map.of("shop_id", id, "party_id", partyId)))); } @Override diff --git a/src/main/java/com/rbkmoney/newway/poller/event_stock/impl/party_mngmnt/party/PartyRevisionChangedHandler.java b/src/main/java/com/rbkmoney/newway/poller/event_stock/impl/party_mngmnt/party/PartyRevisionChangedHandler.java index 260fab1..2506193 100644 --- a/src/main/java/com/rbkmoney/newway/poller/event_stock/impl/party_mngmnt/party/PartyRevisionChangedHandler.java +++ b/src/main/java/com/rbkmoney/newway/poller/event_stock/impl/party_mngmnt/party/PartyRevisionChangedHandler.java @@ -25,6 +25,7 @@ import org.springframework.transaction.annotation.Transactional; import java.util.ArrayList; import java.util.List; +import java.util.stream.Collectors; @Slf4j @Component @@ -79,17 +80,20 @@ public class PartyRevisionChangedHandler extends AbstractPartyManagementHandler private void updateShopsRevision(MachineEvent event, String partyId, long revision, Integer changeId) { List shops = shopDao.getByPartyId(partyId); - List shopIds = new ArrayList<>(); + List ids = new ArrayList<>(); shops.forEach(shopSource -> { long sequenceId = event.getEventId(); - shopIds.add(shopSource.getId()); + ids.add(shopSource.getId()); ShopUtil.resetBaseFields(event, changeId, sequenceId, shopSource); shopSource.setRevision(revision); }); log.info("Shops has been prepared for saving, eventId={}, partyId={}, count={}", event.getEventId(), partyId, shops.size()); shopDao.saveBatch(shops); - shopDao.switchCurrent(shopIds); + List shopIds = shops.stream() + .map(Shop::getShopId) + .collect(Collectors.toList()); + shopDao.switchCurrent(shopIds, partyId); log.info("Shops revisions has been saved, eventId={}, partyId={}, count={}", event.getEventId(), partyId, shops.size()); } @@ -106,7 +110,10 @@ public class PartyRevisionChangedHandler extends AbstractPartyManagementHandler log.info("Contractors has been prepared for saving, eventId={}, partyId={}, count={}", event.getEventId(), partyId, contractors.size()); contractorDao.saveBatch(contractors); - contractorDao.switchCurrent(contractorIds); + List ids = contractors.stream() + .map(Contractor::getContractorId) + .collect(Collectors.toList()); + contractorDao.switchCurrent(ids, partyId); log.info("Contractors revisions has been saved, eventId={}, partyId={}, count={}", event.getEventId(), partyId, contractors.size()); } @@ -144,7 +151,10 @@ public class PartyRevisionChangedHandler extends AbstractPartyManagementHandler } log.info("Contracts has been prepared for saving, eventId={}, partyId={}", eventId, partyId); contractDao.saveBatch(contracts); - contractDao.switchCurrent(contractIds); + List contIds = contracts.stream() + .map(Contract::getContractId) + .collect(Collectors.toList()); + contractDao.switchCurrent(contIds, partyId); log.info("Contracts has been saved, eventId={}, partyId={}, count={}", eventId, partyId, contracts.size()); contractAdjustmentDao.save(allAdjustments);