修改客户订单-- 修改后的产品在原来中不存在

pull/451/head
starrysky 2019-08-17 14:21:24 +08:00
parent f809070e53
commit 0abc318c3a
3 changed files with 48 additions and 1 deletions

View File

@ -3,6 +3,8 @@ package me.zhengjie.modules.wms.order.repository;
import me.zhengjie.modules.wms.order.domain.CustomerOrderProduct;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import java.util.List;
@ -12,5 +14,10 @@ import java.util.List;
*/
public interface CustomerOrderProductRepository extends JpaRepository<CustomerOrderProduct, Long>, JpaSpecificationExecutor {
List<CustomerOrderProduct> findByCustomerOrderIdAndStatusTrue(Long customerId);
List<CustomerOrderProduct> findByCustomerOrderIdAndStatusTrue(Long customerOrderId);
@Modifying
@Query(value = "delete s_customer_order_product where product_code = ?1 and customer_order = ?2", nativeQuery = true)
void deleteByProductCodeAndCustomerOrderId(String productCode, Long customerOrderId);
}

View File

@ -1,12 +1,15 @@
package me.zhengjie.modules.wms.order.service.dto;
import lombok.Data;
import java.sql.Timestamp;
/**
* @author
* @date 2019-08-17
*/
@Data
public class CustomerOrderProductDTO {
private Long id;

View File

@ -23,7 +23,11 @@ import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
@ -110,13 +114,34 @@ public class CustomerOrderServiceImpl implements CustomerOrderService {
CustomerOrder customerOrder = new CustomerOrder();
BeanUtils.copyProperties(updateCustomerOrderRequest, customerOrder);
// 修改客户订单概要信息
customerOrderRepository.save(customerOrder);
// 修改产品信息之前查询该订单中原来的产品信息key为产品code
List<CustomerOrderProduct> customerOrderProductListBeforeUpdate = customerOrderProductRepository.findByCustomerOrderIdAndStatusTrue(customerOrder.getId());
Map<String, CustomerOrderProduct> customerOrderProductMapBefore = customerOrderProductListBeforeUpdate.stream().collect(Collectors.toMap(CustomerOrderProduct::getProductCode, Function.identity()));
List<CustomerOrderProductDTO> customerOrderProductRequestList = updateCustomerOrderRequest.getCustomerOrderProductList();
if(CollectionUtils.isEmpty(customerOrderProductRequestList)){
throw new BadRequestException("订单产品不能为空!");
}
Map<String, CustomerOrderProductDTO> customerOrderProductMapAfter = customerOrderProductRequestList.stream().collect(Collectors.toMap(CustomerOrderProductDTO::getProductCode, Function.identity()));
//需要将订单中原来订单对应的产品删除了的数据
List<String> deleteTargetList = new ArrayList<>();
//比较量个map中key不一样的数据
for(Map.Entry<String, CustomerOrderProduct> entry:customerOrderProductMapBefore.entrySet()){
String productCode = entry.getKey();
//修改后的map记录对应的key在原来中是否存在
CustomerOrderProductDTO customerOrderProductDTOTemp = customerOrderProductMapAfter.get(productCode);
if(null == customerOrderProductDTOTemp){
deleteTargetList.add(entry.getKey());
}
}
List<CustomerOrderProduct> customerOrderProductList = new ArrayList<>();
for(CustomerOrderProductDTO customerOrderProductDTO : customerOrderProductRequestList){
CustomerOrderProduct customerOrderProduct = new CustomerOrderProduct();
@ -125,6 +150,18 @@ public class CustomerOrderServiceImpl implements CustomerOrderService {
customerOrderProduct.setStatus(true);
}
customerOrderProductRepository.saveAll(customerOrderProductList);
/**
* :
* 1. a b c
* 2. a c
* 3.
*/
if(!CollectionUtils.isEmpty(deleteTargetList)){
for(String prductCode : deleteTargetList){
customerOrderProductRepository.deleteByProductCodeAndCustomerOrderId(prductCode, customerOrder.getId());
}
}
}
@Override