Commit c20ad837 authored by Xenia Ulyanova's avatar Xenia Ulyanova
Browse files

WaybillSyncService major update. Now waybill number attribute is considered ...

WaybillSyncService major update. Now waybill number attribute is considered  to be unique, not externalId
parent 26a40785
Showing with 49 additions and 6 deletions
+49 -6
......@@ -28,6 +28,14 @@ public interface WaybillDAO extends GenericDAO<Waybill> {
*/
List<Waybill> findByNumber(String number);
/**
* Находит самую последнюю накладную по номеру.
* Самая последняя - это накладная с максимальным полем {@link Waybill#externalId}
* @param number
* @return
*/
Waybill findLatestByNumber(String number);
/**
* Находит накладные по номер ИМ отправления
*
......
......@@ -42,6 +42,11 @@ public class WaybillDAOImpl extends GenericDAOImpl<Waybill> implements WaybillDA
return getMapper().findByNumber(number);
}
@Override
public Waybill findLatestByNumber(String number) {
return getMapper().findLatestByNumber(number);
}
@Override
public List<Waybill> findByNumberIn(String numberIn) {
return waybillMapper.findByNumberIn(numberIn);
......
......@@ -16,6 +16,8 @@ public interface WaybillMapper extends GenericMapper<Waybill> {
List<Waybill> findByNumber(String number);
Waybill findLatestByNumber(String number);
List<Waybill> findByNumberIn(String numberIn);
List<WaybillAndItemRawSearch> findRawByNumberOrNumberIn(@Param("number") String number,
......
......@@ -63,6 +63,10 @@
SELECT * FROM waybill WHERE number=#{number}
</select>
<select id="findLatestByNumber" resultMap="waybillResultMap">
SELECT * FROM waybill WHERE number=#{number} ORDER BY external_id DESC LIMIT 1;
</select>
<select id="findByNumberIn" resultMap="waybillResultMap">
SELECT * FROM waybill WHERE number_in=#{numberIn}
</select>
......
......@@ -14,6 +14,8 @@ import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
import static org.junit.Assert.assertEquals;
/**
* Using the random-beans lib for easy objects population
* https://github.com/benas/random-beans/wiki
......@@ -130,8 +132,29 @@ public class WaybillDAOTest extends BaseDAOTest {
n.equals(waybillNotSyncedButWillBe.getNumber()) ||
n.equals(waybillSynced.getNumber()))
.collect(Collectors.toList());
Assert.assertEquals(1, numbers.size());
Assert.assertEquals(waybillNotSyncedButWillBe.getNumber(), numbers.get(0));
assertEquals(1, numbers.size());
assertEquals(waybillNotSyncedButWillBe.getNumber(), numbers.get(0));
}
@Test
public void testFindLatestByNumber(){
Waybill waybillOne = getRandomWaybill();
Waybill waybillTwo = getRandomWaybill();
Waybill waybillThree = getRandomWaybill();
String sameNumber = waybillOne.getNumber();
waybillTwo.setNumber(sameNumber);
waybillTwo.setExternalId(waybillOne.getExternalId()+1L);
waybillThree.setExternalId(waybillOne.getExternalId()-1L);
waybillThree.setNumber(sameNumber);
waybillDAO.create(waybillOne);
waybillDAO.create(waybillTwo);
waybillDAO.create(waybillThree);
List<Waybill> waybills = waybillDAO.findByNumber(waybillOne.getNumber());
assertEquals(3, waybills.size());
Waybill latestWaybill = waybillDAO.findLatestByNumber(waybillOne.getNumber());
assertEquals(waybillTwo.getUuid(), latestWaybill.getUuid());
}
private Waybill getRandomWaybill() {
......
......@@ -17,6 +17,7 @@ import com.cdek.warehouse.ek4integration.sync.WaybillSyncService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.ListUtils;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;
import org.slf4j.Logger;
......@@ -466,8 +467,8 @@ public class WaybillSyncServiceImpl implements WaybillSyncService {
* @return записанная накладная или null если синхронизация не требуется
*/
public Waybill createWaybill(JSOrderSync order) {
Waybill found = waybillDAO.findByExternalId(order.code);
// игонрируем пустую накладную, если её получили впервые
Waybill found = waybillDAO.findLatestByNumber(ObjectUtils.toString(order.orderNumber, ""));
// игнорируем пустую накладную, если её получили впервые
if (found == null && (order.cargo == null || order.cargo.size() == 0)) {
LOGGER.debug("Ignoring empty waybill: " + order.orderNumber + ".");
return null;
......
......@@ -212,7 +212,7 @@ public class WaybillSyncServiceTest {
Waybill waybill = new Waybill();
waybill.setDateUpdated(new Date(0));
when(waybillDAO.findByExternalId(anyLong())).thenReturn(waybill);
when(waybillDAO.findLatestByNumber(anyString())).thenReturn(waybill);
WaybillItem item = new WaybillItem();
item.setDateUpdated(new Date(0));
......@@ -223,7 +223,7 @@ public class WaybillSyncServiceTest {
waybillSyncServiceImpl.syncByTariffs(null, null, null, null);
verify(waybillDAO, times(1)).findByExternalId(18879038L);
verify(waybillDAO, times(1)).findLatestByNumber("1022898254");
verify(waybillItemDAO, times(1)).findByExternalId(23610067L);
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment