Fix for father

This commit is contained in:
k.struzhkin 2020-04-15 16:58:31 +03:00
parent a18bb0dd5e
commit 145b286a2f
7 changed files with 37 additions and 23 deletions

View File

@ -18,7 +18,7 @@ public interface ContractDao extends GenericDao {
void updateNotCurrent(List<Long> ids) throws DaoException;
void switchCurrent(List<Long> ids) throws DaoException;
void switchCurrent(List<String> ids, String partyId) throws DaoException;
List<Contract> getByPartyId(String partyId);
}

View File

@ -18,7 +18,7 @@ public interface ContractorDao extends GenericDao {
void updateNotCurrent(List<Long> ids) throws DaoException;
void switchCurrent(List<Long> ids) throws DaoException;
void switchCurrent(List<String> ids, String partyId) throws DaoException;
List<Contractor> getByPartyId(String partyId);
}

View File

@ -22,6 +22,6 @@ public interface ShopDao extends GenericDao {
void saveWithUpdateCurrent(Shop shopSource, Long oldEventId, String eventName);
void switchCurrent(List<Long> ids);
void switchCurrent(List<String> ids, String partyId);
}

View File

@ -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<Long> ids) throws DaoException {
public void switchCurrent(List<String> 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

View File

@ -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<Long> ids) throws DaoException {
public void switchCurrent(List<String> 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

View File

@ -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<Long> ids) throws DaoException {
public void switchCurrent(List<String> 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

View File

@ -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<Shop> shops = shopDao.getByPartyId(partyId);
List<Long> shopIds = new ArrayList<>();
List<Long> 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<String> 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<String> 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<String> 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);