|
|
@@ -0,0 +1,251 @@
|
|
|
+package org.dromara.order.service.impl;
|
|
|
+
|
|
|
+import cn.hutool.core.collection.CollUtil;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import lombok.RequiredArgsConstructor;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.dromara.common.core.utils.MapstructUtils;
|
|
|
+import org.dromara.common.core.utils.StringUtils;
|
|
|
+import org.dromara.common.mybatis.core.page.PageQuery;
|
|
|
+import org.dromara.common.mybatis.core.page.TableDataInfo;
|
|
|
+import org.dromara.common.redis.utils.SequenceUtils;
|
|
|
+import org.dromara.order.domain.OrderMain;
|
|
|
+import org.dromara.order.domain.OrderProduct;
|
|
|
+import org.dromara.order.domain.bo.OrderMainBo;
|
|
|
+import org.dromara.order.domain.bo.OrderProductBo;
|
|
|
+import org.dromara.order.domain.vo.OrderMainVo;
|
|
|
+import org.dromara.order.domain.vo.OrderProductVo;
|
|
|
+import org.dromara.order.mapper.OrderMainMapper;
|
|
|
+import org.dromara.order.mapper.OrderProductMapper;
|
|
|
+import org.dromara.order.service.IOrderMainService;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+
|
|
|
+import java.util.Collection;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 订单主信息Service业务层处理
|
|
|
+ *
|
|
|
+ * @author LionLi
|
|
|
+ * @date 2025-12-26
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@RequiredArgsConstructor
|
|
|
+@Service
|
|
|
+public class OrderMainServiceImpl extends ServiceImpl<OrderMainMapper, OrderMain> implements IOrderMainService {
|
|
|
+
|
|
|
+ private final OrderMainMapper baseMapper;
|
|
|
+
|
|
|
+ private final OrderProductMapper orderProductMapper;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 查询订单主信息
|
|
|
+ *
|
|
|
+ * @param id 主键
|
|
|
+ * @return 订单主信息
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public OrderMainVo queryById(Long id) {
|
|
|
+ // 1. 查询主订单
|
|
|
+ OrderMainVo orderMainVo = baseMapper.selectVoById(id);
|
|
|
+ if (orderMainVo == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 2. 查询关联的商品列表
|
|
|
+ List<OrderProductVo> orderProductVoList = orderProductMapper.selectVoList(
|
|
|
+ new LambdaQueryWrapper<OrderProduct>().eq(OrderProduct::getOrderId, orderMainVo.getId())
|
|
|
+ );
|
|
|
+
|
|
|
+ // 3. 组装数据
|
|
|
+ orderMainVo.setOrderProductList(orderProductVoList);
|
|
|
+
|
|
|
+ // 4. 返回已组装好的对象
|
|
|
+ return orderMainVo;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 分页查询订单主信息列表
|
|
|
+ *
|
|
|
+ * @param bo 查询条件
|
|
|
+ * @param pageQuery 分页参数
|
|
|
+ * @return 订单主信息分页列表
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public TableDataInfo<OrderMainVo> queryPageList(OrderMainBo bo, PageQuery pageQuery) {
|
|
|
+ LambdaQueryWrapper<OrderMain> lqw = buildQueryWrapper(bo);
|
|
|
+ Page<OrderMainVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
|
|
|
+ return TableDataInfo.build(result);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 查询符合条件的订单主信息列表
|
|
|
+ *
|
|
|
+ * @param bo 查询条件
|
|
|
+ * @return 订单主信息列表
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public List<OrderMainVo> queryList(OrderMainBo bo) {
|
|
|
+ LambdaQueryWrapper<OrderMain> lqw = buildQueryWrapper(bo);
|
|
|
+ return baseMapper.selectVoList(lqw);
|
|
|
+ }
|
|
|
+
|
|
|
+ private LambdaQueryWrapper<OrderMain> buildQueryWrapper(OrderMainBo bo) {
|
|
|
+ Map<String, Object> params = bo.getParams();
|
|
|
+ LambdaQueryWrapper<OrderMain> lqw = Wrappers.lambdaQuery();
|
|
|
+ lqw.orderByAsc(OrderMain::getId);
|
|
|
+ lqw.eq(StringUtils.isNotBlank(bo.getOrderNo()), OrderMain::getOrderNo, bo.getOrderNo());
|
|
|
+ lqw.eq(StringUtils.isNotBlank(bo.getShipmentNo()), OrderMain::getShipmentNo, bo.getShipmentNo());
|
|
|
+ lqw.eq(StringUtils.isNotBlank(bo.getSubOrderNo()), OrderMain::getSubOrderNo, bo.getSubOrderNo());
|
|
|
+ lqw.eq(bo.getCompanyId() != null, OrderMain::getCompanyId, bo.getCompanyId());
|
|
|
+ lqw.eq(bo.getCustomerId() != null, OrderMain::getCustomerId, bo.getCustomerId());
|
|
|
+ lqw.eq(bo.getUserId() != null, OrderMain::getUserId, bo.getUserId());
|
|
|
+ lqw.eq(bo.getShippingAddressId() != null, OrderMain::getShippingAddressId, bo.getShippingAddressId());
|
|
|
+ lqw.eq(StringUtils.isNotBlank(bo.getPurchaseReason()), OrderMain::getPurchaseReason, bo.getPurchaseReason());
|
|
|
+ lqw.eq(StringUtils.isNotBlank(bo.getInvoiceType()), OrderMain::getInvoiceType, bo.getInvoiceType());
|
|
|
+ lqw.eq(StringUtils.isNotBlank(bo.getPayType()), OrderMain::getPayType, bo.getPayType());
|
|
|
+ lqw.eq(bo.getWarehouseId() != null, OrderMain::getWarehouseId, bo.getWarehouseId());
|
|
|
+ lqw.eq(bo.getCreditLimit() != null, OrderMain::getCreditLimit, bo.getCreditLimit());
|
|
|
+ lqw.eq(bo.getExpectedDeliveryTime() != null, OrderMain::getExpectedDeliveryTime, bo.getExpectedDeliveryTime());
|
|
|
+ lqw.eq(StringUtils.isNotBlank(bo.getBusinessStaff()), OrderMain::getBusinessStaff, bo.getBusinessStaff());
|
|
|
+ lqw.eq(StringUtils.isNotBlank(bo.getCustomerService()), OrderMain::getCustomerService, bo.getCustomerService());
|
|
|
+ lqw.eq(StringUtils.isNotBlank(bo.getBusinessDept()), OrderMain::getBusinessDept, bo.getBusinessDept());
|
|
|
+ lqw.eq(StringUtils.isNotBlank(bo.getUserDept()), OrderMain::getUserDept, bo.getUserDept());
|
|
|
+ lqw.eq(bo.getProductQuantity() != null, OrderMain::getProductQuantity, bo.getProductQuantity());
|
|
|
+ lqw.eq(bo.getShippingFee() != null, OrderMain::getShippingFee, bo.getShippingFee());
|
|
|
+ lqw.eq(bo.getTotalAmount() != null, OrderMain::getTotalAmount, bo.getTotalAmount());
|
|
|
+ lqw.eq(bo.getPayableAmount() != null, OrderMain::getPayableAmount, bo.getPayableAmount());
|
|
|
+ lqw.eq(StringUtils.isNotBlank(bo.getPaymentStatus()), OrderMain::getPaymentStatus, bo.getPaymentStatus());
|
|
|
+ lqw.eq(StringUtils.isNotBlank(bo.getOrderSource()), OrderMain::getOrderSource, bo.getOrderSource());
|
|
|
+ lqw.eq(StringUtils.isNotBlank(bo.getOrderStatus()), OrderMain::getOrderStatus, bo.getOrderStatus());
|
|
|
+ lqw.eq(bo.getOrderTime() != null, OrderMain::getOrderTime, bo.getOrderTime());
|
|
|
+ lqw.eq(bo.getConfirmTime() != null, OrderMain::getConfirmTime, bo.getConfirmTime());
|
|
|
+ lqw.eq(bo.getShippingTime() != null, OrderMain::getShippingTime, bo.getShippingTime());
|
|
|
+ lqw.eq(bo.getReceivingTime() != null, OrderMain::getReceivingTime, bo.getReceivingTime());
|
|
|
+ lqw.eq(bo.getShippedQuantity() != null, OrderMain::getShippedQuantity, bo.getShippedQuantity());
|
|
|
+ lqw.eq(bo.getUnshippedQuantity() != null, OrderMain::getUnshippedQuantity, bo.getUnshippedQuantity());
|
|
|
+ lqw.eq(bo.getPackageCount() != null, OrderMain::getPackageCount, bo.getPackageCount());
|
|
|
+ lqw.eq(bo.getSignedQuantity() != null, OrderMain::getSignedQuantity, bo.getSignedQuantity());
|
|
|
+ lqw.eq(bo.getAfterSaleCompleted() != null, OrderMain::getAfterSaleCompleted, bo.getAfterSaleCompleted());
|
|
|
+ lqw.eq(bo.getAfterSalePending() != null, OrderMain::getAfterSalePending, bo.getAfterSalePending());
|
|
|
+ lqw.eq(StringUtils.isNotBlank(bo.getDeliveryDesc()), OrderMain::getDeliveryDesc, bo.getDeliveryDesc());
|
|
|
+ lqw.eq(StringUtils.isNotBlank(bo.getPushStatus()), OrderMain::getPushStatus, bo.getPushStatus());
|
|
|
+ lqw.eq(StringUtils.isNotBlank(bo.getAttachmentPath()), OrderMain::getAttachmentPath, bo.getAttachmentPath());
|
|
|
+ lqw.eq(StringUtils.isNotBlank(bo.getDeliveryType()), OrderMain::getDeliveryType, bo.getDeliveryType());
|
|
|
+ lqw.eq(StringUtils.isNotBlank(bo.getOrderCategory()), OrderMain::getOrderCategory, bo.getOrderCategory());
|
|
|
+ lqw.eq(StringUtils.isNotBlank(bo.getProductCode()), OrderMain::getProductCode, bo.getProductCode());
|
|
|
+ lqw.eq(StringUtils.isNotBlank(bo.getCancelReason()), OrderMain::getCancelReason, bo.getCancelReason());
|
|
|
+ lqw.eq(StringUtils.isNotBlank(bo.getExpenseType()), OrderMain::getExpenseType, bo.getExpenseType());
|
|
|
+ lqw.eq(StringUtils.isNotBlank(bo.getCustomerNo()), OrderMain::getCustomerNo, bo.getCustomerNo());
|
|
|
+ lqw.eq(StringUtils.isNotBlank(bo.getStatus()), OrderMain::getStatus, bo.getStatus());
|
|
|
+ return lqw;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 新增订单主信息
|
|
|
+ *
|
|
|
+ * @param bo 订单主信息
|
|
|
+ * @return 是否新增成功
|
|
|
+ */
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ @Override
|
|
|
+ public Boolean insertByBo(OrderMainBo bo) {
|
|
|
+ // 1. 校验商品列表
|
|
|
+ List<OrderProductBo> orderProductBos = bo.getOrderProductBos();
|
|
|
+ if (CollUtil.isEmpty(orderProductBos)) {
|
|
|
+ throw new IllegalArgumentException("订单商品列表不能为空");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 可选:校验每个商品的数量、价格等
|
|
|
+ for (OrderProductBo productBo : orderProductBos) {
|
|
|
+ if (productBo.getOrderQuantity() == null || productBo.getOrderQuantity() <= 0) {
|
|
|
+ throw new IllegalArgumentException("商品数量必须大于0");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 2. 生成订单号并转换主单
|
|
|
+ String orderNo = SequenceUtils.generateOrderCode("RS");
|
|
|
+ bo.setOrderNo(orderNo);
|
|
|
+ OrderMain orderMain = MapstructUtils.convert(bo, OrderMain.class);
|
|
|
+
|
|
|
+ validEntityBeforeSave(orderMain);
|
|
|
+
|
|
|
+ // 3. 插入主订单
|
|
|
+ boolean mainSaved = baseMapper.insert(orderMain) > 0;
|
|
|
+ if (!mainSaved) {
|
|
|
+ throw new RuntimeException("主订单保存失败");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 4. 转换并填充子订单商品
|
|
|
+ Long orderId = orderMain.getId();
|
|
|
+ List<OrderProduct> orderProducts = orderProductBos.stream()
|
|
|
+ .map(productBo -> {
|
|
|
+ OrderProduct product = MapstructUtils.convert(productBo, OrderProduct.class);
|
|
|
+ product.setOrderId(orderId); // 关联主单ID
|
|
|
+ product.setOrderNo(orderNo); // 关联订单号
|
|
|
+ return product;
|
|
|
+ })
|
|
|
+ .collect(Collectors.toList());
|
|
|
+
|
|
|
+ // 5. 批量插入订单商品
|
|
|
+ boolean productsSaved = orderProductMapper.insertBatch(orderProducts);
|
|
|
+
|
|
|
+
|
|
|
+ return productsSaved;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 修改订单主信息
|
|
|
+ *
|
|
|
+ * @param bo 订单主信息
|
|
|
+ * @return 是否修改成功
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public Boolean updateByBo(OrderMainBo bo) {
|
|
|
+ OrderMain update = MapstructUtils.convert(bo, OrderMain.class);
|
|
|
+ validEntityBeforeSave(update);
|
|
|
+ return baseMapper.updateById(update) > 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 修改状态
|
|
|
+ *
|
|
|
+ * @param bo 信息
|
|
|
+ * @return 结果
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public int updateStatus(OrderMainBo bo) {
|
|
|
+ OrderMain order = new OrderMain();
|
|
|
+ order.setId(bo.getId());
|
|
|
+ order.setOrderStatus(bo.getOrderStatus());
|
|
|
+ return baseMapper.updateById(order);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 保存前的数据校验
|
|
|
+ */
|
|
|
+ private void validEntityBeforeSave(OrderMain entity) {
|
|
|
+ //TODO 做一些数据校验,如唯一约束
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 校验并批量删除订单主信息信息
|
|
|
+ *
|
|
|
+ * @param ids 待删除的主键集合
|
|
|
+ * @param isValid 是否进行有效性校验
|
|
|
+ * @return 是否删除成功
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
|
|
|
+ if (isValid) {
|
|
|
+ //TODO 做一些业务上的校验,判断是否需要校验
|
|
|
+ }
|
|
|
+ return baseMapper.deleteByIds(ids) > 0;
|
|
|
+ }
|
|
|
+}
|