diff --git a/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/business/controller/admin/shippingInvoice/InvoiceController.java b/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/business/controller/admin/shippingInvoice/InvoiceController.java index ad3ef5dcb..108eafd12 100644 --- a/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/business/controller/admin/shippingInvoice/InvoiceController.java +++ b/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/business/controller/admin/shippingInvoice/InvoiceController.java @@ -29,6 +29,7 @@ import org.jeecg.modules.quartz.service.IQuartzJobService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.core.env.Environment; +import org.springframework.dao.CannotAcquireLockException; import org.springframework.format.annotation.DateTimeFormat; import org.springframework.transaction.annotation.Transactional; import org.springframework.ui.freemarker.FreeMarkerTemplateUtils; @@ -269,7 +270,7 @@ public class InvoiceController { } catch (IOException | ParseException e) { log.error(e.getMessage()); return Result.error("Sorry, server error, please try later"); - } catch (MessagingException e) { + } catch (MessagingException | InterruptedException e) { throw new RuntimeException(e); } } @@ -438,7 +439,7 @@ public class InvoiceController { } catch (IOException | ParseException e) { log.error(e.getMessage()); return Result.error("Sorry, server error, please try later"); - } catch (MessagingException | TemplateException e) { + } catch (MessagingException | TemplateException | InterruptedException e) { throw new RuntimeException(e); } } @@ -1356,4 +1357,24 @@ public class InvoiceController { throw new RuntimeException(e); } } + @PostMapping("/editOrderTest") + public Result editOrderTest(@RequestBody Map params) { + System.out.println("Edit Order Test : " + params.get("orderId")); + String orderId = params.get("orderId"); + try { + PlatformOrder orderToEdit = platformOrderService.selectForUpdateSkipLock(orderId); + + if(orderToEdit == null) { + return Result.error(404,"Order not found."); + } + orderToEdit.setCanSend("2"); + boolean edited = platformOrderService.updateById(orderToEdit); + return Result.OK(edited); + } catch (Exception e) { + if(e instanceof CannotAcquireLockException) { + return Result.error(409,"Order is already being edited by another user."); + } + throw new RuntimeException(e); + } + } } diff --git a/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/business/domain/shippingInvoice/ShippingInvoiceFactory.java b/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/business/domain/shippingInvoice/ShippingInvoiceFactory.java index cfb07dd3e..1bc12723e 100644 --- a/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/business/domain/shippingInvoice/ShippingInvoiceFactory.java +++ b/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/business/domain/shippingInvoice/ShippingInvoiceFactory.java @@ -123,7 +123,7 @@ public class ShippingInvoiceFactory { public ShippingInvoice createShippingInvoice(String customerId, List orderIds, String type, String start, String end) throws UserException { log.info("Creating an invoice with arguments:\n client ID: {}, order IDs: {}]", customerId, orderIds); // find orders and their contents of the invoice - Map> uninvoicedOrderToContent = platformOrderService.fetchOrderData(orderIds); + Map> uninvoicedOrderToContent = platformOrderService.fetchOrderDataForUpdate(orderIds); Set platformOrders = uninvoicedOrderToContent.keySet(); List savRefunds = savRefundWithDetailService.findUnprocessedRefundsByClient(customerId); List shopIds = platformOrders.stream() @@ -166,10 +166,10 @@ public class ShippingInvoiceFactory { * channel price, this exception will be thrown. */ @Transactional - public CompleteInvoice createCompleteShippingInvoice(String username, String customerId, BigDecimal balance, List orderIds, String shippingMethod, String start, String end) throws UserException, MessagingException { + public CompleteInvoice createCompleteShippingInvoice(String username, String customerId, BigDecimal balance, List orderIds, String shippingMethod, String start, String end) throws UserException, MessagingException, InterruptedException { log.info("Creating a complete invoice for \n client ID: {}, order IDs: {}]", customerId, orderIds); // find orders and their contents of the invoice - Map> uninvoicedOrderToContent = platformOrderService.fetchOrderData(orderIds); + Map> uninvoicedOrderToContent = platformOrderService.fetchOrderDataForUpdate(orderIds); Set platformOrders = uninvoicedOrderToContent.keySet(); List savRefunds = savRefundWithDetailService.findUnprocessedRefundsByClient(customerId); List shopIds = platformOrders.stream() diff --git a/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/business/mapper/PlatformOrderContentMapper.java b/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/business/mapper/PlatformOrderContentMapper.java index a87d0113f..82a1714f3 100644 --- a/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/business/mapper/PlatformOrderContentMapper.java +++ b/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/business/mapper/PlatformOrderContentMapper.java @@ -46,7 +46,9 @@ public interface PlatformOrderContentMapper extends BaseMapper fetchOrderContent(List orderIds); + List fetchOrderContentForUpdate(List orderIds); List searchSkuDetail(List skuIDs); @@ -84,4 +86,5 @@ public interface PlatformOrderContentMapper extends BaseMapper searchShoumanOrderContent(); List searchShoumanOrderContentByPlatformOrderId(@Param("platformOrderId") String platformOrderId); + } diff --git a/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/business/mapper/PlatformOrderMapper.java b/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/business/mapper/PlatformOrderMapper.java index 9be8a94ae..a55d785e7 100644 --- a/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/business/mapper/PlatformOrderMapper.java +++ b/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/business/mapper/PlatformOrderMapper.java @@ -1,6 +1,7 @@ package org.jeecg.modules.business.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import org.apache.ibatis.annotations.MapKey; import org.apache.ibatis.annotations.Param; import org.jeecg.modules.business.domain.api.mabang.getorderlist.Order; import org.jeecg.modules.business.domain.api.yd.YDTrackingNumberData; @@ -171,12 +172,12 @@ public interface PlatformOrderMapper extends BaseMapper { List fetchOrderInShopsWithoutShopifyNote(@Param("shops") List shopCodes); List fetchOrderInShopsReadyForAbnNumberJob(@Param("shops") List shopCodes); - List fetchUninvoicedShippedOrderIDInShops(@Param("startDate") String startDate, + List fetchUninvoicedShippedOrderIDInShops(@Param("startDate") String startDate, @Param("endDate") String endDate, @Param("shops") List shops, @Param("warehouses") List warehouses); - List fetchUninvoicedOrderIDInShopsAndOrderTime(@Param("startDate") String startDate, + List fetchUninvoicedOrderIDInShopsAndOrderTime(@Param("startDate") String startDate, @Param("endDate") String endDate, @Param("shops") List shops, @Param("erpStatuses") List erpStatuses, @@ -255,4 +256,8 @@ public interface PlatformOrderMapper extends BaseMapper { @Param("start") String startDate, @Param("end") String endDate, @Param("order") String order, @Param("column") String column, @Param("offset") Integer offset, @Param("size") Integer pageSize); + @MapKey("id") + Map selectBatchIdsForUpdate(@Param("ids") List orderIds); + + PlatformOrder selectForUpdateSkipLock(@Param("id") String orderId); } diff --git a/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/business/mapper/xml/PlatformOrderContentMapper.xml b/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/business/mapper/xml/PlatformOrderContentMapper.xml index 6e4f96b9c..969f1497b 100644 --- a/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/business/mapper/xml/PlatformOrderContentMapper.xml +++ b/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/business/mapper/xml/PlatformOrderContentMapper.xml @@ -76,6 +76,14 @@ WHERE erp_status <> 5 AND platform_order_id IN #{item} + - SELECT po.id FROM platform_order po JOIN logistic_channel lc ON po.logistic_channel_name = lc.zh_name @@ -538,7 +540,7 @@ AND po.shipping_time between #{startDate} AND #{endDate} AND po.erp_status = 3; - SELECT po.id FROM platform_order po JOIN logistic_channel lc ON po.logistic_channel_name = lc.zh_name @@ -576,7 +578,8 @@ item="erpStatus" > #{erpStatus} - ; + + FOR UPDATE; + + diff --git a/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/business/service/IPlatformOrderService.java b/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/business/service/IPlatformOrderService.java index 8273ce7cf..49a80bc45 100644 --- a/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/business/service/IPlatformOrderService.java +++ b/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/business/service/IPlatformOrderService.java @@ -108,6 +108,8 @@ public interface IPlatformOrderService extends IService { */ Map> fetchOrderData(List orderIds); + Map> fetchOrderDataForUpdate(List orderIds); + Map> fetchOrderDataByInvoiceCode(String invoiceCode); @@ -150,8 +152,6 @@ public interface IPlatformOrderService extends IService { List fetchOrderInShopsReadyForAbnNumberJob(List shopCodes); - List fetchUninvoicedShippedOrderIDInShops(String startDate, String endDate, List shops, List warehouses); - /** * Fetch all platform orders between 2 dates and of status erp_status 4 or 5 * this list will then be archived @@ -293,4 +293,6 @@ public interface IPlatformOrderService extends IService { List listByClientAndShops(String clientId, List shopIds, String startDate, String endDate, String invoicingMethod, Integer pageNo, Integer pageSize, List warehouses, String order, String column); int countListByClientAndShops(String clientId, List shopIDs, String start, String end, String invoicingMethod, List warehouses); + + PlatformOrder selectForUpdateSkipLock(String orderId); } diff --git a/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/business/service/PlatformOrderShippingInvoiceService.java b/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/business/service/PlatformOrderShippingInvoiceService.java index 00eb5ad39..a61c2c8d8 100644 --- a/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/business/service/PlatformOrderShippingInvoiceService.java +++ b/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/business/service/PlatformOrderShippingInvoiceService.java @@ -207,8 +207,7 @@ public class PlatformOrderShippingInvoiceService { return new Period(beginZoned, endZoned, ""); } public List getShippingOrderIdBetweenDate(List shops, String start, String end, List wareHouses) { - List orders = platformOrderMapper.fetchUninvoicedShippedOrderIDInShops( start, end, shops, wareHouses); - return orders.stream().map(PlatformOrder::getId).collect(Collectors.toList()); + return platformOrderMapper.fetchUninvoicedShippedOrderIDInShops( start, end, shops, wareHouses); } /** * Make an invoice based on parameters. @@ -265,7 +264,7 @@ public class PlatformOrderShippingInvoiceService { * @throws IOException exception related to invoice file IO. */ @Transactional - public InvoiceMetaData makeCompleteInvoice(ShippingInvoiceOrderParam param) throws UserException, ParseException, IOException, MessagingException { + public InvoiceMetaData makeCompleteInvoice(ShippingInvoiceOrderParam param) throws UserException, ParseException, IOException, MessagingException, InterruptedException { String username = ((LoginUser) SecurityUtils.getSubject().getPrincipal()).getUsername(); // Creates invoice by factory CompleteInvoice invoice = factory.createCompleteShippingInvoice(username, param.clientID(), null, param.orderIds(), param.getType(), param.getStart(), param.getEnd()); @@ -282,18 +281,16 @@ public class PlatformOrderShippingInvoiceService { * @throws IOException */ @Transactional - public InvoiceMetaData makeCompleteInvoicePostShipping(ShippingInvoiceParam param, String method, String ... user) throws UserException, ParseException, IOException, MessagingException { + public InvoiceMetaData makeCompleteInvoicePostShipping(ShippingInvoiceParam param, String method, String ... user) throws UserException, ParseException, IOException, MessagingException, InterruptedException { String username = user.length > 0 ? user[0] : ((LoginUser) SecurityUtils.getSubject().getPrincipal()).getUsername(); - List platformOrderList; + List orderIds; if(method.equals(POSTSHIPPING.getMethod())) { //On récupère les commandes entre 2 dates d'expédition avec un status 3 - platformOrderList = platformOrderMapper.fetchUninvoicedShippedOrderIDInShops(param.getStart(), param.getEnd(), param.shopIDs(), param.getWarehouses()); + orderIds = platformOrderMapper.fetchUninvoicedShippedOrderIDInShops(param.getStart(), param.getEnd(), param.shopIDs(), param.getWarehouses()); } else { // On récupère les commandes entre 2 dates de commandes avec un status (1,2) ou (1,2,3) - platformOrderList = platformOrderMapper.fetchUninvoicedOrderIDInShopsAndOrderTime(param.getStart(), param.getEnd(), param.shopIDs(), param.getErpStatuses(), param.getWarehouses()); + orderIds = platformOrderMapper.fetchUninvoicedOrderIDInShopsAndOrderTime(param.getStart(), param.getEnd(), param.shopIDs(), param.getErpStatuses(), param.getWarehouses()); } - // on récupère seulement les ID des commandes - List orderIds = platformOrderList.stream().map(PlatformOrder::getId).collect(Collectors.toList()); // Creates invoice by factory CompleteInvoice invoice = factory.createCompleteShippingInvoice(username, param.clientID(), param.getBalance() ,orderIds, method, param.getStart(), param.getEnd()); return getInvoiceMetaDataAndInsert(username, invoice); @@ -711,6 +708,8 @@ public class PlatformOrderShippingInvoiceService { String internalCode = entry.getKey().getClient().getInternalCode(); invoiceList.add(new InvoiceMetaData("", "error", internalCode, clientId, e.getMessage())); log.error(e.getMessage()); + } catch (InterruptedException e) { + throw new RuntimeException(e); } System.gc(); } diff --git a/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/business/service/impl/PlatformOrderMabangServiceImpl.java b/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/business/service/impl/PlatformOrderMabangServiceImpl.java index 1831da244..61845e741 100644 --- a/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/business/service/impl/PlatformOrderMabangServiceImpl.java +++ b/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/business/service/impl/PlatformOrderMabangServiceImpl.java @@ -155,11 +155,13 @@ public class PlatformOrderMabangServiceImpl extends ServiceImpl> findUninvoicedOrders(List shopIds, Date begin, Date end, List warehouses) { List orderList = platformOrderMap.findUninvoicedOrders(shopIds, begin, end, warehouses); - List orderContents = platformOrderContentMap.fetchOrderContent(orderList.stream().map(PlatformOrder::getId).collect(toList())); + List orderContents = platformOrderContentMap.fetchOrderContentForUpdate(orderList.stream().map(PlatformOrder::getId).collect(toList())); Map orderMap = orderList.stream().collect(toMap(PlatformOrder::getId, Function.identity())); return orderContents.stream().collect(groupingBy(platformOrderContent -> orderMap.get(platformOrderContent.getPlatformOrderId()))); } @@ -315,6 +313,12 @@ public class PlatformOrderServiceImpl extends ServiceImpl orderMap.get(platformOrderContent.getPlatformOrderId()))); } @Override + public Map> fetchOrderDataForUpdate(List orderIds) { + Map ordersMapById = platformOrderMap.selectBatchIdsForUpdate(orderIds); + List orderContents = platformOrderContentMap.fetchOrderContentForUpdate(orderIds); + return orderContents.stream().collect(groupingBy(platformOrderContent -> ordersMapById.get(platformOrderContent.getPlatformOrderId()))); + } + @Override public Map> fetchOrderDataByInvoiceCode(String invoiceCode) { List orderList = platformOrderMap.getPlatformOrdersByInvoiceNumber(invoiceCode); List orderIds = orderList.stream().map(PlatformOrder::getId).collect(toList()); @@ -394,10 +398,6 @@ public class PlatformOrderServiceImpl extends ServiceImpl fetchUninvoicedShippedOrderIDInShops(String startDate, String endDate, List shops, List warehouses) { - return platformOrderMap.fetchUninvoicedShippedOrderIDInShops(startDate, endDate, shops, warehouses); - } @Override public List fetchOrdersToArchiveBetweenDate(String startDate, String endDate) { return platformOrderMap.fetchOrdersToArchiveBetweenDate(startDate, endDate); @@ -600,4 +600,9 @@ public class PlatformOrderServiceImpl extends ServiceImpl