|
@@ -0,0 +1,156 @@
|
|
|
+package com.sd.business.service.order.impl;
|
|
|
+
|
|
|
+import cn.hutool.core.bean.BeanUtil;
|
|
|
+import com.baomidou.dynamic.datasource.annotation.DSTransactional;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.IdWorker;
|
|
|
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.fjhx.file.utils.ObsFileUtil;
|
|
|
+import com.ruoyi.common.constant.StatusConstant;
|
|
|
+import com.ruoyi.common.core.domain.BaseIdPo;
|
|
|
+import com.ruoyi.common.exception.ServiceException;
|
|
|
+import com.ruoyi.common.utils.wrapper.IWrapper;
|
|
|
+import com.sd.business.entity.order.dto.OrderExchangeDto;
|
|
|
+import com.sd.business.entity.order.dto.OrderExchangeSelectDto;
|
|
|
+import com.sd.business.entity.order.po.OrderExchange;
|
|
|
+import com.sd.business.entity.order.po.OrderExchangeDetail;
|
|
|
+import com.sd.business.entity.order.po.OrderSku;
|
|
|
+import com.sd.business.entity.order.vo.OrderExchangeDetailVo;
|
|
|
+import com.sd.business.entity.order.vo.OrderExchangeVo;
|
|
|
+import com.sd.business.mapper.order.OrderExchangeMapper;
|
|
|
+import com.sd.business.service.order.OrderExchangeDetailService;
|
|
|
+import com.sd.business.service.order.OrderExchangeService;
|
|
|
+import com.sd.business.service.order.OrderSkuService;
|
|
|
+import com.sd.business.service.sku.SkuSpecService;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Objects;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+
|
|
|
+/**
|
|
|
+ * <p>
|
|
|
+ * 订单退换货 服务实现类
|
|
|
+ * </p>
|
|
|
+ *
|
|
|
+ * @author
|
|
|
+ * @since 2023-09-14
|
|
|
+ */
|
|
|
+@Service
|
|
|
+public class OrderExchangeServiceImpl extends ServiceImpl<OrderExchangeMapper, OrderExchange> implements OrderExchangeService {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private OrderExchangeDetailService orderExchangeDetailService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private OrderSkuService orderSkuService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private SkuSpecService skuSpecService;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Page<OrderExchangeVo> getPage(OrderExchangeSelectDto dto) {
|
|
|
+ IWrapper<OrderExchange> wrapper = getWrapper();
|
|
|
+ wrapper.orderByDesc("oe", OrderExchange::getId);
|
|
|
+ wrapper.eq("oe", OrderExchange::getOrderInfoId, dto.getOrderInfoId());
|
|
|
+ Page<OrderExchangeVo> page = this.baseMapper.getPage(dto.getPage(), wrapper);
|
|
|
+ return page;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public OrderExchangeVo detail(Long id) {
|
|
|
+ OrderExchange orderExchange = this.getById(id);
|
|
|
+ if (orderExchange == null) {
|
|
|
+ throw new ServiceException("未知订单号");
|
|
|
+ }
|
|
|
+
|
|
|
+ OrderExchangeVo result = BeanUtil.toBean(orderExchange, OrderExchangeVo.class);
|
|
|
+ List<OrderExchangeDetail> list = orderExchangeDetailService.list(q -> q.eq(OrderExchangeDetail::getOrderExchangeId, id));
|
|
|
+ List<OrderExchangeDetailVo> orderExchangeDetailList = BeanUtil.copyToList(list, OrderExchangeDetailVo.class);
|
|
|
+
|
|
|
+ // 赋值sku规格id和采购数量
|
|
|
+ orderSkuService.attributeAssign(orderExchangeDetailList, OrderExchangeDetailVo::getOrderSkuId, (item, orderSku) -> {
|
|
|
+ item.setSkuSpecId(orderSku.getSkuSpecId());
|
|
|
+ item.setBuyQuantity(orderSku.getQuantity());
|
|
|
+ });
|
|
|
+
|
|
|
+ // 赋值sku品号品名
|
|
|
+ skuSpecService.attributeAssign(orderExchangeDetailList, OrderExchangeDetailVo::getSkuSpecId, (item, skuSpec) -> {
|
|
|
+ item.setSkuSpecCode(skuSpec.getCode());
|
|
|
+ item.setSkuSpecName(skuSpec.getName());
|
|
|
+ });
|
|
|
+
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ @DSTransactional
|
|
|
+ @Override
|
|
|
+ public void add(OrderExchangeDto orderExchangeDto) {
|
|
|
+ orderExchangeDto.setId(IdWorker.getId());
|
|
|
+
|
|
|
+ List<OrderExchangeDetail> orderExchangeDetailList = orderExchangeDto.getOrderExchangeDetailList();
|
|
|
+
|
|
|
+ // 获取订单sku id集合
|
|
|
+ List<Long> orderSkuIdList = orderExchangeDetailList.stream()
|
|
|
+ .map(OrderExchangeDetail::getOrderSkuId)
|
|
|
+ .collect(Collectors.toList());
|
|
|
+
|
|
|
+ // 根据集合查询订单数量
|
|
|
+ Map<Long, BigDecimal> map = orderSkuService.mapKV(BaseIdPo::getId, OrderSku::getQuantity,
|
|
|
+ q -> q.in(BaseIdPo::getId, orderSkuIdList));
|
|
|
+
|
|
|
+ // 已添加订单产品退货列表
|
|
|
+ List<OrderExchangeDetail> list = new ArrayList<>();
|
|
|
+ if (orderExchangeDto.getType().equals(1)) {
|
|
|
+ list = orderExchangeDetailService.list(q -> q.in(OrderExchangeDetail::getOrderSkuId, orderSkuIdList));
|
|
|
+ }
|
|
|
+
|
|
|
+ for (OrderExchangeDetail orderExchangeDetail : orderExchangeDetailList) {
|
|
|
+
|
|
|
+ Long orderSkuId = orderExchangeDetail.getOrderSkuId();
|
|
|
+
|
|
|
+ // 数量为空,没找到订单sku id
|
|
|
+ BigDecimal quantity = map.get(orderSkuId);
|
|
|
+ if (quantity == null) {
|
|
|
+ throw new ServiceException("未知订单skuId:" + orderSkuId);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 退换货数量大于订单产品购买数量
|
|
|
+ if (orderExchangeDetail.getQuantity().compareTo(quantity) > 0) {
|
|
|
+ throw new ServiceException("退换货数量不能大于订单产品购买数量");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 如果退货,已退货数量+本次退货数量不能大于订单产品数量
|
|
|
+ if (orderExchangeDto.getType().equals(1)) {
|
|
|
+ BigDecimal returnQuantity = list.stream()
|
|
|
+ .filter(item -> Objects.equals(item.getOrderSkuId(), orderSkuId))
|
|
|
+ .map(OrderExchangeDetail::getQuantity)
|
|
|
+ .reduce(BigDecimal.ZERO, BigDecimal::add);
|
|
|
+
|
|
|
+ if (returnQuantity.add(orderExchangeDetail.getQuantity()).compareTo(quantity) > 0) {
|
|
|
+ throw new ServiceException("已记录退货数量加本次退货数量大于订单产品购买数量");
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ orderExchangeDetail.setOrderExchangeId(orderExchangeDto.getId());
|
|
|
+ orderExchangeDetail.setReturnStatus(StatusConstant.NO);
|
|
|
+ orderExchangeDetail.setExchangeStatus(StatusConstant.NO);
|
|
|
+ }
|
|
|
+
|
|
|
+ save(orderExchangeDto);
|
|
|
+ orderExchangeDetailService.saveBatch(orderExchangeDetailList);
|
|
|
+ ObsFileUtil.saveFile(orderExchangeDto.getFileList(), orderExchangeDto.getId());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void delete(Long id) {
|
|
|
+ this.removeById(id);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|